/**
 * create by robot 
 * date Wed Sep 25 11:16:40 CST 2019 
 */ 

var pageIndex=1;
$(document).ready(function(){
	//调用查询
	pageIndex=StringUtil.getQueryString('pageIndex');
	if(pageIndex==undefined||pageIndex==null){
		pageIndex=1;
	}
	queryMerchantOrder(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 queryMerchantOrder(index){
	if(typeof(index)=='undefined'){
		index=1;
	}
	var url=ctx+'/merchantOrder/queryOrderRefund';
	$.ajax({  
        url:url,  
        type:"post", 
        dataType:"json",  
        data:{  
        	"currPage":index,
        	"payTypeCodeList":$("#payTypeCodeList").ySelectedValues(","),
        	"staffCodeList":$("#staffCodeList").ySelectedValues(","),
        	"storeIdList":$("#storeIdList").ySelectedValues(","),
//        	"storeIdList":$("#storeIdList").val(),
        	"timeStatus":$("#timeStatus").val(),
        	"orderType":$("#orderType").val(),
        },  
        success:function(result){  
        	var presentPage = result.currPage;
        	//渲染数据
        	$('#data_content').empty();
        	var header='';
        	$.each(result.merchantOrderList,function(i){
        		var orderNumber1 = (presentPage-1)*12+i+1;//展示的序号
        		var merchantOrder=result.merchantOrderList[i];
        		var payTypeName=''
            		var transNo='';
            		if(merchantOrder.payTypeCode=='wechatPay'){
            			payTypeName='微信';
            			if(StringUtil.isNotBlank(merchantOrder.weChatTransNo)){
                			transNo=merchantOrder.weChatTransNo;
                		}
            		}else if(merchantOrder.payTypeCode=='aliPay'){
            			payTypeName='支付宝';
            			if(StringUtil.isNotBlank(merchantOrder.aliPayTransNo)){
                			transNo=merchantOrder.aliPayTransNo;
                		}
            		}else if(merchantOrder.payTypeCode=='leshuaPay0'){
            			payTypeName='乐刷支付宝支付';
            			if(StringUtil.isNotBlank(merchantOrder.leshuaTransNo)){
                			transNo=merchantOrder.leshuaTransNo;
                		}
            		}else if(merchantOrder.payTypeCode=='leshuaPay1'){
            			payTypeName='乐刷微信支付';
            			if(StringUtil.isNotBlank(merchantOrder.leshuaTransNo)){
                			transNo=merchantOrder.leshuaTransNo;
                		}
            		}else if(merchantOrder.payTypeCode=='leshuaPay2'){
            			payTypeName='乐刷刷卡支付';
            			if(StringUtil.isNotBlank(merchantOrder.leshuaTransNo)){
                			transNo=merchantOrder.leshuaTransNo;
                		}
            		}else if(merchantOrder.payTypeCode=='bank'){
            			payTypeName='银联';
            			if(StringUtil.isNotBlank(merchantOrder.leshuaTransNo)){
                			transNo=merchantOrder.leshuaTransNo;
                		}
            		}
        		var data='';
        		data+='<tr class="odd">';
        		data+='<td class="textc">';
				data+='		 <div class="checker checkeBox">';        		
				data+='		 <span class=""  data-id="'+merchantOrder.id+'"><input type="checkbox"  value=""></span>';
        		data+='		</div>';
        		data+='</td>';
        		data+='<td class="textc">'+orderNumber1+'</td>';
        		data+='<td class="textc">'+merchantOrder.id+'</td>';
        		var orderNumber='';
        		if(StringUtil.isNotBlank(merchantOrder.orderNumber)){
        			orderNumber=merchantOrder.orderNumber;
        		}
        		data+='<td class="textc">'+orderNumber+'</td>';
        		data+='<td class="textc">'+transNo+'</td>';
        		var merchantName='';
        		if(StringUtil.isNotBlank(merchantOrder.merchantName)){
        			merchantName=merchantOrder.merchantName;
        		}
        		/*data+='<td class="textc">'+merchantOrder.merchantCode+'</td>';*/
        		data+='<td class="textc">'+merchantOrder.alias+'</td>';
//        		var storeName='';
//        		if(StringUtil.isNotBlank(merchantOrder.storeName)){
//        			storeName=merchantOrder.storeName;
//        		}
//        		data+='<td class="textc">'+storeName+'</td>';
        		
        		var orderType='';
        		switch (merchantOrder.orderType) {
				case "1":
					orderType="主扫";
					break;
				case "2":
					orderType="被扫";
					break;
				case "3":
					orderType="会员充值";
					break;
				case "4":
					orderType="POS";
					break;
				}
        		data+='<td class="textc">'+orderType+'</td>';
        		data+='<td class="textc">'+payTypeName+'</td>';
        		/*data+='<td class="textc">'+merchantOrder.payTypeCode+'</td>';*/
        		data+='<td class="textc">'+merchantOrder.consumeFee+'</td>';
        		/*data+='<td class="textc">'+merchantOrder.memberCode+'</td>';*/
        		/*data+='<td class="textc">'+merchantOrder.storeId+'</td>';*/
        		var createDt='';
        		if(StringUtil.isNotBlank(merchantOrder.createDt)){
        			createDt=dateFtt("yyyy-MM-dd hh:mm",new Date(merchantOrder.createDt));
        		}
        		data+='<td class="textc">'+createDt+'</td>';
        		/*data+='<td class="textc">'+merchantOrder.staffCode+'</td>';*/
        		var status='';
        		if(merchantOrder.status=='2'){
        			status='退款成功';
        		}else if(merchantOrder.status=='3'){
        			status='退款失败';
        		}else if(merchantOrder.status=='4'){
        			status='退款中';
        		}else if(merchantOrder.status=='0'){
        			status='交易成功';
        		}else if(merchantOrder.status=='1'){
        			status='交易失败';
        		}else if(merchantOrder.status=='5'){
        			status='待付款';
        		}
        		data+='<td class="textc">'+status+'</td>';
        		/*data+='<td class="textc">';
        		//店长
        		if($("#staffTypeSession").val()=='0'){
        			if(merchantOrder.status=='4'){
        				data+='<div class="positionr listOperaters">';
                		data+='		<a href="javascript:v`										oid(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="buinourPassDetail('+merchantOrder.id+')" class="btn btn-primary fa fa-edit ops_more f12">审核通过</a></li>';
                		data+='				<li class="fl ml2 mr2"><a href="javascript:void(0);" onclick="buinourRefuseDetail('+merchantOrder.id+')" class="btn btn-primary fa fa-edit ops_more f12">审核拒绝</a></li>';
                	    data+='			</ul>';
                        data+='		</div>';
                	    data+='</div>';
        			}else
        			if(merchantOrder.status!='2'&&merchantOrder.status!='5'){
        				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="clerkSponsorRefundDetail('+merchantOrder.id+')" class="btn btn-primary fa fa-edit ops_more f12">退款</a></li>';
                	    data+='			</ul>';
                        data+='		</div>';
                	    data+='</div>';
        			}
        			//其他人员
        		}else{
        			if(merchantOrder.status!='2'&&merchantOrder.status!='4'&&merchantOrder.status!='5'){
        				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="clerkSponsorRefundDetail('+merchantOrder.id+')" class="btn btn-primary fa fa-edit ops_more f12">发起退款</a></li>';
                	    data+='			</ul>';
                        data+='		</div>';
                	    data+='</div>';
        			}
        		}
        	    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();
					queryMerchantOrder(api.getCurrent());
				}
			});
        } 
    });  
	//表头列不选中
	$("#allCheck").find("span").removeClass('checked');
}

//初始化新增
function initAddMerchantOrder(){
	window.location=ctx+'/merchantOrder/queryMerchantOrderDetails?pageIndex='+pageIndex;
}

//初始化修改
function showMerchantOrderDetails(id){
	window.location=ctx+'/merchantOrder/queryMerchantOrderDetails?id='+id+'&pageIndex='+pageIndex;
}

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

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

//店长审核通过
function buinourPassDetail(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() {
			buinourPass(id);
			layer.closeAll();
		}
	});
}

function buinourPass(id){
	$.ajax({
		url:ctx+'/merchantOrder/buinourPass',
		data:{
			"id":id
		},
		dataType:"json",
		type:"post",
		success:function(result){
			if(result.data=='1'){
				layer.msg('审核通过成功');
			}else{
				layer.msg('审核通过失败');
			}
			queryMerchantOrder(pageIndex);
		}
	})
}

//店长审核拒绝
function buinourRefuseDetail(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();
			buinourRefuse(id);
		}
	});
}

function buinourRefuse(id){
	$.ajax({
		url:ctx+'/merchantOrder/buinourRefuse',
		data:{
			"id":id
		},
		dataType:"json",
		type:"post",
		success:function(result){
			if(result.data=='1'){
				layer.msg('审核拒绝成功');
			}else{
				layer.msg('审核拒绝失败');
			}
			queryMerchantOrder(pageIndex);
		}
	})	
}
//店长退款
function buinourRefundDetail(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();
			buinourRefund(id);
		}
	});
}

function buinourRefund(){
	$.ajax({
		url:ctx+'/merchantOrder/buinourRefund',
		data:{
			"id":id
		},
		dataType:"json",
		type:"post",
		success:function(result){
			if(result.data=='1'){
				layer.msg('退款发起成功');
			}else{
				layer.msg('退款发起失败');
			}
			queryMerchantOrder(pageIndex);
		}
	})
}

//店员发起退款
function clerkSponsorRefundDetail(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();
			clerkSponsorRefund(id);
		}
	});
}

function clerkSponsorRefund(id){
	$.ajax({
		url:ctx+'/merchantOrder/clerkSponsorRefund',
		data:{
			"id":id
		},
		dataType:"json",
		type:"post",
		success:function(result){
			if(result.data=='1'){
				layer.msg('发起退款成功');
			}else{
				layer.msg('发起退款失败');
			}
			queryMerchantOrder(pageIndex);
		}
	})
}

//时间格式化处理
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;   
}

