var mmg1,mmg2,mmg,h;
function initTab(p){
	var element = layui.element;
	var isInit = false;
	element.on('tab(msgTab)', function(data){
	     if(data.index==1){
	        if(!isInit){
	           isInit = true;
	           flowGridInit(p);
	        }else{
	           loadFlowGrid(p);
	        }
	     }
	});
	userGridInit(p);
}

function userGridInit(p){
	h = WST.pageHeight();
    var cols = [
            {title:'账号', name:'loginName', width: 50,sortable: true},
            {title:'名称', name:'userName' ,width:80,sortable: true},
            {title:'可用金额', name:'userMoney' ,width:200,sortable: true,renderer: function (rowdata, rowindex, value){
	        	return '￥'+rowindex['userMoney'];
	        }},
            {title:'冻结金额', name:'lockMoney' ,width:70,sortable: true,renderer: function (rowdata, rowindex, value){
	        	return '￥'+rowindex['lockMoney'];
	        }},
            {title:'充值送', name:'rechargeMoney' ,width:70,sortable: true,renderer: function (rowdata, rowindex, value){
                return '￥'+rowindex['rechargeMoney'];
            }},
            {title:'操作', name:'op' ,width:20,renderer: function (val,item,rowIndex){
	        	return '<a class="btn btn-blue" href="javascript:tologmoneys(0,'+item['userId']+')"><i class="fa fa-search"></i>查看</a>';
	        }}
            ];
 
    mmg1 = $('.mmg1').mmGrid({
        height: h-127,
        indexCol: true,
        indexColWidth:50, 
        cols: cols,
        method:'POST',
        url: WST.U('admin/logmoneys/pageQueryByUser'), 
        fullWidthRows:true, 
        autoLoad: true,
        remoteSort:true ,
        sortName: 'userMoney',
        sortStatus: 'desc',
        plugins: [
            $('#pg1').mmPaginator({})
        ]
    });
}
function flowGridInit(p){
    var h = WST.pageHeight();
    var laydate = layui.laydate;
    laydate.render({
        elem: '#startDate'
    });
    laydate.render({
        elem: '#endDate'
    });
    var cols = [
            {title:'来源', name:'dataSrc', width: 30},
            {title:'账号', name:'loginName', width: 30},
            {title:'金额', name:'money' ,width:20,renderer: function (rowdata, rowindex, value){
	        	if(rowindex['moneyType']==1){
                    return '<font color="red">+￥'+rowindex['money']+'</font>';
	        	}else{
                    return '<font color="green">-￥'+rowindex['money']+'</font>';
	        	}
	        }},
            {title:'备注', name:'remark',width:370},
            {title:'外部流水', name:'tradeNo',width:120},
            {title:'日期', name:'createTime' ,width:60}
            ];
 
    mmg2 = $('.mmg2').mmGrid({height: h-127,indexCol: true,indexColWidth:50, cols: cols,method:'POST',
        url: WST.U('admin/logmoneys/pageQuery'), fullWidthRows: true, autoLoad: true,
        plugins: [
            $('#pg2').mmPaginator({})
        ]
    });
}
function loadUserGrid(p){
    p=(p<=1)?1:p;
	mmg1.load({page:p,key:$('#key1').val()});
}
function loadFlowGrid(p){
    p=(p<=1)?1:p;
	mmg2.load({page:p,key:$('#key2').val(),startDate:$('#startDate').val(),endDate:$('#endDate').val()});
}
function tologmoneys(t,id){
	location.href= WST.U('admin/logmoneys/tologmoneys','id='+id+"&type="+t+"&startDate="+$('#startDate').val()+"&endDate="+'&endDate='+$('#endDate').val());
}

function moneyGridInit(type,id){
    var h = WST.pageHeight();
    var laydate = layui.laydate;
    laydate.render({
        elem: '#startDate'
    });
    laydate.render({
        elem: '#endDate'
    });
    var cols = [
            {title:'来源', name:'dataSrc', width: 30},
            {title:'金额', name:'money' ,width:20,renderer: function (rowdata, rowindex, value){
	        	if(rowindex['moneyType']==1){
                    return '<font color="red">+￥'+rowindex['money']+'</font>';
	        	}else{
                    return '<font color="green">-￥'+rowindex['money']+'</font>';
	        	}
	        }},
            {title:'备注', name:'remark',width:370},
            {title:'外部流水', name:'tradeNo',width:120},
            {title:'日期', name:'createTime' ,width:60}
            ];
 
    mmg = $('.mmg').mmGrid({height: h-120,indexCol: true,indexColWidth:50, cols: cols,method:'POST',
        url: WST.U('admin/logmoneys/pageQuery','type='+type+'&id='+id), fullWidthRows: true, autoLoad: true,
        plugins: [
            $('#pg').mmPaginator({})
        ]
    });
}

function loadMoneyGrid(t,id){
	mmg.load({page:1,id:id,type:t,startDate:$('#startDate').val(),endDate:$('#endDate').val()});
}

// -------------------------------------------------
var w;
function toAdd(id,type){
    var ll = WST.msg('正在加载信息，请稍候...');
    $.post(WST.U('admin/logmoneys/toAdd',{id:id,type:type}),{},function(data){
        layer.close(ll);
        w =WST.open({type: 1,title:"调节会员资金",shade: [0.6, '#000'],offset:'50px',border: [0],content:data,area: ['550px', '380px'],success:function(){
                layui.form.render();
            }});
    });
}
function editMoney(id,type){
    var params = WST.getParams('.ipt');
    params.targetId = id;
    params.targetType = type;
    var loading = WST.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
    $.post(WST.U('admin/logmoneys/add'),params,function(data,textStatus){
        layer.close(loading);
        var json = WST.toAdminJson(data);
        if(json.status=='1'){
            WST.msg("操作成功",{icon:1});
            closeFrom();
            mmg.load();
        }else{
            WST.msg(json.msg,{icon:2});
        }
    });
}
function closeFrom(){
    layer.close(w);
}