<?php

namespace app\admin\controller\sys_manage;

use app\admin\service\sys_manage\RoleService;
use app\admin\validate\sys_manage\RoleValidate;
use app\common\Tree;
use app\controller\Curd;
use app\model\SysRole;
use support\Request;
use support\Response;

class RoleController extends Curd
{
    public function __construct()
    {
        $this->model = new SysRole();
    }

    /**
     * @Desc 角色列表
     * @Author Gorden
     * @Date 2024/2/20 16:30
     *
     * @param Request $request
     * @return \support\Response
     */
    public function roleList(Request $request)
    {
        return RoleService::roleList($request);
    }

    public function select(Request $request): Response
    {
        $keyword = $request->get('keyword');

        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $format = 'tree';
        $order = $request->get('order', 'desc');
        $field = $field ?? 'role_addtimes';
        if (!empty($keyword)){
            $where['role_name'] = ['like',$keyword];
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, 1000);
    }

    protected function formatTree($items): Response
    {
        $format_items = [];
        foreach ($items as $item) {
            $format_items[] = [
                'name' => $item->role_name,
                'value' => (string)$item->role_id,
                'id' => $item->id ?? $item->role_id,
                'role_id' => $item->id ?? $item->role_id,
                'pid' => $item->pid ?? $item->role_superior_id,
                'role_status' => $item->role_status,
                'role_permission' => $item->role_permission,
                'role_category' => $item->role_category,
                'role_addtimes' => $item->role_addtimes,
            ];
        }
        $tree = new Tree($format_items);
        return json_success('success', $tree->getTree());
    }

    public function afterQuery($items)
    {
        foreach ($items as &$item){
            if (!empty($item->role_permission) && is_json($item->role_permission)) {
                $item->role_permission = json_decode($item->role_permission);
            }
        }

        return $items;
    }

    /**
     * @Desc 角色详情
     * @Author Gorden
     * @Date 2024/2/20 16:36
     *
     * @param $id
     * @return \support\Response
     */
    public function roleInfo($id)
    {
        return RoleService::roleInfo($id);
    }

    public function save()
    {

    }

    /**
     * @Desc 添加角色
     * @Author Gorden
     * @Date 2024/2/20 15:38
     *
     * @param Request $request
     * @return \support\Response|null
     */
    public function addRole(Request $request)
    {
        $validate = new RoleValidate();
        if (!$validate->scene('add')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return RoleService::insertRole($request->post());
    }

    /**
     * @Desc 修改角色
     * @Author Gorden
     * @Date 2024/2/20 16:07
     *
     * @param $id
     * @param Request $request
     * @return \support\Response
     */
    public function updateRole($id, Request $request)
    {
        $validate = new RoleValidate();
        if (!$validate->scene('update')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return RoleService::updateRole(intval($id), $request->post());
    }

    public function updatePermission($id, Request $request)
    {
        $validate = new RoleValidate();
        if (!$validate->scene('permission')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return RoleService::updatePermission(intval($id), $request->post());
    }

    /**
     * @Desc 删除角色
     * @Author Gorden
     * @Date 2024/2/20 16:40
     *
     * @param $id
     * @return \support\Response
     */
    public function delRole(Request $request)
    {
        return RoleService::delRole($request->post());
    }

    /**
     * @Desc 修改角色状态
     * @Author Gorden
     * @Date 2024/2/20 16:50
     *
     * @param $id
     * @param Request $request
     * @return \support\Response
     */
    public function updateStatus($id, Request $request)
    {
        $validate = new RoleValidate();
        if (!$validate->scene('update_status')->check(['role_status' => $request->post('role_status')])) {
            return json_fail($validate->getError());
        }

        return RoleService::updateStatus(intval($id), $request->post('role_status'));
    }
}