var mmg;
function initGrid(){
	var h = WST.pageHeight();
    var cols = [
            {title:'卡券号', name:'cardNo' ,width:30,sortable:false},
            {title:'密码', name:'cardPwd' ,width:30,sortable:false},
            {title:'状态', name:'saleNum' ,width:30,sortable:false,renderer: function(val,item,rowIndex){
            	return getUseStatus(item['isUse']);
            }},
            {title:'消费订单', name:'goodsStock' ,width:30,sortable:false,renderer: function(val,item,rowIndex){
            	return WST.blank(item['orderNo']);
            }},
            {title:'操作', name:'op' ,width:30,sortable:false,renderer:function(val,item,rowIndex){
            	var h = '';
	            if(item['isUse']==0){
			         h +='<a class="btn btn-blue" href="javascript:editCard('+item['id']+')"><i class="fa fa-trash-o"></i>编辑</a> ';
			    }
			    h +='<a class="btn btn-red" href="javascript:delCard('+item['id']+',0)"><i class="fa fa-trash-o"></i>删除</a>';
	            return h;
            }}
        ];
    mmg = $('.mmg').mmGrid({height: (h-85),indexCol: true, indexColWidth:50, cols: cols,method:'POST',checkCol:true,multiSelect:true,
        url: WST.U('admin/goodsvirtuals/stockByPage','id='+$('#vid').val()), fullWidthRows: true, autoLoad: true,remoteSort: true,sortName:'goodsSn',sortStatus:'desc',
        plugins: [
            $('#pg').mmPaginator({})
        ]
    });
}
function loadGrid(){
	var params = WST.getParams('.s-ipt');
	params.id =  $('#vid').val();
	mmg.load(params);
}
function getUseStatus(v){
   switch(v){
   	  case 0:return '未使用';
   	  case 1:return '已下单';
   }
}
function closeWin(){
	layer.close(w);
}
function addCardFunc(id,isContinue){
	var params =WST.getParams('.j-ipt');
	params.id = id;
	params.goodsId = $('#vid').val();
	if(params.cardNo=='' || params.cardPwd=='' || params.lastDate==''){
		WST.msg('请输入完整卡券信息',{icon:2,offset: '100px'});
		return;
	}
	ll = WST.load({msg:'数据处理中，请稍候...',offset: '100px'});
	$.post(WST.U('admin/goodsvirtuals/'+((params.id==0)?"add":"edit")),params,function(data){
		layer.close(ll);
		var json = WST.toAdminJson(data);
		if(json.status==1){
			loadGrid(0);
			if(isContinue){
                $('#cardForm')[0].reset();
			}else{
				closeWin();
			}
			WST.msg(json.msg, {icon: 1,offset: '100px'});
		}else{
			WST.msg(json.msg, {icon: 2,offset: '100px'});
		}
	});
}
var ll,w;
function editCard(id,goodsId){
	ll = WST.load({msg:'正在加载信息，请稍候...'});
	$.post(WST.U('admin/goodsvirtuals/'+((id==0)?'toAdd':'toEdit')),{id:id},function(data){
		layer.close(ll);
		w = WST.open({
			    type: 1,
			    title:"新增卡券",
			    shade: [0.6, '#000'],
			    border: [0],
			    content: data,
			    area: ['400px', '180px']
			});
	});
}

function delCard(id,v){
	if(v==1){
		var rows = mmg.selectedRows();
		if(rows.length==0){
			 WST.msg('请选择要删除的卡券号',{icon:2});
			 return;
		}
		var ids = [];
		for(var i=0;i<rows.length;i++){
	       ids.push(rows[i]['id']); 
		}
		id = ids.join(',');
	}
	var c = WST.confirm({content:'您确定要删除卡券吗?',yes:function(){
		layer.close(c);
		var load = WST.load({msg:'正在删除，请稍后...'});
		$.post(WST.U('admin/goodsvirtuals/del'),{ids:id,id:$('#vid').val()},function(data,textStatus){
			layer.close(load);
		    var json = WST.toAdminJson(data);
		    if(json.status==1){
		    	$('#all').prop('checked',false);
		    	loadGrid();
		    }else{
		    	WST.msg(json.msg,{icon:2});
		    }
		});
	}});
}

var uploading = null;
$(function(){
	var uploader = WST.upload({
        server:WST.U('admin/goodsvirtuals/importCards'),pick:'#importBtn',
    	formData: {dir:'temp',goodsId:$('#vid').val()},
    	callback:function(f,file){
    		layer.close(uploading);
    		uploader.removeFile(file);
    		var json = WST.toAdminJson(f);
    		if(json.status==1){
    			uploader.refresh();
    		    WST.msg('导入数据成功!已导入数据'+json.importNum+"条", {icon: 1});
    		    loadGrid();
    		}else{
    			WST.msg('导入数据失败,出错原因：'+json.msg, {icon: 5});
    		}
	    },
	    progress:function(rate){
	    	uploading = WST.msg('正在导入数据，请稍后...');
	    }
    });
});
