<?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\system;

use app\controller\Base;
use app\services\auth\MenuService;
use app\services\auth\AuthService;

class Menu extends Base{

	/**
	 * @title 权限列表
	 *
	 * @time 2020年01月09日
	 * @param Departments $department
	 * @return Array
	 */
	public function index() {
		$data = app()->make(MenuService::class)->getSystemMenu(false);
		$this->data['data'] = $data;
		return $this->data;
	}
	/**
	 * @title 权限保存
	 *
	 * @time 2020年01月09日
	 * @return Array
	 */
	public function add() {
		$data = request()->post();
		$data['creator_id'] = request()->user['uid'];
		try {
			$result = app()->make(MenuService::class)->createData($data);
			$this->data['data'] = $result;
			$this->data['message']  = '添加成功！';
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 权限更新
	 *
	 * @time 2020年01月09日
	 * @param $id
	 * @param Request $request
	 * @return Array
	 */
	public function edit() {
		$data = request()->post();
		try {
			$result = app()->make(MenuService::class)->saveData($data);
			if($result){
				$this->data['data'] = app()->make(AuthService::class)->getAuthMenu();
			}
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 权限删除
	 *
	 * @time 2020年01月09日
	 * @param $id
	 * @return Array
	 */
	public function delete() {
		$ids = request()->post('ids');
		try {
			$result = app()->make(MenuService::class)->deleteMenu($ids);
			$this->data['data'] = app()->make(MenuService::class)->getSystemMenu();
		} catch (\Exception $e) {
			$this->data['code'] = 0;
			$this->data['message']  = $e->getMessage();
		}
		return $this->data;
	}
	/**
	 * @title 菜单路由
	 *
	 * @time 2020年01月09日
	 * @param $id
	 * @return Array
	 */
	public function routes(Permissions $permission) {
		$map = [];
		$map[] = ['type', '=', 1];
		$map[] = ['hidden', '=', 1];
		$list = $permission->where($map)->field('router,name,icon,id,parent_id')->order('sort asc')->select()->toArray();
		$tree = (new Tree())->listToTree($list, 'id', 'parent_id', 'children');
		$this->data['data'] = [['router' => 'root', 'children' => $tree]];
		return $this->data;
	}
	/**
	 * @title 权限数据
	 *
	 * @time 2020年01月09日
	 * @param $id
	 * @return Array
	 */
	public function permission(Permissions $permission) {
		$map = [];
		$map[] = ['hidden', '=', 1];
		$list = $permission->where($map)->field('router,name,icon,id,parent_id')->order('sort asc')->select()->toArray();
		$data = [];
		foreach($list as $item){
			if($item['type'] == 1){
				$data[$item['id']] = $item;
			}else{
				$data[$item['parent_id']]['module'] = $item;
			}
		}
		$this->data['data'] = $data;
		return $this->data;
	}
	/**
	 * @title 我的菜单
	 * @return Array
	 */
	public function my(AuthService $service){
		$this->data['code'] = 1;
		$this->data['data'] = ['menu' => $service->getAuthMenu($this->request), 'permissions' => $service->getAuthPermissions($this->request)];
		return $this->data;
	}
}
