
<script type="text/html" template>
  <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{ layui.admin.v }}-1" media="all">
</script>


<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
  <div class="layadmin-user-login-main">
    <div class="layadmin-user-login-box layadmin-user-login-header">
      <h2>注册</h2>
    </div>
    <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
      <div class="layui-form-item username layui-hide">
        <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
        <input type="text" name="username" id="LAY-user-login-username" lay-verify="username" placeholder="用户名" class="layui-input">
      </div>      

      <div class="layui-form-item email  layui-hide">
        <label class="layadmin-user-login-icon layui-icon layui-icon-email" for="LAY-user-login-email"></label>
        <input type="text" name="email" id="LAY-user-login-email" placeholder="邮箱" class="layui-input">
      </div>

      <div class="layui-form-item email-verify  layui-hide">
        <div class="layui-row">
          <div class="layui-col-xs7">
            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
            <input type="text" name="captcha" id="LAY-user-login-vercode" placeholder="邮箱验证码" class="layui-input">
          </div>
          <div class="layui-col-xs5">
            <div style="margin-left: 10px;">
              <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid" id="LAY-user-reg-getsmscode">获取验证码</button>
            </div>
          </div>
        </div>
      </div>

      <div class="layui-form-item phone  layui-hide">
        <label class="layadmin-user-login-icon layui-icon layui-icon-cellphone" for="LAY-user-login-cellphone"></label>
        <input type="text" name="phone" id="LAY-user-login-cellphone" placeholder="手机号码" class="layui-input">
      </div>

      <div class="layui-form-item phone-verify layui-hide">
        <div class="layui-row">
          <div class="layui-col-xs7">
            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
            <input type="text" name="phone_captcha"  placeholder="短信验证码" class="layui-input">
          </div>
          <div class="layui-col-xs5">
            <div style="margin-left: 10px;">
              <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid" id="LAY-user-reg-getsmscode-phone">获取验证码</button>
            </div>
          </div>
        </div>
      </div>

      <div class="layui-form-item qq layui-hide">
        <label class="layadmin-user-login-icon layui-icon layui-icon-login-qq" for="LAY-user-login-qq"></label>
        <input type="text" name="qq" id="LAY-user-login-qq" placeholder="QQ号" class="layui-input">
      </div>

      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
        <input type="password" name="password" id="LAY-user-login-password"  placeholder="密码" class="layui-input">
      </div>
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-repass"></label>
        <input type="password" name="repass" id="LAY-user-login-repass" placeholder="确认密码" class="layui-input">
      </div>
      <div class="layui-form-item">
        <div class="layui-row">
          <div class="layui-col-xs1">
            <input type="checkbox" name="accept_agreement" lay-skin="primary">

          </div>
          <div class="layui-col-xs11">
            <div style="margin-left: 10px;">
              <div style="cursor: pointer;color:#009688 !important" class="layui-form-mid user-agreement-title layui-word-aux">  
              </div>
          </div>
        </div>


          
        </div>  
      </div>

      <div class="layui-form-item">
        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-reg-submit">同意条款并注册</button>
      </div>
      <div class="layui-trans layui-form-item layadmin-user-login-other">        
        <a lay-href="/user/login" class="layadmin-user-jump-change layadmin-link layui-hide-xs">用已有帐号登入</a>
        <a lay-href="/user/login" class="layadmin-user-jump-change layadmin-link layui-hide-sm layui-show-xs-inline-block">登入</a>
      </div>
    </div>
  </div>
  

</div>

<script>
function refresh_img(ele) {
  ele.src = '/common/captcha?t='+ new Date().getTime()
}

function wait_60s () {
  $ = layui.$
  window.second_total = window.second_total - 1
  $("#LAY-user-reg-getsmscode-phone").text(window.second_total+"秒后可重发")
  if (window.second_total <= 0) {
    $("#LAY-user-reg-getsmscode-phone").text("获取手机验证码")
    $("#LAY-user-reg-getsmscode-phone").removeClass("layui-btn-disabled")
    clearInterval(window.count_down_interval)
  }
}

function send_sms(ele) {
  $ = layui.$
  admin = layui.admin

  var code = $(ele).val()
  if (code.length == 4) {
    var phone = $("#LAY-user-login-cellphone").val()

    // 发送短信
    admin.req({
      url: '/phone-captcha' //实际使用请改成服务端真实接口
      ,data: JSON.stringify({"phone": phone,"check_exist":0,"code":code})
      ,type: "post"
      ,fail: fail
      ,contentType:"application/json"
      ,dataType: "json"
      ,done: function(res){
        var code = res.code
        if (code == 0) {
          window.second_total = 60
          $("#LAY-user-reg-getsmscode-phone").addClass("layui-btn-disabled")
          window.count_down_interval = setInterval('wait_60s()', 1000);
          layer.closeAll();
          layer.msg("验证码已发送到您的手机，请收到后输入继续注册")
        }
      }
    });

  }

  function fail (argument) {
    $(".captcha-img").click()
  }

}

layui.use(['admin', 'form', 'user'], function(){
  var $ = layui.$
  ,setter = layui.setter
  ,admin = layui.admin
  ,form = layui.form
  ,router = layui.router();

  form.render();

  var set_title = function (argument) {
    var sys_name = layui.data('layuiAdmin').sys_name
    $("title").text(sys_name+"系统注册")
    $("h2").text(sys_name+"系统注册")
  }

  // 获取短信，弹出图形验证码
  $("#LAY-user-reg-getsmscode-phone").click(function function_name (argument) {
    var phone = $("#LAY-user-login-cellphone").val()
    if (phone == "") {
      layer.alert("请输入手机号")
      return
    }

    layer.open({
      type: 1,
      title: false,
      closeBtn: 0,
      shadeClose: true,
      skin: 'yourClass',
      content: '<div class="layui-form-item"><img onclick="refresh_img(this)" class="captcha-img" style="cursor: pointer;margin:-5px 10px 0px 20px;width:100px;float:left;" src="/common/captcha"><input oninput="send_sms(this)" style="margin:15px 20px 0px 0px;width:200px;" type="text" name="code" autocomplete="off"  placeholder="输入验证码获取手机短信"  class="layui-input"></div>'
    });

    $("input[name='code']").focus()
  })

  // 获取系统信息
  admin.get_sys_info(set_title)

  // 获取注册信息
  admin.req({
    url: '/common/register-info' //实际使用请改成服务端真实接口
    ,type: "get"
    ,contentType:"application/json"
    ,dataType: "json"        
    ,done: function(res){
      var data = res.data
      var username = data.username
      var qq = data.qq
      var email = data.email
      var phone = data.phone
      if (username.need) {
        $(".username").removeClass("layui-hide")
      }

      if (qq.need) {
        $(".qq").removeClass("layui-hide")
      }

      if (email.need) {
        $(".email").removeClass("layui-hide")
      }      

      if (phone.need) {
        $(".phone").removeClass("layui-hide")
      }

      if (email.need && email.verify) {
        $(".email-verify").removeClass("layui-hide")
      }      

      if (phone.need && phone.verify) {
        $(".phone-verify").removeClass("layui-hide")
      }
      var agreement_title = data.user_agreement.title
      var agreement_data = data.user_agreement.data
      $(".user-agreement-title").text("《"+agreement_title+"》")
      $(".user-agreement-title").click(function (argument) {
        layer.open({
          type: 1,
          title: agreement_title,
          skin: 'layui-layer-rim',
          area: '1000px',
          content: '<div style="padding: 10px;">'+agreement_data+'</div>'
        });
      })


    }
  });    

  //发送邮件验证码
  $("#LAY-user-reg-getsmscode").click(function (argument) {
    var email = $("input[name='email']").val()
    var data = {"email":email,"check_exist":0}
    admin.req({
      url: '/email-captcha' //实际使用请改成服务端真实接口
      ,type: "post"
      ,data: JSON.stringify(data)
      ,contentType:"application/json"
      ,dataType: "json"        
      ,done: function(res){
        layer.msg('验证码已发送至你的邮箱，请注意查收', {
          offset: '15px'
          ,icon: 1
          ,time: 1000
        });
      }
    });    
  })

  //提交
  form.on('submit(LAY-user-reg-submit)', function(obj){
    var field = obj.field;
    
    //确认密码
    if(field.password !== field.repass){
      return layer.msg('两次密码输入不一致');
    }
    
    var accept_agreement = $("input[name='accept_agreement']").prop("checked")
    var data = {"accept_agreement":accept_agreement, "username": field.username,"email":field.email,"password":field.password,"captcha":field.captcha,"phone":field.phone,"phone_captcha":field.phone_captcha,"qq":field.qq}
    
    //请求接口
    admin.req({
      url: '/user' //实际使用请改成服务端真实接口
      ,type: "post"
      ,data: JSON.stringify(data)
      ,contentType:"application/json"
      ,dataType: "json"        
      ,done: function(res){
        layer.msg('注册成功!', {
          offset: '15px'
          ,icon: 1
          ,time: 1000
        },function (argument) {
          location.href = "/console/index.html#/user/login"
        });
      }
    });  
    
    return false;
  });
});
</script>