mui.init({})
var UserCode = helper.cookies.get("usercode");
var openid = helper.cookies.get("openid");
$(function() {
//	投诉咨询切换
	$('.order_type').find('input[type="radio"]').on('change', function() {
			if($(this).val() === "108") { //投诉
				$('.complaintOrder').show();  //receive
				$('.receive').hide();
			} else {
				$('.receive').show();
				$('.complaintOrder').hide();
			}
		});
	//工单有效性 
	$('#order_valid').find('input[type="radio"]').on('change', function() {
		//是否有效工单项(0有效，1无效)
		if($(this).val() === "1") { //无效
			$('.order_note').show();
		} else {
			$('.order_note').hide();
		}
	});
	//提交按钮
	$('#submit').on('tap', function() {
		var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
		var myreg11 = /^(([0\+]\d{2,3}-)?((0\d{2,3}))?(-)?)?(\d{7,8})(-(\d{3,}))?$/;
		var str = $("#order_tel").val();	
		if($('#order_type').val() == '') 
			mui.alert('请输入工单类型！') //请输入工单类型
		} else if($('#order_name').val() == '') {
			mui.alert('请输入客户姓名！') //请输入客户姓名
		} else if($('#order_tel').val() == '') {
			mui.alert('请输入电话！') //请输入客户姓名
		}else if (!myreg.test(str)&&!myreg11.test(str)) {
       		mui.alert('手机号格式不正确！')
        }else if($('#order_rengin').val() == '') {
			mui.alert('请输入项目名称！') //请输入项目名称
		}  else if($('#order_room').val() == '') {
			mui.alert('请输入房间号！') //请输入房间号
		} else if($('#ower_relationship').val() == '') {
			mui.alert('请输入房东关系！') //房东关系
		} else if($('#order_reception').val() == '') {
			mui.alert('请填写详细描述！') //填写详细描述
		} else {
			$.ajax({
				type: "post",
				dataType: 'json',
				url: huayi.config.callcenter_url + '/WxLogin/AddWorkOrder',
				async: true,
				data: {
					gdly: $('#order_source').attr('data-index'), //工单来源
					gdlx: $('.order_type').find('input[type="radio"]:checked').val(), //工单类型
					callCustomer: $('#order_name').val(), //客户姓名
					tel: $('#order_tel').val(), //否	string	联系电话
					areaid: $("#order_rengin").attr('data-index'), //区域名称
					proid: $("#order_pro").attr('data-index'), //项目名称
					buildingid: $("#order_floor").attr('data-index'), //楼号
					roomno: $('#order_room').val(), //房间号
					OwnerRelations: $('#ower_relationship').attr('data-index'), //房东关系
					customerresponseid: $('#order_response').find('input[type="radio"]:checked').val(), //否	int	客户反应
					fkdep: $('#complaintOrder_type').attr('data-index'), //否	int	处理部门（投诉类型）
					kfdepid: $('#serviceOrder_type').attr('data-index'), //否int	客服类类型	
					isquality: $('#is_quality_assurance').attr('data-index'), //否	int	是,//否过质保
					fkcont: $('#order_reception').val(), //描述详情
					auditusercode: $('#order_responsible').val(), //否	string	受理人
					iseffective: $('#order_valid').find('input[type="radio"]:checked').val(), //	否	int	是否有效工单项(0有效，1无效)
					note: $('#order_note').val(), // 无效原因	否	string	备注 (iseffective，1无效时填写备注)
					clbm: $('#recive_type').attr('data-index'),	//否	int	咨询类工单指派部门id
					clid: $('#ower_recive').attr('data-index'), //	否	string	接收人id $(this)
					//$('#order_receiveUser option').find("option:selected").val(),
					openid: openid,
				},
				success: function(data) {
					if(data.state == "success") {
						debugger
						//alert($('#order_reception').val()), //描述详情
						mui.toast('创建成功');
						$('#order_type').val(''); //工单类型
						$('#order_name').val(''), //客户姓名
						$('#order_tel').val(''), //否	string	联系电话
						$("#order_rengin").attr(''), //区域名称
						$("#order_pro").attr(''), //项目名称
						$("#order_floor").attr(''), //楼号
						$('#order_room').val(''), //房间号
						$('#ower_relationship').attr(''), //房东关系
						$('#order_response').find('input[type="radio"]:checked').val(), //否	int	客户反应
						$('#complaintOrder_type').attr(''), //否	int	处理部门（投诉类型）
						$('#serviceOrder_type').attr(''), //否int	客服类类型	
						$('#is_quality_assurance').attr(''), //否	int	是,//否过质保
						$('#order_reception').val(''), //描述详情
						$('#order_responsible').val(''), //否	string	受理人
						$('#order_valid').find('input[type="radio"]:checked').val(), //	否	int	是否有效工单项(0有效，1无效)
						$('#order_note').val(''), // 无效原因	否	string	备注 (iseffective，1无效时填写备注)
						$('#recive_type').attr(''), //$('#order_transfer_dept').attr('data-id')	否	int	咨询类工单指派部门id
						$('#ower_recive').attr(''), //	否	string	接收人id $(this)
						window.location.reload();
					} else {
						mui.toast(data.message);
					}
				}
			});
		}
	})

//	接收部门
	function getDepts() {}
	var recive = [];
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + 'Department/GetDeptList',
		async: false,
		data: {},
		dataType: 'json',
		success: function(data) {
			console.log(data)
			recive = data.data;
			//alert(JSON.stringify(data));
		}
	});	
	//项目名称
	var areaList = [];
	console.log(areaList);
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + 'CusRegionCategory/GetListTreeDrop',
		async: false,
		data: {},
		dataType: 'json',
		success: function(data) {
			console.log(data)
			areaList = data.data;
			//alert(JSON.stringify(data));
		}
	});
//接收人员
function getReceiveUser() {
	renyuan=[]
	console.log(renyuan);
	$.ajax({
		url: huayi.config.callcenter_url + 'UserAccount/GetDeptUserList',
		type: 'get',
		data: {
			deptid: $('#recive_type').attr('data-index'),
		},
		dataType: "json",
		async: true,
		success: function(res) {
			
			if(res.state.toLowerCase() === "success") {
				res = res.data;
				if(res && res.length > 0) {
					res.forEach(function(v, i) {
						var obj1 = {};
						obj1.value = v.F_UserId;
						obj1.text = v.F_UserName;
//						alert(obj1.text);
						renyuan.push(obj1);
						//$('<option value="' + v.F_UserId + '">' + v.F_UserName + '</option>').appendTo('#order_recive');
					});
					cityPicker5.setData(renyuan);
				}
			}
		},
	});
}
//	受理人
function getOrderResponsible() {
	 shouli = [];	
	$.ajax({
		url: huayi.config.callcenter_url + 'CallInScreen/GetUserListByBuildingidDrop',
		type: 'get',
		data: {
			depid: $('#complaintOrder_type').attr('data-index'), //	否	int	部门id
			arearid: $('#order_rengin').attr('data-index'), //	否	int	区域id
			proid: $('#order_pro').attr('data-index'), //	是	int	姓名/项目id
			buildingid: $('#order_floor').attr('data-index') //	否	int	楼号id
		},
		dataType: "json",
		async: true,
		success: function(res) {
			if(res.state.toLowerCase() === "success") {
				res = res.data;
				
				if(res && res.length > 0) {
					res.forEach(function(v, i) {
						var obj = {};
						obj.value = v.F_UserCode;
						obj.text = v.F_UserName;
						
						shouli.push(obj);
						
						//$('<option value="' + v.F_UserCode + '">' + v.F_UserName + '</option>').appendTo('#order_responsible');
					});
					zhuTiPicker7.setData(shouli);
				}
			}
		},
	});
}
	//房东关系
	var keyList = [];
	console.log(keyList);
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + 'Dictionary/GetDicValueListByFlagWx',
		async: false,
		data: {
			flag: 'FZGX'
		},
		dataType: 'json',
		success: function(data) {
			var key_data = data.data;
			console.log(data.data);
			$(key_data).each(function(i, n) {
				var obj = {};
				obj.value = n.F_DictionaryValueId;
				obj.text = n.F_Name;
				keyList.push(obj)
			})
		}
	});

	(function($, doc) {
		$.init();
		$.ready(function() {
//			//项目名称
			var ZTiPickers = new $.PopPicker({
							layer: 3
			});
			
			var ZXs = doc.getElementById('showZhuTiPicker');
			var ZXResults = doc.getElementById('order_rengin');
			var ZXResults2 = doc.getElementById('order_pro');
			var ZXResults3 = doc.getElementById('order_floor');
			ZXs.addEventListener('tap', function(event) {
				ZTiPickers.setData(areaList);
				ZTiPickers.show(function(items) {
					console.log(items)
					ZXResults.value =items[0].text + "-" + items[1].text + "-" + items[2].text;
					ZXResults.setAttribute("data-index", items[0].id);
					ZXResults2.setAttribute("data-index", items[1].id);
					ZXResults3.setAttribute("data-index", items[2].id);
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
			//接收部门
			var RCiPickers = new $.PopPicker({
							layer: 3
			});
			RCiPickers.setData(recive);
			var RCs = doc.getElementById('showUserPicker1');
			var RCResults = doc.getElementById('recive_type');
			var RCResults2 = doc.getElementById('recive_type1');
			var RCResults3 = doc.getElementById('recive_type2');
			RCs.addEventListener('tap', function(event) {
				RCiPickers.show(function(items) {
					console.log(items)
					if (items[2].text==undefined) {
						RCResults.value =items[1].text;
						RCResults.setAttribute("data-index", items[1].id);
					} else if (items[1].text==undefined) {
						RCResults.value = items[0].text;
						RCResults.setAttribute("data-index", items[0].id);
					} else{
						RCResults.value =items[2].text
						RCResults.setAttribute("data-index", items[2].id);
					}
					//RCResults.value =items[0].text + "-" + items[1].text + "-" + items[2].text;
					//RCResults.setAttribute("data-index", items[0].id);
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
			//	接收人员
			cityPicker5 = new $.PopPicker();
			
			var showCityPickerButton5 = doc.getElementById('showCityPicker5');
			var cityResult5 = doc.getElementById('ower_recive');
			showCityPickerButton5.addEventListener('tap', function(event) {
				getReceiveUser();
				cityPicker5.show(function(items) {
					
					cityResult5.value = items[0].text;
					cityResult5.setAttribute("data-index", items[0].value);
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
//			房东关系
			var cityPicker3 = new $.PopPicker();
			cityPicker3.setData(keyList);
			var showCityPickerButton = doc.getElementById('showCityPicker3');
			var cityResult3 = doc.getElementById('ower_relationship');
			showCityPickerButton.addEventListener('tap', function(event) {
				cityPicker3.show(function(items) {
					cityResult3.value = items[0].text;
					cityResult3.setAttribute("data-index", items[0].value);
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
			//受理人
			zhuTiPicker7 = new $.PopPicker();
			
			var showZhuTiPickerButton7 = doc.getElementById('showUserPicker2');
			var zhuTiResult7 = doc.getElementById('order_responsible');
			showZhuTiPickerButton7.addEventListener('tap', function(event) {
				getOrderResponsible()
				
				zhuTiPicker7.show(function(items) {
					zhuTiResult7.value = items[0].text;
					zhuTiResult7.setAttribute("data-index", items[0].value);
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
			//投诉类型
			var userPicker =  new $.PopPicker({
					layer: 3
			});
			userPicker.setData(cityData3);
			var showUserPickerButton = doc.getElementById('showUserPicker');
			var userResult = doc.getElementById('complaintOrder_type');
			var userResult2 = doc.getElementById('serviceOrder_type');
			var userResult3 = doc.getElementById('is_quality_assurance');
			showUserPickerButton.addEventListener('tap', function(event) {
				userPicker.show(function(items) {
					if (items[2].text==undefined) {
						userResult.value = items[0].text + "-" + items[1].text;
					} else if (items[1].text==undefined) {
						userResult.value = items[0].text;
					} else{
						userResult.value = items[0].text + "-" + items[1].text + "-" + items[2].text
					}
					userResult.setAttribute("data-index", items[0].value);
					userResult2.setAttribute("data-index", items[1].value);
					userResult3.setAttribute("data-index", items[2].value);
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
			
		})
	})(mui, document);

})

