package zr.zrpower.collengine.web.servlet;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletException;
import zr.zrpower.collectionengine.*;
import zr.zrpower.collectionengine.service.ConfigService;
import zr.zrpower.common.util.FunctionMessage;
import javax.servlet.http.HttpServletResponse;
import zr.zrpower.web.bean.MessageBox;

public class ActionSubConfigMsg extends MessageBox{
  public ActionSubConfigMsg() {
  }
  //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("editprint")) {
       EditPrint(request, response);
   }
  if (strAction.equals("delprint")) {
     DeletePrint(request, response);
  }
  if (strAction.equals("edit")) {
       EditConfig(request, response);
   }

   if (strAction.equals("del1")) {
     DelConfig(request, response);
   }

   //保存设置的字段属性
   if (strAction.equals("setfield")) {
     SaveCtlFieldInfo(request, response); //可控字段
   }
   //生成默认模板
   if (strAction.equals("templaet")) {
     Savetemplaet(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 EditPrint(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
     FunctionMessage fm = new FunctionMessage(1);
     COLL_DOC_PRINT RelID = new COLL_DOC_PRINT();
     RelID.fullDataFromRequest(request);
      String type=request.getParameter("DOCID");
     try {

	   ConfigService NoteRel = new ConfigService();

       fm = NoteRel.EditPrint(RelID);
       if (fm.getResult()) {
         RelID = null;
         NoteRel = null;
         response.sendRedirect("ZrCollEngine/ConfigMsg/PrintConfig/Right.jsp?SelectedItems="+type);
       }
       else {
         this.Msg = fm.getMessage();
         this.isOk = fm.getResult();
       }
     }
     catch (Exception ex) {
       this.Msg = "文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
     }

     this.out = response.getWriter();
     this.returnPath = "ZrCollEngine/ConfigMsg/PrintConfig/Right.jsp?SelectedItems="+type;
     //this.GetBox();
  }
 //删除打印模板配置
    private void DeletePrint(HttpServletRequest request,
                            HttpServletResponse response) throws IOException {
      String type=request.getParameter("DOCID");
      try {
        FunctionMessage fm = new FunctionMessage(1);
		ConfigService NotePrint = new ConfigService();
        fm = NotePrint.DeletePrint(request.getParameter("ID"));
        if (fm.getResult()) {
          this.Msg = fm.getMessage();
          this.isOk = fm.getResult();
        }
        else {
          this.Msg = fm.getMessage();
          this.isOk = fm.getResult();
        }
        NotePrint = null;
        response.sendRedirect("ZrCollEngine/ConfigMsg/PrintConfig/Right.jsp?SelectedItems="+type);
      }
      catch (Exception ex) {
        this.Msg = "连接文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
      }
      this.out = response.getWriter();
      this.returnPath = "ZrCollEngine/ConfigMsg/PrintConfig/Right.jsp?SelectedItems="+type;
      //this.GetBox();
    }





  //修改配置表
   private void EditConfig(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
     FunctionMessage fm = new FunctionMessage(1);
     CollectionInfo RelID = new CollectionInfo();
     RelID.fullDataFromRequest(request);
     try {

	   ConfigService NoteRel = new ConfigService();

       fm = NoteRel.editConfig(RelID);
       if (fm.getResult()) {
         NoteRel = null;
         response.sendRedirect("ZrCollEngine/ConfigMsg/MainConfig/List.jsp");
       }
       else {
         this.Msg = fm.getMessage();
         this.isOk = fm.getResult();
       }
     }
     catch (Exception ex) {
       this.Msg = "文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
     }

     this.out = response.getWriter();
     this.returnPath = "ZrCollEngine/ConfigMsg/MainConfig/List.jsp";
     //this.GetBox();
  }

  //删除配置文件
 private void DelConfig(HttpServletRequest request, HttpServletResponse response)throws  IOException{
    try {
	   ConfigService configManage = new ConfigService();

       configManage.delConfig(request.getParameter("ID"));
             configManage = null;
             response.sendRedirect("ZrCollEngine/ConfigMsg/MainConfig/List.jsp");
        }
        catch (Exception ex)
        {
        this.Msg = "连接客户端插件管理中间件服务失败！<br>详细错误信息："+ex.toString();
        }
       this.out = response.getWriter();
       this.returnPath = "ZrCollEngine/ConfigMsg/MainConfig/List.jsp";
       //this.GetBox();
   }


   private void SaveCtlFieldInfo(HttpServletRequest request, HttpServletResponse response) {
      try {
        String FID = request.getParameter("FID");
        String Fields = request.getParameter("CtlFieldInfoStr");
        ConfigService configManage = new ConfigService();
        configManage.SaveCtlFieldInfo(FID,Fields);

        response.sendRedirect("ZrCollEngine/ConfigMsg/MainConfig/setfield.jsp?ID="+FID);
      }
      catch (Exception ex) {

      }
    }

    private void Savetemplaet(HttpServletRequest request, HttpServletResponse response) {
          try {
            String ID = request.getParameter("ID");
            String type = request.getParameter("type");
            String fields = request.getParameter("fields");
            ConfigService configManage = new ConfigService();
            configManage.Savetemplaet(ID,type,fields);

            response.sendRedirect("ZrCollEngine/ConfigMsg/MainConfig/CloseWindow.jsp");
          }
          catch (Exception ex) {

          }
    }

}

