$(function(){
	$('#creditReportingUpdateBtn').removeClass("loadingCircle");
	
	// 日期的处理
	$('[name=regDate],[name=regDuee]').each(function(i,ele){
		$(ele).val($(ele).attr('orival').substring(0,4)+'-'+$(ele).attr('orival').substring(4,6)+'-'+$(ele).attr('orival').substring(6,8));
	});
	
	// 获取字典项数据
	function getDictCode(paramName){
		var dictOptionsData = {};
		$.ajax({
			url:'/orgbaseInfo/doFindDictCode',
			type:'get',
			data:{
				'paramName':paramName
			},
			async:false,
			success:function(data){
				dictOptionsData = data;
			},
			error:function(status){
				new $.zui.Messager('获取字典项时出错:'+status, {
				    type: 'danger'
				}).show();
			}
		});
		return dictOptionsData;
	};
	
	// '登记注册号类型' / '客户类型' / '国别' / '注册资本(币种)' / '组织机构类别' / '组织机构类别细分' / '经济行业分类' / '经济类型' / '基本户状态' / '企业规模' / '机构状态' 
    $('[name=regNoType],[name=custType],[name=countryCode],[name=capitalCurrency],[name=orgType],[name=orgSubType],[name=economicType],[name=accountStatus],[name=entScope],[name=orgStatus]').each(function(i,ele){
		var selectTypeOptions = getDictCode($(ele).attr('name'));
		if( selectTypeOptions ){
			for( var i in selectTypeOptions){
				$(ele).append('<option value="'+i+'">'+selectTypeOptions[i]+'</option>')
			};
			$(ele).find('option[value='+$(ele).attr('orival')+']').prop('selected',true);
		};
	});
    // '经济行业分类'--处理为带搜索项的
    var industryOptions = getDictCode('industry');
    $('.allIndustry').empty();
    for( var i in industryOptions ){
    		$('.allIndustry').append('<li class="companyli" value="'+i+'">'+industryOptions[i]+'</li>');
    		if( $('#industryId').val() == i ){
    			$('#industry').val(industryOptions[i])
    		}
    };
	withSearchDropDownBox({
		allOptionSelector:'.allIndustry li',//所有的选项的选择器
		triggerSelector:'#industry',//触发选择(显示下拉框)的表单的选择器(存放选择的name)
		resultIdSelector:'#industryId',//表单元素选择器(存放选择的id)
		dropdownSelector:'.industryListBox',//下拉框的选择器
		searchBtn:'.industryListBox button'// 查询按钮选择器
	});
    
    // 选择行政区划
	$('#regAreaCode').click(function(){
		chooseProvinceEtc({
			triggerId:'#regAreaCode',// 触发选择元素的选择器, 即存放最终选择结果的选择器, 该元素需要至少有相对定位属性
			originalId:'.originalVal'// 原先的纯文字元素的选择器, 选填
		})
	});
	
	// 日期
	$('[name=regDate],[name=regDuee]').datetimepicker(
		{	
			weekStart: 1,
		    autoclose: 1,
		    todayHighlight: 0,
		    startView: 2,
		    minView: 2,
		    forceParse: 0,
		    showMeridian: 1,
		    startDate:'1970-01-01',
		    endDate:'2999-12-31',
		    format: "yyyy-mm-dd"
		}
	);
	$('[name=regDate],[name=regDuee]').datetimepicker('setStartDate','1970-01-01');
	$('[name=regDate],[name=regDuee]').datetimepicker('setEndDate','2999-12-31');
	
	//日期输入框可删除
	$('.deleteTime').each(function(i,ele){
		$(ele).parent().find('input').change(function(){
			if( $(this).val()!=='' ){
				var transDate = $(this).val().split('-')[0]+$(this).val().split('-')[1]+$(this).val().split('-')[2];
				$(this).val(transDate)
				$(ele).show();
				//日期有值时去掉验证效果
				$(ele).parent().find('input').removeClass('error');
				$("#"+$(ele).parent().find('input').attr('name')+"-error").hide();
			}else{
				$(ele).hide();
			}
		});
		$(ele).parent().find('input').change();
	});
	$('.deleteTime').click(function(){
		$(this).parent().find('input').val('');
		$(this).hide();
	});
	
	// 注册资本输入时添加分隔符
	$('[name=regCapital]').val(commaDelimiter($('[name=regCapital]').val()))
	$('[name=regCapital]').keyup(function(){
		$(this).val( commaDelimiter($(this).val()));
	});
	
	// 取消
	$('#creditReportingBackBtn').click(function(){
		$(".yk_businessTabContentBox").load('/orgbaseInfo/viewOrgbaseInfo?custId='+enterPriseBusinessQuery.enterPriseDomesticCompanyId);
		return false;
	});
	
	// 保存
	$('#creditReportingUpdateBtn').click(function(){
		$("#updateOrgbaseInfoNews").validate({
			 promptPosition : "bottomLeft",
			 submitHandler : function(form) {
				 if( $('#regAreaCode').find('.originalVal').length<=0 && $('#regAreaCode').find('.resultcounty').length<=0 ){
					 new $.zui.Messager('注册(登记)地行政区划请选择到区县', {
					    type: 'danger'
					 }).show();
					 return;
				 }
				var postdata = {};
				postdata['orgChnName'] = $('[name=orgChnName]').val();
				postdata['orgEngName'] = $('[name=orgEngName]').val();
				postdata['custId'] = $('[name=custId]').val();
				postdata['loanCardNo'] = $('[name=loanCardNo]').val();
				postdata['regNoType'] = $('[name=regNoType]').val();
				postdata['regNo'] = $('[name=regNo]').val();
				postdata['orgCreditCode'] = $('[name=orgCreditCode]').val();
				
				postdata['orgbase.custType'] = $('[name=custType]').val();
				postdata['orgbase.accountPermitNo'] = $('[name=accountPermitNo]').val();
				postdata['orgbase.isInstitution'] = $('[name=isInstitution]').attr('tval');
				
				postdata['orgAttr.regAddr'] = $('[name=regAddr]').val();
				postdata['orgAttr.countryCode'] = $('[name=countryCode]').val();
				if( $('#regAreaCode').find('.originalVal').length<=0 ){
					postdata['orgAttr.regAreaCode'] = $('#regAreaCode').find('.resultcounty').attr('deptCode');
				}else{
					postdata['orgAttr.regAreaCode'] = $('#regAreaCode').attr('originalcode');
				};
				postdata['orgAttr.regDate'] = $('[name=regDate]').val();
				postdata['orgAttr.regDuee'] = $('[name=regDuee]').val();
				postdata['orgAttr.bizScope'] = $('[name=bizScope]').val();
				postdata['orgAttr.capitalCurrency'] = $('[name=capitalCurrency]').val();
				postdata['orgAttr.regCapital'] = $('[name=regCapital]').val().split(',').join('');
				postdata['orgAttr.orgType'] = $('[name=orgType]').val();
				postdata['orgAttr.orgSubType'] = $('[name=orgSubType]').val();
				if( $("#industryId").val()!='' ){
					postdata['orgAttr.industry'] = $("#industryId").val();
				};
				postdata['orgAttr.economicType'] = $('[name=economicType]').val();
				
				postdata['orgStatus.accountStatus'] = $('[name=accountStatus]').val();
				postdata['orgStatus.enterpriseScope'] = $('[name=entScope]').val();
				postdata['orgStatus.orgStatus'] = $('[name=orgStatus]').val();
				
				postdata['orgContact.officeAddr'] = $('[name=officeAddr]').val();
				postdata['orgContact.phoneNo'] = $('[name=phoneNo]').val();
				postdata['orgContact.financePhoneNo'] = $('[name=financePhoneNo]').val();
				
				$('#creditReportingUpdateBtn').addClass("loadingCircle");
				
				$.ajax({
					url:'/orgbaseInfo/doUpdateOrgbaseInfo',
					type:'post',
					data:postdata,
					success:function(data){
						if( data.success ){
							if( data.data ){
								new $.zui.Messager('修改基本信息成功', {
								    type: 'success'
								}).show();
								$(".yk_businessTabContentBox").load('/orgbaseInfo/viewOrgbaseInfo?custId='+enterPriseBusinessQuery.enterPriseDomesticCompanyId);
							}else{
								new $.zui.Messager('修改基本信息失败', {
								    type: 'danger'
								}).show();
							};
						}else{
							new $.zui.Messager('修改基本信息请求失败:'+data.msg, {
							    type: 'danger'
							 }).show();	
						};
						$('#creditReportingUpdateBtn').removeClass("loadingCircle");
					},
					error:function(status){
						new $.zui.Messager('修改基本信息时出错:'+status, {
						    type: 'danger'
						}).show();	
						$('#creditReportingUpdateBtn').removeClass("loadingCircle");
					}
				})
				
			}, 
			rules : {
				"orgChnName":{
					maxlength:120
				},
				"orgEngName":{
					maxlength:80
				},
				"loanCardNo":{
					maxlength:16
				},
				"regNo":{
					maxlength:20
				},
				'orgCreditCode':{
					maxlength:18
				},
				'accountPermitNo':{
					maxlength:20
				},
				'regAddr':{
					maxlength:120
				},
				'bizScope':{
					maxlength:200
				},
				'regCapital':{
					decimalLengthValid2:true,
					decimalNumberValid3:true,
					decimalPointValid:true
				},
				'officeAddr':{
					maxlength:120
				},
				'phoneNo':{
					maxlength:35
				},
				'financePhoneNo':{
					maxlength:35
				}
			},
			messages : {
				"orgChnName":{
					maxlength:'最多不超过120个字符'
				},
				"orgEngName":{
					maxlength:'最多不超过80个字符'
				},
				"loanCardNo":{
					maxlength:'最多不超过16个字符'
				},
				"regNo":{
					maxlength:'最多不超过20个字符'
				},
				'orgCreditCode':{
					maxlength:'最多不超过18个字符'
				},
				'accountPermitNo':{
					maxlength:'最多不超过20个字符'
				},
				'regAddr':{
					maxlength:'最多不超过120个字符'
				},
				'bizScope':{
					maxlength:'最多不超过200个字符'
				},
				'officeAddr':{
					maxlength:'最多不超过120个字符'
				},
				'phoneNo':{
					maxlength:'最多不超过35个字符'
				},
				'financePhoneNo':{
					maxlength:'最多不超过35个字符'
				}
			} 
		});
	});
	
	
	
	
	
	
})