var ns;
var id = helper.request.queryString("id");
var phone = helper.request.queryString("phone");
var depName = helper.request.queryString("depName");
var name = helper.request.queryString("name");

var objs = {};
$.ajax({
	type: "get",
	url: huayi.config.callcenter_url + "CallInScreen/GetCurrUserDep",
	dataType: 'json',
	async: true,
	data: {
		token: $.cookie("token")
	},
	success: function(data) {
		if(data.state.toLowerCase() == 'success') {
			if(data.data.groname == '客服组') {
				//console.log(data.data.groname)
				$('.customerService').show();
				ns = 1;
			} else {
				$('.market').show();
				ns = 2;
			}
		}
	}
});
$(document).ready(function() {
	//工单来源
	getOrderSource();
	//工单类型
	getOrderType();
	//工单状态
	getOrderState();
	//来电单位
	$('#testInput1').comboSelect();
	$('#testInput2').comboSelect();
	$('#testInput3').comboSelect();
	$('#testInput4').comboSelect();
	//编辑器配置 客服部
	KindEditor.ready(function(K) {
		window.changeEditor = K.create('#feedBackCon'); //修改弹出框编辑器
	})
	//编辑器配置 市场部 备注
	KindEditor.ready(function(K) {
		window.marketEditor = K.create('#remark_market');
	})
	//保存
	$('.customerSubmit').click(function() {
		saveCustomer();
	})
	$('.marketSubmit').click(function() {
		saveMarket();
	})
	if(id) {
		$("#CallID").val(id);
		$("#tel").val(phone);
		$("#tel_market").val(phone);
		if(unescape(decodeURI(name)) != 'null' && unescape(decodeURI(name)) != 'undefined') {
			$("#name_market").val(unescape(decodeURI(name)));
			$("#name").val(unescape(decodeURI(name)));
		}
		if(unescape(decodeURI(depName)) != 'null' && unescape(decodeURI(depName)) != 'undefined') {
			$("#testInput1_input").val(unescape(decodeURI(depName)));
			$("#testInput3_input").val(unescape(decodeURI(depName)));
			 BindLDdep($("#testInput1"),$("#testInput2"),$("#testInput3"),$("#testInput4"),unescape(decodeURI(depName)))
		}else{
			BindLDdep($("#testInput1"),$("#testInput2"),$("#testInput3"),$("#testInput4"),'')
		}
		
}else{
	BindLDdep($("#testInput1"),$("#testInput2"),$("#testInput3"),$("#testInput4"),'');
}
})

//工单类型
function getOrderType() {
	$.get(huayi.config.callcenter_url + 'WorkType/GetTreeList', {
		"token": $.cookie("token"),
	}, function(result) {
		result = $.parseJSON(result);
		$.fn.zTree.init($("#TreeDemo"), setting3, result.data); //实例化树形图
		$.fn.zTree.init($("#TreeDemo_market"), setting3, result.data); //实例化树形图
	});
}
var setting3 = {
	data: {
		key: {
			name: "text"
		},
		simpleData: {
			enable: true,
			idKey: "id",
			rootPId: 0
		}
	},
	callback: {
		onClick: changeTreeClick
	}
}

function changeTreeClick(event, treeId, treeNode) {
	 if (treeNode.level == 2) {
	var chanId = treeNode.id;
	var changeName = treeNode.text;
	$('#typeclass').val(changeName);
	$('#typeclass_market ').val(changeName);
	$('#typeclassId').val(chanId);
	$('#typeclassId_market').val(chanId);
	var pidnode = treeNode.getParentNode();
	$('.addTree').hide();
	}
};

$('.down').click(function() {
	if($('.treeList').css('display') == 'block') {
		$('.treeList').css('display', 'none')
	} else {
		$('.treeList').css('display', 'block')
	}
})
$('#typeclass').click(function() {
	$('.treeList').css('display', 'block')
})
$('.treeList').mouseleave(function() {
	$(this).css('display', 'none')
})
$('#typeclass_market').click(function() {
	$('.treeList_market').css('display', 'block')
})
$('.treeList_market').mouseleave(function() {
	$(this).css('display', 'none')
})
//工单状态
function getOrderState() {
	$.ajax({
		url: huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag",
		type: 'get',
		async: true,
		data: {
			flag: 'GDZT',
			token: $.cookie("token")
		},
		dataType: "json",
		success: function(res) {
			if(res.state == "success") {
				//console.log(res.data);
				$('<option value="">全部</option>').appendTo($("#State"));
				$(res.data).each(function(i, n) {
					if(n.F_DictionaryValueId == '472') {
						$('<option value="' + n.F_DictionaryValueId + '" selected="selected">' + n.F_Name + '</option>').appendTo($("#State"));
					} else {
						$('<option value="' + n.F_DictionaryValueId + '">' + n.F_Name + '</option>').appendTo($("#State"));
					}
				})
				//              		
			}

		}
	})
}
//工单来源
function getOrderSource(obj) {
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag",
		dataType: 'json',
		async: true,
		data: {
			token: $.cookie("token"),
			flag: "GDLY"
		},
		success: function(data) {
			var Count = data.data;
			$('<option value="">全部</option>').appendTo($("#DicValueList"));
			$(Count).each(function(i, n) {
				if(n.F_DictionaryValueId == "468") {
					$('<option value="' + n.F_DictionaryValueId + '" selected="selected">' + n.F_Name + '</option>').appendTo($("#DicValueList"));
					$('<option value="' + n.F_DictionaryValueId + '" selected="selected">' + n.F_Name + '</option>').appendTo($("#DicValueList_market"));
				} else {
					$('<option value="' + n.F_DictionaryValueId + '">' + n.F_Name + '</option>').appendTo($("#DicValueList"));
					$('<option value="' + n.F_DictionaryValueId + '">' + n.F_Name + '</option>').appendTo($("#DicValueList_market"));
				}

			})
		}
	});
}

function BindLDdep(obj1,obj2,obj3,obj4,res1) {
					$.ajax({
						type: "get",
						url: huayi.config.callcenter_url + "Customer/BindLDdep",
						dataType: 'json',
						async: true,
						data: {
//							keypara: str,
							token: $.cookie("token")
						},
						success: function(data) {
							var Count = data.data.dep;
							var html = '',html2 = '';
							if(res1){
								$("<option value='" + res1 + "' selected>" + res1 + "</option>").appendTo(obj1);
								$("<option value='" + res1 + "' selected>" + res1 + "</option>").appendTo(obj3);
							}
							$(Count).each(function(i, n) {
								html = "<option value='" + n + "'>" + n + "</option>";
									if(n!==res1){
										html2 = "<option value='" + n + "'>" + n + "</option>";
									}
								$(html2).appendTo(obj1);
								$(html2).appendTo(obj3);
								$(html).appendTo(obj2);
								$(html).appendTo(obj4);
							});
							obj1.comboSelect();
							obj2.comboSelect();
							obj3.comboSelect();
							obj4.comboSelect();
						}
					});
				};
//客服部保存	
function saveCustomer() {
	if($('#name').val() == '') {
		layer.confirm('请输入姓名！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#tel').val() == '') {
		layer.confirm('请输入电话！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($.trim($('#testInput1_input').val()) == '') {
		layer.confirm('请选择来电单位！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($.trim($('#testInput2_input').val()) == '') {
		layer.confirm('请选择反馈单位！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#DicValueList').val() == '') {
		layer.confirm('请选择工单来源！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#State').val() == '') {
		layer.confirm('请选择工单状态！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#typeclassId').val() == '') {
		layer.confirm('请选择工单类型！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	$.ajax({
		type: "post",
		url: huayi.config.callcenter_url + "CallInScreen/AddWorkOrder",
		dataType: 'json',
		async: true,
		beforeSend: function() { //触发ajax请求开始时执行
			$('.customerSubmit').attr("disabled", true);
			$('.customerSubmit').text('保存中...');
		},
		data: {
			token: $.cookie("token"),
			callCustomer: $('#name').val(), //姓名
			tel: $('#tel').val(), //号码
			lddep: $('#testInput1_input').val(), //来电单位
			fkdep: $('#testInput2_input').val(), //反馈单位
			gdly: $('#DicValueList').val(), //工单来源
			gdlx: $('#typeclassId').val(), //工单类型id
			gdzt: $('#State').val(), //工单状态id  
			province: $("#Province").val(),
			fkcont:  helper.HtmlUtil.htmlEncodeByRegExp(changeEditor.html()), //反馈内容    
			sendmsg: $('#express').val(), //快递信息     
			note1: $('#remark1').val(), //备注一     
			note2: $('#remark2').val(), //备注二     
			note3: $('#remark3').val(), //备注三    
			callid: $("#CallID").val()
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				layer.msg("保存成功！");
				clear();
				$('.customerSubmit').attr("disabled", false);
				$('.customerSubmit').text('保存');
			}
		}
	});

}
// 市场部保存
function saveMarket() {
	if($('#name_market').val() == '') {
		layer.confirm('请输入姓名！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#tel_market').val() == '') {
		layer.confirm('请输入电话！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($.trim($('#testInput3_input').val()) == '') {
		layer.confirm('请选择来电单位！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($.trim($('#testInput4_input').val()) == '') {
		layer.confirm('请选择反馈单位！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#DicValueList_market').val() == '') {
		layer.confirm('请选择工单来源！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#typeclassId_market').val() == '') {
		layer.confirm('请选择工单类型！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	$.ajax({
		type: "post",
		url: huayi.config.callcenter_url + "CallInScreen/AddWorkOrder",
		dataType: 'json',
		async: true,
		beforeSend: function() { //触发ajax请求开始时执行
			$('.marketSubmit').attr("disabled", true);
			$('.marketSubmit').text('保存中...');
		},
		data: {
			token: $.cookie("token"),
			callCustomer: $('#name_market').val(), //姓名
			tel: $('#tel_market').val(), //号码
			lddep: $('#testInput3_input').val(), //来电单位
			fkdep: $('#testInput4_input').val(), //反馈单位
			gdly: $('#DicValueList_market').val(), //工单来源
			gdlx: $('#typeclassId_market').val(), //工单类型id
			fkcont: helper.HtmlUtil.htmlEncodeByRegExp(marketEditor.html()), //反馈内容
			callid: $("#CallID").val()
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				layer.msg("保存成功！");
				clear();
				$('.marketSubmit').attr("disabled", false);
				$('.marketSubmit').text('保存');
			}
		}
	});

}

function clear() {
	if(ns == 1) {
		$('#name').val('');
		$('#tel').val(''); //号码
		$('#testInput1_input').val(''); //来电单位
		$('#testInput2_input').val(''); //反馈单位
		$('#orderSource').val(''); //工单来源
		$('#typeclassId').val(''); //工单类型id
		$('#State').val(''); //工单状态id   
		$('#feedBackCon').val(''); //反馈内容    
		$('#express').val(''); //快递信息     
		$('#remark1').val(''); //备注一     
		$('#remark2').val(''); //备注二     
		$('#remark3').val(''); //备注三    
		$('#typeclass').val('');
		changeEditor.html('');
	} else {
		$('#name_market').val('');
		$('#tel_market').val(''); //号码
		$('#testInput3_input').val(''); //来电单位
		$('#testInput4_input').val(''); //反馈单位
		$('#remark_market').val(''); //备注内容
		marketEditor.html('');
	}

}