var ns;
var id = helper.request.queryString("id");
var phone = helper.request.queryString("phone");
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 == '客服组') {
				$('.customerService').show();
				ns = 1;
			} else {
				$('.market').show();
				ns = 2;
			}
		}
	}
});
if(id) {
	$("#tel").val(phone);
	$("#tel_market").val(phone);
	$("#CallID").val(id);
}
	$('#testInput1').comboSelect();
	$('#testInput2').comboSelect();
	$('#testInput3').comboSelect();
	$('#testInput4').comboSelect();
	$(function(){
		GDLY(); //工单来源
		State();//工单状态
		getOrderType();//工单类型
		//保存
		$('.customerSubmit').click(function() {
			saveCustomer();
		});
		$('.marketSubmit').click(function() {
			saveMarket();
		});
		
	})
		 	//编辑器配置
	 KindEditor.ready(function(K) {
	                window.changeEditor = K.create('#feedBackCon');//修改弹出框编辑器
	                XQ();
	        })
			 	//编辑器配置
	 KindEditor.ready(function(K) {
	                window.changeMarket = K.create('#remark_market');//修改弹出框编辑器
	                	
	        })
//		BindLDdep($("#testInput1"),$("#testInput2"),$("#testInput3"),$("#testInput4")).then(function(){
//			XQ();
//		})
	
//工单状态
 function State(){
 	$.ajax({
		url: huayi.config.callcenter_url + "Dictionary/GetDicValueListByFlag",
		type: 'get',
		async: false,
		data: {
			flag: 'GDZT',
			token: $.cookie("token")
		},
		dataType: "json",
		success: function(res) {
			if(res.state == "success") {
				$(res.data).each(function(i, n) {
					$('<option value="' + n.F_DictionaryValueId + '">' + n.F_Name + '</option>').appendTo($("#State"));
				})
				//              		
			}

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

			})
		}
	});
}
	
	//获取详情
function XQ(){
$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "WorkOrder/GetWorkOrder",
		dataType: 'json',
		async: true,
		 cache: false,		
		data: {
				"orderid": id,
				"token": $.cookie("token")
		},
		success: function(result) {
					if(result.data.data.length > 0) {
			var data = result.data.data[0];
			var state = data.State;
			var state_;
			$("#xq_gdlx").text(data.GDLXName);
			$("#xq_cjsj").text(data.CreateTime);
			//              $("#xq_gdzt").html(GetStateName(data.State));
			$("#name").val(data.Customer);
			$("#name_market").val(data.Customer);
			$("#tel_market").val(data.CustomerTel);
			$("#tel").val(data.CustomerTel);
			//
			$("#testInput1_input").val(data.Address);
			$("#testInput2_input").val(data.Source);
			$("#testInput3_input").val(data.Address);
			$("#testInput4_input").val(data.Source);
			$("#typeclass").val(data.GDLXName);
			$("#typeclass_market").val(data.GDLXName);
			changeEditor.html(data.Detail);			
			changeMarket.html(data.Detail);			
//			$("#remark_market").text(data.Detail);
			$("#express").text(data.Files);
			$("#remark1").text(data.City);
			$("#remark2").text(data.Province);
			$("#remark3").text(data.County);
			if(state == 0) {
				state_ = 471;
			} else {
				state_ = 472;
			}
			$("#State").val(state_);
			$("#States").val(data.State);
			$("#DicValueList").val(data.Type);
			$("#DicValueList_market").val(data.Type);
			$("#typeclassId").val(data.TypeClass);
			$("#typeclassId_market").val(data.TypeClass);
			BindLDdep($("#testInput1"),$("#testInput2"),$("#testInput3"),$("#testInput4"),data.Address,data.Source)
		}
		}
	});
}
//工单类型
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);
		$('#typeclassId').val(chanId);
		$('#typeclass_market').val(changeName);
		$('#typeclassId_market').val(chanId);
		var pidnode = treeNode.getParentNode();
	}
	
};

$('.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')
})

$('.down_market').click(function() {
	if($('.treeList_market').css('display') == 'block') {
		$('.treeList_market').css('display', 'none')
	} else {
		$('.treeList_market').css('display', 'block')
	}
})
$('#typeclass_market').click(function() {
	$('.treeList_market').css('display', 'block')
})
$('.treeList_market').mouseleave(function() {
	$(this).css('display', 'none')
})
//来电单位  
function getAccount(obj) {
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "Customer/BindLDdep",
		dataType: 'json',
		async: true,
		data: {
			token: $.cookie("token")
		},
		success: function(data) {
			var Count = data.data.dep;
			$(Count).each(function(i, n) {
				$("<option value='" + n + "'>" + n + "</option>").appendTo($(obj));
			})
			//					$(obj).selectpicker('refresh');
		}
	});

}
//客服部保存
function saveCustomer() {
	$.ajax({
		type: "post",
		url: huayi.config.callcenter_url + "WorkOrder/EditWorkOrderDetail",
		dataType: 'json',
		async: true,
		data: {
			token: $.cookie("token"),
			orderid: id,
			customer: $('#name').val(), //姓名
			lddep: $('#testInput1_input').val(), //来电单位
			fkdep: $('#testInput2_input').val(), //反馈单位
			type: $('#DicValueList').val(), //工单来源
			typeclass: $('#typeclassId').val(), //工单类型id
			gdzt: $('#State').val(), //工单状态id   
			detail: helper.HtmlUtil.htmlEncodeByRegExp(changeEditor.html()), //反馈内容    
			files: $('#express').val(), //快递信息     
			remark1: $('#remark1').val(), //备注一     
			remark2: $('#remark2').val(), //备注二     
			remark3: $('#remark3').val() //备注三    					 callid:$("#CallID").val()
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				layer.msg("保存成功！");
				var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
				parent.layer.close(index); //再执行关闭
				parent.$('#workorderlist').bootstrapTable('refresh');
				parent.layer.msg("修改成功！");
				clear();
			}
		}
	});

}
//市场部保存
function saveMarket() {
	if($('#name_market').val() == '') {
		layer.confirm('请输入姓名！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#tel_market').val() == '') {
		layer.confirm('请输入电话！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}
	if($('#testInput3_input').val() == '') {
		layer.confirm('请选择来电单位！', {
			icon: 2,
			btn: ['确定']
		});
		return;
	}

	$.ajax({
		type: "post",
		url: huayi.config.callcenter_url + "WorkOrder/EditWorkOrderDetail",
		dataType: 'json',
		async: true,
		data: {
			token: $.cookie("token"),
			orderid: $("#CallID").val(),
			customer: $('#name_market').val(), //姓名
			lddep: $('#testInput3_input').val(), //来电单位
			fkdep: $('#testInput4_input').val(), //反馈单位
				type: $('#DicValueList_market').val(), //工单来源
			typeclass: $('#typeclassId_market').val(), //工单类型id
//			detail:encodeURIComponent(changeMarket.html()), //备注   
			detail:helper.HtmlUtil.htmlEncodeByRegExp(changeMarket.html()), //备注   
		},
		success: function(data) {
			if(data.state.toLowerCase() == 'success') {
				layer.msg("保存成功！");
				var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
				parent.layer.close(index); //再执行关闭
				parent.$('#workorderlist_').bootstrapTable('refresh');
				parent.layer.msg("修改成功！");
				clear();
			}
		}
	});

}
function BindLDdep(obj1,obj2,obj3,obj4,res1,res2) {
					$.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 html1 = '', html2 = '';
							$("<option value='" + res1 + "'>" + res1 + "</option>").appendTo(obj1);
							$("<option value='" + res2 + "'>" + res2 + "</option>").appendTo(obj2);
							$("<option value='" + res1 + "'>" + res1 + "</option>").appendTo(obj3);
							$("<option value='" + res2 + "'>" + res2 + "</option>").appendTo(obj4);
							$(Count).each(function(i, n) {
								if(n !==res1){
									html1 = "<option value='" + n + "'>" + n + "</option>";	
								}
								if(n !==res2){
									html2 = "<option value='" + n + "'>" + n + "</option>";	
								}
								$(html1).appendTo(obj1);
								$(html2).appendTo(obj2);
								
								$(html1).appendTo(obj3);
								$(html2).appendTo(obj4);
							});
							obj1.comboSelect();
							obj2.comboSelect();
							obj3.comboSelect();
							obj4.comboSelect();
						}
					});
				};
		
function clear() {
	if(ns == 1) {
		$('#name').val('');
		$('#tel').val(''); //号码
		$('#testInput1_input').val(''); //来电单位
		$('#testInput2_input').val(''); //反馈单位
		$('#orderSource').val(''); //工单来源
		$('#typeclassId').val(''); //工单类型id
	
		$('#State').val(''); //工单状态id   
		changeEditor.html('') //反馈内容    
		$('#express').val(''); //快递信息     
		$('#remark1').val(''); //备注一     
		$('#remark2').val(''); //备注二     
		$('#remark3').val(''); //备注三    
	} else {
		$('#name_market').val('');
		$('#tel_market').val(''); //号码
		$('#testInput3_input').val(''); //来电单位
		$('#testInput4_input').val(''); //反馈单位
			$('#typeclassId_market').val(''); //工单类型id
		changeMarket.html('') //反馈内容
//		$('#remark_market').val(''); //备注内容
	}

}


