package zr.zrpower.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.entity.sys.*;
import zr.zrpower.ejb.sys.SecurityService;

public class ActionMenu
    extends MessageBox {

  //Initialize global variables
  public void init() throws ServletException {
  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html; charset=GBK");
    String strAct = request.getParameter("Act");
    this.isOk = true;
    this.Msg = "非法调用，操作不被接受，你的操作已被记录！";

    if(strAct!=null){
        try {
          SecurityService MenuMgr = new SecurityService();
          if (strAct.equals("edit")) {
            String strMENUNO = request.getParameter("MENUNO");
            String strPARENTNO = request.getParameter("PARENTNO");
            BPIP_MENU Menu = new BPIP_MENU();
            Menu.fullDataFromRequest(request);
            if (MenuMgr.UpdateMenu(Menu)){
              {
                MenuMgr = null;
                Menu = null;
                response.sendRedirect("zrsysmanage/menu/List.jsp?Menu_No="+strPARENTNO+"&isLoad=yes");}
            }
            else {
              this.isOk = false;
              this.Msg = "修改失败，请与系统管理员联系！";
              this.returnPath = "zrsysmanage/menu/EditFrm.jsp?Menu_No="+strPARENTNO+"&SelectedItems="+strMENUNO;
            }
          }
          else if (strAct.equals("delete")) {
            String strMENUNO = request.getParameter("MENUNO");
            String strPARENTNO = request.getParameter("PARENTNO");
            if (strMENUNO != null) {
                if (MenuMgr.DeleteMenu(strMENUNO))
                {
                  MenuMgr = null;
                  response.sendRedirect("zrsysmanage/menu/List.jsp?Menu_No="+strPARENTNO+"&isLoad=yes");
                }
                else
                {
                  this.isOk = false;
                  this.Msg = "删除菜单出错或有关联数据,不能删除!";
                  this.returnPath = "zrsysmanage/menu/EditFrm.jsp?Menu_No="+strPARENTNO+"&SelectedItems="+strMENUNO;
                }
            }
            else {
              this.isOk = false;
              this.Msg = "删除菜单出错!";
              this.returnPath = "zrsysmanage/menu/EditFrm.jsp?Menu_No="+strPARENTNO+"&SelectedItems="+strMENUNO;
            }
          }
          else if (strAct.equals("add")) {
            String strPARENTNO = request.getParameter("PARENTNO");
            BPIP_MENU Menu = new BPIP_MENU();
            Menu.fullDataFromRequest(request);

            if (MenuMgr.AddMenu(Menu,strPARENTNO))
               {
               {
                 Menu = null;
                 MenuMgr = null;
                 response.sendRedirect("zrsysmanage/menu/List.jsp?Menu_No="+strPARENTNO+"&isLoad=yes");
               }
               }
            else
               {this.Msg = "增加菜单失败！";}
          }


        }
        catch (Exception ex){
           this.Msg = "连接中间件服务器失败，请稍后再试！";
        }

    }
    this.out = response.getWriter();
    //this.GetBox();
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}
