var sell_manage_tool = null; 
$(function () { 
	initSellManageTool(); //建立Sell管理对象
	sell_manage_tool.init(); //如果需要通过下拉框查询，首先初始化下拉框的值
	$("#sell_manage").datagrid({
		url : 'Sell/list',
		fit : true,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		border : false,
		pagination : true,
		pageSize : 5,
		pageList : [5, 10, 15, 20, 25],
		pageNumber : 1,
		sortName : "sellId",
		sortOrder : "desc",
		toolbar : "#sell_manage_tool",
		columns : [[
			{
				field : "sellId",
				title : "销售id",
				width : 70,
			},
			{
				field : "productObj",
				title : "销售产品",
				width : 140,
			},
			{
				field : "customerObj",
				title : "销售客户",
				width : 140,
			},
			{
				field : "price",
				title : "销售单价",
				width : 70,
			},
			{
				field : "sellCount",
				title : "销售数量",
				width : 70,
			},
			{
				field : "sellDate",
				title : "销售日期",
				width : 140,
			},
			{
				field : "payWay",
				title : "支付方式",
				width : 140,
			},
			{
				field : "logistics",
				title : "物流公司",
				width : 140,
			},
			{
				field : "wayNumber",
				title : "运单号",
				width : 140,
			},
		]],
	});

	$("#sellEditDiv").dialog({
		title : "修改管理",
		top: "50px",
		width : 700,
		height : 515,
		modal : true,
		closed : true,
		iconCls : "icon-edit-new",
		buttons : [{
			text : "提交",
			iconCls : "icon-edit-new",
			handler : function () {
				if ($("#sellEditForm").form("validate")) {
					//验证表单 
					if(!$("#sellEditForm").form("validate")) {
						$.messager.alert("错误提示","你输入的信息还有错误！","warning");
					} else {
						$("#sellEditForm").form({
						    url:"Sell/" + $("#sell_sellId_edit").val() + "/update",
						    onSubmit: function(){
								if($("#sellEditForm").form("validate"))  {
				                	$.messager.progress({
										text : "正在提交数据中...",
									});
				                	return true;
				                } else { 
				                    return false; 
				                }
						    },
						    success:function(data){
						    	$.messager.progress("close");
						    	console.log(data);
			                	var obj = jQuery.parseJSON(data);
			                    if(obj.success){
			                        $.messager.alert("消息","信息修改成功！");
			                        $("#sellEditDiv").dialog("close");
			                        sell_manage_tool.reload();
			                    }else{
			                        $.messager.alert("消息",obj.message);
			                    } 
						    }
						});
						//提交表单
						$("#sellEditForm").submit();
					}
				}
			},
		},{
			text : "取消",
			iconCls : "icon-redo",
			handler : function () {
				$("#sellEditDiv").dialog("close");
				$("#sellEditForm").form("reset"); 
			},
		}],
	});
});

function initSellManageTool() {
	sell_manage_tool = {
		init: function() {
			$.ajax({
				url : "Product/listAll",
				type : "post",
				success : function (data, response, status) {
					$("#productObj_productId_query").combobox({ 
					    valueField:"productId",
					    textField:"productName",
					    panelHeight: "200px",
				        editable: false, //不允许手动输入 
					});
					data.splice(0,0,{productId:0,productName:"不限制"});
					$("#productObj_productId_query").combobox("loadData",data); 
				}
			});
			$.ajax({
				url : "Customer/listAll",
				type : "post",
				success : function (data, response, status) {
					$("#customerObj_customerId_query").combobox({ 
					    valueField:"customerId",
					    textField:"customerName",
					    panelHeight: "200px",
				        editable: false, //不允许手动输入 
					});
					data.splice(0,0,{customerId:0,customerName:"不限制"});
					$("#customerObj_customerId_query").combobox("loadData",data); 
				}
			});
		},
		reload : function () {
			$("#sell_manage").datagrid("reload");
		},
		redo : function () {
			$("#sell_manage").datagrid("unselectAll");
		},
		search: function() {
			var queryParams = $("#sell_manage").datagrid("options").queryParams;
			queryParams["productObj.productId"] = $("#productObj_productId_query").combobox("getValue");
			queryParams["customerObj.customerId"] = $("#customerObj_customerId_query").combobox("getValue");
			queryParams["sellDate"] = $("#sellDate").datebox("getValue"); 
			queryParams["payWay"] = $("#payWay").val();
			queryParams["logistics"] = $("#logistics").val();
			queryParams["wayNumber"] = $("#wayNumber").val();
			$("#sell_manage").datagrid("options").queryParams=queryParams; 
			$("#sell_manage").datagrid("load");
		},
		exportExcel: function() {
			$("#sellQueryForm").form({
			    url:"Sell/OutToExcel",
			});
			//提交表单
			$("#sellQueryForm").submit();
		},
		remove : function () {
			var rows = $("#sell_manage").datagrid("getSelections");
			if (rows.length > 0) {
				$.messager.confirm("确定操作", "您正在要删除所选的记录吗？", function (flag) {
					if (flag) {
						var sellIds = [];
						for (var i = 0; i < rows.length; i ++) {
							sellIds.push(rows[i].sellId);
						}
						$.ajax({
							type : "POST",
							url : "Sell/deletes",
							data : {
								sellIds : sellIds.join(","),
							},
							beforeSend : function () {
								$("#sell_manage").datagrid("loading");
							},
							success : function (data) {
								if (data.success) {
									$("#sell_manage").datagrid("loaded");
									$("#sell_manage").datagrid("load");
									$("#sell_manage").datagrid("unselectAll");
									$.messager.show({
										title : "提示",
										msg : data.message
									});
								} else {
									$("#sell_manage").datagrid("loaded");
									$("#sell_manage").datagrid("load");
									$("#sell_manage").datagrid("unselectAll");
									$.messager.alert("消息",data.message);
								}
							},
						});
					}
				});
			} else {
				$.messager.alert("提示", "请选择要删除的记录！", "info");
			}
		},
		edit : function () {
			var rows = $("#sell_manage").datagrid("getSelections");
			if (rows.length > 1) {
				$.messager.alert("警告操作！", "编辑记录只能选定一条数据！", "warning");
			} else if (rows.length == 1) {
				$.ajax({
					url : "Sell/" + rows[0].sellId +  "/update",
					type : "get",
					data : {
						//sellId : rows[0].sellId,
					},
					beforeSend : function () {
						$.messager.progress({
							text : "正在获取中...",
						});
					},
					success : function (sell, response, status) {
						$.messager.progress("close");
						if (sell) { 
							$("#sellEditDiv").dialog("open");
							$("#sell_sellId_edit").val(sell.sellId);
							$("#sell_sellId_edit").validatebox({
								required : true,
								missingMessage : "请输入销售id",
								editable: false
							});
							$("#sell_productObj_productId_edit").combobox({
								url:"Product/listAll",
							    valueField:"productId",
							    textField:"productName",
							    panelHeight: "auto",
						        editable: false, //不允许手动输入 
						        onLoadSuccess: function () { //数据加载完毕事件
									$("#sell_productObj_productId_edit").combobox("select", sell.productObjPri);
									//var data = $("#sell_productObj_productId_edit").combobox("getData"); 
						            //if (data.length > 0) {
						                //$("#sell_productObj_productId_edit").combobox("select", data[0].productId);
						            //}
								}
							});
							$("#sell_customerObj_customerId_edit").combobox({
								url:"Customer/listAll",
							    valueField:"customerId",
							    textField:"customerName",
							    panelHeight: "auto",
						        editable: false, //不允许手动输入 
						        onLoadSuccess: function () { //数据加载完毕事件
									$("#sell_customerObj_customerId_edit").combobox("select", sell.customerObjPri);
									//var data = $("#sell_customerObj_customerId_edit").combobox("getData"); 
						            //if (data.length > 0) {
						                //$("#sell_customerObj_customerId_edit").combobox("select", data[0].customerId);
						            //}
								}
							});
							$("#sell_price_edit").val(sell.price);
							$("#sell_price_edit").validatebox({
								required : true,
								validType : "number",
								missingMessage : "请输入销售单价",
								invalidMessage : "销售单价输入不对",
							});
							$("#sell_sellCount_edit").val(sell.sellCount);
							$("#sell_sellCount_edit").validatebox({
								required : true,
								validType : "integer",
								missingMessage : "请输入销售数量",
								invalidMessage : "销售数量输入不对",
							});
							$("#sell_sellDate_edit").datebox({
								value: sell.sellDate,
							    required: true,
							    showSeconds: true,
							});
							$("#sell_payWay_edit").val(sell.payWay);
							$("#sell_payWay_edit").validatebox({
								required : true,
								missingMessage : "请输入支付方式",
							});
							$("#sell_logistics_edit").val(sell.logistics);
							$("#sell_logistics_edit").validatebox({
								required : true,
								missingMessage : "请输入物流公司",
							});
							$("#sell_wayNumber_edit").val(sell.wayNumber);
							$("#sell_wayNumber_edit").validatebox({
								required : true,
								missingMessage : "请输入运单号",
							});
							$("#sell_sellMemo_edit").val(sell.sellMemo);
						} else {
							$.messager.alert("获取失败！", "未知错误导致失败，请重试！", "warning");
						}
					}
				});
			} else if (rows.length == 0) {
				$.messager.alert("警告操作！", "编辑记录至少选定一条数据！", "warning");
			}
		},
	};
}
