{{ define "member/invite.htm" }}
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <title>我的邀请 - 会员中心 - 看丽人</title>
  {{ template "_common.htm" .}}
  <script src="{{.global.host_img}}/assets/js/jsrender.min.js"></script>
  <script type="text/javascript">
    LoadMemberInfo();
  </script>
</head>

<body>
  {{ template "_header.htm" .}}

  <main role="main">
    <div class="album py-3 bg-light">
      <div class="container">
        <form class="form-signin">
          <div class="alert alert-dark border-bottom"><a href="/member">会员中心</a> > 我的邀请</div>
          <div class="ibox-content">
          <div class="alert alert-warning" role="alert">
            <h3>邀请规则</h3>
            <p>1.每邀请一位新用户并充值成功，赠送金币<font color="red">10个</font>；</p>
            <p>2.上述新用户邀请不设人数上限；</p>
            <p>邀请链接：{{.global.host_www}}/member/reg?userid=admin</p>
          </div>
          <table class="table">
            <thead>
              <tr>
                <th>被邀请人</th>
                <th>状态</th>
                <th>奖励积分</th>
              </tr>
            </thead>
            <tbody id="frmResult">
            </tbody>
          </table>
          <nav id="frmPagination" style="display: none">
            <ul class="pagination justify-content-center">
              <li class="page-item" id="prePage">
                <a class="page-link" href="javascript:GoPre()">上一页</a>
              </li>
              <li class="page-item disabled" id="totalPage">
                <a class="page-link"><span id="spCurrentPage">..</span>/<span id="spTotalPage">..</span>页</a>
              </li>
              <li class="page-item" id="nextPage">
                <a class="page-link" href="javascript:GoNext()">下一页</a>
              </li>
            </ul>
          </nav>
        </div>
        </form>
      </div>
    </div>

  </main>
  {{ template "_footer.htm" }}

  <script id="tplInviteRow" type="text/x-jsrender">
    <tr>
      <td>{%:Userid%}</td>
      <td>{%if InviteStatus%}<font color="green">已完成</font>{%else%}<font color="red">未完成</font>{%/if%}</td>
      <td>10</td>
    </tr>
  </script>

  <script type="text/javascript">
    var gokeep_token = GetCookie(TokenName());
    if (gokeep_token == null) {
      DirectTo("/member/login");
    }
    var currentpage = 1;
    const pagesize = 10;

    var page = currentpage;

    var urlParams = new URLSearchParams(window.location.search);
    if (urlParams.get('page') > 0) {
      page = parseInt(urlParams.get('page'));
      currentpage = page;
    }
    if (urlParams.has('page') && urlParams.has('page_size')) {
      GotoPage(page);
    }


    $.views.settings.delimiters("{%", "%}"); // 设置js render标签

    $.views.helpers({
      unix_to_str : function(time){
        time = parseInt(time) * 1000;
        var datetime = new Date();
        datetime.setTime(time);
        var year = datetime.getFullYear();
        var month = datetime.getMonth() + 1;
        var date = datetime.getDate();
        return year + "-" + month + "-" + date;
      }
    })

    function GoNext() {
      currentpage = currentpage + 1;
      GotoPage(currentpage);
    }

    function GoPre() {
      currentpage = currentpage - 1;
      GotoPage(currentpage);
    }

    function GotoPage(page) {
      if (page == 1) {
        $("#prePage").addClass('disabled');
      } else {
        $("#prePage").removeClass('disabled');
      }
      $("#spCurrentPage").text(page);
      var data = {
        page:page,
        page_size:pagesize,
      };
      window.history.pushState("listResult", "内容", "/member/invite?"+$.param( data ));
      $.ajax({
        type: "GET",
        dataType: 'json',
        data: data,
        url: "/api/v1/auth/member/invite",
        beforeSend: function (xhr) {
            xhr.setRequestHeader('Authorization', 'Bearer ' + gokeep_token);
        }
      }).done(function (data) {
        if (data.code == 0) {
          if (data.result.total == 0) {
            $("#frmPagination").html("<p class='text-center'>暂无记录</p>");
            return;
          }
          $("#frmPagination").show();
          var total = Math.ceil(data.result.total / pagesize);
          if ((data.result.length < pagesize) || (page == total)) {
            $("#nextPage").addClass('disabled');
          } else {
            $("#nextPage").removeClass('disabled');
          }
          $("#spTotalPage").text(total);

          var content = $("#tplInviteRow").render(data.result.inviters);
          $("#frmResult").html(content);
        }
      }).fail(function (data) {
        Log("fail:", data);
        ShowMsg("获取列表失败");
      });
    }

    GotoPage(currentpage);
  </script>
  {{ template "_analytics.htm" }}
</body>
</html>
{{ end }}