$(function(){
	$('.addSure').removeClass("loadingCircle");
	if( $("#type").val() == 'update' ){
		var historicOccurDate = $('[name=occurDate]').attr('orival');
	}
	
	// 获取字典项数据
	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=disposeType]').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>')
			};
			if( $("#type").val() == 'update' ){
				$(ele).find('option[value='+$(ele).attr('orival')+']').prop('selected',true);
			};
		};
	});
    
    if( $("#type").val()=='add' ){
    		$('[name="currencyType"]').find('option[code=CNY]').prop('selected',true);
    };
	
	if( $("#type").val() == 'update' ){
		// 日期的处理
		$('[name=occurDate],[name=disposeDate]').each(function(i,ele){
			$(ele).val($(ele).attr('orival').substring(0,4)+'-'+$(ele).attr('orival').substring(4,6)+'-'+$(ele).attr('orival').substring(6,8));
		});
	};
	
	// 日期
	$('[name=occurDate],[name=disposeDate]').datetimepicker(
		{	
			weekStart: 1,
		    autoclose: 1,
		    todayHighlight: 0,
		    startView: 2,
		    minView: 2,
		    forceParse: 0,
		    showMeridian: 1,
		    startDate:'1970-01-01',
		    endDate:'2116-12-31',
		    format: "yyyy-mm-dd"
		}
	);
	$('[name=occurDate],[name=disposeDate]').datetimepicker('setStartDate','1970-01-01');
	$('[name=occurDate],[name=disposeDate]').datetimepicker('setEndDate','2116-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=recycledAmount],[name=balance]").each(function(i,ele){
		$(ele).val( commaDelimiter($(ele).val()) );
		$(ele).keyup(function(){
			$(this).val( commaDelimiter($(this).val()));
		});
	});
	
	//表单验证
	function badCreditFormValid(e){
		$("#addUpdateBadCreditForm").validate({
			promptPosition : "bottomLeft",
			submitHandler : function(form) {
				
				var formurl = '',tipword = '',formchoose = '';
				if( $("#type").val()=='add' ){
					formurl = '/orgbaseInfo/badCreditAsset/doAdd';
					tipword = '新增';
					formchoose = '#myModal-addBadCredit';
				}else{
					formurl = '/orgbaseInfo/badCreditAsset/doUpdate';
					tipword = '修改';
					formchoose = '#myModal-updateBadCredit'
				};
				var postdata = {
					'custId':enterPriseBusinessQuery.enterPriseDomesticCompanyId,
					'occurDate':$('[name=occurDate]').val(),
					'borrowerName':$('[name=borrowerName]').val(),
					'loanCardNo':$('#loanCardNo').val(),
					'balance':$('[name=balance]').val().split(',').join(''),
					'disposeDate':$('[name=disposeDate]').val(),
					'disposeType':$('[name=disposeType]').val(),
					'recycledAmount':$('[name=recycledAmount]').val().split(',').join(''),
					'regNo':$("#regNo").val(),
					'orgCode':$("#orgCode").val(),
					'currency.id':$('[name=currencyType]').val()
				};
				if( $("#type").val()=='update' ){
					postdata.bizNo = $('[name=bizNo]').val();
					postdata.historicOccurDate = historicOccurDate;
				};
				$('.addSure').addClass("loadingCircle");
				$.ajax({
					url:formurl,
					type:'post',
					data:postdata,
					success:function(data){
						if( data.success ){
							new $.zui.Messager(tipword+'成功', {
								type: 'success'
							}).show();
							$('.addSure').removeClass("loadingCircle");
							//关闭弹框
							$(formchoose).modal("hide");
							//弹框被隐藏, 且过渡效果执行完毕
							$(formchoose).on('hidden.zui.modal', function(){
								$('.addBadCreditDetailBox,.updateBadCreditDetailBox,.viewBadCreditDetailBox').empty();
								//刷新表格
								$('#badCreditDetailList').setGridParam({
									mtype:'GET',
									url : '/orgbaseInfo/badCreditAsset/doList',
									datatype : 'json',
									postData: {
										'fastSearch':enterPriseBusinessQuery.enterPriseDomesticCompanyId
									},
									page:sendlistpage
								});
								$('#badCreditDetailList').trigger('reloadGrid');
							});
						}else{
							new $.zui.Messager(tipword+'失败:'+ data.msg, {
								type: 'danger'
							}).show();
							$('.addSure').removeClass("loadingCircle");
						}
					},
					error:function(status){
						new $.zui.Messager(tipword+'时出错:'+status, {
							type: 'danger'
						}).show();
						$('.addSure').removeClass("loadingCircle");
					}
				})
			},
			rules : {
				'bizNo':{//处置编号
					required : true,
					maxlength:60
				},
				'occurDate':{//业务发生日期
					required : true
				},
				'currencyType':{//币种
					required: true
				},
				'balance':{//处置余额
					required : true,
					decimalLengthValid3:true,
					decimalNumberValid:true,
					decimalPointValid:true
				},
				'disposeDate':{//处置事件日期
					required:true
				},
				'disposeType':{//主要资产处理方式
					required:true
				},
				'recycledAmount':{//已回收金额
					required : true,
					decimalLengthValid3:true,
					decimalNumberValid:true,
					decimalPointValid:true
				}
			},
			messages : {
				'bizNo':{//处置编号
					required : '请输入处置编号',
					maxlength:'最多60个字符'
				},
				'occurDate':{//业务发生日期
					required : '请选择业务发生日期'
				},
				'currencyType':{//币种
					required: '请选择币种'
				},
				'balance':{//处置余额
					required : '请输入处置余额'
				},
				'disposeDate':{//处置事件日期
					required:'请选择处置事件日期'
				},
				'disposeType':{//主要资产处理方式
					required:'请选择主要资产处理方式'
				},
				'recycledAmount':{//已回收金额
					required : '请输入已回收金额'
				}
			}
		})
	};
	badCreditFormValid();
	
	//确定保存按钮
	$(".addSure").click(badCreditFormValid);
	
})