var reg = /,$/gi;

var openid = helper.cookies.get("openid");
var id = helper.request.queryString("id");

if(id) {
	//获取工单详情
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + 'WxLogin/GetWorkOrder',
		async: true,
		data: {
			//openid: openid,
			openid: 'oKOqcv31fh76rtNmJDjKc39oIsfA', ///测试 ID
			workorderid: id
		},
		success: function(data) {
			var con = JSON.parse(data);
			var Str = con.data.data;
			$("#question").text(Str[0].Detail);
			//图片展示
			var Files = Str[0].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");
				})
			}
		}
	});
}

$("#submit").on('tap', submitHandle);

function submitHandle() {
	mui(this).button('loading');
	if(!$("#clcont").val()) {
		mui.toast("请填写处理意见");
	} else {
		$.ajax({
			type: "post",
			url: huayi.config.callcenter_url + 'WxLogin/DealWorkOrder',
			async: true,
			data: {
				//openid: helper.cookies.get("openid"),//string	是
				openid: 'oKOqcv31fh76rtNmJDjKc39oIsfA', ///测试 ID
				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	是	反映问题	

				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(this).button('reset');
				if(data.state = "success") {
					window.location.href = 'WorkList.html?'
				}
			}
		});
	}

}

getTypeList();
//信息分类
function getTypeList() {
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + 'WxLogin/GetTypeList',
		async: false,
		data: {
			//openid: helper.cookies.get("openid"),
			openid: 'oKOqcvw24ZMtsRKlpjBFKHAWuDmI' ///测试 ID
		},
		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 = [];
							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);
				}
			}
		}
	});
}

getCounty();
//获取乡镇 及交办单位
function getCounty() {
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "WxLogin/GetAreaList",
		dataType: 'json',
		async: true,
		data: {
			openid: 'oKOqcvw24ZMtsRKlpjBFKHAWuDmI', //微信openid 测试用
			//openid: helper.cookies.get("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 = [];
							res.forEach(function(item, index) {
								t_countyDatas.push({
									value: item.F_DictionaryValueId,
									text: item.F_Name
								});
							});
							var countyPicker = new $.PopPicker();
							countyPicker.setData(t_countyDatas);
							//乡镇
							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) {
								countyPicker.show(function(items) {
									unitRes.value = items[0].text;
									unitRes.setAttribute("data-index", items[0].value);
								});
							}, false);

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

//时间控件
(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("StartTime").value = rs.text;
					_self.picker.dispose();
					_self.picker = null;
				});
			}

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