/**
 * create by robot 
 * date Thu Sep 12 16:16:36 CST 2019 
 */ 

var pageIndex=1;
$(document).ready(function(){
	//调用查询
	pageIndex=StringUtil.getQueryString('pageIndex');
	if(pageIndex==undefined||pageIndex==null){
		pageIndex=1;
	}
	queryMerchantMenber(pageIndex);
	//绑定hover操作
	$(document).on('mouseenter','.listOperaters',function(){
		$(this).children(".ops_itemDiv").show();
		var width=15;
		$(this).children('div').find('li').each(function(){
			//获取li宽度
			width+=$(this).width();
		});
		$(this).children('div').width(width);
	})
	$(document).on('mouseleave','.listOperaters',function(){
		$(this).children(".ops_itemDiv").hide();
	});

	//日历控件
	layui.use(['layer', 'laydate'], function() {
		var layer = layui.layer;
		laydate = layui.laydate;
		laydate.render({
		elem: '#date1' //指定元素
		});
		laydate.render({
			elem: '#date2' //指定元素
		});
	});


	//行选中事件
	$("#allCheck").find("span").click(function() {
		$(this).toggleClass('checked');
		if($(this).hasClass('checked')){
			//所有列选中
		  $(".checker").find("span").each(function () {
		         $(this).addClass("checked");
		    })
		}else{
			//所有列取消选中
			$(".checker").find("span").each(function () {
				 $(this).removeClass("checked");
		    })
		}
	});

	//单个选中
	$(document).on('click','.checkeBox span',function() {
		$(this).toggleClass('checked');
		//设置头部事件
		var length=$('.checkeBox span').length;
		var count=0;
		$(".checkeBox").find("span").each(function () {
	        if($(this).hasClass("checked")){
	        	count++;
	        }
	   })
		//全选
	   if(count==length){
		   $("#allCheck").find("span").addClass('checked');
	   }else{
		   $("#allCheck").find("span").removeClass('checked');
	   }
	})
	//左右div高度相等
	var left_content_height = $(".right-content").height();
	$(".left-content").height(left_content_height+35);
	var sub_left_height=$(".res-menu").height(left_content_height-60);
});

//查询
function queryMerchantMenber(index){
	if(typeof(index)=='undefined'){
		index=1;
	}
	var url=ctx+'/merchantMenber/queryMerchantMenberByPage';
	$.ajax({  
        url:url,  
        type:"post", 
        dataType:"json",  
        data:{  
        	"currPage":index,
        	"memberName":$("#memberName").val(),
        	"phone":$("#phone").val(),
        	"userId":$("#userId").val(),
        	"alias":$("#alias").val(),
        },  
        success:function(result){  
        	var presentPage = result.currPage;
        	//渲染数据
        	$('#data_content').empty();
        	var header='';
        	$.each(result.merchantMenberList,function(i){
        		var orderNumber = (presentPage-1)*12+i+1;
        		var merchantMenber=result.merchantMenberList[i];
        		var data='';
        		data+='<tr class="odd">';
        		data+='<td class="textc">';
				data+='		 <div class="checker checkeBox">';        		data+='		 <span class=""  data-id="'+merchantMenber.id+'"><input type="checkbox"  value=""></span>';
        		data+='		</div>';
        		data+='</td>';
        		data+='<td class="textc">'+orderNumber+'</td>';
        		data+='<td class="textc">'+merchantMenber.userId+'</td>';
//        		var storeId='';
//        		if(StringUtil.isNotBlank(merchantMenber.storeId)){
//        			storeId=merchantMenber.storeId;
//        		}
//        		data+='<td class="textc">'+storeId+'</td>';
        		
        		data+='<td class="textc">'+merchantMenber.alias+'</td>';
        		var memberName='';
        		if(StringUtil.isNotBlank(merchantMenber.memberName)){
        			memberName=merchantMenber.memberName;
        		}
        		data+='<td class="textc">'+memberName+'</td>';
        		data+='<td class="textc"><a href="javascript:void(0);" onclick="enlargePicture(\''+merchantMenber.logo+'\')"><img src="'+merchantMenber.logo+'" onerror="javascript:this.src=\'https://www.shouyinbei.net/resources/images/error.jpg\'"   height="30px" width="30px"></img></a></td>'
        		var nickName='';
        		if(StringUtil.isNotBlank(merchantMenber.nickName)){
        			nickName=merchantMenber.nickName;
        		}
        		data+='<td class="textc">'+nickName+'</td>';
        		data+='<td class="textc">'+merchantMenber.phone+'</td>';
        		var comsumeMoney='';
        		if(StringUtil.isNotBlank(merchantMenber.comsumeMoney)){
        			comsumeMoney=merchantMenber.comsumeMoney;
        		}
        		data+='<td class="textc">'+comsumeMoney+'</td>';
        		
        		
        		/*var profitMoney='';
        		if(StringUtil.isNotBlank(merchantMenber.profitMoney)){
        			profitMoney=merchantMenber.profitMoney;
        		}
        		data+='<td class="textc">'+profitMoney+'</td>';*/
        		var usableRechargeFee=Number(merchantMenber.usableRechargeFee);
        		var usableGiveFee=Number(merchantMenber.usableGiveFee);
        		var money=usableRechargeFee+usableGiveFee;
        		data+='<td class="textc">'+money+'</td>';
        		/*var status='';
        		if(StringUtil.isNotBlank(merchantMenber.status)){
        			if(merchantMenber.status=='0'){
        				status='不显示';
        			}else if(merchantMenber.status=='1'){
        				status='显示';
        			}
        		}
        		data+='<td class="textc">'+status+'</td>';*/
        		var createDt='';
        		if(StringUtil.isNotBlank(merchantMenber.createDt)){
					createDt= dateFtt("yyyy-MM-dd hh:mm:ss",new Date(merchantMenber.createDt));
        		}
        		data+='<td class="textc">'+createDt+'</td>';
        		/*data+='<td class="textc">'+merchantMenber.orderNumber+'</td>';*/
        		data+='<td class="textc">';
        		data+='<div class="positionr listOperaters">';
        		data+='		<a href="javascript:void(0);"  class="icon-large dlblock w16 h16"><img src="'+resourcePath+'/images/angle-down.png"/></a>';
        		data+='		<div class="ops_box ops_itemDiv positiona clearfix whitebg">';
        		data+='			<ul>';
        		data+='				<li class="fl ml2 mr2"><a href="javascript:void(0);" onclick="showRechargeDetails(this)" data-membercode='+merchantMenber.memberCode + ' class="layui-btn layui-btn-radius layui-btn-sm fa fa-remove ops_more f12">充值记录</a></li>';
        		data+='				<li class="fl ml2 mr2"><a href="javascript:void(0);" onclick="showConsumptionDetails(this)" data-membercode='+merchantMenber.memberCode + ' class="layui-btn layui-btn-radius layui-btn-sm fa fa-detail ops_more f12">消费记录</a></li>';
        	    data+='			</ul>';
                data+='		</div>';
        	    data+='</div>';
        	    data+='</td>';
//        		data+='<td class="textc">';
//        		//商家跟管理员可以进行操作
//        		if(result.admin=='admin'||result.userType=='merchant'){
//        			data+='<div class="positionr listOperaters">';
//            		data+='		<a href="javascript:void(0);"  class="icon-large dlblock w16 h16"><img src="'+resourcePath+'/images/angle-down.png"/></a>';
//            		data+='		<div class="ops_box ops_itemDiv positiona clearfix whitebg">';
//            		data+='			<ul>';
//            		data+='				<li class="fl ml2 mr2"><a href="javascript:void(0);" onclick="showMerchantMenberDetails('+merchantMenber.id+')" class="layui-btn fa fa-edit ops_more f12">编辑</a></li>';
//            		
//            	    data+='			</ul>';
//                    data+='		</div>';
//            	    data+='</div>';
//        		}else{
//        			//其它用户只能查看 不能编辑 
//        			data+='				<li class="fl ml2 mr2"><a href="javascript:void(0);" onclick="otherMerchantMenberDetails('+merchantMenber.id+')" class="layui-btn fa fa-detail ops_more f12">详情</a></li>';
//        		}
//        	    data+='</td>';
        	    data+='</tr>';
        	    $('#data_content').append(data);
        	});
        	//分页
        	$('#pageCount').html(result.pageCount);
      	$('#currPage').html(result.currPage);
          $('#totalCount').html(result.count);
           	//设置分页
           	pageIndex=result.currPage;
			$('#pagination').pagination({
				pageCount: result.pageCount,
				jump: true,
				coping: true,
				jump: true,
				homePage: '首页',
				endPage: '末页',
				prevContent: '上页',
				nextContent: '下页',
				current:result.currPage,
				callback:function(api){
					pageIndex=api.getCurrent();
					queryMerchantMenber(api.getCurrent());
				}
			});
        } 
    });  
	//表头列不选中
	$("#allCheck").find("span").removeClass('checked');
}

//初始化新增
function initAddMerchantMenber(){
	window.location=ctx+'/merchantMenber/queryMerchantMenberDetails?pageIndex='+pageIndex;
}

//初始化修改
function showMerchantMenberDetails(id){
	window.location=ctx+'/merchantMenber/queryMerchantMenberDetails?id='+id+'&pageIndex='+pageIndex;
}

//其它用户查看详情
function otherMerchantMenberDetails(id){
	window.location=ctx+'/merchantMenber/otherMerchantMenberDetails?id='+id+'&pageIndex='+pageIndex;
}

//初始化删除
function initDeleteMerchantMenber(id){
	
		layer.open({
		type: 1,
		closeBtn: 1,		title: '提示',
		offset: 'm', //具体配置参考：offset参数项
		area: ['300px', '180px'],
		content: '<div style="padding:10px;" class="textc mt20">确认删除吗？</div>',
		btn: ['确认','取消'],
		btnAlign: 'r'	, //按钮居中
		shade: 0.3 ,//不显示遮罩
		yes: function() {
			layer.closeAll();
			deleteMerchantMenber(id,true);
		}
	});
}

//删除
function deleteMerchantMenber(id,showMsg){
	$.ajax({
		url:ctx+'/merchantMenber/deleteMerchantMenber',
		data:[{
			name:'id',
			value:id
		}],
		type:'POST',
		dataType:'json',
		success:function(){
			queryMerchantMenber(pageIndex);
			if(showMsg){
				layer.msg('删除成功!');	
			}
		}
	});
}
//头部编辑
function initEditMerchantMenber(){
	var count=0;
	var id=0;
	$(".checkeBox").find("span").each(function () {
         if($(this).hasClass("checked")){
        	 count++;
        	 id=$(this).data('id');
         }
    })
    if(count>1){
    	layer.msg('只能选中一行记录进行操作!');
    	return ;
    }
	 if(count==0){
    	layer.msg('请选中一行记录!');
    	return ;
	  }
	 if(id>0){
	  showMerchantMenberDetails(id);
	 }
}
//头部删除
function initBatchDeleteMerchantMenber(){
	var count=0;
	$(".checkeBox").find("span").each(function () {
         if($(this).hasClass("checked")){
        	 count++;
         }
    })
	
	 if(count==0){
    	layer.msg('请选中一行记录!');
    	return ;
	  }
	

	layer.open({
		type: 1,
		closeBtn: 1,
		title: '提示',
		offset: 'm', //具体配置参考：offset参数项
		area: ['300px', '180px'],
		content: '<div style="padding:10px;" class="textc mt20">确认删除吗？</div>',
		btn: ['确认','取消'],
		btnAlign: 'r', //按钮居中
		shade: 0.3, //不显示遮罩
		yes: function() {
			layer.closeAll();
			deleteBatchMerchantMenber();
		}
	});
	
}
//批量删除
function deleteBatchMerchantMenber(){
	
	$(".checkeBox").find("span").each(function () {
        if($(this).hasClass("checked")){
        	var id=$(this).data('id');
        	deleteMerchantMenber(id,false);
        	layer.msg('删除成功!');	
        }
   })
}

//充值记录
function showRechargeDetails(obj){
	var membercode=$(obj).data('membercode');
	window.location=ctx+'/merchantMenber/showRechargeDetails?memberCode='+membercode;
}

//消费记录
function showConsumptionDetails(obj){
	var membercode=$(obj).data('membercode');
	window.location=ctx+'/merchantMenber/showConsumptionDetails?memberCode='+membercode;
}

//点击放大图片
function enlargePicture(billPicUrl){
	//alert("即将弹出图片");
	var data='';
	data+='<img src="'+billPicUrl+'"  class="img" onerror="javascript:this.src=\'https://www.shouyinbei.net/resources/images/error.jpg\'" >';
	$('#picture').append(data);
	$('#pictureWindow').show();
}

function hidePictureWindow(){
	$('#picture').empty();
	$('#pictureWindow').hide();
}

function dateFtt(fmt,date)
{ //author: meizz
	var o = {
		"M+" : date.getMonth()+1,                 //月份
		"d+" : date.getDate(),                    //日
		"h+" : date.getHours(),                   //小时
		"m+" : date.getMinutes(),                 //分
		"s+" : date.getSeconds(),                 //秒
		"q+" : Math.floor((date.getMonth()+3)/3), //季度
		"S"  : date.getMilliseconds()             //毫秒
	};
	if(/(y+)/.test(fmt))
		fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("("+ k +")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
	return fmt;
}

