var grid;
function toView(id){
	location.href=WST.U('admin/orders/view',{id:id});
}
function showDetail(id,from){
    parent.showBox({title:'订单详情',type:2,content:WST.U('admin/orders/view',{id:id,from:1}),area: ['1020px', '500px'],btn:['关闭']});
}
function initRefundGrid(){
    var p = WST.arrayParams('.j-ipt');
	var h = WST.pageHeight();
    var cols = [
            {title:'订单编号', name:'orderNo', width: 50,sortable:true,renderer: function (val,item,rowIndex){
	        	return "<a style='cursor:pointer' onclick='javascript:showDetail("+ item['orderId'] +",1);'>"+item['orderNo']+"</a>";
	        }},
            {title:'申请人', name: 'loginName',sortable: false},
            {title:'配送方式', name:'deliverTypeName', width: 30,sortable:true},
            {title:'实收金额', name: 'realTotalMoney',sortable:false,renderer: function (val,item,rowIndex){
	        	return "¥"+val;
	        }},
	        {title:'申请退款金额', name:'backMoney',sortable:false,renderer: function (val,item,rowIndex){
	        	return "¥"+val;
	        }},
	        {title:'申请时间', name: 'createTime',sortable:false},
	        {title:'退款状态', name: 'isRefund',renderer: function (val,item,rowIndex){
	        	return (item['isRefund']==1)?"已退款":"未退款";
	        }},
	        {title:'退款备注', name:'refundRemark'},
            {title:'操作' , width: 100,name:'status', renderer:function(val,item,rowIndex){
	            var h = '';
	            if(item['isRefund']==0){
	            	if(WST.GRANT.TKDD_04)h += "<a class='btn btn-blue' href='javascript:toRefund(" + item['refundId'] + ")'><i class='fa fa-search'></i>退款</a> ";
	            }
	            h += "<a  class='btn btn-blue' href='javascript:toView(" + item['orderId'] + ",0)'><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/orderrefunds/refundPageQuery',p.join('&')), 
	        		fullWidthRows: true, 
	        		autoLoad: true,
	        		remoteSort: true,
	        		sortName:'createTime',
	        		sortStatus:'desc',
			        plugins: [
			            $('#pg').mmPaginator({})
			        ]
	    	 });
}

function loadGrid(){
	var p = WST.getParams('.j-ipt');
    p.page = 1;
	mmg.load(p);
}
var w;
function toRefund(id){
	var ll = WST.msg('正在加载信息，请稍候...');
	$.post(WST.U('admin/orderrefunds/toRefund',{id:id}),{},function(data){
		layer.close(ll);
		w =WST.open({type: 1,title:"订单退款",shade: [0.6, '#000'],offset:'50px',border: [0],content:data,area: ['550px', '410px']});
	});
}
function orderRefund(id){
	$('#editFrom').isValid(function(v){
		if(v){
        	var params = {};
        	params.backMoney = $('#backMoney').val();
        	params.backScore = $('#backScore').val();
        	params.content = $.trim($('#content').val());
        	params.id = id;
        	ll = WST.msg('正在加载信息，请稍候...');
		    $.post(WST.U('admin/orderrefunds/orderRefund'),params,function(data){
		    	layer.close(ll);
		    	var json = WST.toAdminJson(data);
				if(json.status==1){
					layer.close(w);
					WST.msg(json.msg, {icon: 1,time:2500},function(){
                        loadGrid();
                    });
				}else{
					WST.msg(json.msg, {icon: 2});
				}
		   });
		}
    })
}