<?php
/**
 * Created by PhpStorm.
 * User: 中闽 < 1464674022@qq.com >
 * Date: 2019/12/5
 * Time: 17:44
 */


namespace app\admin\controller;

use app\admin\controller\base\Permissions;
use app\admin\model\AdminLog;
use app\admin\model\Urlconfig;
use app\common\model\ConfigTab as configTabModel;
use think\Cache;
use think\Db;

class Webconfig extends Permissions
{
    protected function _initialize()
    {
        parent::_initialize();
        //扩展配置标签
        $tabs = (new configTabModel())->where('status', configTabModel::STATUS_OPEN)->order('sort desc')->select();
        $this->assign('tabs', $tabs);
    }

    public function index()
    {
        $this->assign('web_config', Db::name('webconfig')->where('id', 1)->find());
        $this->assign('is_close_site_key', (new Urlconfig())->getCloseSiteKey());
        $this->assign('admin_log_num', (new AdminLog())->count());
        $this->assign('backend_pass', (new Urlconfig())->getBackendPass());
        return $this->fetch();
    }

    public function publish()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $validate = new \think\Validate([
                ['file_type', 'requireWith:name', '上传类型不能为空'],
                ['file_size', 'requireWith:name', '上传大小不能为空'],
                ['host', 'url'],
            ]);
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
            if (!isset($post['is_log'])) {
                $post['is_log'] = 0;
            }
            if (!isset($post['is_close_site'])) {
                $post['is_close_site'] = 0;
            }
            $model = (new \app\common\model\Webconfig())->where('id', 1)->find();
            if (false == $model->allowField(true)->save($post)) {
                $this->error('提交失败');
            } else {
                Cache::clear();
                $this->success('提交成功', 'admin/webconfig/index');
            }
        }
    }

    public function appointmentConfig()
    {
        $this->assign('web_config', Db::name('webconfig')->where('id', 1)->find());
        return $this->fetch();
    }
}
