$(document).ready(function() {
autosize($('textarea[class*=autosize]'));

$('.chosen-select').chosen({
	disable_search_threshold: 5,
	width: '100%',
	no_results_text: '未找到相关选项!'
}); 

$(".date-time-picker").datetimepicker({
	icons : {
		time : 'fa fa-clock-o',
		date : 'fa fa-calendar',
		up : 'fa fa-chevron-up',
		down : 'fa fa-chevron-down',
		previous : 'fa fa-chevron-left',
		next : 'fa fa-chevron-right',
		today : 'fa fa-arrows ',
		clear : 'fa fa-trash',
		close : 'fa fa-times'
	},
	format : "YYYY-MM-DD HH:mm:ss",
});
/*控制选择时间开始结束的关系*/
//$('.date-picker').datepicker({
$("body").off("focus").on("focus",".date-picker",function(){
	$(this).parents("body").find(".date-picker").prop("readonly",true);
	$(this).parents("body").find(".date-picker").datepicker({
	maxDate:new Date(),
	icons : {
		time : 'fa fa-clock-o',
		date : 'fa fa-calendar',
		up : 'fa fa-chevron-up',
		down : 'fa fa-chevron-down',
		previous : 'fa fa-chevron-left',
		next : 'fa fa-chevron-right',
		today : 'fa fa-arrows ',
		clear : 'fa fa-trash',
		close : 'fa fa-times'
	},
	dateFormat : "yy-mm-dd",
	changeYear: true,         
    changeMonth: true,
    monthNames: ['一月','二月','三月','四月','五月','六月',  
    '七月','八月','九月','十月','十一月','十二月'],  
    monthNamesShort: ['1月','2月','3月','4月','5月','6月',  
    '7月','8月','9月','10月','11月','12月'],  
    dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],  
    dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],  
    dayNamesMin: ['日','一','二','三','四','五','六'],  
    weekHeader: '周',  
    showMonthAfterYear: true,  
    yearSuffix: '年',
    yearRange:"1800:2200",
    //默认全部都是不能大于当前日期，可以大于或必须大于当前日期的做如下处理：
    beforeShow:function(input){
    	var maxToday = ["end","operatingPeriodEnd","loanDate","issueEnd","validPeriodBegin","auditDate","repaymentDate","interset.financingActualDate"];//<--------可以大于当前日期但不一定必须大于的集合
    	for(var j=0;j<maxToday.length;j++){
		   	if( this.name == maxToday[j] ){
		   		$(".date-picker[name='"+maxToday[j]+"']").datepicker("option","maxDate","+36500")
		   		break;
		   	}
	   };
	   var minToday = ["expiryDate","serviceEnd","endDate","operatingPeriodEnd","validPeriodEnd"];//<----------必须大于当前日期的集合
	   for(var q=0;q<minToday.length;q++){
		   	if( this.name == minToday[q] ){
		   		$(".date-picker[name='"+minToday[q]+"']").datepicker("option","maxDate","+36500");
		   		$(".date-picker[name='"+minToday[q]+"']").datepicker("option","minDate","+0");
		   		break;
		   	}
	   };
	   var thisWeek = ["workTime"];//<-------- 52周只可选择本周时间
	   var bigm;
	   var smallm;
	   switch( new Date().getDay() ){
	   		case 0 : bigm = 0;smallm = -6;break;
	   		case 1 : bigm = 6;smallm = 0;break;
	   		case 2 : bigm = 5;smallm = -1;break;
	   		case 3 : bigm = 4;smallm = -2;break;
	   		case 4 : bigm = 3;smallm = -3;break;
	   		case 5 : bigm = 2;smallm = -4;break;
	   		case 6 : bigm = 1;smallm = -5;break;
	   }
   	   for(var m=0;m<thisWeek.length;m++){
		   	if( this.name == thisWeek[m] ){
		   		if( $(this).attr("weekth")>$("#presentWeekth").val() ){
		   			$(".date-picker[name='"+thisWeek[m]+"']").datepicker("option","maxDate","+"+(bigm+($(this).attr("weekth")-$("#presentWeekth").val())*7));
			   		$(".date-picker[name='"+thisWeek[m]+"']").datepicker("option","minDate","+"+(bigm+($(this).attr("weekth")-$("#presentWeekth").val())*7)-6);
		   		}else if( $(this).attr("weekth")==$("#presentWeekth").val() ){
			   		if( $(this).hasClass("continuenextweek") ){//只有当前周可修改为下周继续
			   			$(".date-picker[name='"+thisWeek[m]+"']").datepicker("option","maxDate","+"+(bigm+7));
			   			$(".date-picker[name='"+thisWeek[m]+"']").datepicker("option","minDate","+"+(bigm+7-6));
			   		}else{
			   			$(".date-picker[name='"+thisWeek[m]+"']").datepicker("option","maxDate","+"+bigm);
				   		$(".date-picker[name='"+thisWeek[m]+"']").datepicker("option","minDate","+0");
			   		}
		   		} 
		   		break;
		   	}
	   };
    },
    onSelect: function(selectedDate){  
	   var option;
	   var timeArray = [
	   		{
	   			"from":"serviceStart",
	   			"to":"serviceEnd"
	   		},
			{
				"from":"operatingPeriodFrom",
				"to":"operatingPeriodEnd"
			},
			{
				"from":"signatureDate",
				"to":"endDate"
			},
			{
				"from":"validPeriodBegin",
				"to":"validPeriodEnd"
			},
			{
				"from":"issueStart",
				"to":"issueEnd"
			},
			{
				"from":"firstIssueStart",
				"to":"issueEnd"
			}
			
	   ];
	   for(var i=0;i<timeArray.length;i++){
	   		if( this.name == timeArray[i].from ){
	   			option = "minDate";
	   		}else if( this.name == timeArray[i].to ){
	   			option = "maxDate";
	   		}
	   		if( option ){
	   			$(".date-picker[name='"+timeArray[i].from+"'],.date-picker[name='"+timeArray[i].to+"']").not(this).datepicker("option", option, selectedDate);  	  
	   			return;
	   		}
	   		
	   };
    }
  });
	
	
});
$("body").off("focus").on("focus",".loanFirstAbnormal",function(){//<---------对贷后-收放款管理的处理(后面的时间要大于前面的)		
	$(this).parents("body").find(".loanFirstAbnormal").prop("readonly",true);
	$(this).parents("body").find(".loanFirstAbnormal").datepicker({
		//minDate:new Date(),
	icons : {
		time : 'fa fa-clock-o',
		date : 'fa fa-calendar',
		up : 'fa fa-chevron-up',
		down : 'fa fa-chevron-down',
		previous : 'fa fa-chevron-left',
		next : 'fa fa-chevron-right',
		today : 'fa fa-arrows ',
		clear : 'fa fa-trash',
		close : 'fa fa-times'
	},
	dateFormat : "yy-mm-dd",
	changeYear: true,         
    changeMonth: true,
    monthNames: ['一月','二月','三月','四月','五月','六月',  
    '七月','八月','九月','十月','十一月','十二月'],  
    monthNamesShort: ['1月','2月','3月','4月','5月','6月',  
    '7月','8月','9月','10月','11月','12月'],  
    dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],  
    dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],  
    dayNamesMin: ['日','一','二','三','四','五','六'],  
    weekHeader: '周',  
    showMonthAfterYear: true,  
    yearSuffix: '年',
    yearRange:"1800:2200",
    onSelect: function(selectedDate){  
//	   var option2;
//		   var repayNum = $(this).attr("name").split("[")[1].split("]")[0];
//		   $(".loanFirstAbnormal").each(function(i,ele){
//			   if( i>repayNum ){
//				   option2 = "minDate";
//				   $(".loanFirstAbnormal").eq(i).datepicker("option",option2,selectedDate);
//			   }
//			   else if( i<repayNum ){
//				   option2 = "maxDate";
//				   $(".loanFirstAbnormal").eq(i).datepicker("option",option2,selectedDate);
//			   }
//		   });
    }
  });
	
	
});
/*$('.date-picker').datetimepicker({
	icons : {
		time : 'fa fa-clock-o',
		date : 'fa fa-calendar',
		up : 'fa fa-chevron-up',
		down : 'fa fa-chevron-down',
		previous : 'fa fa-chevron-left',
		next : 'fa fa-chevron-right',
		today : 'fa fa-arrows ',
		clear : 'fa fa-trash',
		close : 'fa fa-times'
	},
	format : "YYYY-MM-DD",
});*/

$('.multiselect').multiselect({
	 enableFiltering: true,
	 enableHTML: true,
	 buttonClass: 'btn btn-white btn-primary',
	 templates: {
		button: '<button type="button" class="multiselect dropdown-toggle" data-toggle="dropdown"><span class="multiselect-selected-text"></span> &nbsp;<b class="fa fa-caret-down"></b></button>',
		ul: '<ul class="multiselect-container dropdown-menu"></ul>',
		filter: '<li class="multiselect-item filter"><div class="input-group"><span class="input-group-addon"><i class="fa fa-search"></i></span><input class="form-control multiselect-search" type="text"></div></li>',
		filterClearBtn: '<span class="input-group-btn"><button class="btn btn-default btn-white btn-grey multiselect-clear-filter" type="button"><i class="fa fa-times-circle red2"></i></button></span>',
		li: '<li><a tabindex="0"><label></label></a></li>',
       divider: '<li class="multiselect-item divider"></li>',
       liGroup: '<li class="multiselect-item multiselect-group"><label></label></li>'
	 }
	});

	//控制企业选择-下拉框不随内容过多而撑开
	$("select[name$='BusinessInfo.id']").each(function(){
		$(this).parent().css({"display":"block"});
	})
});
/** 设置百分数默认的% **/
$("body").delegate(".percentages","keyup",function(){
	var valueresult=$(this).val();
		var position=valueresult.indexOf("%")+1;
		var strNew=valueresult.substring(position);
	if(strNew!=''){
		$(this).val(valueresult.substring(0,position));
	}
    if(valueresult.indexOf('%')<0){
    	$(this).val(valueresult+"%");
    }
})

//设置集合下标顺序递增（重新排序）
function reordering(){
	$(".tableList tbody tr").each(function(i){
		var index=i;
		$(this).find("td").each(function(){
			$(this).children().each(function(){
				if($(this).attr('name')!=undefined){
					var thisname=$(this).attr('name');
					var newname=reassemble(thisname,index);
					$(this).attr("name",newname);
				}else if($(this).attr('id')!=undefined){
					var id=$(this).attr('id');
					var newid=reassemble(id,index);
					$(this).attr("id",newid);
				}
			})
		})
	})
}
function reassemble(initname,i){
    var n = parseInt(initname.replace(/[^0-9]/ig,""));
    var pos=initname.indexOf(n);
	if(pos==-1){return initname}
    var length=n.toString().length;
	var prev=initname.substring(0,pos);
	var num=initname.substring(pos,pos+length);
	var next=initname.substring(pos+length);
    num=i;
    return prev+num+next;
}