var code = "200",token = "",check = true,flag = false;
mui.ready(function() {
	var showCityPickerButton = dc('showCityPicker3');
	mui.ajax({
		data: {},
		dataType: "json",
		type: 'post',
		url: base_path + '/placeInfo/get_placeInfo_detail',
		timeout: 10000,
		success: function(data) {
			//console.log(data);
			var tips = "";
			var roleName = data.returnMsg.placeInfo.roleName;
			if(data.resultCode == "200") {

				switch(data.returnCode) {
					case 2001:
						tips = '<h2>恭喜您</h2><h4>已激活"' + roleName + '"身份 !</h4>';
						break;
					case 2002:
						tips = "<h2>正在审核中</h2><h4>请耐心等待 !</h4>";
						check = false;
						break;
					case 2003:
						tips = "<h2>审核未通过</h2><h4>请核对好以下信息重新填写提交!</h4>"
						break;
					case 2005:
						tips = '<h2>恭喜您</h2><h4>已激活"' + roleName + '"身份 !</h4>';
						break;
					default:
						tips = '<h2>非常抱歉</h2><h4>您还未激活身份 !</h4>';
						break;
				}
				$(".tips").html(tips);
				var userNickname = data.returnMsg.placeInfo.userName;
				userNickname == null ? '' : dc('ipt-name').value = userNickname;
				var tel = data.returnMsg.placeInfo.tel;
				tel == null ? '' : dc('ipt-phone').value = tel;
				var name = data.returnMsg.placeInfo.name;
				name == null ? '' : dc('ipt-shop-name').value = name;
				var areaName = data.returnMsg.placeInfo.areaName;
				areaName == null ? '' : showCityPickerButton.value = areaName;
				var detailsAdress = data.returnMsg.placeInfo.addr;
				detailsAdress == null ? '' : dc('ipt-address').value = detailsAdress;
				var cityPicker3; //级联示例
				switch(roleName) {
					case "省代":
						cityPicker3 = new mui.PopPicker({
							layer: 1
						});
						break;
					case "代理":
						cityPicker3 = new mui.PopPicker({
							layer: 2
						});
						break;
					case "门店":
						cityPicker3 = new mui.PopPicker({
							layer: 3
						});
						break;
					case "母婴店":
						cityPicker3 = new mui.PopPicker({
							layer: 3
						});
						break;
					default:
					   cityPicker3 = new mui.PopPicker({
							layer: 3
						});
						break;
				}
				cityPicker3.setData(cityData3);
				var cityResult3 = document.getElementById('cityResult3');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						var str3 = (items[2] || {}).text;
						var str2 = (items[1] || {}).text;
						typeof(str3) == 'undefined' ? str3 = '': str3;
						typeof(str2) == 'undefined' ? str2 = '': str2;
						showCityPickerButton.value = (items[0] || {}).text + " " + str2 + " " + str3;
						change_input();
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
				getToken();
			} else {
				mui.alert('获取信息失败，请刷新重试！')
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	});
	listener_ipt();
});
function listener_ipt() {
	mui.each(document.querySelectorAll('.mui-input-group input'), function(index, el) {
		el.onblur = function() {
			change_input();
		}
	});
}
function getToken() {
	mui.ajax({
		type: 'post',
		url: base_path + '/drp/handler_place',
		data: {},
		dataType: 'json',
		timeout: 10000,
		context: $('body'),
		success: function(data) {
			code = data.returnCode;
			token = data.returnMsg.addApplyToken;
			console.log(data.returnMsg.addApplyToken);
			document.querySelector('.loading-bg').style.display = 'none';
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	});
}
mui('body').on('tap', '#btn_submit', function() {
	this.setAttribute('disabled','disabled');
	check_input();
});
mui('body').on('tap', '#guize-picture', function(e) {
	mui(this).popover('toggle');
});
function check_input() {
	var btn_submit = dc('btn_submit');
	mui(".mui-input-group input").each(function() {
		//若当前input为空，则alert提醒
		if(this.value == "") {
			var label = this.previousElementSibling;
			mui.alert(label.innerText + "不允许为空");
			check = false;
			btn_submit.className = 'mui-btn mui-btn-block mui-btn-outlined mui-active';
			return false;
		}
	});
	if(true) {
		btn_submit.className = 'mui-btn mui-btn-block mui-btn-red';
		//console.log(check && code != "200");
		$.ajax({
			data: {
				applyUserName: dc('ipt-name').value,
				applyTel: dc('ipt-phone').value,
				applyName: dc('ipt-shop-name').value,
				applyArea: dc('showCityPicker3').value,
				applyAddr: dc('ipt-address').value
			},
			dataType: "json",
			type: 'post',
			url: base_path + '/drp/add_apply/' + token,
			timeout: 10000,
			success: function(data) {
				//console.log(data);
				if(data.returnCode == "200") {
					mask.show(); //显示遮罩
					$('.mui-backdrop').html('<div class="yes-dalog"><div><div class="content-yes"><span><span class="mui-icon mui-icon-checkmarkempty"></span></span></div><h4>已提交审核！</h4><h5>信息将进入后台审核，请耐心等候</h5><div class="btn-yes">确认</div></div></div>');
					$('.btn-yes').on('tap', function() {
						flag = true;
						mask.close(); //关闭遮罩
						mui.openWindow({
							url: base_path + '/drp/placeInfo_detail?dates=' + new Date().getTime() //add_information.html
						});
					});
				} else {
					mui.alert(data.returnMsg.returnMsg);
				}
			},
			error: function(xhr, type, errorThrown) {
				//异常处理；
				console.log(type);
			}
		});

	}
}

function change_input() {
	var phone = /^(13|14|15|17|18|400|800|00|01|02|03|05|06|07|08|09)[0-9]{5,10}/; //手机号码，座机,400-800
	var name = /[^\u4e00-\u9fa5]/; //中文
	var shop = /[^A-Za-z0-9·\-^\u4e00-\u9fa5]/; //中文英文数字
	mui(".mui-input-group input").each(function() {
		switch(this.id) {
			case 'ipt-name':
				if(name.test(this.value) || this.value.indexOf(" ") >= 0) {
					check = false;
					this.focus();
					if(this.value != '') {
						mui.alert('请输入正确的姓名格式');
						return false;
					}
				} else {
					check = true;
				}
				break;
			case 'ipt-phone':
				if(phone.test(this.value) == false || this.value.indexOf(" ") >= 0) {
					check = false;
					this.focus();
					if(this.value != '') {
						mui.alert('请输入正确的电话号码');
						return false;
					}
				} else {
					check = true;
				}
				break;
			case 'ipt-shop-name':
				if(shop.test(this.value) || this.value.indexOf(" ") >= 0) {
					check = false;
					this.focus();
					if(this.value != '') {
						mui.alert('请输入正确的店铺名称');
						return false;
					}
				} else {
					check = true;
				}
				break;
			case 'ipt-address':
				if(shop.test(this.value) || this.value.indexOf(" ") >= 0) {
					check = false;
					this.focus();
					if(this.value != '') {
						mui.alert('请输入正确的联系地址');
						return false;
					}
				} else {
					check = true;
				}
				break;
			case 'showCityPicker3':
				if(this.value != '') {
					check = true;
				} else {
					check = false;
					return false;
				}
				break;
			default:
				check = false;
				break;
		}
	});
	var btn_submit = dc('btn_submit');
	if(check && code != "200") {
		btn_submit.className = 'mui-btn mui-btn-block mui-btn-red';
		btn_submit.removeAttribute("disabled");
	} else {
		btn_submit.className = 'mui-btn mui-btn-block mui-btn-outlined mui-active';
	}
}
var mask = mui.createMask(function() {
	return flag; //callback
});