{{ define "kas/login.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>
    <script type="text/javascript">
        const ISGOKEEP = true;
    </script>
    {{ template "_common.htm" .}}
</head>

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

    <main role="main">
        <div class="album py-3 bg-light">
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <div class="alert border-bottom">后台管理 - 登录</div>
                    </div>
                </div>
                <div class="py-2">
                    <label for="iptUserid" class="sr-only">账号</label>
                    <input type="text" id="iptUserid" class="form-control" placeholder="输入账号" required="" autofocus="">
                </div>
                <div class="py-2">
                    <label for="iptPassword" class="sr-only">密码</label>
                    <input type="password" id="iptPassword" class="form-control" placeholder="输入密码" required="">
                </div>
                <div class="py-2">
                    <button class="btn btn-danger btn-block" type="button" id="btnLogin">登录</button>
                </div>
            </div>
        </div>

    </main>

    {{ template "kas/_footer.htm" .}}
    <script type="text/javascript">
        $('#btnLogin').click(function (event) {
            if ($("#iptUserid").val() == "" || $("#iptPassword").val() == "") {
                ShowMsg("请输入用户名和密码");
                return;
            }
            $.ajax({
                type: "POST",
                dataType: 'json',
                contentType: "application/json;",
                url: "/api/v1/login",
                data: JSON.stringify({
                    userid: $("#iptUserid").val(),
                    password: $("#iptPassword").val()
                }),
                success: function (data) {
                    if (data.code == 200) {
                        SetCookie(TokenName(), data.token, 1);
                        localStorage.setItem('time_login', NowUnix);
                        DirectTo('/kas')
                    } else {
                        ShowMsg("登录失败，用户名或者密码可能错误");
                    }
                },
                error: function () {
                    ShowMsg("登录失败");
                }
            });
        });

        // 如果已经登录了，直接去会员中心首页
        var gokeep_token = GetCookie(TokenName())

        $.ajax({
            type: "GET",
            dataType: 'json',
            contentType: "application/json;",
            url: "/api/v1/auth/member/info",
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'Bearer ' + gokeep_token);
            }
        }).done(function (data) {
            if (data.code == 0) {
                DirectTo("/kas/");
            }
        });

    </script>
</body>

</html>
{{ end }}