<%@page contentType="text/html; charset=UTF-8"%>
<%@include file="/inc/init.jsp"%>
<%@include file="/inc/checklogin.jsp"%>
<%
	String action = pu.getString("action");
	String referer = pu.referer();
	Order_list_db oldb = new Order_list_db();
	int orderid = pu.getInt("orderid");
	String orderState = pu.getString("state");
	String price = pu.getString("price");
	String admin_remark = pu.getString("admin_remark");
	String emsnumber = pu.getString("kd_number");
	String address = pu.getString("address");
	String kd_type = pu.getString("kd_type");
	String fromkey = pu.getString("keyword");
	String signdate = pu.getString("signdate");
	String surname2 = pu.getString("surname2");//备注尊称
	//添加产品
	if (action.equals("update")) {
		oldb.update(orderid, "state", orderState);
		oldb.update(orderid, "price", price);
		oldb.update(orderid, "admin_remark", admin_remark);
		oldb.update(orderid, "address", address);
		oldb.update(orderid, "kd_type", kd_type);
		oldb.update(orderid, "retain3", fromkey);
		oldb.update(orderid, "retain4", signdate);
		oldb.update(orderid, "retain5", surname2);
		if (StringUtil.isFine(emsnumber)) {
			DOC orderDOC = oldb.getOneByKDNumber(emsnumber);
			if (!DOCUtil.isFine(orderDOC)
					|| orderDOC.geti("orderid") == orderid) {//未使用过此或是自身
				oldb.update(orderid, "kd_number", emsnumber);
				boolean FHsms=pu.getInt("FHsms")==1;//是否发货短信
				DOC rsDoc=new DOC();
				if(FHsms){
					SmsUtil su=new SmsUtil();
					rsDoc=su.sendSms(orderid,"3");
				}
				session.setAttribute("msg", "订单 " + orderid + " 更新成功！"+(FHsms?"<br />短信发送结果："+rsDoc.get("message"):""));
			} else {
				session.setAttribute("msg", "订单 " + orderid
						+ " 更新部分成功，<br />快递号" + emsnumber + "已经被订单"
						+ orderDOC.get("orderid") + "使用！");
			}
		} else {
			oldb.update(orderid, "kd_number", emsnumber);
			session.setAttribute("msg", "订单 " + orderid + " 更新成功！");
		}
		response.sendRedirect(referer);
		return;
	} else if (action.equals("del")) {
		boolean isOk = false;
		DOC doc = oldb.getOne(orderid);
		if (DOCUtil.isFine(doc)) { //有内容才进去下一步骤 
			int productid = doc.geti("productid");
			int classid = doc.geti("classid");
			price = doc.get("price"); //共用上面的一个变量
			String price2 = doc.get("price2");
			String number = doc.get("number");
			int pay_type = doc.geti("pay_type");
			String state = doc.get("state");
			String contact = doc.get("contact");
			String phone = doc.get("phone");
			int gender = doc.geti("gender");
			String address2 = doc.get("address");
			String remark = doc.get("remark");
			String ip = doc.get("ip");
			String create_time = doc.get("create_time");
			admin_remark = doc.get("admin_remark");
			kd_type = doc.get("kd_type");
			String kd_number = doc.get("kd_number");
			String retain1 = doc.get("retain1");
			String retain2 = doc.get("retain2");
			String retain3 = doc.get("retain3");
			String retain4 = doc.get("retain4");
			String retain5 = doc.get("retain5");
			String retain6 = doc.get("retain6");
			String retain7 = doc.get("retain7");
			String retain8 = doc.get("retain8");
			String retain9 = doc.get("retain9");
			String retain10 = doc.get("retain10");
			String retain11 = doc.get("retain11");
			String retain12 = doc.get("retain12");
			String retain13 = doc.get("retain13");
			String retain14 = doc.get("retain14");
			String retain15 = doc.get("retain15");
			Order_list_reback olr = new Order_list_reback();
			isOk = olr.insert(orderid, productid, classid, price,
					price2, number, pay_type, state, contact, phone,
					gender, address2, remark, admin_remark, ip,
					create_time, kd_type, kd_number, retain1, retain2,
					retain3, retain4, retain5, retain6, retain7,
					retain8, retain9, retain10, retain11, retain12,
					retain13, retain14, retain15);
		}
		doc = null;
		if (isOk) {
			oldb.delete(orderid);
			session.setAttribute("msg", "订单 " + orderid + " 删除成功！");
			response.sendRedirect(referer);
			return;
		} else {
			session.setAttribute("msg", "订单 " + orderid + " 删除失败！");
			response.sendRedirect(referer);
			return;
		}
	} else if(action.equals("single")){
		oldb.update(orderid, "state", orderState);
		if (StringUtil.isFine(emsnumber)) {
			DOC orderDOC = oldb.getOneByKDNumber(emsnumber);
			if (!DOCUtil.isFine(orderDOC)
					|| orderDOC.geti("orderid") == orderid) {//未使用过此或是自身
				oldb.update(orderid, "kd_number", emsnumber);
				oldb.update(orderid, "kd_type", kd_type);
				boolean FHsms=pu.getInt("FHsms")==1;//是否发货短信
				DOC rsDoc=new DOC();
				if(FHsms){
					SmsUtil su=new SmsUtil();
					rsDoc=su.sendSms(orderid,"3");
				}
				session.setAttribute("msg", "订单 " + orderid + " 更新成功！"+(FHsms?"<br />短信发送结果："+rsDoc.get("message"):""));
			} else {
				session.setAttribute("msg", "订单 " + orderid
						+ " 更新部分成功，<br />快递号" + emsnumber + "已经被订单"
						+ orderDOC.get("orderid") + "使用！");
			}
		} else {
			oldb.update(orderid, "kd_number", emsnumber);
			oldb.update(orderid, "kd_type", kd_type);
			session.setAttribute("msg", "订单 " + orderid + " 更新成功！");
		}
		response.sendRedirect(referer);
		return;
	} else if(action.equals("batchUpdateKd")){
		String[] kd_numbers=pu.getString("kd_number").split(",");
		for(int i=0;i<kd_numbers.length;i++){
			oldb.updateByKdNumber(kd_numbers[i], "state", orderState);
		}
		session.setAttribute("msg", "批量更新"+kd_numbers.length+"条记录成功！");
		response.sendRedirect(referer);
		return;
	} else if(action.equals("batchUpdateState")){
		String[] orderids=pu.getString("orderids").split(",");
		for(int i=0;i<orderids.length;i++){
			oldb.update(IntUtil.s2i(orderids[i]), "state", orderState);
		}
		session.setAttribute("msg", "批量更新"+orderids.length+"条记录成功！");
		response.sendRedirect(referer);
		return;
	}
%>