/**
 * 修改增加业务信息
 * */

//编辑器配置
KindEditor.ready(function(K) {
	window.editor = K.create('#F_BusContent', {
		imgMaxSize: 10 * 1024 * 1024,
		minHeight: '300px',
		uploadJson: '../../js/kindeditor/asp.net/upload_json.ashx',
		fileManagerJson: '../../js/kindeditor/asp.net/file_manager_json.ashx',
		allowFileManager: true,
		afterCreate: function() {
			this.loadPlugin('autoheight');
		},
		items: [
			'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
			'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
			'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
			'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
			'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
			'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image',
			'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
			'anchor', 'link', 'unlink', '|', 'about'
		]
	});
}); 
 
$(function() {
	autosize($('textarea'));
// 	//获取所属行业//获取税务类别//获取客户信誉等级
// 	$.when(helper.getDropList.getlistDropByDic($('#customer_subtrade'), 'KHSSHY'),
// 			helper.getDropList.getlistDropByDic($('#customer_taxCategory'), 'SWLB'),
// 			helper.getDropList.getlistDropByDic($('#customer_layer'), 'KHLayer'))
// 		.done(function() {
			var edit_id = helper.request.queryString("edit_id");
			if (edit_id) {
				getCustomer(edit_id);
			}
			//绑定验证(所有 input.form-control)
			$('#tab_user_contents').find("input[class='form-control']").on('blur keyup', valideInput);
		// });

});

//获取详情
function getCustomer(ids) {
	$.getJSON(huayi.config.callcenter_url + "WoBill/GetBus", {
		id: ids, //id
		token: $.cookie("token")
	}, function(data) {
		if (data.state.toLowerCase() == "success") {
			data = data.data;
			if (data) {
				$('#F_BusName').val(data.F_BusName);//	string 业务名称	
				editor.html(decodeURIComponent(data.F_BusContent));//	string 业务内容	
				// $('#F_BusContent').val(data.F_BusContent);//	string 业务内容	
				$('#F_BusLabel').val(data.F_BusLabel);//	string 业务标签
				$('#F_Remark').val(data.F_Remark);//	string 备注
				$('#F_BusName').trigger('blur');
			}
		}
	});
}

//保存
function saveCustomer() {
// 	if (!$.trim($('#F_Name').val()) ||
// 		!$.trim($('#F_Telephone').val())) {
// 		layer.msg('带红色星号的是必填项，请填写或选择后再保存！');
// 		return;
// 	}
	var wURL, loadIndex;
	var edit_id = helper.request.queryString("edit_id");
	if (edit_id) {
		wURL = "WoBill/UpdateBus";
	} else {
		wURL = "WoBill/AddBus";
	}
	$.ajax({
		type: 'post',
		url: huayi.config.callcenter_url + wURL,
		dataType: 'json',
		async: true,
		beforeSend: function() { //触发ajax请求开始时执行
			$('#save_btns').attr("disabled", true);
			$('#save_btns').text('保存中...');
			loadIndex = layer.load();
		},
		data: {
			F_Id: edit_id,//主ID
			F_BusName: $('#F_BusName').val(),//	string 业务名称	
			F_BusLabel: $('#F_BusLabel').val(),//	string 业务标签
			// F_BusContent: helper.filter.delHtmlTag($('#F_BusContent').val()),//	string 业务内容	
			F_BusContent: encodeURIComponent(editor.html()),//	string 业务内容	
			F_Remark: helper.filter.delHtmlTag($('#F_Remark').val()),//	string 备注
			token: $.cookie("token")
		},
		success: function(data) {
			layer.close(loadIndex);
			if (data.state === "success") {
				var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);
				parent.$('#workorderlist').bootstrapTable('refresh');
				parent.layer.msg("保存成功");
			} else {
				$('#save_btns').attr("disabled", false);
				$('#save_btns').text('保存');
			}
		},
		error: function(textStatus) {
			layer.close(loadIndex);
			layer.confirm('网络繁忙，请稍后再试...', {
				icon: 7,
				closeBtn: 0,
				btn: ['确定'] //按钮
			});
			$('#save_btns').attr("disabled", false);
			$('#save_btns').text('保存');
		},
		complete: function(XMLHttpRequest, textStatus) {
			layer.close(loadIndex);
			if (textStatus == 'timeout') {
				var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
				xmlhttp.abort();
				layer.confirm('网络超时，请稍后再试...', {
					icon: 7,
					closeBtn: 0,
					btn: ['确定'] //按钮
				});
			}
			$('#save_btns').attr("disabled", false);
			$('#save_btns').text('保存');
		},
	});
}


//验证(所有 input.form-control)的方法
function valideInput() {
	var _that = $(this);
	//console.log($(this).attr('id'));
	switch ($(this).attr('id')) {
		case 'F_BusName': //业务名称
			valideMethods(_that, regexs.businessname, '格式不正确（只能输入中文、英文、数字、括号和顿号 ）！', true);
			break;
		case 'F_BusLabel': //业务标签
			valideMethods(_that, regexs.chOrenOrnum, '格式不正确（只能输入中文、英文、数字）！', true);
			break;
	}
	if ($('#tab_user_contents').find('.has-error').length > 0) {
		$('.project_save').attr('disabled', true);
		$('#save_btns').off('click', saveCustomer);
	} else {
		$('.project_save').attr('disabled', false);
		$('#save_btns').off('click', saveCustomer);
		$('#save_btns').on('click', saveCustomer);
	}
}
/**
 * 验证方法
 * _this :传递的this
 * regRlues: 验证的规则
 * msg: 提示信息
 * isRes:boolean 是否允许验证的内容为空，true:允许为空，false:不允许为空
 * */
function valideMethods(_this, regRlues, msg, isRes) {
	var txtTpips = _this.parent().parent().find('.text-tips');
	if (txtTpips) {
		txtTpips.parent().remove();
	}
	if (!isRes) {
		//不为空
		if (!regRlues.test($.trim(_this.val()))) {
			_this.parent().parent().removeClass('has-success has-feedback').addClass('has-error has-feedback');
			_this.parent().find('.glyphicon').remove();
			$('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>').appendTo(_this.parent());
			$('<div class="col-md-10 col-md-offset-2"><span class="text-tips">' + msg + '</span></div>').appendTo(_this.parent()
				.parent());
			$('.project_save').attr('disabled', true);
		} else {
			_this.parent().parent().removeClass('has-error has-feedback').addClass('has-success has-feedback');
			_this.parent().find('.glyphicon').remove();
			$('<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>').appendTo(_this.parent());
		}
	} else {
		//为空不验证；但是有内容就验证；
		if ($.trim(_this.val())) {
			if (!regRlues.test($.trim(_this.val()))) {
				_this.parent().parent().removeClass('has-success has-feedback').addClass('has-error has-feedback');
				_this.parent().find('.glyphicon').remove();
				$('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>').appendTo(_this.parent());
				$('<div class="col-md-10 col-md-offset-2"><span class="text-tips">' + msg + '</span></div>').appendTo(_this.parent()
					.parent());
				$('.project_save').attr('disabled', true);
			} else {
				_this.parent().parent().removeClass('has-error has-feedback').addClass('has-success has-feedback');
				_this.parent().find('.glyphicon').remove();
				$('<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>').appendTo(_this.parent());
			}
		} else {
			_this.parent().find('.glyphicon').remove();
			_this.parent().parent().removeClass('has-error has-success has-feedback');
		}
	}
}
