$(window).ready(function () {
  let token = localStorage.getItem("token");
  let url_path = window.location.pathname;
  //登录验证
  if (url_path.indexOf("login.html") == -1) {
    if (token) {
      getAjaxRequst("/api/Check/GetUserProfile", {}, function (res) {
        if (res.code != 200) {
          toastr.error("登录信息已过期，请重新登录");
          localStorage.removeItem("token");
          window.location.href = "login.html";
        } else {
          localStorage.setItem("account", res.result.account);
          localStorage.setItem("name", res.result.name);
          $("#user-account").text(res.result.account);
          $("#user-name").text(res.result.name);
          if (res.result.account == "System") {
            $("#admin-auth").removeClass("hidden");
          }
        }
      });
    } else {
      toastr.error("请先登录");
      window.location.href = "login.html";
    }
  }

  // 登录提交
  $("#login-btn").click(function () {
    let account = $("#login-mobile").val();
    let password = $("#login-pwd").val();
    if (account && password) {
      postAjaxRequst(
        "/api/Check/Login",
        { account: account, password: password, appKey: "openauth" },
        function (res) {
          if (res.code == 200) {
            toastr.success(res.message);
            localStorage.setItem("token", res.token);
            setTimeout(function () {
              window.location.href = "index.html";
            }, 1000);
          } else {
            toastr.error(res.message);
          }
        }
      );
    } else {
      alert("Please fill all the fields");
    }
  });
  $("#logout").click(function () {
    localStorage.clear();
    window.location.href = "login.html";
  });
});
