const serverUrl = "http://localhost:3086/";
let otpid;
let sendotp;
let unibaseid;
async function showOtpBox() {
  let inputdetails = document.getElementById("txt_PhoneorMail").value;

  let sendnumber = await fetch(
    serverUrl +
      "account/getuserbyphoneormail/" +
      inputdetails +
      "/" +
      inputdetails
  );
  let textdata = await sendnumber.json();
  if (textdata.result != null) {
    unibaseid = textdata.result.unibaseId;
    var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");

    var raw = JSON.stringify({
      FirstName: "",
      LastName: "",
      ContactNumber: "",
      Email: "",
      TenantName: "",
      ContactOrEmail: inputdetails,
      IsSignup: false,
      IsRegisterUser: false,
      IsForgotPswd: true,
      UnibaseId: unibaseid,
      OtpId: 0,
      UserOtp: "",
    });

    var requestOptions = {
      method: "POST",
      headers: myHeaders,
      body: raw,
      redirect: "follow",
    };

    fetch(serverUrl + "account/sendotp", requestOptions)
      .then((response) => response.text())
      .then(function (result) {
        optid = result;
        document.getElementById("div_ForgotPswd").style.display = "none";
        document.getElementById("modal_Otp").style.display = "block";
        document.getElementById("div_ValidationSummary").innerHTML = "";
      })
      .catch((error) => console.log("error", error));
  } else {
    let html =
      '<div class="p-2"><span class="error-warning">Error !</span> Email/Phonenumber does not exist</div>';
    document.getElementById("div_ValidationSummary").innerHTML = "";
    let error = document.getElementById("div_ValidationSummary");
    error.insertAdjacentHTML("beforeend", html);
  }
}

/*verify otp*/
async function showPasswordBox() {
  let inputdetails = document.getElementById("txt_Code").value;
  if (inputdetails.length > 0) {
    var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");
    var raw = JSON.stringify({
      FirstName: "",
      LastName: "",
      ContactNumber: "",
      Email: "",
      TenantName: "",
      ContactOrEmail: "",
      IsForgotPswd: false,
      IsRegisterUser: false,
      IsSignup: true,
      UnibaseId: "",
      OtpId: optid,
      UserOtp: inputdetails,
    });

    var requestOptions = {
      method: "POST",
      headers: myHeaders,
      body: raw,
      redirect: "follow",
    };

    fetch(serverUrl + "account/verifyotp", requestOptions)
      .then((response) => response.text())
      .then(function (result) {
        let data = JSON.parse(result);
        if (data.result.contactConfirmed == true) {
          document.getElementById("modal_Otp").style.display = "none";
          document.getElementById("div_ResetPswd").style.display = "block";
          document.getElementById("div_ValidationSummary").innerHTML = "";
        } else {
          let html =
            '<div class="p-2" ><span class="error-warning">Error !</span> Enter Valid OTP</div>';
          document.getElementById("div_ValidationSummary").innerHTML = "";
          let error = document.getElementById("div_ValidationSummary");
          error.insertAdjacentHTML("beforeend", html);
        }
      })
      .catch((error) => console.log("error", error));
  } else {
  }
}

function showSuccessBox() {
  let txt_NewPassword = document.getElementById("txt_NewPassword").value;
  let txt_ConfirmPassword = document.getElementById(
    "txt_ConfirmPassword"
  ).value;
  if (txt_NewPassword == txt_ConfirmPassword) {
    var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");
    var raw = JSON.stringify({
      Password: txt_NewPassword,
      UserName: unibaseid,
    });
    var requestOptions = {
      method: "POST",
      headers: myHeaders,
      body: raw,
      redirect: "follow",
    };
    fetch(serverUrl + "account/updatepassword", requestOptions)
      .then((response) => response.text())
      .then(function (result) {
        let updatepassword = JSON.parse(result);
        let message = updatepassword.message;
        if (message == "Password Updated Successfully") {
          document.getElementById("div_ValidationSummary").innerHTML = "";
          document.getElementById("div_ResetPswd").style.display = "none";
          document.getElementById("div_SuccessMsg").style.display = "block";
        }
      })
      .catch((error) => console.log("error", error));
  } else {
    let html =
      '<div class="p-2"> <span class="error-warning">Error !</span> Password Does not Match</div>';
    document.getElementById("div_ValidationSummary").innerHTML = "";
    let error = document.getElementById("div_ValidationSummary");
    error.insertAdjacentHTML("beforeend", html);
  }
}
