mui.plusReady(function() {
	mui.init({
		beforeback: function() { //获得父页面的webview
			var list = plus.webview.currentWebview().opener(); //触发父页面的自定义事件(refresh),从而进行刷新
			mui.fire(list, 'reload');
			//返回true,继续页面关闭逻辑
			return true;
		}
	});
	var shopAddress = "";
     $('.header_con').append('<p class="header_con_bc">保存</p>')
	$(".oper .next").css("width", "100%")
	$(".oper .pre").css("width", "0px")
	// 判断是编辑还是新增
	var self = plus.webview.currentWebview();
	// //console.log(self);
	var shopId = self.id ? self.id : 0;
	// //console.log(shopid);
	if (shopId > 0) {
		//console.log(22);
		JZL.ajax(qlgUrl('app/shops/getShopJoinInfo'), {
			shopId: shopId
		}, function(data) {
			//console.log(data);
			if (1 == data.status) {
				var data = data.data;
				getMap(data.lng, data.lat);
				mui.each(data, function(index, element) {
					//console.log(element);
					if ($('#' + index).attr("type") == "hidden") {
						var imgindex = index.substring(0, index.length - 3);
						var obj = '#' + imgindex + ''
						// //console.log('#' + imgindex + '')
						if (imgindex != "") {
							if ($(obj).is('img')) {
								$(obj).attr("src", hyhImgUrl(element))
								$('#' + index + '').val(element)
							}
						}

					} else if ($('#' + index).is("select")) {

						switch (index) {
							case 'provinceId':
								getAreaName(element, 1, function(data) {
									getArea(index, 1, 0, '请选择省', data.data.areaId);
									// 									$('#'+index).html('<option selected value="'+data.data.areaId+'">'+data.data.areaName+'</option>')
								})
								break;
							case 'cityId':
								getAreaName(element, 2, function(data) {
									$('#' + index).html('<option selected value="' + data.data.areaId + '">' + data.data.areaName +
										'</option>')
								});
								break;
							case 'countyId':
								getAreaName(element, 3, function(data) {
									$('#' + index).html('<option selected value="' + data.data.areaId + '">' + data.data.areaName +
										'</option>')
								});
								break;
							case 'townId':
								getAreaName(element, 4, function(data) {
									$('#' + index).html('<option selected value="' + data.data.areaId + '">' + data.data.areaName +
										'</option>')
								});
								break;
							case 'villageId':
								getAreaName(element, 5, function(data) {
									$('#' + index).html('<option selected value="' + data.data.areaId + '">' + data.data.areaName +
										'</option>')
								});
								break;
							case 'bankName':
								getBankList(element);

								break;
							default:
								break;
						}
						// $('#'+index+' option:selected').text(element)
						// $('#'+index+' option:selected').attr('value',element)
						// $('#'+index+' option:selected').attr('text',element)
					} else {
						$('#' + index).val(element)
						// //console.log(this);
					}
				})
				 shopAddress = $('#shopAddress').val();
			} else {
				mui.alert(data.msg)
			}
		})
		setTimeout(function() {
			$('#shopAddress').val(shopAddress)
		}, 1000)
	} else {
		
		getBankList();
		getArea('provinceId', 1, 0, '请选择省');
		getLocation(function(data) {
			//console.log(11);
			if (1 == data.status) {
				$('#lng').val(data.lng);
				$('#lat').val(data.lat);
				getMap(data.lng, data.lat)
				//console.log(JSON.stringify(data));
			} else {
				mui.alert(data.errStr);
				return;
			}
		});
	JZL.getItems('editorShopInfo')
	}

	function getAreaName(id, level, callback) {
		JZL.ajax(qlgUrl("app/Position/getAreaName"), {
			id: id,
			level: level
		}, function(data) {
			callback(data);
		})
	}
	//下一步
	mui('.next').on('tap', '.bc_btn', function() {
		var params = JZL.getParams(".inp")
		// //console.log(params);
		// var html=
		var idx = $(this).index() + 1;
		var prenum = idx + 1;
		// checkStep(idx)
		if (checkStep(idx)) {
			if (prenum !== 1) {
				// //console.log(1);
				$(".pre").attr("display", "block")
				$(".oper .pre").css("width", "50%")
				$(".oper .next").css("width", "50%")
			}
			if (prenum == 1) {
				// //console.log(1);
				$(".oper .next").css("width", "100%")
				$(".oper .pre").css("width", "0px")
			}
			$('.con-nav ul li').eq(idx).addClass("active")
			$('.next .bc_btn').eq(idx).show().siblings().hide()
			$('.pre .pre_btn').eq(idx).show().siblings().hide()
			$('.con-content .num').eq(idx).show().siblings().hide()
			JZL.saveItems('.localinp', 'auth_personal');
			backTop()
		}
	})
	//上一步
	mui('.pre').on('tap', '.pre_btn', function() {
		var idx = $(this).index() - 1;
		var prenum = idx + 1;
		if (checkStep(idx)) {
			if (prenum !== 1) {
				$(".pre").attr("display", "block")
				$(".oper .pre").css("width", "50%")
				$(".oper .next").css("width", "50%")
			}
			if (prenum == 1) {
				$(".oper .next").css("width", "100%")
				$(".oper .pre").css("width", "0px")
			}
			$('.con-nav ul li').eq(idx + 1).removeClass("active")
			$('.next .bc_btn').eq(idx).show().siblings().hide()
			$('.pre .pre_btn').eq(idx).show().siblings().hide()
			$('.con-content .num').eq(idx).show().siblings().hide()
			JZL.getItems('auth_personal');
			backTop()
		}
	})
	//五级联动
	$('.selectarea').on("change", ".area", function() {
		var areas = ['province', 'city', 'county', 'town', 'village'];
		var areaStr = ['请选择省', '请选择市', '选择区县', '选择乡镇', '选择村社区'];
		var level = $(this).attr('data-level'); //1
		pid = $("#" + areas[level - 1] + "Id  option:selected").val();
		var sonLevel = +level + 1;
		getArea(areas[level] + 'Id', sonLevel, pid, areaStr[level]);

	})

	function getArea(area, level, pid, selectAreaStr, defaultId) {
		JZL.ajax(qlgUrl('app/Position/listQuery'), {
			level: level,
			pid: pid
		}, function(data) { //服务器返回响应，根据响应结果，分析是否登录成功；  
			data = toJson(data);
			if (data.status == 1) {
				data = data.data;
				var html = '<option value="">' + selectAreaStr + '</option>';
				if ('undefined' == typeof(defaultId)) {
					$.each(data, function() {
						html += '<option value="' + this.areaId + '">' + this.areaName + '</option>'
					});
				} else {
					$.each(data, function() {
						if (this.areaId == defaultId) {
							selected = 'selected';
						} else {
							selected = '';
						}
						html += '<option value="' + this.areaId + '" ' + selected + '>' + this.areaName + '</option>'
					});
				}

				// //console.log(area)
				$('#' + area).html(html);

			} else {
				// 				mui.alert('发生错误请刷新后重试！');
				// 				//							location.reload();
				mui.alert(data.msg)
			}
		})
	}

	// 上传图片

	$(".photos_con").on("tap", '.sfzzm', function() {
		UP.init("idCardFrontImg", "test", "idCardFront")
		openCamera();
	})

	$(".photos_con").on("tap", '.sfzfm', function() {
		UP.init("idCardBackImg", "test", "idCardBack")
		openCamera();
	})
	$(".photos_con").on("tap", '.weituoshu', function() {
		UP.init("commissionImg", "test", "commission")
		openCamera();
	})
	$(".photos_con").on("tap", '.shopsimg', function() {
		UP.init("businessLicenceImg", "test", "businessLicence")
		openCamera();
	})
	$(".photos_con").on("tap", '.querenshu', function() {
		UP.init("confirmationImg", "test", "confirmation")
		openCamera();
	})

	function getBankList(defaultBankName) {
		JZL.ajax(qlgUrl('app/auth/getBankNameList'), {

			},
			function(data) {
				//服务器返回响应，根据响应结果，分析是否登录成功；  
				//console.log(data)
				data = toJson(data);
				if (data.status == 1) {
					data = data.data;
					var html = '<option value="">选择银行</option>';
					// var html="";
					if ('undefined' == typeof(defaultBankName)) {
						$.each(data, function() {
							// //console.log(data);
							html += '<option value="' + this.bankName + '">' + this.bankName + '</option>'
						});
					} else {
						// //console.log(data);
						$.each(data, function() {
							// //console.log(data);
							if (this.bankName == defaultBankName) {
								selected = 'selected';
							} else {
								selected = '';
							}
							html += '<option value="' + this.bankName + '" ' + selected + '>' + this.bankName + '</option>'
						});
					}

					$('.bankname').append(html);

				} else {
					// mui.alert('发生错误请刷新后重试！');
					//							location.reload();
					mui.alert(data.msg)
				}
			})
	}

	//获取协议
	JZL.ajax(qlgUrl('app/Tags/articleDetail'), {
		articleId: 112
	}, function(data) {
		//console.log(data);
		// if (1==data.status) {
		// $('.zcxycontent').html(data.data.articleContent)
		$('.confirmationtext_con').html(data.articleContent)

		// }

	});


	function checkStep(step) {
		var params = JZL.getParams(".inp")
		// 		//console.log(params);
		// 		//console.log(step);
		switch (step) {
			case 1:
				if ('' == params.shopName) {
					mui.alert("请输入店铺名称")
					return;
				}
				if ('' == params.phone) {
					mui.alert("请输入手机号码")
					//console.log(params);
					return;
				}
				if ('' == params.userName) {
					mui.alert("请输入姓名")
					return;

				}

				if ('' == params.provinceId) {
					mui.alert("请输入所在省")
					return;

				}
				if ('' == params.cityId) {
					mui.alert("请输入所在市")
					return;

				}
				if ('' == params.countyId) {
					mui.alert("请输入所在区")
					return;

				}
				if ('' == params.townId) {
					mui.alert("请输入所在镇")
					return;
				}
				if ('' == params.villageId) {
					mui.alert("请输入所在村")
					return;
				}
				if ('' == params.shopAddress) {
					mui.alert("请输入详细地址")
					return;
				}
				if ('' == params.lat) {
					mui.alert("请在地图上选择位置")
					return;
				}
				break;
			case 2:
				if ('' == params.accountName) {
					mui.alert("请输入持卡人姓名")
					return;

				}
				if ('' == params.bankName) {
					mui.alert("请输入银行名称")
					return;

				}
				if ('' == params.bankNo) {
					mui.alert("请输入银行卡号")
					return;

				}
				if ('' == params.userName) {
					mui.alert("请输入开户行")
					return;

				}
				break;
			case 3:
				if ('' == params.idCardFrontImg) {
					mui.alert("请上传身份证正面照")
					return;
				}
				if ('' == params.idCardBackImg) {
					mui.alert("请上传身份证反面照")
					return;
				}
				if ('' == params.commissionImg) {
					mui.alert("请上传直营人委托书照片")
					return;
				}
				if ('' == params.businessLicenceImg) {
					mui.alert("请上传营业执照照片")
					return;
				}
				break;
			case 4:


				if ('' == params.commissionImg) {
					mui.alert("请上传手持确认书照片")
					return;
				}

				break;
			default:
				break;
		}
		return true;
	}
	
	// 保存
	mui('.header').on('tap','.header_con_bc',function(){
		JZL.saveItems (".inp","editorShopInfo");
		mui.toast('保存成功');
	})
	//提交
	var flag = false;


	mui('.next').on('tap', '.finish', function() {
		if (flag == true) return;
		flag = true
		var params = JZL.getParams(".inp")
		if (shopId > 0) {
			params.shopId = shopId
		}

		JZL.ajax(qlgUrl('app/shops/shopJoin'), params, function(data) {

			//console.log(data);
			if (1 == data.status) {
				// JZL.saveItems ("inp","editorShopInfo");
				mui.toast(data.msg)
				localStorage.removeItem("editorShopInfo")
				mui.back()
				// JZL.openWindow("myshops.html", "myshops.html")
				// JZL.openWindow("myshop.html", "myshop.html")

			} else {
				mui.alert(data.msg)
			}


		})

	})

})
