<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\SysConfigGroupModel;
use app\admin\model\v1\SysConfigModel;
use app\admin\model\v1\SysConfigTypeModel;
use app\admin\validate\v1\SysConfigValidate;

/**
 * 配置项控制器
 */
class SysConfig
{
    // 获取分组列表
    public function groups()
    {
        $groups = SysConfigGroupModel::field([
                'id', 
                'name'
            ])
            ->language(request()->lang_id)
            ->enabled()
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();

        return success('获取成功', $groups);
    }

    // 获取配置类型
    public function types()
    {
        $types = SysConfigTypeModel::field([
                'name', 
                'value'
            ])
            ->order(['sort' => 'asc'])
            ->select();

        return success('获取成功', $types);
    }

    // 配置项分页
    public function index()
    {
        $param = request()->param([
            'title',
            'page/d' => 1,
            'size/d' => 10
        ]);

        $configs = SysConfigModel::alias('cfg')
            ->field([
                'cfg.id',
                'cfg.title',
                'cfg.name',
                'cfg.type',
                'cfg.sort',
                'grp.name' => 'group_name',
                'type.name' => 'type_name'
            ])
            ->join('sys_config_group grp', 'grp.id = cfg.group_id')
            ->join('sys_config_type type', 'type.value = cfg.type')
            ->where('grp.language_id', '=', request()->lang_id)
            ->where(function($query) use($param) {
                if (!empty($param['title'])) {
                    $query->where('cfg.title', 'like', "%{$param['title']}%");
                }
            })
            ->order(['cfg.group_id' => 'asc', 'cfg.sort' => 'asc', 'cfg.id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'],
                'page'      => $param['page']
            ]);

        return success('获取成功', $configs);
    }

    // 配置项详情
    public function read()
    {
        $id = request()->param('id');

        $config = SysConfigModel::withoutField([
                'created_at',
                'updated_at',
                'deleted_at'
            ])
            ->bypk($id)
            ->find();
        if (empty($config)) {
            return error('配置项不存在');
        }

        return success('获取成功', $config);
    }

    // 配置项新增
    public function save()
    {
        $post = request()->post([
            'group_id',
            'title',
            'name',
            'value',
            'extra',
            'type' => 'text',
            'sort',
            'remark'
        ]);

        $validate = new SysConfigValidate;
        if (!$validate->scene('add')->check($post)) {
            return error($validate->getError());
        }

        $config = SysConfigModel::create($post);
        if ($config->isEmpty()) {
            return error('操作失败');
        }
        return success('操作成功');
    }

    // 配置项更新
    public function update()
    {
        $id  = request()->param('id');
        $put = request()->put([
            'group_id',
            'title',
            'name',
            'value',
            'extra',
            'type' => 'text',
            'sort',
            'remark'
        ]);

        $validate = new SysConfigValidate;
        if (!$validate->check(array_merge($put, ['id' => $id]))) {
            return error($validate->getError());
        }

        $config = SysConfigModel::bypk($id)->find();
        if (empty($config)) {
            return error('请确认要操作对象是否存在');
        }

        if (!$config->save($put)) {
            return error('操作失败');
        }
        return success('操作成功');
    }

    // 配置项删除
    public function delete()
    {
        $id = request()->param('id');

        $config = SysConfigModel::bypk($id)->find();
        if (empty($config)) {
            return error('请确认要操作对象是否存在');
        }

        if (!$config->delete()) {
            return error('操作失败');
        }
        return success('操作成功');
    }
}
