mui.init({
	beforeback: function() { //获得父页面的webview
		var list = plus.webview.currentWebview().opener(); //触发父页面的自定义事件(refresh),从而进行刷新
		mui.fire(list, 'refresh');
		//返回true,继续页面关闭逻辑
		return true;
	}
});
mui.plusReady(function() {
	JZL.getItems('auth_company');
	var id = '';
	var count = 0;
	$('.header_con_bc').on('tap', function() {
		JZL.saveItems('.localinp', 'auth_company');
		mui.toast('保存成功');
	})

	if ('' == $('#headImg').val()) {
		$("#headImgTag").attr("src", "../img/touxiang.jpg")

	}
	JZL.ajax(qlgUrl('app/auth/getAuthInfo'), {
		isCompany: 1
	}, function(data) {
		//console.log(data);

		if ('undefined' != typeof(data.data) && 1 == data.status) { //编辑
			count = 1;
			$('.mobileCode').hide()
			$('.userPhone').hide()
			$('.payPwd').show()
			id = data.data.id
			
			//赋值
			////console.log(data);
			mui.each(data.data, function(index, element) {
				if ($('#' + index).attr("type") == "hidden") {
					// var imgindex = index.substring(0, index.length - 3);
					var imgindex = index + 'Tag'
					var obj = '#' + imgindex
					// //console.log('#' + imgindex + '')
					if (imgindex != "") {
						if ($(obj).is('img')) {
							$(obj).attr("src", hyhImgUrl(element))
							$('#' + index + '').val(element)
						}
					}

				} else {
					$('#' + index).val(element)
				}


			})

		} else { // 添加
			count = 0
			$('.userPhone').show()
			$('.mobileCode').show()
			$('.payPwd').hide()
			JZL.getItems('auth_company');

		}

	})
	mui('.mui-content').on('tap', '.yhk', function() {
		JZL.openWindow('yhk.html', 'yhk.html');

	})
	mui('.mui-content').on('tap', '.hhrrz', function() {
		JZL.openWindow('hhrrz.html', 'hhrrz.html');

	})

	var wait = 120;

	function time() {
		if (wait == 0) {
			$('.HQYZM').removeAttr("disabled");
			$('.HQYZM').val("重新发送");
			wait = 120;
		} else {
			$('.HQYZM').attr("disabled", true);
			$('.HQYZM').css("background", "#c0a0a0");
			$('.HQYZM').val("重新发送(" + wait + ")");
			wait--;
			setTimeout(function() {
					time()
				},
				1000)
		}
	}
	//获取验证码
	mui('.mui-table-view-cell').on('tap', '.HQYZM', function() {
		var userPhone = $('.indiv-tel').val();
		// 		if (userPhone == '') {
		// 			mui.alert('手机号不能为空!');
		// 			return;
		// 		}
		// 判断手机号码格式
		// 		if (!(
		// 				/^134[0-8]\d{7}$|^13[^4]\d{8}$|^14[5-9]\d{8}$|^15[^4]\d{8}$|^16[6]\d{8}$|^17[0-8]\d{8}$|^18[\d]{9}$|^19[8,9]\d{8}$/
		// 				.test(userPhone))) {
		// 			mui.alert("手机号码有误,请重填!");
		// 			return;
		// 		}
		// $(".HQYZM").attr("disabled", true);
		JZL.ajax(qlgUrl('app/auth/getPhoneCode'), {
			userPhone: userPhone
		}, function(data) {
			//console.log(data);
			if (1 == data.status) {
				time();
			}
			mui.toast(data.msg);
		})


	})

	// 上传头像
	$("#headerimg").on("tap", '#headImgTag', function() {
		UP.init("headImg", "test", "headImgTag")
		openCamera();
	})

	$('.bc_btn').on('tap', function() {


		var params = JZL.getParams(".inp");
		if ('' == params.trueName) {
			mui.alert("请输入直营人姓名");
			return;

		}
		if ('' == params.idCard) {
			mui.alert("请输入身份证号");
			return;

		}
		if ('' == params.companyName) {
			mui.alert("请输入合作名称");
			return;
		}
		if ('' == params.companyAddress) {
			mui.alert("请输入地址");
			return;
		}


		if ('' == params.companyName) {
			mui.alert("请输入合作名称");
			return;
		}
		if (0 == count) { //添加
			if ('' == $('#mobileCode').val()) {
				mui.alert("请输入验证码");
				return;
			}
			params.mobileCode = $('#mobileCode').val();

		} else if (1 == count) { //编辑
			if ('' == $('#payPwd').val()) {
				mui.alert("请输入操作密码");
				return;
			}
			params.authId = id;
			params.payPwd = $('#payPwd').val();
		}
		params.isCompany = 1
		JZL.ajax(qlgUrl('app/auth/setAuthInfo'), params, function(data) {
			//console.log(data);
			if (data.status == 1) {
				JZL.saveItems('.localinp', 'auth_company');
				localStorage.setItem("authType","2")
				mui.toast("提交成功 请等待审核")
				// $(".mui-table-view").attr("readonly", "readonly")
				$(".mobileCode").hide()
				$('.userPhone').hide()
				mui.back();
			} else {
				mui.alert(data.msg);
			}

		})
	})
})