<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | [ 权限管理 ]
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\system\controller;


use app\BaseController;
use app\model\system\AdminRole;
use app\model\system\RolePath;
use app\Request;
use library\services\UtilService;

class Role extends BaseController
{

    /**
     * 列表数据
     * @return mixed
     */
    public function list()
    {
        $listAr = (new AdminRole)->getRoleData();
        $result = [];
        foreach ($listAr as $v) {
            $d              = [];
            $d['name']      = $v['name'];
            $d['status']    = $v['status'];
            $d['is_system'] = $v['is_system'];
            $d['id']        = $v['id'];
            $result[]       = $d;
        }
        return app('json')->success($result);
    }

    /**
     * 保存角色数据
     * @param Request $request
     * @return mixed
     */
    public function save(Request $request)
    {
        $post = UtilService::getMore([
            ['id', '0'],
            ['name', '', 'empty', '请输入角色名'],
            ['status', 0],
        ], $request);
        $bool = (new AdminRole)->saveRole($post);
        return $bool ? app('json')->success("操作成功", []) : app('json')->fail(AdminRole::getErrorInfo());
    }

    /**
     * 删除数据
     * @param Request $request
     * @return mixed
     */
    public function del(Request $request)
    {
        [$id] = UtilService::getMore(
            [
                ['id', '0'],
            ],
            $request, true
        );
        if(in_array(strval($id),["1","53","54","55"])){
            return app("json")->fail("当前角色不能删除");
        }
        AdminRole::del($id);
        return app('json')->success("删除成功", []);
    }


    /**
     * 配置管理
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setingList()
    {
        $post          = UtilService::getMore([
            ['id', '0'],
        ]);
        $rolePath = new RolePath;
        $rolePath->getAll();
        $listAr        = $rolePath->getRoleData();
        $rData         = AdminRole::where('id', $post['id'])->find();
        $rAr           = [];
        $rAr['data']   = $listAr;
        $rAr['seting'] = empty($rData) ? [] : json_decode($rData['module'], true);
        return app('json')->success($rAr);
    }


    /**
     * 保存配置
     * @param Request $request
     * @return mixed
     */
    public function setingSave(Request $request)
    {
        $post = UtilService::getMore([
            ['id', '0'],
            ['data', '', 'empty', '请输入参数'],
        ]);
        $iAr  = [];
        foreach ($post['data'] as $k => $v) {
            if (!empty($v)) $iAr[$k] = true;
        }
        AdminRole::where('id', $post['id'])->save(['module' => json_encode($iAr)]);
        return app('json')->success("保存数据成功", []);
    }
}