<?php

namespace app\admin\controller\Role;

use app\BaseController;
use app\common\model\Role\Role as ModelRole;
use app\Request;

class Role extends BaseController
{
    /**
     * 获取用户角色列表
     *
     * @param Request $request
     * @return array
     * @date 2022-03-02
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getUserRoleList(Request $request)
    {
    }

    /**
     * 获取角色列表
     *
     * @param Request $request
     * @date 2022-02-25
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getRoleList(Request $request): array
    {
        $role_status = $request->param('role_status');
        $role = $request->param('role');
        $scope = explode(',', $request->param('scope', ''));
        $con = [
            // 'role_status' => 1
        ];
        if ($role) {
            $con['role_name'] = $role;
        }
        if ($role_status) {
            $con['role_status'] = $role_status;
        }
        $query = ModelRole::scope($scope)->withSearch(['role_name'], $con)->where($con);
        $select =  self::pageWrapper($query)->field([
            'role_name',
            'role_status',
            'role_guid',
        ])->append([
            'role_status_text'
        ])->order([
            'role_id' => 'desc'
        ])->select();
        $count =  $query->count();
        return [
            'code' => 0,
            'data' => $select,
            'count' => $count,
            'msg' => 'ok'
        ];
    }

    /**
     * 获取角色菜单
     *
     * @param Request $request
     * @date 2022-03-08
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getRoleMenu(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'role_guid' => 'require'
        ]);
        dump(ModelRole::getByRoleGuid($params['role_guid'])->menus);
        return [
            'code' => 0,
            'msg' => 'ok',
            // 'data' => $data
        ];
    }

    /**
     * 编辑角色
     *
     * @param Request $request
     * @date 2022-02-28
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function editRole(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'role_guid' => 'require',
            'role_name' => 'require',
        ]);
        $role = ModelRole::where([
            'role_guid' => $params['role_guid']
        ])->find();
        if (!$role) {
            throwErrorMsg("角色不存在", 1);
        }
        $role->save([
            'role_name' => $params['role_name'],
        ]);
        return [
            'code' => 0,
            'msg' => '编辑成功'
        ];
    }

    /**
     * 添加角色
     *
     * @param Request $request
     * @date 2022-02-28
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function addRole(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'role_name' => 'require',
        ]);
        ModelRole::create([
            'role_name' => $params['role_name'],
        ]);
        return [
            'code' => 0,
            'msg' => '添加成功'
        ];
    }

    /**
     * 删除角色
     *
     * @param Request $request
     * @date 2022-02-28
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function deleteRole(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'role_guid' => 'require',
        ]);
        $roles = ModelRole::where([
            'role_guid' => ['in', $params['role_guid']]
        ])->select();
        $roles->delete();
        return [
            'code' => 0,
            'msg' => "删除成功"
        ];
    }

    /**
     * 更新角色状态
     *
     * @param Request $request
     * @date 2022-02-28
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function updateRoleStatus(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'role_guid' => 'require',
            'role_status' => 'require|in:1,2',
        ]);
        $role_status = $params['role_status'];
        $roles = ModelRole::where([
            'role_guid' => explode(',', $params['role_guid'])
        ])->select();
        $roles->update([
            'role_status' => $role_status
        ]);
        return [
            'code' => 0,
            'msg' => "更新成功"
        ];
    }
}
