/**
 * 工单处理
 * */
var openid = helper.cookies.get("openid");
var id = helper.request.queryString("id");
var countyPicker; //乡镇
if(id) {
	//获取信息分类 、乡镇 及交办单位后获取 详情信息
	$.when(getTypeList(), getCounty()).then(function() {
		getWorkDetails(); //获取工单详情
	});
	
	var userCode = helper.cookies.get("usercode");
	$('#h_inquser').val(userCode);
	//获取当前日期
	var nowDate = helper.DateFormat.getNowDate();
	$('#h_inqtime').val(nowDate);
	$("#submit").on('tap', submitHandle);
}

function getWorkDetails() {
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + 'WxLogin/GetWorkOrder',
		async: true,
		data: {
			openid: openid,
			workorderid: id
		},
		success: function(data) {
			if($('#hy_loading')){
				$('#hy_loading').fadeOut();
				$('#hy_loading').remove();
			}
			data = JSON.parse(data);
			if(data.state.toLowerCase() == "success") {
				var con = data.data;
				if(con) {
					if(con.data && con.data.length > 0) {
						var res = con.data[0];
						$('#h_userName').val(res.Customer); //string	是	客户姓名	

						countyPicker.pickers[0].setSelectedValue(res.County); //int	否	乡镇
						var sItems = countyPicker.getSelectedItems();
						$('#h_county').val(sItems[0].text);
						$('#h_county').attr('data-index', sItems[0].value);

						$('#h_address').val(res.Address); //string	否	住址
						$('#h_contents').val(res.Detail); //string	是	反映问题	

						//						$('#h_inquser').val(res.Inquser); //string	否	调查员
						//						$('#h_inqtime').val(res.Inqtime); //string	否	调查时间	
						//						$('#h_infoType').val(res.InfoTypeID); //int	否	信息分类id，默认为0
						//						$('#h_unit').val(res.UnitID); //int	否	交办单位id，默认为0
						//						$('#h_cont').val(res.clcontent); //string	否	问题处理情况
						//						$('#h_answer').val(res.Answer); //string	否	回访反馈情况	
						//						$('#h_remark').val(res.Remark); //string	否	备注

						//图片展示
//						var Files = res.File;
//						if(Files) {
//							$(Files).each(function(i, n) {
//								$('<div class="img-box"><img src="' + n.F_FileUrl + '" alt="" class="image-item" data-preview-src="" data-preview-group="1" /><div>').appendTo("#image-list");
//							})
//						}
					}
				}

			}
		}
	});
}

function submitHandle() {
	var that = this;
	if(!$.trim($("#h_userName").val())) {
		mui.toast("请您输入姓名后再提交");
		return;
	}
	if(!$.trim($("#h_contents").val())) {
		mui.toast("请您输入反映问题后再提交");
		return;
	}
	$.ajax({
		type: "post",
		url: huayi.config.callcenter_url + 'WxLogin/DealWorkOrder',
		async: true,
		beforeSend: function() {
			mui(that).button('loading');
		},
		data: {
			openid: openid, //string	是
			orderid: id, //string	是	工单编号	
			cusname: $('#h_userName').val(), //string	是	客户姓名	
			country: $('#h_county').attr('data-index'), //int	否	乡镇	
			address: $('#h_address').val(), //string	否	住址
			content: $('#h_contents').val(), //string	是	反映问题	
			issend:	$('#h_resut').find('input[type="radio"]:checked').val(),
			inquser: $('#h_inquser').val(), //string	否	调查员
			inqtime: $('#h_inqtime').val(), //string	否	调查时间	
			infotypeid: $('#h_infoType').attr('data-index'), //int	否	信息分类id，默认为0
			unitid: $('#h_unit').attr('data-index'), //int	否	交办单位id，默认为0
			cont: $('#h_cont').val(), //string	否	问题处理情况
			answer: $('#h_answer').val(), //string	否	回访反馈情况	
			remark: $('#h_remark').val(), //string	否	备注

		},
		dataType: 'json',
		success: function(data) {
			mui(that).button('reset');
			if(data.state.toLowerCase() == "success") {
				mui.alert("提交成功", ' ', function() {
					window.location.href = 'WorkCL.html';
				});
			}
		},
		error: function(textStatus) {
			mui(that).button('reset');
			mui.toast('网络繁忙，请稍后再试...');
		},
		complete: function(XMLHttpRequest, textStatus) {
			mui(that).button('reset');
			if(textStatus == 'timeout') {
				var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
				xmlhttp.abort();
				mui.toast('网络超时，请稍后再试...');
			}
		},
	});

}

//信息分类
function getTypeList() {
	var dtd = $.Deferred(); //在函数内部，新建一个Deferred对象
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + 'WxLogin/GetTypeList',
		async: false,
		data: {
			openid: openid,
		},
		dataType: 'json',
		success: function(data) {
			if(data.state.toLowerCase() == "success") {
				var res = data.data;
				if(res.length > 0) {
					//下拉弹出
					(function($, doc) {
						$.init();
						$.ready(function() {
							var t_countyDatas = [{
								value: 0,
								text: '请选择信息分类'
							}];
							res.forEach(function(item, index) {
								t_countyDatas.push({
									value: item.F_DictionaryValueId,
									text: item.F_Name
								});
							});
							var infoTypesPicker = new $.PopPicker();
							infoTypesPicker.setData(t_countyDatas);
							//信息分类
							var showInfoTypebtn = doc.getElementById('showInfoTypePicker');
							var infotypeRes = doc.getElementById('h_infoType');
							showInfoTypebtn.addEventListener('tap', function(event) {
								infoTypesPicker.show(function(items) {
									infotypeRes.value = items[0].text;
									infotypeRes.setAttribute("data-index", items[0].value);
								});
							}, false);
						});
					})(mui, document);
				}
				dtd.resolve(); // 改变Deferred对象的执行状态
			}
		}
	});
	return dtd.promise(); // 返回promise对象
}

//获取乡镇 及交办单位
function getCounty() {
	var dtd = $.Deferred(); //在函数内部，新建一个Deferred对象
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "WxLogin/GetAreaList",
		dataType: 'json',
		async: true,
		data: {
			openid: openid,
		},
		success: function(data) {
			if(data.state.toLowerCase() == "success") {
				var res = data.data;
				if(res.length > 0) {
					//下拉弹出
					(function($, doc) {
						$.init();
						$.ready(function() {
							var t_countyDatas = [{
								value: 0,
								text: '请选择乡镇'
							}];
							res.forEach(function(item, index) {
								t_countyDatas.push({
									value: item.F_DictionaryValueId,
									text: item.F_Name
								});
							});
							var t_unitDatas = [{
								value: 0,
								text: '请选择交办单位'
							}];
							res.forEach(function(item, index) {
								t_unitDatas.push({
									value: item.F_DictionaryValueId,
									text: item.F_Name
								});
							});
							countyPicker = new $.PopPicker();
							countyPicker.setData(t_countyDatas);
							var unitPicker = new $.PopPicker();
							unitPicker.setData(t_unitDatas);
							//乡镇
							var showCountybtn = doc.getElementById('showCountyPicker');
							var countyRes = doc.getElementById('h_county');
							showCountybtn.addEventListener('tap', function(event) {
								countyPicker.show(function(items) {
									countyRes.value = items[0].text;
									countyRes.setAttribute("data-index", items[0].value);
								});
							}, false);
							//交办单位
							var showUnitbtn = doc.getElementById('showUnitPicker');
							var unitRes = doc.getElementById('h_unit');
							showUnitbtn.addEventListener('tap', function(event) {
								unitPicker.show(function(items) {
									unitRes.value = items[0].text;
									unitRes.setAttribute("data-index", items[0].value);
								});
							}, false);

						});
					})(mui, document);
				}
				dtd.resolve(); // 改变Deferred对象的执行状态
			}
		}
	});
	return dtd.promise(); // 返回promise对象
}

//时间控件
(function($, doc) {
	$.init();
	var btns = $('.btn');
	btns.each(function(i, btn) {
		btn.addEventListener('tap', function() {
			var _self = this;
			if(_self.picker) {
				_self.picker.show(function(rs) {
					_self.picker.dispose();
					_self.picker = null;
				});
			} else {
				var optionsJson = this.getAttribute('data-options') || '{}';
				var options = JSON.parse(optionsJson);
				var id = this.getAttribute('id');
				_self.picker = new $.DtPicker(options);
				_self.picker.show(function(rs) {
					document.getElementById("h_inqtime").value = rs.text;
					_self.picker.dispose();
					_self.picker = null;
				});
			}

		}, false);
	});
})(mui, document);
