/**
 * create by robot 
 * date Mon Dec 16 17:32:09 CST 2019 
 */ 

var pageIndex=1;
$(document).ready(function(){
	//调用查询
	pageIndex=StringUtil.getQueryString('pageIndex');
	if(pageIndex==undefined||pageIndex==null){
		pageIndex=1;
	}
	queryDeviceSignUnbind(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 queryDeviceSignUnbind(index){
	if(typeof(index)=='undefined'){
		index=1;
	}
	var url=ctx+'/deviceSignUnbind/queryDeviceSignUnbindByPage';
	$.ajax({  
        url:url,  
        type:"post", 
        dataType:"json",  
        data:{  
        	"currPage":index,
        	"orderNo":$('#orderNo').val(),
        	"merchantName":$('#merchantName').val(),
        	"status":$('#status').val(),
			"userId":$('#userId').val(),
        /*	"startTime":$('#date1').val(),
        	"endTime":$('#date2').val(),*/
        },  
        success:function(result){  
        	var presentPage = result.currPage;
        	//渲染数据
        	$('#data_content').empty();
        	var header='';
        	$.each(result.deviceSignUnbindList,function(i){
        		var orderNumber = (presentPage-1)*12+i+1;
        		var deviceSignUnbind=result.deviceSignUnbindList[i];
        		var data='';
        		data+='<tr class="odd">';
        		data+='<td class="textc">';
				data+='		 <div class="checker checkeBox">';        		data+='		 <span class=""  data-id="'+deviceSignUnbind.id+'"><input type="checkbox"  value=""></span>';
        		data+='		</div>';
        		data+='</td>';
        		data+='<td class="textc">'+orderNumber+'</td>';
        		data+='<td class="textc">'+deviceSignUnbind.userId+'</td>';
        		data+='<td class="textc">'+(deviceSignUnbind.orderNo == null ? '' : deviceSignUnbind.orderNo)+'</td>';
        		data+='<td class="textc">'+deviceSignUnbind.merchantName+'</td>';
        		data+='<td class="textc">'+(deviceSignUnbind.venderModel == null ? '' : deviceSignUnbind.venderModel)+'</td>';
        		data+='<td class="textc">'+(deviceSignUnbind.sn == null ? '' : deviceSignUnbind.sn)+'</td>';
        		var status='';
        		if(StringUtil.isNotBlank(deviceSignUnbind.status)){
        			if(deviceSignUnbind.status=='1'){
        				status='确定激活';
        			}
        			if(deviceSignUnbind.status=='2'){
        				status='已激活';
        			}
        			if(deviceSignUnbind.status=='3'){
        				status='确定解绑';
        			}
        			if(deviceSignUnbind.status=='4'){
        				status='已解绑';
        			}
        		}
        		data+='<td class="textc">'+status+'</td>';
        		data+='<td class="textc">';
        		if(deviceSignUnbind.status=='1'){
        			data+='<a href="javascript:void(0);" onclick="updateStatus('+deviceSignUnbind.id+','+deviceSignUnbind.status+')">确定激活</a>';
        		}else if(deviceSignUnbind.status=='3'){
        			data+='<a href="javascript:void(0);" onclick="updateStatus('+deviceSignUnbind.id+','+deviceSignUnbind.status+')">确定解绑</a>';
        		}else if(deviceSignUnbind.status=='2'){
        			data+='已绑定';
        		}else if(deviceSignUnbind.status=='4'){
        			data+='已解绑';
        		}else if(deviceSignUnbind.status=='5'){
        			data+='已拒绝绑定';
        		}else if(deviceSignUnbind.status=='6'){
        			data+='已拒绝解绑';
        		}
        	    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();
					queryDeviceSignUnbind(api.getCurrent());
				}
			});
        } 
    });  
	//表头列不选中
	$("#allCheck").find("span").removeClass('checked');
}

//初始化新增
function initAddDeviceSignUnbind(){
	window.location=ctx+'/deviceSignUnbind/queryDeviceSignUnbindDetails?pageIndex='+pageIndex;
}

//初始化修改
function showDeviceSignUnbindDetails(id){
	window.location=ctx+'/deviceSignUnbind/queryDeviceSignUnbindDetails?id='+id+'&pageIndex='+pageIndex;
}

//初始化删除
function initDeleteDeviceSignUnbind(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();
			deleteDeviceSignUnbind(id,true);
		}
	});
}

//删除
function deleteDeviceSignUnbind(id,showMsg){
	$.ajax({
		url:ctx+'/deviceSignUnbind/deleteDeviceSignUnbind',
		data:[{
			name:'id',
			value:id
		}],
		type:'POST',
		dataType:'json',
		success:function(){
			queryDeviceSignUnbind(pageIndex);
			if(showMsg){
				layer.msg('删除成功!');	
			}
		}
	});
}
//头部编辑
function initEditDeviceSignUnbind(){
	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){
	  showDeviceSignUnbindDetails(id);
	 }
}
//头部删除
function initBatchDeleteDeviceSignUnbind(){
	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();
			deleteBatchDeviceSignUnbind();
		}
	});
	
}
//批量删除
function deleteBatchDeviceSignUnbind(){
	
	$(".checkeBox").find("span").each(function () {
        if($(this).hasClass("checked")){
        	var id=$(this).data('id');
        	deleteDeviceSignUnbind(id,false);
        	layer.msg('删除成功!');	
        }
   })
}


function toExamine(id){
	var htmlStr = '<div style="margin: 10px;"><label class="layui-form-label">理由：</label><textarea class="layui-textarea" id="certReason"></textarea></div>';
	layer.open({
		type:1,
		title:'实名认证审核',
		area: '350px;',
		shade:1,
		maxmin:true,
		content:htmlStr,
		btn:['通过','不通过'],
		yes:function(){
			var certReason = $("#certReason").val();
			examine(3,id,certReason);
		},
		btn2:function(){
			var certReason = $("#certReason").val();
			if(StringUtil.isBlank(certReason)){
				layer.msg('请填写不通过理由');
				return;
			}
			examine(2,id,certReason);
			//layer.closeAll();
		}
	})
}





function updateStatus(id,status){
		var htmlStr='<div style="margin: 10px;"><label class="layui-form-label">理由：</label><textarea class="layui-textarea" id="certReason"></textarea></div>';
	layer.open({
		type:1,
		title:'实名认证审核',
		area: '350px;',
		shade:1,
		maxmin:true,
		content:htmlStr,
		btn:['通过','不通过'],
		yes:function(){
			var certReason = $("#certReason").val();
			if(status=='1'){
				Status(2,id,certReason);
			}else if(status=='3'){
				Status(4,id,certReason);
			}
		},
		btn2:function(){
			var certReason = $("#certReason").val();
			if(StringUtil.isBlank(certReason)){
				layer.msg('请填写不通过理由');
				return;
			}
			if(status=='1'){
				Status(5,id,certReason);
			}else if(status=='3'){
				Status(6,id,certReason);
			}
			
		}
	})
}

function Status(status,id,certReason){
	$.ajax({
		url:ctx+'/deviceSignUnbind/updateStatus',
		data:{
			"id":id,
			"status":status,
			"certReason":certReason,
		},
		dataType:'json',
		type:'post',
		success:function(res){
			queryDeviceSignUnbind(pageIndex);
			layer.msg(res.msg);
			layer.closeAll();
		}
	})
}


