<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\controller\auth;

use app\controller\Base;
use app\services\auth\RoleService;

class Role extends Base{

	/**
	 * @title 角色列表
	 * 
	 * @time 2019年12月09日
	 * @return string|Json
	 */
	public function index() {
		$tree = app()->make(RoleService::class)->getRolesList($this->request, true);
		
		$this->data['code'] = 1;
		$this->data['data'] = $tree;
		return $this->data;
	}

	/**
	 * @title 角色修改
	 * @time 2019年12月11日
	 * @param $id
	 * @param Request $request
	 * @return Array
	 */
	public function edit(RoleService $service) {
		try {
			$service->updateRole($this->request);
			$this->data['code'] = 1;
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message'] = $e->getMessage();
		}
		return $this->data;
	}

	/**
	 * @title 角色授权
	 * @time 2019年12月11日
	 * @param $id
	 * @param Request $request
	 * @return Array
	 */
	public function auth() {
		$role_id = $this->request->param('role_id', '');
		$auth = $this->request->param('auth', '');
		$service = app()->make(RoleService::class);
		try {
			$service->updateRolePermission($role_id, $auth);
			$service->updateRoleAuth($this->request);
			$this->data['code'] = 1;
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message'] = $e->getMessage();
		}
		return $this->data;
	}
	
	/**
	 * @title 删除角色
	 * 
	 * @time 2019年12月11日
	 * @param $id
	 * @throws FailedException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @return Json
	 */
	public function delete(){
		try {
			$service = app()->make(RoleService::class)->deleteRole($this->request);
			$this->data['code'] = 1;
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message'] = $e->getMessage();
		}
		return $this->data;
	}
}
