<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: sai <1430792918@qq.com>
// +----------------------------------------------------------------------
namespace plugin\saiadmin\app\controller\system;

use app\dice\helper\AdminScopeHelper;
use plugin\saiadmin\basic\BaseController;
use plugin\saiadmin\app\validate\system\SystemRoleValidate;
use plugin\saiadmin\app\logic\system\SystemRoleLogic;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * 角色控制器（按渠道隔离）
 */
class SystemRoleController extends BaseController
{
    public function __construct()
    {
        $this->logic = new SystemRoleLogic();
        $this->validate = new SystemRoleValidate;
        parent::__construct();
    }

    #[Permission('角色数据列表', 'core:role:index')]
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['name', ''],
            ['code', ''],
            ['status', ''],
        ]);
        $requestDeptId = AdminScopeHelper::pickRequestDeptId(
            $request->input('dept_id'),
            $request->all()
        );
        $data = $this->logic->indexList($where, $requestDeptId);
        return $this->success($data);
    }

    #[Permission('角色数据读取', 'core:role:read')]
    public function read(Request $request): Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        if ($model) {
            $data = is_array($model) ? $model : $model->toArray();
            $role = $this->logic->model->find($id);
            if ($role) {
                $this->logic->assertRoleWritable($role);
            }
            return $this->success($data);
        }
        return $this->fail('not found');
    }

    #[Permission('角色数据添加', 'core:role:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        $data['dept_id'] = $this->logic->resolveRequestDeptId(
            AdminScopeHelper::pickRequestDeptId($data['dept_id'] ?? null, $data)
        );
        $this->validate('save', $data);
        $result = $this->logic->add($data);
        if ($result) {
            return $this->success('add success');
        }
        return $this->fail('add failed');
    }

    #[Permission('角色数据修改', 'core:role:update')]
    public function update(Request $request): Response
    {
        $data = $request->post();
        $role = $this->logic->model->find($data['id'] ?? 0);
        if ($role) {
            $this->logic->assertRoleWritable($role);
            if (!isset($data['dept_id']) || $data['dept_id'] === '' || $data['dept_id'] === null) {
                $data['dept_id'] = $role->dept_id;
            }
        }
        $this->validate('update', $data);
        $result = $this->logic->edit($data['id'], $data);
        if ($result) {
            return $this->success('update success');
        }
        return $this->fail('update failed');
    }

    #[Permission('角色数据删除', 'core:role:destroy')]
    public function destroy(Request $request): Response
    {
        $ids = $request->post('ids', '');
        if (empty($ids)) {
            return $this->fail('please select data to delete');
        }
        $result = $this->logic->destroy($ids);
        if ($result) {
            return $this->success('delete success');
        }
        return $this->fail('delete failed');
    }

    #[Permission('角色数据列表', 'core:role:index')]
    public function getMenuByRole(Request $request): Response
    {
        $id = $request->get('id');
        $data = $this->logic->getMenuByRole($id);
        return $this->success($data);
    }

    #[Permission('角色菜单权限', 'core:role:menu')]
    public function menuPermission(Request $request): Response
    {
        $id = $request->post('id');
        $menu_ids = $request->post('menu_ids');
        $this->logic->saveMenuPermission($id, $menu_ids);
        return $this->success('operation success');
    }

    public function accessRole(Request $request): Response
    {
        $where = ['status' => 1];
        $requestDeptId = AdminScopeHelper::pickRequestDeptId(
            $request->input('dept_id'),
            $request->all()
        );
        $data = $this->logic->accessRole($where, $requestDeptId);
        return $this->success($data);
    }
}
