package cn.pluss.platform.controller.menuInfo;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.MenuInfo;
import cn.pluss.platform.entity.UserInfo;
import cn.pluss.platform.menuInfo.MenuInfoService;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menuInfo")
public class MenuInfoController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private MenuInfoService menuInfoService;

    /**
     * 引导页
     *
     * @return
     */
    @GetMapping
    public String index() {
        return "auth/menu/menu_v2";
    }

    /**
     * 获取菜单
     *
     */
    @GetMapping("/getMenuTree")
    @ResponseBody
    public Result<List<MenuInfo>> getMenuTree(MenuInfo menuInfo) {
        List<MenuInfo> menuTree = menuInfoService.getMenuTree(menuInfo);
        return ResultGenerator.genSuccessResult(menuTree);
    }

    /**
     * 获取角色配置菜单
     *
     */
    @GetMapping("/getRoleConfigMenuTree")
    @ResponseBody
    public Result<List<MenuInfo>> getRoleConfigMenuTree(MenuInfo menuInfo, String roleId) {
        List<MenuInfo> menuTree = menuInfoService.getRoleConfigMenuTree(menuInfo, roleId);
        return ResultGenerator.genSuccessResult(menuTree);
    }

    /**
     * 获取菜单
     *
     * @throws IOException
     */
    @RequestMapping(value = "/getSubMenus", method = RequestMethod.POST)
    @ResponseBody
    public List<MenuInfo> getSubMenus(String parentId) throws IOException {

        MenuInfo menuInfo = new MenuInfo();
        if (!StringUtil.isEmpty(parentId)) {
            menuInfo.setParentId(Integer.valueOf(parentId));
        }
        List<MenuInfo> menuList = menuInfoService.queryMenuInfoList(menuInfo);
        String json = JSONObject.toJSONString(menuList);
        return menuList;
    }


    /**
     * 保存对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/saveMenu", method = RequestMethod.POST)
    @ResponseBody
    public MenuInfo saveMenu(MenuInfo menu) throws IOException {
        menu.setVisible(0);
        if (!StringUtil.isEmpty(menu.getIsVisible())) {

            if ("on".equals(menu.getIsVisible())) {
                menu.setVisible(1);
            } else {
                menu.setVisible(0);
            }

        }

        String userId = request.getHeader("userId");
        UserInfo userInfo = uiService.getById(userId);

        if (menu.getCreator() == null) {
            menu.setCreator(userInfo.getId() + "");
        }
        if (StringUtils.isNotEmpty(menu.getId())) {
            menuInfoService.updateMenuInfo(menu);
        } else {
            Integer id = menuInfoService.saveMenuInfo(menu);
            MenuInfo menu2 = new MenuInfo();
            List<MenuInfo> menuList = menuInfoService.queryMenuInfoListOrderById(menu2);
            menu.setId(menuList.get(0).getId());
        }
        String json = JSONObject.toJSONString(menu);
        System.out.println(json);
        return menu;
    }


    /**
     * 查询当前用户可用的菜单
     *
     * @throws IOException
     */
    @RequestMapping(value = "/permitSubMenus", method = RequestMethod.POST)
    @ResponseBody
    public List<MenuInfo> permitSubMenus(String parentId) throws IOException {
        Integer pid = null;
        if (!StringUtil.isEmpty(parentId)) {
            pid = Integer.valueOf(parentId);
        }

        String userId = request.getHeader("userId");
        UserInfo userInfo = uiService.getById(userId);

        if ("admin".equals(userInfo.getLoginName())) {
            MenuInfo menuInfo = new MenuInfo();

            if (!StringUtil.isEmpty(parentId)) {
                menuInfo.setParentId(pid);
            }
            return menuInfoService.queryMenuInfoList(menuInfo);
        }
        Map<String, Object> menuparam = new HashMap<>();
        menuparam.put("id", userInfo.getId());
        menuparam.put("parentId", pid);
        return menuInfoService.queryPermitMenuInfoById(menuparam);
    }


    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryMenuInfoByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryMenuInfoByPage(Integer currPage, Integer id) {

        if (currPage == null) {
            currPage = 1;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        map.put("id", id);
        List<MenuInfo> menuInfoList = menuInfoService.queryMenuInfoPage(map);
        Integer count = menuInfoService.queryMenuInfoPageCount(map);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("menuInfoList", menuInfoList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMenuInfoDetails", method = RequestMethod.GET)
    public String queryMenuInfoDetails(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            MenuInfo menuInfo = new MenuInfo();
            menuInfo.setId(id + "");
            menuInfo = menuInfoService.queryMenuInfo(menuInfo);
            request.setAttribute("menuInfo", menuInfo);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "menuInfo/menuInfoDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMenuInfoBaseInfo", method = RequestMethod.GET)
    public String queryMenuInfoBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            MenuInfo menuInfo = new MenuInfo();
            menuInfo.setId(id + "");
            menuInfo = menuInfoService.queryMenuInfo(menuInfo);
            request.setAttribute("menuInfo", menuInfo);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "menuInfo/menuInfoBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMenuInfoExtendsInfo", method = RequestMethod.GET)
    public String queryMenuInfoExtendsInfo() {

        return "menuInfo/menuInfoExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMenuInfoFunctionsInfo", method = RequestMethod.GET)
    public String queryMenuInfoFunctionsInfo() {

        return "menuInfo/menuInfoFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/saveMenuInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result<Object> saveMenuInfo(MenuInfo menuInfo) {
        if (StringUtils.isNotEmpty(menuInfo.getId())) {
            menuInfoService.updateMenuInfo(menuInfo);
        } else {
            menuInfoService.saveMenuInfo(menuInfo);
        }
        return ResultGenerator.genSuccessResult("保存成功!", null);
    }

    /**
     * 修改对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/updateMenuInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateMenuInfo(Integer id) {
        MenuInfo menuInfo = new MenuInfo();
        menuInfo.setId(id + "");
        menuInfoService.updateMenuInfo(menuInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/deleteMenuInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteMenuInfo(Integer id) {
        MenuInfo menuInfo = new MenuInfo();
        menuInfo.setId(id + "");
        menuInfoService.deleteMenuInfo(menuInfo);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

}


