<?php

namespace app\controller\manager;

use app\service\Tool;
use app\model\{System as MSystem, Log};
use app\service\Image;
use think\facade\Cache;

class System extends Base
{
    /**
     * 获取当前系统设置
     *
     * @return void
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $item = input('post.item/a');
            $img  = input('post.img');
            $system = MSystem::getSystem();
            if (empty($system)) {
                if(!empty($img)){
                    $item['mark_img'] = $img;
                }
                $system = MSystem::create($item);
                Log::write('system', 'index', "系统设置,ID:{$system->id}");
            } else {
                if (!empty($img)) {
                    Image::delImg($system['mark_img']);
                    $item['mark_img'] = $img;
                }
                MSystem::update($item, ['id' => $system['id']]);
                Log::write('system', 'index', "系统设置,ID:{$system['id']}");
            }
            return $this->json();
        } else {
            $item = MSystem::getSystem();
            $positions = Image::getMarkPosition();
            
            $this->data['item'] = $item;
            $this->data['positions'] = $positions;
            return $this->view();
        }
    }

    public function other()
    {
        return $this->view();
    }

    public function clearCache()
    {
        Cache::clear();
        $cachePath = app()->getRuntimePath().'cache';
        $tempPath = app()->getRuntimePath().'temp';
        Tool::removeByPath($cachePath);
        Tool::removeByPath($tempPath);
        clearstatcache();
        return $this->json();
    }
}