<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.0</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        <link rel="stylesheet" href="./static/css/font.css">
        <link rel="stylesheet" href="./static/css/xadmin.css">
        
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <script src="./static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./static/js/xadmin.js"></script>
    </head>
    <body onload="InitParams({{.sharecode}})">
    <input type="text" id="token" value={{.token}} hidden=true>
    <div class="x-body layui-anim layui-anim-up">
        <blockquote class="layui-elem-quote">欢迎{{.rolename}}：
            <span class="x-red">{{.username}}</span>！当前时间:{{.sysTime}}
        </blockquote>
    </div>
    <div class="x-body layui-anim layui-anim-up">
        <blockquote class="layui-elem-quote">
            推广码: <input id="sharecode" type="text" />
            {{"         "}}
            <input value="复制" class="layui-btn" type="button" onclick="copyToClip()" />
            <br>
            <br>
            <input value="刷新推广码" class="layui-btn" style="width:128px;" type="button" onclick="randCode()" />
            {{"         "}}
            <input value="更新" class="layui-btn" style="width:128px;" type="button" onclick="UpdateShareCode()">
        </blockquote>
    </div>

    <script language="JavaScript">
      var currShareCode = ""
      function InitParams(sharecode) {
        if (sharecode.length > 0) {
            document.getElementById("sharecode").value = sharecode;
            currShareCode = sharecode
        }
      }
      function copyToClip() {
        var aux = document.createElement("input");
        aux.setAttribute("value", currShareCode);
        document.body.appendChild(aux);
        aux.select();
        document.execCommand("copy");
        document.body.removeChild(aux);
        layer.msg('复制成功!',{icon:1,time:1000});
      }

      var randChats = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
      function generateMixed(n) {
          var res = "";
          for(var i = 0; i < n ; i ++) {
              var id = Math.ceil(Math.random()*(randChats.length-1));
              res += randChats[id];
          }
          return res;
      }
      // 随机兑换码
      function randCode() {
        currShareCode = generateMixed(6)
        document.getElementById("sharecode").value = currShareCode;
      }
      
      function UpdateShareCode () {
        var token = document.getElementById("token").value;
        var sharecode = document.getElementById("sharecode").value;

        if (sharecode.length != 6){
          alert("兑换码长度错误！6位大写字母");
          return;
        }
        // 兑换码字符检查
        for (var i = 0; i < sharecode.length; i ++){
          var isMatch = false
          for (var j = 0; j < randChats.length; j ++) {
            if (sharecode[i] == randChats[j]){
              isMatch = true;
              break;
            }
          }
          if (isMatch == false){
            alert("兑换码错误！请使用大写字母");
            return;
          }
        }

        var text = "token=" + encodeURI(token);
        text += "&sharecode=" + encodeURI(sharecode);

        $.ajax({
          type:"post",
          url:"/welcome?" + text,
          success:function (data) {
            // alert(data.status);
            if (data.status){
              window.location.href="/welcome";
              if (data.info){
                layer.msg(data.info,{icon:1,time:1000});
              }
            }else{
              if (data.info){
                alert(data.info);
              }else{
                window.location.href="/accountlogin";
              }
            }
          }
        });
      }
    </script>
    </body>
</html>