package com.yihu.hos.system.controller;

import com.yihu.hos.system.model.SystemMenu;
import com.yihu.hos.system.model.SystemMenuAction;
import com.yihu.hos.system.service.MenuManager;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 菜单管理
 */
@RequestMapping("/menu")
@Controller
public class MenuActionController extends BaseController {
    @Resource(name = MenuManager.BEAN_ID)
    private MenuManager menuManager;

    /**
     * 菜单管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("initial")
    public String menuInitial(Model model) {
        model.addAttribute("contentPage", "system/menu/menu");
        return "partView";
    }

    /**
     * 获取菜单列表
     */
    @RequestMapping("getMenuList")
    @ResponseBody
    public Result getMenuList(HttpServletRequest request) {
        try {
            Map<String, Object> params = new HashMap<>();
            String name = request.getParameter("name");
            params.put("name", name);
            return menuManager.getMenuList(params);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 菜单编辑界面
     */
    @RequestMapping("editorOrganization")
    public String editorOrganization(Model model, String menuId) {
        try {
            //是否编辑
            if (menuId != null && menuId.length() > 0) {
                //获取菜单信息
                SystemMenu menu = menuManager.getMenuById(menuId);
                model.addAttribute("model", menu);
            }
            model.addAttribute("contentPage", "system/menu/editorMenu");
            return "pageView";
        } catch (Exception ex) {
            model.addAttribute("contentPage", "system/menu/editorMenu");
            return "pageView";
        }
    }

    /**
     * 新增菜单
     */
    @RequestMapping("addMenu")
    @ResponseBody
    public Result addMenu(HttpServletRequest request) {
        try {
            SystemMenu obj = new SystemMenu();
            BeanUtils.populate(obj, request.getParameterMap());
            obj.setValid("true");

            return menuManager.addMenu(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 修改菜单
     */
    @RequestMapping("updateMenu")
    @ResponseBody
    public Result updateMenu(HttpServletRequest request) {
        try {
            SystemMenu obj = new SystemMenu();
            BeanUtils.populate(obj, request.getParameterMap());

            return menuManager.updateMenu(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }


    /**
     * 修改菜单状态
     */
    @RequestMapping("activityMenu")
    @ResponseBody
    public Result activityMenu(String orgId, String activityFlag) {
        try {
            return menuManager.activityMenu(orgId, activityFlag);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除菜单状态
     */
    @RequestMapping("deleteMenu")
    @ResponseBody
    public Result deleteMenu(HttpServletRequest request) {
        try {
           return menuManager.deleteMenu(request.getParameter("ids"));
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

//==============================menuAction==================================

    /**
     * 菜单管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/menuAction/initial")
    public String menuActionInitial(Model model) {
        model.addAttribute("contentPage", "system/menuaction/menuAction");
        return "partView";
    }

    /**
     * 获取菜单列表
     */
    @RequestMapping("/menuAction/getMenuActionList")
    @ResponseBody
    public Result getMenuActionList(HttpServletRequest request) {
        try {
            Map<String, Object> params = new HashMap<>();
            String name = request.getParameter("name");
            String menuid = request.getParameter("menuId");
            params.put("name", name);
            params.put("menuId", menuid);
            return menuManager.getMenuActionList(params);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
    @RequestMapping("/menuAction/deleteMenuAction")
    @ResponseBody
    public Result deleteMenuAction(HttpServletRequest request) {
        try {
           String id=request.getParameter("id");
            return menuManager.deleteMenuAction(id);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 菜单编辑界面
     */
    @RequestMapping("/menuAction/editorMenuAction")
    public String editorMenuAction(Model model, String menuId) {
        try {
            //是否编辑
            if (menuId != null && menuId.length() > 0) {
                //获取菜单信息
                SystemMenuAction menu = menuManager.getMenuActionById(menuId);
                model.addAttribute("model", menu);
            }
            model.addAttribute("contentPage", "system/menuaction/editorMenuAction");
            return "pageView";
        } catch (Exception ex) {
            model.addAttribute("contentPage", "system/menu/editorMenu");
            return "pageView";
        }
    }

    /**
     * 新增菜单
     */
    @RequestMapping("/menuAction/addMenuAction")
    @ResponseBody
    public Result addMenuAction(HttpServletRequest request) {
        try {
            SystemMenuAction obj = new SystemMenuAction();
            BeanUtils.populate(obj, request.getParameterMap());
            obj.setFlag("true");

            return menuManager.addMenuAction(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 修改菜单
     */
    @RequestMapping("/menuAction/updateMenuAction")
    @ResponseBody
    public Result updateMenuAction(HttpServletRequest request) {
        try {
            SystemMenuAction obj = new SystemMenuAction();
            BeanUtils.populate(obj, request.getParameterMap());

            return menuManager.updateMenuAction(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }


    /**
     * 修改菜单状态
     */
    @RequestMapping("/menuAction/activityMenuAction")
    @ResponseBody
    public Result activityMenuAction(String orgId, String activityFlag) {
        try {
            return menuManager.activityMenuAction(orgId, activityFlag);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }
}
