<header class="container">
  <div class="auth-modal <%= login ? 'open' : '' %>" id="auth-modal">
    <div class="auth-backdrop" id="auth-backdrop"></div>
    <div class="auth-modal-content">
      <div class="content">
        <form id="login-form" class="form">
          <h3>Login</h3>
          <input type="email" name="email" placeholder="Email Address" />
          <input type="password" name="password" placeholder="Password" />
          <button class="btn mt1" type="submit" autocomplete="current-password">
            Login
          </button>
        </form>
      </div>
    </div>
  </div>

  <div class="sign-up-modal modal" id="sign-up-modal">
    <div class="modal-backdrop"></div>
    <div class="modal-content">
      <div class="content">
        <h3 class="accent-light">Sign Up</h3>
        <form id="sign-up-form" class="form">
          <div class="form-flex">
            <div class="form-element-wrapper">
              <div class="form-element">
                <label for="email" class="label">Email Address</label>
                <input required type="text" name="email" id="email" placeholder="hunter@hunter.com">
              </div>
            </div>
            <div class="form-element-wrapper">
              <div class="form-element">
                <label for="email" class="label">Password</label>
                <input required type="password" name="password" id="password" placeholder="#!s321tr3ong" autocomplete="current-password">
              </div>
            </div>
          </div>

          <div class="form-flex">
            <div class="form-element-wrapper">
              <div class="form-element">
                <label for="fname" class="label">First Name</label>
                <input required type="text" placeholder="Power" id="fname" name="fname">
              </div>
            </div>
            <div class="form-element-wrapper">
              <div class="form-element">
                <label for="lname" class="label">Last Name</label>
                <input required type="text" placeholder="hunter" id="lname" name="lname">
              </div>
            </div>
          </div>

          <div class="form-flex">
            <div class="form-element-wrapper">
              <div class="form-element">
                <label for="gender" class="label">
                  Gender
                </label>

                <select required name="gender" id="gender">
                  <option disabled> Choose </option>
                  <option value="male">Male</option>
                  <option value="male">Female</option>
                </select>
              </div>
            </div>

            <div class="form-element-wrapper">
              <div class="form-element">
                <label for="dob" class="label">
                  Date of birth
                </label>
                <input required type="date" name="dob" id="dob" />
              </div>
            </div>
          </div>



          <div class="form-element-wrapper">
            <div class="label">Please choose an account type</div>
            <div class="form-element">
              <input type="radio" name="account_type" value="freelancer" id="freelancer" required />
              <label for="freelancer">
                Freelancer
              </label>
            </div>
            <div class="form-element">
              <input type="radio" name="account_type" value="client" id="client" required />
              <label for="client">
                Client
              </label>
            </div>
          </div>
          <div class="form-element-wrapper hidden" id="company-select">
            <div class="form-element">
              <label for="cname" class="label">
                Company Name
              </label>
              <input type="text" id="cname" name="company_name" placeholder="My Company" />
            </div>
          </div>
          <div class="form-element-wrapper">
            <div class="form-element">
              <button type="submit" class="btn fullwidth">
                Register
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>

  <nav>
    <div class="logo">
      <a href="/">
        <img src="/assets/images/logo.svg" alt="LOGO" width="38" height="38">
        Dropin.
      </a>
    </div>
    <div class="flex align-center gap">
      <div class="nav-backdrop" id="nav-drawer">
        <div class="nav-backdrop-sheet" id="nav-backdrop-sheet"></div>
        <ul class="nav-links">
          <li class="<%= view === "index" ? "active": '' %>">
            <a href="/">
              <span class="material-symbols-outlined">
                home
              </span>
              Home
            </a>
          </li>
          <li class="<%= view === "about" ? "active": '' %>">
            <a href="/about-us">
              <span class="material-symbols-outlined">
                info
              </span>
              About
            </a>
          </li>
          <li class="<%= view === "services" ? "active": '' %>">
            <a href="/services">
              <span class="material-symbols-outlined">
                settings
              </span>
              Services
            </a>
          </li>
          <li class="<%= view === "support" ? "active": '' %>">
            <a href="/support">
              <span class="material-symbols-outlined">
                support_agent
              </span>
              Support
            </a>
          </li>
          <li class="mob-auth-links">
            <% if (user != null) { %>
            <a href="/app" class="block fullwidth">
              <button>Go to Dashboard</button>
            </a>
            <% } else { %>
            <button id="sign-in-btn">Sign in</button>
            <button id="sign-up-btn">Sign up</button>
            <% } %>

          </li>
        </ul>
      </div>
      <div class="side">
        <% if (user != null) { %>
        <a href="/app">
          <button id="desk-sign-out-btn">Go to Dashboard</button>
        </a>
        <% } else { %>
        <button id="desk-sign-in-btn">Sign in</button>
        <button id="desk-sign-up-btn">Sign up</button>
        <% } %>
      </div>
    </div>

    <div class="burger" id="nav-trigger">
      <span class="material-symbols-outlined">
        menu
      </span>
    </div>
  </nav>
</header>

<script>
  const signUpForm = document.querySelector("#sign-up-form");
  const companySelect = document.querySelector("#company-select");



  signUpForm.account_type.forEach(radio => {
    radio.addEventListener("input", (e) => {
      if (e.target.value === "client") {
        companySelect.classList.remove("hidden");
        companySelect.querySelector("input").required = true;
      } else {
        companySelect.classList.add("hidden");
        companySelect.querySelector("input").removeAttribute("required")
        companySelect.querySelector("input").required = false;
      }
    })
  });

  signUpForm.addEventListener("submit", async (e) => {
    const {
      fname,
      lname,
      dob,
      account_type,
      gender,
      email,
      password,
      company_name
    } = signUpForm;

    e.preventDefault();
    const obj = {
      gender: gender.value,
      email: email.value,
      account_type: account_type.value,
      first_name: fname.value,
      last_name: lname.value,
      dob: new Date(dob.value),
      password: password.value,
      company_name: company_name.value
    }
    showLoader();
    try {
      const res = await fetch("/api/sign-up", {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify(obj)
      });

      const d = await res.json();
      if (res.status === 200) {
        hideLoader();
        closeSignUpMoal();
        showSnackBar(d.message, "success");
        setTimeout(() => {
          window.location = "/app";
        }, 400)
      } else {
        hideLoader();
        closeSignUpMoal();
        showSnackBar(d.message, "error");
      }



    } catch (e) {
      hideLoader();
      closeSignUpMoal();
      showSnackBar("Something went wrong", "error");
    }


  });
</script>