package imis_mate.servlet;

import imis_mate.DAO.MateBuyDAO;
import imis_mate.DAO.MaterialBuyDetailDAO;
import imis_mate.bean.MaterialBuyBean;
import imis_mate.bean.MaterialBuyDetailBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MaterialBuyInsertServlet extends HttpServlet {
	private String buyNo;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pattern = request.getParameter("pattern");
		String success = "数据添加成功!";
		String fail = "数据添加失败!";

		// 添加明细按扭操作
		if (pattern.equals("insertdetail")) {
			if (BuyNo(request, response)) {
				insertMaterialBuyDetail(request, response);

				request.getRequestDispatcher(
						"../imis_mate/MaterialBuyDetail_insert.jsp").forward(
						request, response);

			} else {

				request.setAttribute("MaterialTypeNoExisted", "该采购编号已存在");
				request.getRequestDispatcher(
						"../imis_mate/MaterialBuyTable_insert.jsp").forward(
						request, response);
			}
		}

		// 查询明细按扭
		else if (pattern.equals("selectDetail")) {
			if (BuyNo(request, response)) {
			selectDetail(request,response);
			request.getRequestDispatcher(
			"../imis_mate/MaterialBuyTable_insert.jsp").forward(
			request, response);}
			else{
				request.setAttribute("MaterialTypeNoExisted", "该采购编号已存在");
				request.getRequestDispatcher(
						"../imis_mate/MaterialBuyTable_insert.jsp").forward(
						request, response);}

		}
		
		// 提交明细按扭
		
		else if (pattern.equals("updataDetail")) {
			
			updataDetail(request, response);
			request.getRequestDispatcher(
					"../imis_mate/MaterialBuyTable_insert.jsp").forward(
					request, response);
			
			

		}
		// 添加下一个明细按扭
		else if (pattern.equals("updatNextDetail")) {
			updatNextDetail(request,response);
			request.getRequestDispatcher(
			"../imis_mate/MaterialBuyDetail_insert.jsp").forward(
			request, response);

		}
		// 信息提交
		else if (pattern.equals("insertall")) {
			if(insertAll(request, response)){
					
					request.setAttribute("success", "信息添加成功");
					request.getRequestDispatcher("../imis_mate/successful.jsp").forward(request, response);
				
				
			}
			else{
				request.setAttribute("fail", "信息添加失败");
				request.getRequestDispatcher("../imis_mate/fail.jsp").forward(request,response);
			}
			}
		else if(pattern.equals("goback")){
			goback(request,response);
		
			
			request.getRequestDispatcher(
			"../imis_mate/MaterialBuyTable_insert.jsp").forward(
			request, response);
		}

		}

	private void goback(HttpServletRequest request, HttpServletResponse response) {
		// 明细添加返回按纽
		
		MaterialBuyBean mbb = (MaterialBuyBean) request.getSession().getAttribute("MaterialBuyBeanNew");
		HttpSession session = request.getSession();
		HashMap materialbuy = new HashMap();
		

		// MaterialBuyBeanNew为未插入的采购单信息
		request.setAttribute("buyNo", mbb.getBuyNo());
		if(session.getAttribute("materialbuy")==null){
			ArrayList list = new ArrayList();
		materialbuy.put(mbb.getBuyNo(),list);
		}
		else
		{
			materialbuy = (HashMap) request.getSession().getAttribute("materialbuy");
		}
			
		ArrayList temp = new ArrayList();
		
		if((materialbuy.get(mbb.getBuyNo()))!=null)
				{	temp = (ArrayList) materialbuy.get(mbb.getBuyNo());}
		
		//System.out.println("buyNo"+mbb.getBuyNo()+"list"+temp.size());
		
		session.setAttribute("MaterialBuyBeanNew", mbb);
		session.setAttribute("materialDetailList", temp);//采购的list
		request.setAttribute("MaterialBuyBean", mbb);
		request.getSession().removeAttribute("new");
		
		
		
		
	}

	private void updatNextDetail(HttpServletRequest request, HttpServletResponse response) {
		// 添加下一条明细
		
		HashMap materialbuy = new HashMap(); 
		MaterialBuyBean mbb = new MaterialBuyBean();
		HttpSession session = request.getSession();
		mbb=(MaterialBuyBean) session.getAttribute("MaterialBuyBeanNew");
		
		
		if(session.getAttribute("materialbuy")==null){
			ArrayList list = new ArrayList();
		materialbuy.put(mbb.getBuyNo(),list);
		}
		else{
			
			materialbuy = (HashMap) request.getSession().getAttribute("materialbuy");
	}
		ArrayList temp = new ArrayList();
		
		if((materialbuy.get(mbb.getBuyNo()))!=null)
				{	temp = (ArrayList) materialbuy.get(mbb.getBuyNo());
				//System.out.println(temp.size());
				}
		
		
		MaterialBuyDetailBean mbdb = new MaterialBuyDetailBean();
		mbdb.setBuyNo(request.getParameter("buyNo"));
		mbdb.setMaterialTypeName(request.getParameter("materialTypeName"));
		mbdb.setMaModel(request.getParameter("maModel"));
		mbdb.setMaBrand(request.getParameter("maBrand"));
		mbdb.setMaMadeIn(request.getParameter("maMadeIn"));
		mbdb.setMaterialTypeNo(request.getParameter("materialTypeNo"));
		mbdb.setPriceUnit((request.getParameter("priceUnit")));
		mbdb.setInCount((request.getParameter("inCount")));
		
		
		temp.add(mbdb);
		
		materialbuy.remove(mbb.getBuyNo());
		materialbuy.put(mbb.getBuyNo(),temp);
		
		session.setAttribute("materialbuy", materialbuy); //hashmap
		session.setAttribute("materialDetailList", temp);//采购的list
		request.setAttribute("MaterialBuyBean", mbb);
		request.setAttribute("buyNo", mbb.getBuyNo());
	}

	private void selectDetail(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		MaterialBuyBean mbb = new MaterialBuyBean();
		HttpSession session = request.getSession();
		HashMap materialbuy = new HashMap();
		mbb.setBuyNo(request.getParameter("buyNo").trim());
		mbb.setPayType(request.getParameter("payType"));
		mbb.setRePay(request.getParameter("rePay"));
		mbb.setAcPay(request.getParameter("acPay"));
		mbb.setInDate(request.getParameter("inDate"));
		mbb.setBuyDep(request.getParameter("buyDep"));
		mbb.setBuyBy(request.getParameter("buyBy"));
		mbb.setApplyDep(request.getParameter("applyDep"));
		mbb.setApplyBy(request.getParameter("applyBy"));
		mbb.setUseDep(request.getParameter("useDep"));
		mbb.setUseBy(request.getParameter("useBy"));
		mbb.setBuyFor(request.getParameter("buyFor"));
		mbb.setSupplyNo(request.getParameter("supplyNo"));
		mbb.setPerBy(request.getParameter("perBy"));
		mbb.setReMark(request.getParameter("remark"));

		// MaterialBuyBeanNew为未插入的采购单信息
		request.setAttribute("buyNo", mbb.getBuyNo());
		if(session.getAttribute("materialbuy")==null){
			ArrayList list = new ArrayList();
		materialbuy.put(mbb.getBuyNo(),list);
		}
		else
		{
			materialbuy = (HashMap) request.getSession().getAttribute("materialbuy");
		}
			
		ArrayList temp = new ArrayList();
		
		if((materialbuy.get(mbb.getBuyNo()))!=null)
				{	temp = (ArrayList) materialbuy.get(mbb.getBuyNo());}
		
		//System.out.println("buyNo"+mbb.getBuyNo()+"list"+temp.size());
		
		session.setAttribute("MaterialBuyBeanNew", mbb);
		session.setAttribute("materialDetailList", temp);//采购的list
		request.setAttribute("MaterialBuyBean", mbb);
		
		
	}

	//信息提交
	private boolean insertAll(HttpServletRequest request, HttpServletResponse response) {
		boolean flag = false;
		HashMap materialbuy = new HashMap(); 
		HttpSession session = request.getSession();
		MaterialBuyBean mbb = new MaterialBuyBean();
		materialbuy = (HashMap) session.getAttribute("materialbuy");
		
		mbb.setBuyNo(request.getParameter("buyNo"));
		mbb.setPayType(request.getParameter("payType"));
		mbb.setRePay(request.getParameter("rePay"));
		mbb.setAcPay(request.getParameter("acPay"));
		mbb.setInDate(request.getParameter("inDate"));
		mbb.setBuyDep(request.getParameter("buyDep"));
		mbb.setBuyBy(request.getParameter("buyBy"));
		mbb.setApplyDep(request.getParameter("applyDep"));
		mbb.setApplyBy(request.getParameter("applyBy"));
		mbb.setUseDep(request.getParameter("useDep"));
		mbb.setUseBy(request.getParameter("useBy"));
		mbb.setBuyFor(request.getParameter("buyFor"));
		mbb.setSupplyNo(request.getParameter("supplyNo"));
		mbb.setPerBy(request.getParameter("perBy"));
		mbb.setReMark(request.getParameter("remark"));
		ArrayList temp = new ArrayList();
		temp = (ArrayList) materialbuy.get(mbb.getBuyNo());
		
		MateBuyDAO mbdao = new MateBuyDAO();
		MaterialBuyDetailDAO mbdd = new MaterialBuyDetailDAO();
		if(mbdao.save(mbb)){
			flag = mbdd.save(temp);
		};
		session.removeAttribute("materialbuy");
		session.removeAttribute("MaterialBuyBeanNew");
		session.removeAttribute("materialDetailList");
		return flag;
		
	}

	// 将采购明细放如list中
	private void updataDetail(HttpServletRequest request, HttpServletResponse response) {
		
		
		HashMap materialbuy = new HashMap(); 
		MaterialBuyBean mbb = new MaterialBuyBean();
		HttpSession session = request.getSession();
		mbb=(MaterialBuyBean) session.getAttribute("MaterialBuyBeanNew");
		
		
		if(session.getAttribute("materialbuy")==null){
			ArrayList list = new ArrayList();
		materialbuy.put(mbb.getBuyNo(),list);
		}
		else{
			
			materialbuy = (HashMap) request.getSession().getAttribute("materialbuy");
	}
		ArrayList temp = new ArrayList();
		
		if((materialbuy.get(mbb.getBuyNo()))!=null)
				{	temp = (ArrayList) materialbuy.get(mbb.getBuyNo());
				//System.out.println(temp.size());
				}
		
		if(session.getAttribute("new")!=null){
		MaterialBuyDetailBean mbdb = new MaterialBuyDetailBean();
		mbdb.setBuyNo(request.getParameter("buyNo"));
		mbdb.setMaterialTypeName(request.getParameter("materialTypeName"));
		mbdb.setMaModel(request.getParameter("maModel"));
		mbdb.setMaBrand(request.getParameter("maBrand"));
		mbdb.setMaMadeIn(request.getParameter("maMadeIn"));
		mbdb.setMaterialTypeNo(request.getParameter("materialTypeNo"));
		mbdb.setPriceUnit((request.getParameter("priceUnit")));
		mbdb.setInCount((request.getParameter("inCount")));
		
		
		temp.add(mbdb);
		
		}
		
		materialbuy.remove(mbb.getBuyNo());
		materialbuy.put(mbb.getBuyNo(),temp);
		
		session.setAttribute("materialbuy", materialbuy); //hashmap
		session.setAttribute("materialDetailList", temp);//采购的list
		request.setAttribute("MaterialBuyBean", mbb);
//		request.removeAttribute("materialTypeName");
	//	request.removeAttribute("buyNo");
		session.removeAttribute("new");
		
	}

	// 判断所输的信息是否存在
	private boolean BuyNo(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		boolean flag = false;
		MateBuyDAO dao = new MateBuyDAO();
		String buyNo = request.getParameter("buyNo").trim();
		if (dao.getMateBuyByBuyNo(buyNo) != null) {
			flag = false;
		} else {
			flag = true;
		}
		return flag;

	}

	//提交明细按纽操作
	private void insertMaterialBuyDetail(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		MaterialBuyBean mbb = new MaterialBuyBean();
		mbb.setBuyNo(request.getParameter("buyNo").trim());
		mbb.setPayType(request.getParameter("payType"));
		mbb.setRePay(request.getParameter("rePay"));
		mbb.setAcPay(request.getParameter("acPay"));
		mbb.setInDate(request.getParameter("inDate"));
		mbb.setBuyDep(request.getParameter("buyDep"));
		mbb.setBuyBy(request.getParameter("buyBy"));
		mbb.setApplyDep(request.getParameter("applyDep"));
		mbb.setApplyBy(request.getParameter("applyBy"));
		mbb.setUseDep(request.getParameter("useDep"));
		mbb.setUseBy(request.getParameter("useBy"));
		mbb.setBuyFor(request.getParameter("buyFor"));
		mbb.setSupplyNo(request.getParameter("supplyNo"));
		mbb.setPerBy(request.getParameter("perBy"));
		mbb.setReMark(request.getParameter("remark"));

		// MaterialBuyBeanNew为未插入的采购单信息
		request.setAttribute("buyNo", mbb.getBuyNo());
		request.getSession().setAttribute("MaterialBuyBeanNew", mbb);
		
	}

}