<?php
namespace app\admin\controller\menu;

use app\admin\controller\BaseController;
use app\admin\server\menu\MenuServer;

class Menu extends BaseController
{

    /**
     * Notes:获取菜单列表
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/14
     */
    public function getMenuList()
    {
        $keywords = $this->request->get('keywords');
        $result         = MenuServer::getMenuList($keywords);
        return json_success($result, '成功');
    }

    /**
     * Notes:修改菜单
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/14
     */
    public function updateMenu()
    {
        $menu_id      = $this->request->post('menu_id');
        $menu_name    = $this->request->post('menu_name','');
        $menu_pid     = $this->request->post('menu_pid','');
        $menu_path    = $this->request->post('menu_path','');
        $menu_icon    = $this->request->post('menu_icon','');
        $menu_route   = $this->request->post('menu_route','');
        $menu_params  = $this->request->post('menu_params','');
        $menu_sort    = $this->request->post('menu_sort','');
        $menu_is_menu = $this->request->post('menu_is_menu');
        $menu_is_show = $this->request->post('menu_is_show');
        $this->validateCheck('menu\MenuValidate', ['menu_id'=>$menu_id], 'update');
        $result         = MenuServer::updateMenu($menu_id, $menu_name, $menu_pid, $menu_path, $menu_icon, $menu_route, $menu_params, $menu_sort, $menu_is_menu, $menu_is_show);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:删除菜单
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/14
     */
    public function delMenu()
    {
        $menu_id = $this->request->get('menu_id');
        $this->validateCheck('menu\MenuValidate', ['menu_id' => $menu_id], 'info');
        $result         = MenuServer::delMenu($menu_id);
        if ($result){
            return json_success($result, '删除成功');
        }else{
            throw new \Exception('删除失败！');
        }

    }

    /**
     * Notes:添加菜单
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/14
     */
    public function addMenu()
    {
        $menu_name    = $this->request->post('menu_name','');
        $menu_pid     = $this->request->post('menu_pid',0);
        $menu_path    = $this->request->post('menu_path','');
        $menu_icon    = $this->request->post('menu_icon','');
        $menu_route   = $this->request->post('menu_route','');
        $menu_params  = $this->request->post('menu_params','');
        $menu_sort    = $this->request->post('menu_sort',1);
        $menu_is_menu = $this->request->post('menu_is_menu');
        $menu_is_show = $this->request->post('menu_is_show');
        $this->validateCheck('menu\MenuValidate', ['menu_name' => $menu_name, 'menu_pid' => $menu_pid, 'menu_is_menu' => $menu_is_menu, 'menu_is_show' => $menu_is_show], 'create');
        $result         = MenuServer::insertMenu($menu_name, $menu_pid, $menu_path, $menu_icon, $menu_route, $menu_params, $menu_sort, $menu_is_menu, $menu_is_show);
        return json_success($result, '添加成功');
    }

    /**
     * Notes:查询菜单详情
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/13
     */
    public function menuInfo()
    {
        $menu_id = $this->request->get('menu_id');
        $this->validateCheck('menu\MenuValidate', ['menu_id' => $menu_id], 'info');
        $result         = MenuServer::menuInfo($menu_id);
        if (!empty($result))
        return json_success($result, '成功');
    }

    /*
     * Notes:角色三级菜单
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/15
     */
    public function menuLevel()
    {
        $result         = MenuServer::menuLevel();
        return json_success($result, '成功');
    }

    /*
   * Notes:角色二级菜单
   * @return \support\Response
   * User: ZQ
   * Date: 2022/9/15
   */
    public function secondLevel()
    {
        $result         = MenuServer::secondLevel();
        return json_success($result, '成功');
    }

    /**
     * Notes:修改菜单状态
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/14
     */
    public function updateStatus()
    {
        $menu_id      = $this->request->get('menu_id');
        $menu_is_show = $this->request->get('menu_is_show');
        $this->validateCheck('menu\MenuValidate', ['menu_id'=>$menu_id], 'update');
        $result         = MenuServer::updateStatus($menu_id, $menu_is_show);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:菜单列表
     * @return \support\Response
     * User: yym
     * Date: 2022/9/21
     */
    public function getHomeMenuList()
    {
        $admin_info = $this->request->admin_info;
        return json_success(MenuServer::getHomeMenuList($admin_info->toArray()));
    }

}
