/* 取消订单 */
function cancel(id){
	var title = "取消订单";
	var box = WST.open({title:title,type:1,content:$('#cancelBox'),area: ['500px', '260px'],btn:['确定','取消'],end:function(){$('#cancelBox').hide();},btn:['确定','取消'],yes:function(){
		var params = {};
		params = WST.getParams('.cancel-ipt');
		if(params.reason <= 0){
			WST.msg('请选择取消订单的原因',{icon:2});
			return;
		}
		if(params.reason == 10000 && params.content.length<=0){
			WST.msg('请填写原因',{icon:2});
			return;
		}
		// 订单id
		params.id = id;
		var ll = WST.load({msg:'正在提交信息，请稍候...'});
		$.post(WST.U('admin/orders/cancel'),params,function(data){
			var json = WST.toAdminJson(data);
			if(json.status>0){
				$('#cancelForm')[0].reset();
				$('#rejectTr').hide();
				WST.msg(json.msg,{icon:1});
				loadGrid();
				layer.close(box);
		    	layer.close(ll);
			}else{
				WST.msg(json.msg,{icon:2});
			}
		});
	}});
}
function changeRejectType(v){
	(v==10000)?$('#rejectTr').show():$('#rejectTr').hide();
}




var mmg;
function initGrid(){
	var p = WST.arrayParams('.j-ipt');
	var h = WST.pageHeight();
    var cols = [
            {title:'订单编号', name:'orderNo', width: 50,sortable:true, renderer:function(val,item,rowIndex){
                var h = "";
	            h += "<img class='order-source' src='"+WST.conf.ROOT+"/wstshop/admin/view/img/order_source_"+item['orderSrc']+".png'>";	
                h += "<a style='cursor:pointer' onclick='javascript:showDetail("+ item['orderId'] +",1);'>"+item['orderNo']+"</a>";
	            return h;
            }},
            {title:'收货人', name:'userName', width: 120,sortable:true},
            {title:'实收金额', name:'realTotalMoney', width: 30,sortable:true, renderer:function(val,item,rowIndex){return '￥'+val;}},
            {title:'支付方式', name:'payTypeName' , width: 30,sortable:true},
            {title:'配送方式', name:'deliverTypeName', width: 30,sortable:true},
            {title:'下单时间', name:'createTime', width: 100,sortable:true},
            {title:'订单状态', name:'orderStatus', width: 30,sortable:true, renderer:function(val,item,rowIndex){
            	 if(item['orderStatus']==-1){
                     return "<span class='statu-no'><i class='fa fa-ban'></i> "+item.status+"</span>";
                 }else if(item['orderStatus']==-3){
                 	 return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else if(item['orderStatus']==2){
                     return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else{
            	 	return "<span class='statu-wait'><i class='fa fa-clock-o'></i> "+item.status+"</span>";
            	 }
            }},
            {title:'操作' , width: 30,name:'status', renderer:function(val,item,rowIndex){
            	var h = "";
	            h += "<a class='btn btn-blue' href='javascript:toView(" + item['orderId'] + ")'><i class='fa fa-search'></i>详情</a> ";
	            return h;
            }}
            ];
 
    mmg = $('.mmg').mmGrid({height: (h-88),indexCol: true,indexColWidth:50, cols: cols,method:'POST',nowrap:true,
        url: WST.U('admin/orders/pageQuery',p.join('&')), fullWidthRows: true, autoLoad: true,remoteSort: true,sortName:'createTime',sortStatus:'desc',
        plugins: [
            $('#pg').mmPaginator({})
        ]
    });  
}
function toView(id){
	location.href=WST.U('admin/orders/view','id='+id);
}
function loadGrid(){
	var p = WST.getParams('.j-ipt');
    p.page = 1;
	mmg.load(p);
}

function loadWaitDeliveryGrid(){
	initWaitDeliveryGrid();
}
function initWaitDeliveryGrid(){
	var p = WST.arrayParams('.j-ipt');
	var h = WST.pageHeight();
    var cols = [
            {title:'订单编号', name:'orderNo', width: 50,sortable:true, renderer:function(val,item,rowIndex){
                var h = "";
	            h += "<img class='order-source' src='"+WST.conf.ROOT+"/wstshop/admin/view/img/order_source_"+item['orderSrc']+".png'>";	
                h += "<a style='cursor:pointer' onclick='javascript:showDetail("+ item['orderId'] +",1);'>"+item['orderNo']+"</a>";
	            if(item['noticeDeliver']==1){
	        		return h += '<span class="notice"><img class="notice_icon" src="'+WST.conf.ROOT+'/wstshop/admin/view/img/nocite_deliver.png" alt="发货提醒" width="20" height="20" />尽快发货</span>';
	        	}
	        	return h;
            }},
            {title:'收货人', name:'userName', width: 30,sortable:true},
            { title: '用户留言', name: 'orderRemarks',width: 30,isSort: false,render:function(val,item,rowIndex){
	        	return '<span style="color:#ff6c00">'+WST.blank(val)+'</span>'
	        }},
	        { title: '订单总金额', name: 'totalMoney',width: 20,isSort: false},
            {title:'实收金额', name:'realTotalMoney', width: 20,sortable:true, renderer:function(val,item,rowIndex){return '￥'+val;}},
            {title:'支付方式', name:'payTypeName' , width: 20,sortable:true},
            {title:'配送方式', name:'deliverTypeName', width: 20,sortable:true},
            {title:'下单时间', name:'createTime', width: 80,sortable:true},
            {title:'订单状态', name:'orderStatus', width: 20,sortable:true, renderer:function(val,item,rowIndex){
            	 if(item['orderStatus']==-1){
                     return "<span class='statu-no'><i class='fa fa-ban'></i> "+item.status+"</span>";
                 }else if(item['orderStatus']==-3){
                 	 return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else if(item['orderStatus']==2){
                     return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else{
            	 	return "<span class='statu-wait'><i class='fa fa-clock-o'></i> "+item.status+"</span>";
            	 }
            }},
            {title:'操作' , width:200,name:'op',align:'center', renderer:function(val,item,rowIndex){
            	 var h = "";
	            h += "<a  class='btn btn-blue' href='javascript:view(" + item['orderId'] + ")'><i class='fa fa-search'></i>详情</a> ";
	            h += "<a class='btn btn-blue' href='javascript:deliver(" + item['orderId'] + ","+item['deliverType']+")'><i class='fa fa-truck'></i>发货</a> ";
	            if(item['allowDel']==1)h += "<a class='btn btn-red' href='javascript:del(" + item['orderId'] + ")'><i class='fa fa-trash-o'></i>删除</a> ";
	            h += "<a class='btn btn-red' href='javascript:cancel(" + item['orderId'] + ")'><i class='fa fa-ban'></i>取消订单</a> ";
	            return h;
            }}
            ];
 
    mmg = $('.mmg').mmGrid({height: (h-88),indexCol: true,indexColWidth:50, cols: cols,method:'POST',
        url: WST.U('admin/orders/waitDeliveryByPage',p.join('&')), fullWidthRows: true, autoLoad: true,remoteSort: true,sortName:'createTime',sortStatus:'desc',
        plugins: [
            $('#pg').mmPaginator({})
        ]
    });  
}
function loadDeliveryGrid(){
	var p = WST.arrayParams('.j-ipt');
	grid.set('url',WST.U('admin/orders/deliveryByPage',p.join('&')));
}
function initDeliveryGrid(){
	var p = WST.arrayParams('.j-ipt');
	var h = WST.pageHeight();
    var cols = [
            {title:'订单编号', name:'orderNo', width: 80,sortable:true, renderer:function(val,item,rowIndex){
               var h = "";
	            h += "<img class='order-source' src='"+WST.conf.ROOT+"/wstshop/admin/view/img/order_source_"+item['orderSrc']+".png'>";	
                h += "<a style='cursor:pointer' onclick='javascript:showDetail("+ item['orderId'] +",1);'>"+item['orderNo']+"</a>";
	            return h;
            }},
            {title:'收货人', name:'userName', width: 30,sortable:true},
            { title: '用户留言', name: 'orderRemarks',width: 30,isSort: false,render:function(val,item,rowIndex){
	        	return '<span style="color:#ff6c00">'+WST.blank(val)+'</span>'
	        }},
	        { title: '订单总金额', name: 'totalMoney',width: 30,isSort: false},
            {title:'实收金额', name:'realTotalMoney', width: 30,sortable:true, renderer:function(val,item,rowIndex){return '￥'+val;}},
            {title:'支付方式', name:'payTypeName' , width: 30,sortable:true},
            {title:'配送方式', name:'deliverTypeName', width: 30,sortable:true},
            {title:'下单时间', name:'createTime', width: 60,sortable:true},
            {title:'订单状态', name:'status', width: 30,sortable:true, renderer:function(val,item,rowIndex){
            	 if(item['orderStatus']==-1){
                     return "<span class='statu-no'><i class='fa fa-ban'></i> "+item.status+"</span>";
                 }else if(item['orderStatus']==-3){
                 	 return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else if(item['orderStatus']==2){
                     return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else{
            	 	return "<span class='statu-wait'><i class='fa fa-clock-o'></i> "+item.status+"</span>";
            	 }
            }},
            {title:'操作' , width: 120,name:'op', renderer:function(val,item,rowIndex){
            	var h = "";
	            h += "<a class='btn btn-blue' href='javascript:view(" + item['orderId'] + ")'><i class='fa fa-search'></i>详情</a> ";
	            if(item['allowDel']==1)h += "<a class='btn btn-red' href='javascript:del(" + item['orderId'] + ")'><i class='fa fa-trash-o'></i>删除</a> ";
	            return h;
	            }}
            ];
 
    mmg = $('.mmg').mmGrid({height: (h-88),indexCol: true,indexColWidth:50, cols: cols,method:'POST',nowrap:true,
        url: WST.U('admin/orders/deliveryByPage',p.join('&')), fullWidthRows: true, autoLoad: true,remoteSort: true,sortName:'createTime',sortStatus:'desc',
        plugins: [
            $('#pg').mmPaginator({})
        ]
    });  
}
function loadWaitPayGrid(){
	var p = WST.arrayParams('.j-ipt');
	grid.set('url',WST.U('admin/orders/waitPayByPage',p.join('&')));
}
function initWaitPayGrid(){
	var p = WST.arrayParams('.j-ipt');
	var h = WST.pageHeight();
    var cols = [
            {title:'订单编号', name:'orderNo', width: 80,sortable:true, renderer:function(val,item,rowIndex){
                var h = "";
	            h += "<img class='order-source' src='"+WST.conf.ROOT+"/wstshop/admin/view/img/order_source_"+item['orderSrc']+".png'>";	
                h += "<a style='cursor:pointer' onclick='javascript:showDetail("+ item['orderId'] +",1);'>"+item['orderNo']+"</a>";
	            return h;
            }},
            {title:'收货人', name:'userName', width: 30,sortable:true},
            {title: '用户留言', name: 'orderRemarks',isSort: false,render:function(val,item,rowIndex){
	        	return '<span style="color:#ff6c00">'+WST.blank(val)+'</span>'
	        }},
	        {title: '订单总金额', name: 'totalMoney',width: 30,isSort: false},
            {title:'实收金额', name:'realTotalMoney', width: 30,sortable:true, renderer:function(val,item,rowIndex){return '￥'+val;}},
            {title:'配送方式', name:'deliverTypeName', width: 30,sortable:true},
            {title:'下单时间', name:'createTime', width: 100,sortable:true},
            {title:'订单状态', name:'status', width: 30,sortable:true, renderer:function(val,item,rowIndex){
            	 if(item['orderStatus']==-1){
                     return "<span class='statu-no'><i class='fa fa-ban'></i> "+item.status+"</span>";
                 }else if(item['orderStatus']==-3){
                 	 return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else if(item['orderStatus']==2){
                     return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else{
            	 	return "<span class='statu-wait'><i class='fa fa-clock-o'></i> "+item.status+"</span>";
            	 }
            }},
            {title:'操作' , width: 250,name:'status',align:'center', renderer:function(val,item,rowIndex){
            	var h = "";
	            h += "<a class='btn btn-blue' href='javascript:view(" + item['orderId'] + ")'><i class='fa fa-search'></i>详情</a> ";
                h += "<a class='btn btn-blue' href='javascript:editOrderMoney(" + item['orderId'] + ")'><i class='fa fa fa-pencil'></i>修改价格</a> ";
	            if(item['allowDel']==1)h += "<a class='btn btn-red' href='javascript:del(" + item['orderId'] + ")'><i class='fa fa-trash-o'></i>删除</a> ";
	            h += "<a class='btn btn-red' href='javascript:cancel(" + item['orderId'] + ")'><i class='fa fa-ban'></i>取消订单</a> ";
	            return h;
            }}
            ];
 
    mmg = $('.mmg').mmGrid({height: (h-88),indexCol: true,indexColWidth:50, cols: cols,method:'POST',
        url: WST.U('admin/orders/waitPayByPage',p.join('&')), fullWidthRows: true, autoLoad: true,remoteSort: true,sortName:'createTime',sortStatus:'desc',
        plugins: [
            $('#pg').mmPaginator({})
        ]
    });  
}
function initAbnormalGrid(){
	var p = WST.arrayParams('.j-ipt');
	var h = WST.pageHeight();
    var cols = [
            {title:'订单编号', name:'orderNo', width: 120,sortable:true, renderer:function(val,item,rowIndex){
                var h = "";
	            h += "<img class='order-source' src='"+WST.conf.ROOT+"/wstshop/admin/view/img/order_source_"+item['orderSrc']+".png'>";	
                h += "<a style='cursor:pointer' onclick='javascript:showDetail("+ item['orderId'] +",1);'>"+item['orderNo']+"</a>";
	            return h;
            }},
            {title:'收货人', name:'userName', width: 80,sortable:true},
            {title: '用户留言', name: 'orderRemarks',isSort: false,render:function(val,item,rowIndex){
	        	return '<span style="color:#ff6c00">'+WST.blank(val)+'</span>'
	        }},
	        {title: '订单总金额', name: 'totalMoney',isSort: false},
            {title:'实收金额', name:'realTotalMoney', width: 30,sortable:true, renderer:function(val,item,rowIndex){return '￥'+val;}},
            {title:'配送方式', name:'deliverTypeName', width: 30,sortable:true},
            {title:'下单时间', name:'createTime', width: 100,sortable:true},
            {title:'订单状态', name:'status', width: 30,sortable:true, renderer:function(val,item,rowIndex){
            	 if(item['orderStatus']==-1){
                     return "<span class='statu-no'><i class='fa fa-ban'></i> "+item.status+"</span>";
                 }else if(item['orderStatus']==-3){
                 	 return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else if(item['orderStatus']==2){
                     return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else{
            	 	return "<span class='statu-wait'><i class='fa fa-clock-o'></i> "+item.status+"</span>";
            	 }
            }},
            {title:'操作' , width: 120,name:'status', renderer:function(val,item,rowIndex){
            	var h = "";
	            h += "<a class='btn btn-blue' href='javascript:view(" + item['orderId'] + ")'><i class='fa fa-search'></i>详情</a> ";
	            if(item['allowDel']==1)h += "<a class='btn btn-red' href='javascript:del(" + item['orderId'] + ")'><i class='fa fa-trash-o'></i>删除</a> ";
	            return h;
            }}
            ];
 
    mmg = $('.mmg').mmGrid({height: (h-88),indexCol: true,indexColWidth:50, cols: cols,method:'POST',nowrap:true,
        url: WST.U('admin/orders/abnormalByPage',p.join('&')), fullWidthRows: true, autoLoad: true,remoteSort: true,sortName:'createTime',sortStatus:'desc',
        plugins: [
            $('#pg').mmPaginator({})
        ]
    });  
}
function loadReceivedGrid(){
	var p = WST.arrayParams('.j-ipt');
	grid.set('url',WST.U('admin/orders/receivedByPage',p.join('&')));
}
function initReceivedGrid(){
	var p = WST.arrayParams('.j-ipt');
	var h = WST.pageHeight();
    var cols = [
            {title:'订单编号', name:'orderNo', width: 50,sortable:true, renderer:function(val,item,rowIndex){
               var h = "";
	            h += "<img class='order-source' src='"+WST.conf.ROOT+"/wstshop/admin/view/img/order_source_"+item['orderSrc']+".png'>";	
                h += "<a style='cursor:pointer' onclick='javascript:showDetail("+ item['orderId'] +",1);'>"+item['orderNo']+"</a>";
	            return h;
            }},
            {title:'收货人', name:'userName', width: 80,sortable:true},
            {title: '用户留言', name: 'orderRemarks',isSort: false,render:function(val,item,rowIndex){
	        	return '<span style="color:#ff6c00">'+WST.blank(val)+'</span>'
	        }},
	        {title: '订单总金额', name: 'totalMoney',isSort: false},
            {title:'实收金额', name:'realTotalMoney', width: 30,sortable:true, renderer:function(val,item,rowIndex){return '￥'+val;}},
            {title:'配送方式', name:'deliverTypeName', width: 30,sortable:true},
            {title:'下单时间', name:'createTime', width: 100,sortable:true},
            {title:'订单状态', name:'status', width: 30,sortable:true, renderer:function(val,item,rowIndex){
            	 if(item['orderStatus']==-1){
                     return "<span class='statu-no'><i class='fa fa-ban'></i> "+item.status+"</span>";
                 }else if(item['orderStatus']==-3){
                 	 return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else if(item['orderStatus']==2){
                     return "<span class='statu-yes'><i class='fa fa-check-circle'></i> "+item.status+"</span>";
            	 }else{
            	 	return "<span class='statu-wait'><i class='fa fa-clock-o'></i> "+item.status+"</span>";
            	 }
            }},
            {title:'操作' , width: 100,name:'status', renderer:function(val,item,rowIndex){
            	var h = "";
	            h += "<a class='btn btn-blue' href='javascript:view(" + item['orderId'] + ")'><i class='fa fa-search'></i>详情</a> ";
	            if(item['allowDel']==1)h += "<a class='btn btn-red' href='javascript:del(" + item['orderId'] + ")'><i class='fa fa-trash-o'></i>删除</a> ";
	            return h;
            }}
            ];
 
    mmg = $('.mmg').mmGrid({height: (h-88),indexCol: true,indexColWidth:50, cols: cols,method:'POST',nowrap:true,
        url: WST.U('admin/orders/receivedByPage',p.join('&')), fullWidthRows: true, autoLoad: true,remoteSort: true,sortName:'createTime',sortStatus:'desc',
        plugins: [
            $('#pg').mmPaginator({})
        ]
    });  
}
function view(id,from){
	location.href=WST.U('admin/orders/view',{id:id,from:from});
}
function del(id){
    var c = WST.confirm({content:'您确定要删除订单吗?',yes:function(){
		layer.close(c);
		var load = WST.load({msg:'正在删除，请稍后...'});
		$.post(WST.U('admin/orders/del'),{id:id},function(data,textStatus){
			layer.close(load);
		    var json = WST.toAdminJson(data);
		    if(json.status==1){
		    	WST.msg(json.msg,{icon:1});
		    	loadGrid();
		    }else{
		    	WST.msg(json.msg,{icon:2});
		    }
		});
	}});
}
function editOrderMoney(id){
	var ll = WST.load({msg:'正在加载记录，请稍候...'});
	$.post(WST.U('admin/orders/getMoneyByOrder'),{id:id},function(data){
    	layer.close(ll);
    	var json = WST.toAdminJson(data);
		if(json.status>0 && json.data){
			var tmp = json.data;
			$('#m_orderNo').html(tmp.orderId);
			$('#m_goodsMoney').html(tmp.goodsMoney);
			$('#m_deliverMoney').html(tmp.deliverMoney);
			$('#m_totalMoney').html(tmp.totalMoney);
			$('#m_realTotalMoney').html(tmp.realTotalMoney);
			WST.open({type: 1,title:"修改订单价格",shade: [0.6, '#000'],border: [0],
				content: $('#editMoneyBox'),area: ['450px', '280px'],btn: ['确定','取消'],end:function(){$('#editMoneyBox').hide();},
				yes:function(index, layero){
					var newOrderMoney = $('#m_newOrderMoney').val();
					WST.confirm({content:'您确定修改后的订单价格为¥<span class="j-warn-order-money">'+newOrderMoney+'</span>吗？',yes:function(cf){
						var ll = WST.load({msg:'正在提交信息，请稍候...'});
						$.post(WST.U('admin/orders/editOrderMoney'),{id:id,orderMoney:newOrderMoney},function(data){
							var json = WST.toAdminJson(data);
							if(json.status>0){
								$('#newOrderMoney').val();
								WST.msg(json.msg,{icon:1});
								loadGrid();
								layer.close(cf);
								layer.close(index);
						    	layer.close(ll);
							}else{
								WST.msg(json.msg,{icon:2});
							}
						});
					}});
				}
			});
		}
    });
}
function deliver(id,type){
	if(type==1){
    	var c = WST.confirm({content:'您确定用户已提货吗?',yes:function(){
		layer.close(c);
		var ll = WST.load({msg:'正在提交信息，请稍候...'});
			$.post(WST.U('admin/orders/deliver'),{id:id},function(data){
				var json = WST.toAdminJson(data);
				if(json.status>0){
					WST.msg(json.msg,{icon:1});
					loadGrid();
			    	layer.close(ll);
				}else{
					WST.msg(json.msg,{icon:2});
				}
			});
		}});
    }else{
        WST.open({type: 1,title:"请输入发货快递信息",shade: [0.6, '#000'], border: [0],
			content: $('#deliverBox'),area: ['350px', '190px'],btn: ['确定发货','取消'],end:function(){$('#deliverBox').hide();},
			yes:function(index, layero){
				var params = {};
				params.expressId = $('#expressId').val();
				params.expressNo = $('#expressNo').val();
                params.id = id;
				if(params.expressId == '' || params.expressNo == '') {
					WST.msg('请填写快递信息', {icon: 2});
					return;
				}
				var ll = WST.load({msg:'正在提交信息，请稍候...'});
					$.post(WST.U('admin/orders/deliver'),params,function(data){
					var json = WST.toAdminJson(data);
					if(json.status>0){
						$('#deliverForm')[0].reset();
						WST.msg(json.msg,{icon:1});
						loadGrid();
						layer.close(index);
				    	layer.close(ll);
					}else{
						WST.msg(json.msg,{icon:2});
					}
				});
			}
	    });
    }
}
function toExport(status,type){
	var params = {};
	params = WST.getParams('.j-ipt');
	params.orderStatus = status;
	params.type = type;
	var box = WST.confirm({content:"您确定要导出订单吗?",yes:function(){
		layer.close(box);
		location.href=WST.U('admin/orders/toExport',params);
         }});
}
function showDetail(id){
    parent.showBox({title:'订单详情',type:2,content:WST.U('admin/orders/view',{id:id,from:1}),area: ['1020px', '500px'],btn:['关闭']});
}