package zr.zrpower.queryengine;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.queryengine.service.QueryButton;
import zr.zrpower.queryengine.mode.QUERY_CONFIG_BUTTON;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.common.util.FunctionMessage;



public class QueryConfigButton
    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 strAction = request.getParameter("Act");
    if (!strAction.equals(null)) {
      if (strAction.equals("edit")) {
        EditButton(request, response);
      }

      if (strAction.equals("del")) {
        DeleteButton(request, response);
      }

      if (strAction.equals("add")) {
        AddButton(request, response);
      }

    }
  }

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

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

  //修改配置按扭信息
  private void EditButton(HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
    FunctionMessage fm = new FunctionMessage(1);
    QUERY_CONFIG_BUTTON Button = new QUERY_CONFIG_BUTTON();
    Button.fullDataFromRequest(request);

    String FID = request.getParameter("FID");

    try {

      QueryButton QueryButton = new QueryButton();
      fm = QueryButton.EditButton(Button);
      if (fm.getResult()) {
        response.sendRedirect("ZrQueryEngine/ButtonConfig/List.jsp?FID="+FID);
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      QueryButton = null;
    }
    catch (Exception ex) {
      this.Msg = "配置按扭管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "ZrQueryEngine/ButtonConfig/List.jsp?FID="+FID;
    //this.GetBox();

  }

  //删除配置按扭
  private void DeleteButton(HttpServletRequest request,
                            HttpServletResponse response) throws IOException {

    String FID = request.getParameter("FID");
    try {
      FunctionMessage fm = new FunctionMessage(1);
      QueryButton FlowPackage = new QueryButton();
      fm = FlowPackage.DeleteButton(request.getParameter("ID"));



      if (fm.getResult()) {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }

      response.sendRedirect("ZrQueryEngine/ButtonConfig/List.jsp?FID="+FID);
      FlowPackage = null;
    }
    catch (Exception ex) {
      this.Msg = "配置按扭管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }
    this.out = response.getWriter();
    this.returnPath = "ZrQueryEngine/ButtonConfig/List.jsp?FID="+FID;
    //this.GetBox();
  }

  //添加配置按扭
  private void AddButton(HttpServletRequest request,
                         HttpServletResponse response) throws
      IOException {
    FunctionMessage fm = new FunctionMessage(1);
    QUERY_CONFIG_BUTTON ButtonID = new QUERY_CONFIG_BUTTON();

    String FID = request.getParameter("FID");
    try {
      QueryButton FlowPackage = new QueryButton();
      ButtonID.fullDataFromRequest(request);
      fm = FlowPackage.AddButton(ButtonID);

      if (fm.getResult()) {
        response.sendRedirect("ZrQueryEngine/ButtonConfig/List.jsp?FID="+FID);
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      FlowPackage = null;
    }
    catch (Exception ex2) {
      this.Msg = "配置按扭管理中间件服务失败！<br>详细错误信息：<br>   " + ex2.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "ZrQueryEngine/ButtonConfig/List.jsp?FID="+FID;
    //this.GetBox();

  }

}
