<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<jsp:include page="../../common.jsp" />
<style type="text/css">
.weui-cell {
	justify-content: space-between;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
	<c:if test="${ storeId ne  'null' }">
        <title>会员开卡</title>
    </c:if>
    <c:if test="${ storeId eq  'null' }">
    <title>请扫商家会员码</title>
    </c:if>
<title>会员开卡</title>
</head>
<body>
	<div class="page-title tcenter h40 bluebg white">
	<c:if test="${ storeId ne  'null' }">
		<span>会员开卡</span>
	</c:if>
	<c:if test="${ storeId eq  'null' }">
	<span>请扫商家会员码</span>
	</c:if>
	</div>
	
	<c:if test="${ storeId ne  'null' }">
	<div class="weui-cells weui-cells_form bdbe">
		<div class="weui-cell weui-cell_warn">
			<div class="weui-cell__hd">
				<span class="red">*</span>手机号
			</div>
			<div class="weui-cell__bd">
				<input class="weui-input tleft pl10" type="number" id="phone"
					pattern="[0-9]*" value="weui input error" placeholder="请输入手机号码">
			</div>
		</div>
	</div>
	<div class="weui-cells weui-cells_form bdbe">
		<div class="weui-cell weui-cell_warn">
			<div class="weui-cell__hd">
				<span class="red">*</span>验证码
			</div>
			<div class="weui-cell__bd">
				<input class="weui-input tleft pl10" type="number" id="phoneverify"
					pattern="[0-9]*" value="weui input error" placeholder="请输入验证码">
			</div>
			<div class="weui-cell__bd tright">
				<a href="javascript:void(0);onclick=verification()" id="obtain"
					class="weui-btn_primary radius5 pt10 pb10 pl10 pr10 dlblock white f12">获取验证码</a>
			</div>
		</div>
	</div>
	<div class="weui-cells weui-cells_form bdbe">
		<div class="weui-cell weui-cell_warn">
			<div class="weui-cell__hd">会员名</div>
			<div class="weui-cell__bd">
				<input class="weui-input tleft pl10" id="nickName" type="text"
					placeholder="请输入会员名">
			</div>
		</div>
	</div>
	<div class="weui-cells weui-cells_form bdbe">
		<div class="weui-cell weui-cell_warn">
			<div class="weui-cell__hd">生日&nbsp;</div>
			<div class="weui-cell__bd pl10">
				<input class="weui-input tleft pl10" data-toggle='date'  type="text" id="my-input" />
			</div>
		</div>
	</div>
	<div class="btn mt60 ml20 mr20">
		<a href="#" onclick="openCardSuccess()"
			class="weui-btn weui-btn_primary radius10 f14 pt15 pb15">完成</a>
	</div>
	</c:if>
	<c:if test="${ storeId  eq 'null'}">
	   <div class="margin50-top " style="text-align:center;margin-top:80px;">
	         <img alt="" src="${resourcePath}/images/member.png"  width="150">
	         <div class="mt20">请扫商家会员码</div>
	   </div>
	</c:if>
	<input id="storeId" type="hidden" value="${storeId }">
 	<input id="openid" type="hidden"  value="${openid }">
 	<input id="userPhone" type="hidden" value="${userPhone }">
</body>
<script type="text/javascript">

$("#my-input").datetimePicker({
	times: function () {
        return [
        ];
    }
});
	function verification() {
		var phoneReg = /(^1[3|4|5|7|8|9]\d{9}$)|(^09\d{8}$)/;
		var phone=$.trim($("#phone").val());
		var storeId=$("#storeId").val();
		if (!phoneReg.test(phone)) {
			alert("请输入正确的手机号");
		}else{
			var herf = 'javascript:void(0); onclick=verification()';
            var _this = $("#obtain");
            _this.removeAttr("href");
			$.ajax({
				url:ctx+'/vip/getPhoneCodeVIP',
			    data:{
			    	"phone":phone,
			    	"storeId":storeId,
			    },
			    dataType:"json",
			    type:"post",
			    success:function(data){
			    	//成功
			    	if(data.code=='1'){
			            var sec = 60;
			            var timer = window.setInterval(function() {
			                sec--;
			                if (sec > 0) {
			                    _this.text(sec + "秒重新发送!");
			                } else {
			                    // 去掉定时器
			                    window.clearInterval(timer);
			                    _this.text("获取验证码");
			                    _this.attr("href","javascript:void(0); onclick=verification()");
			                }
			            }, 1000);
			    	}else if(data.code=='0'){
                        alert("获取验证码失败，请稍后再试");
                        _this.text("获取验证码");
                        _this.attr("href",href);
                    }else if(data.code=='2'){
                    	//系统错误
                    	alert("系统繁忙！");
                        _this.text("获取验证码");
                        _this.attr("href",href);
                    }else if(data.code=='3'){
                    	alert("手机号已被注册 ！");
                    	 _this.text("获取验证码");
                         _this.attr("href",href);
                    } 
			    }
			})
	   }
	}
    
	//
	function openCardSuccess() {
		var phone = $("#phone").val();
		var phoneverify = $("#phoneverify").val();
		var nickName = $("#nickName").val();
		var birthdayDt = $("#my-input").val();
		var storeId=$("#storeId").val();
		var openid=$("#openid").val();

		if (phone == "") {
			alert("请输入手机号");
			return;
		}

		if (phoneverify == "") {
			alert("请输入验证码");
			return;
		}

		if (nickName == "") {
			alert("请输入会员名");
			return;
		}

		if (birthdayDt == "") {
			alert("请输入生日");
			return;
		}

		if(storeId==""){
			alert("页面已超时，请重新扫码");
			return;
		}

		$.ajax({
			url : ctx + '/vip/openCardSuccess',
			data : {
				"phone" : phone,
				"nickName" : nickName,
				"birthdayDt" : birthdayDt,
				"phoneverify" : phoneverify,
				"storeId":storeId,
				"openid":openid,
			},
			dataType : "json",
			type : "post",
			success : function(result) {
				//成功
				if (result.code == "1") {
					alert("开卡成功");
					window.location = ctx + "/vip/myVIPCard?storeId="+storeId + "&phone=" + phone;
				} else if (result.code == "0") {
					alert("验证码错误");
				} else if (result.code == "2") {
					alert("系统繁忙");
				}else if(result.code=="3"){
					alert("页面已超时，请重新扫码");
				} else {
					alert(result.msg);
				}
			}

		})
	}
</script>
</html>