$(function () {
  const url = window.location.href;
  const searchParams = new URLSearchParams(new URL(url).search);
  const rParam = searchParams.get("r");
  let submitFlag = false;
  if (localStorage.getItem("invitCode")) {
    $("#keyword input").val(localStorage.getItem("invitCode"));
  }
  $(".login_btn").on("click", register);
  $("#login-code input").on("keydown", function (e) {
    if (e.key === "Enter" || e.keyCode === 13) {
      register();
    }
  });

  function register() {
    let _lang = $("#language").val();

    var email = $("#login_email input").val();
    var password = $("#login_pwd input").val();
    var confirmPassword = $("#confirm_pwd input").val();
    var code = $("#login-code input").val();
    var uuid = localStorage.getItem("uuid");

    if (email == "" || email == undefined) {
      $("#login_email").addClass("error");
      $("#login_email .error_tip").show();
      return;
    } else {
      $("#login_email").removeClass("error");
      $("#login_email .error_tip").hide();
    }

    if (password == "" || password == undefined) {
      $("#login_pwd").addClass("error");
      $("#login_pwd .error_tip").show();
      return;
    } else {
      $("#login_pwd").removeClass("error");
      $("#login_pwd .error_tip").hide();
    }

    if (confirmPassword == "" || confirmPassword == undefined) {
      $("#confirm_pwd").addClass("error");
      $("#confirm_pwd .error_tip").show();
      return;
    } else {
      $("#confirm_pwd").removeClass("error");
      $("#confirm_pwd .error_tip").hide();
    }

    if (code == "" || code == undefined) {
      $("#login-code").addClass("error");
      $("#login-code .error_tip").show();
    } else {
      $("#login-code").removeClass("error");
      $("#login-code .error_tip").hide();
    }

    if (password != confirmPassword) {
      messageFn(_lang === "zh" ? "密码不一致" : "password not match");
      return;
    }

    if (submitFlag) return;
    submitFlag = true;
    $(".loading-mask").show();
    $.ajax({
      url: requestApi + "/signup",
      dataType: "json",
      type: "POST",
      aysc: false,
      contentType: "application/json; charset=utf-8",
      data: JSON.stringify({
        lang: _lang,
        email: email,
        pwd: password,
        kwd: confirmPassword,
        code: code,
        uuid: uuid,
      }),
      success: function (res) {
        try {
          if (res.code === 200) {
            localStorage.setItem("session", res.data.token);
            //   localStorage.setItem("user", JSON.stringify(res.data));
            const expiresDate = new Date();
            const expires =
              res.data.expire == 0 ? 9999999999999 : res.data.expire;
            expiresDate.setDate(expiresDate.getDate() + expires);
            const domain = window.location.host.split(".").slice(-2).join(".");
            Cookies.set("common_session", res.data.token, {
              expires: expiresDate,
              domain,
            });

            messageFn(_lang === "zh" ? "注册成功" : "register success");
            window.location.href = "cli-traffic.html";
          } else {
            messageFn(res.msg);
            reloadCaptcha();
          }
        } catch (err) {
        } finally {
          submitFlag = false;
          $(".loading-mask").hide();
        }
      },
      error: function () {
        submitFlag = false;
        reloadCaptcha();
        $(".loading-mask").hide();
      },
    });
  }

  const $passwordInput = $("#login_pwd input");
  const $confirmPasswordInput = $("#confirm_pwd input");
  const $eyeIcon = $("#eyeIcon");
  const $confirmEyeIcon = $("#confirmEyeIcon");
  const $codeImg = $("#codeImg");

  $eyeIcon.on("click", function () {
    if ($passwordInput.attr("type") === "password") {
      $passwordInput.attr("type", "text");
      $eyeIcon.attr("src", "static/picture/pwd-2.png");
    } else {
      $passwordInput.attr("type", "password");
      $eyeIcon.attr("src", "static/picture/pwd-1.png");
    }
  });

  $confirmEyeIcon.on("click", function () {
    if ($confirmPasswordInput.attr("type") === "password") {
      $confirmPasswordInput.attr("type", "text");
      $confirmEyeIcon.attr("src", "static/picture/pwd-2.png");
    } else {
      $confirmPasswordInput.attr("type", "password");
      $confirmEyeIcon.attr("src", "static/picture/pwd-1.png");
    }
  });

  $codeImg.on("click", function () {
    reloadCaptcha();
  });

  function reloadCaptcha() {
    $.ajax({
      url: requestApi + "/captcha",
      dataType: "json",
      type: "GET",
      async: false,
      success: function (res) {
        if (res.code === 200) {
          $("#codeImg").attr("src", res.data);
          localStorage.setItem("uuid", res.id);
        } else {
          messageFn(res.msg);
        }
      },
      error: function (err) {
        messageFn(err);
      },
    });
  }

  reloadCaptcha();
});
