<?php
if (!function_exists('getSetting')) require_once __DIR__ . '/../includes/settings-helper.php';
$siteName = getSetting('site_name', 'Deutsche Bank Holdings Delaware');
$logo = getSetting('logo_path', 'logo.png');
$favicon = getSetting('favicon_path', 'logo.png');
$logoVersion = file_exists('../' . $logo) ? '?v=' . filemtime('../' . $logo) : '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Open an Account — <?= htmlspecialchars($siteName) ?></title>
  <meta property="og:title" content="Open an Account — <?= htmlspecialchars($siteName) ?>">
  <link rel="icon" type="image/png" href="../<?= htmlspecialchars($favicon) ?>">
  <meta name="theme-color" content="#033d75">
  <link rel="stylesheet" href="../plugin/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="../plugin/fontawesome/css/all.min.css">
  <link rel="stylesheet" href="../assets/css/landing.css">

  <style>
    .db-register-layout {
      display: grid;
      grid-template-columns: 1fr 480px;
      min-height: 100vh;
    }
    .db-register-visual {
      background: linear-gradient(135deg, var(--db-gray-900) 0%, #162544 50%, var(--db-primary-dark) 100%);
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding: var(--db-space-16);
      position: relative;
      overflow: hidden;
    }
    .db-register-visual::before {
      content: "";
      position: absolute;
      inset: 0;
      background:
        linear-gradient(
          135deg,
          rgba(15, 26, 46, 0.92) 0%,
          rgba(3, 61, 117, 0.78) 100%
        );
      z-index: 1;
    }
    .db-register-visual-bg {
      position: absolute;
      inset: 0;
      background-image: url("../assets/images/home/hero-banking-professional.jpg");
      background-size: cover;
      background-position: center;
      opacity: 0.35;
      z-index: 0;
    }
    .db-register-visual::after {
      content: "";
      position: absolute;
      top: -30%;
      right: -30%;
      width: 600px;
      height: 600px;
      border-radius: 50%;
      background: radial-gradient(circle, rgba(255,255,255,0.04) 0%, transparent 70%);
      pointer-events: none;
      z-index: 1;
    }
    .db-register-visual-content {
      position: relative;
      z-index: 2;
      max-width: 480px;
    }
    .db-register-visual-content h2 {
      font-size: var(--db-text-4xl);
      font-weight: 700;
      color: var(--db-white);
      margin-bottom: var(--db-space-5);
      line-height: 1.15;
    }
    .db-register-visual-content p {
      font-size: var(--db-text-lg);
      color: var(--db-gray-400);
      line-height: 1.7;
      margin-bottom: var(--db-space-8);
    }
    .db-register-benefit {
      display: flex;
      align-items: flex-start;
      gap: var(--db-space-4);
      margin-bottom: var(--db-space-5);
    }
    .db-register-benefit i {
      color: var(--db-success);
      font-size: 1.125rem;
      margin-top: 3px;
      flex-shrink: 0;
    }
    .db-register-benefit strong {
      display: block;
      color: var(--db-white);
      font-size: var(--db-text-sm);
      margin-bottom: 2px;
    }
    .db-register-benefit span {
      color: var(--db-gray-500);
      font-size: var(--db-text-sm);
    }
    .db-register-form-side {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: var(--db-space-8);
      background: var(--db-white);
      overflow-y: auto;
    }
    .db-register-form-wrap {
      width: 100%;
      max-width: 420px;
      padding: var(--db-space-4) 0;
    }
    .db-register-form-wrap .db-header-logo {
      display: flex;
      justify-content: center;
      margin-bottom: var(--db-space-6);
    }
    .db-register-form-wrap .db-header-logo img {
      height: 44px;
    }
    .db-register-form-wrap h2 {
      text-align: center;
      font-size: var(--db-text-2xl);
      margin-bottom: var(--db-space-2);
    }
    .db-register-subtitle {
      text-align: center;
      font-size: var(--db-text-sm);
      color: var(--db-gray-500);
      margin-bottom: var(--db-space-8);
    }
    .db-register-form-wrap .db-form-group {
      margin-bottom: var(--db-space-4);
    }
    .db-register-form-wrap .db-form-label {
      display: block;
      font-size: var(--db-text-xs);
      font-weight: 600;
      color: var(--db-gray-700);
      margin-bottom: var(--db-space-1);
      text-transform: uppercase;
      letter-spacing: 0.04em;
    }
    .db-register-form-wrap .db-form-label .optional {
      font-weight: 400;
      color: var(--db-gray-400);
      text-transform: none;
      letter-spacing: 0;
    }
    .db-register-form-wrap .db-form-input,
    .db-register-form-wrap .db-form-select {
      width: 100%;
      padding: 0.7rem 0.875rem;
      font-family: var(--db-font-primary);
      font-size: var(--db-text-sm);
      color: var(--db-gray-800);
      background: var(--db-white);
      border: 1.5px solid var(--db-gray-200);
      border-radius: var(--db-radius-md);
      transition: all var(--db-transition-fast);
      outline: none;
      line-height: 1.5;
    }
    .db-register-form-wrap .db-form-input:focus,
    .db-register-form-wrap .db-form-select:focus {
      border-color: var(--db-primary);
      box-shadow: 0 0 0 3px var(--db-primary-bg);
    }
    .db-register-form-wrap .db-form-input::placeholder {
      color: var(--db-gray-400);
    }
    .db-register-form-wrap .db-form-hint {
      font-size: var(--db-text-xs);
      color: var(--db-gray-400);
      margin-top: var(--db-space-1);
    }
    .db-register-form-wrap .db-form-row {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: var(--db-space-4);
    }
    .db-register-form-wrap .db-form-row-3 {
      grid-template-columns: 1fr 1fr 1fr;
    }
    .db-register-kyc-notice {
      background: var(--db-primary-bg);
      border: 1px solid rgba(3, 61, 117, 0.12);
      border-radius: var(--db-radius-lg);
      padding: var(--db-space-4);
      margin-bottom: var(--db-space-6);
      font-size: var(--db-text-xs);
      color: var(--db-gray-600);
      line-height: 1.6;
    }
    .db-register-kyc-notice i {
      color: var(--db-primary);
      margin-right: var(--db-space-2);
    }
    .db-register-kyc-notice strong {
      color: var(--db-gray-800);
    }
    .db-register-trust {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: var(--db-space-4);
      margin-top: var(--db-space-6);
      padding-top: var(--db-space-5);
      border-top: 1px solid var(--db-gray-100);
      font-size: var(--db-text-xs);
      color: var(--db-gray-400);
    }
    .db-register-trust i {
      color: var(--db-success);
      font-size: 0.7rem;
      margin-right: 3px;
    }
    .db-password-field {
      position: relative;
    }
    .db-password-field .toggle-password {
      position: absolute;
      right: 12px;
      top: 50%;
      transform: translateY(-50%);
      cursor: pointer;
      color: var(--db-gray-400);
      font-size: 1rem;
      background: none;
      border: none;
      padding: 4px;
      line-height: 1;
    }
    .db-password-field .toggle-password:hover {
      color: var(--db-gray-600);
    }
    .db-register-back {
      text-align: center;
      font-size: var(--db-text-sm);
      color: var(--db-gray-500);
      margin-top: var(--db-space-5);
    }
    .db-register-back a {
      color: var(--db-primary);
      font-weight: 600;
      text-decoration: none;
    }

    @media (max-width: 900px) {
      .db-register-layout {
        grid-template-columns: 1fr;
      }
      .db-register-visual {
        display: none;
      }
      .db-register-form-side {
        padding: var(--db-space-6) var(--db-space-4);
        min-height: 100vh;
      }
      .db-register-form-wrap .db-form-row {
        grid-template-columns: 1fr;
      }
    }
  </style>
</head>
<body>

<div class="db-register-layout">

  <!-- Left: Visual panel -->
  <div class="db-register-visual">
    <div class="db-register-visual-bg"></div>
    <div class="db-register-visual-content">
      <h2>Your account,<br>your way.</h2>
      <p>Open an account in minutes and join clients worldwide who trust <?= htmlspecialchars($siteName) ?> for secure personal and business banking.</p>
      <div class="db-register-benefit">
        <i class="fas fa-check-circle"></i>
        <div><strong>Bank-grade encryption</strong><span>Your data is protected with industry-standard security.</span></div>
      </div>
      <div class="db-register-benefit">
        <i class="fas fa-check-circle"></i>
        <div><strong>KYC-verified accounts</strong><span>Every account undergoes identity verification for your safety.</span></div>
      </div>
      <div class="db-register-benefit">
        <i class="fas fa-check-circle"></i>
        <div><strong>PIN-protected transactions</strong><span>Every transfer and payment is secured with your personal PIN.</span></div>
      </div>
      <div class="db-register-benefit">
        <i class="fas fa-check-circle"></i>
        <div><strong>24/7 customer support</strong><span>Help is available whenever you need it.</span></div>
      </div>
    </div>
  </div>

  <!-- Right: Registration form -->
  <div class="db-register-form-side">
    <div class="db-register-form-wrap">

      <a href="../index.html" class="db-header-logo" aria-label="<?= htmlspecialchars($siteName) ?> - Home">
        <img src="../<?= htmlspecialchars($logo) ?>" alt="<?= htmlspecialchars($siteName) ?>" width="160" height="40">
      </a>

      <h2>Open an Account</h2>
      <p class="db-register-subtitle">Fill in your details to get started. All fields marked with an asterisk are required.</p>

      <form method="POST" id="signup-form" class="signup-form" enctype="multipart/form-data">

        <!-- Personal Information -->
        <div class="db-form-row">
          <div class="db-form-group">
            <label class="db-form-label" for="firstname">First Name *</label>
            <input type="text" class="db-form-input" name="firstname" id="firstname" placeholder="Your legal first name" required>
          </div>
          <div class="db-form-group">
            <label class="db-form-label" for="lastname">Last Name *</label>
            <input type="text" class="db-form-input" name="lastname" id="lastname" placeholder="Your legal last name" required>
          </div>
        </div>

        <div class="db-form-group">
          <label class="db-form-label" for="middlename">Middle Name <span class="optional">(optional)</span></label>
          <input type="text" class="db-form-input" name="middlename" id="middlename" placeholder="Your middle name">
        </div>
        <input type="hidden" name="nickname" value="">

        <div class="db-form-group">
          <label class="db-form-label" for="title">Title *</label>
          <select class="db-form-select" name="title" id="title" required>
            <option value="">Select your title</option>
            <option value="Mr">Mr</option>
            <option value="Mrs">Mrs</option>
            <option value="Ms">Ms</option>
            <option value="Miss">Miss</option>
            <option value="Dr">Dr</option>
            <option value="Prof">Prof</option>
          </select>
        </div>

        <div class="db-form-row">
          <div class="db-form-group">
            <label class="db-form-label" for="gender">Gender *</label>
            <select class="db-form-select" name="gender" id="gender" required>
              <option value="">Select gender</option>
              <option value="male">Male</option>
              <option value="female">Female</option>
              <option value="other">Other</option>
            </select>
          </div>
          <div class="db-form-group">
            <label class="db-form-label" for="dob">Date of Birth *</label>
            <input type="date" class="db-form-input" name="dob" id="dob" required>
          </div>
        </div>

        <!-- Contact Information -->
        <div class="db-form-group">
          <label class="db-form-label" for="email">Email Address *</label>
          <input type="email" class="db-form-input" name="email" id="email" placeholder="your@email.com" required>
        </div>

        <div class="db-form-group">
          <label class="db-form-label" for="phone">Phone Number *</label>
          <input type="text" class="db-form-input" name="phone" id="phone" placeholder="+1 (555) 000-0000" required>
        </div>

        <!-- Location & Currency -->
        <div class="db-form-row">
          <div class="db-form-group">
            <label class="db-form-label" for="country">Country *</label>
            <select name="country" id="country" class="db-form-select" required>
              <option value="">Select country</option>
              <option value="Afghanistan">Afghanistan</option>
              <option value="Albania">Albania</option>
              <option value="Algeria">Algeria</option>
              <option value="Argentina">Argentina</option>
              <option value="Australia">Australia</option>
              <option value="Austria">Austria</option>
              <option value="Bahamas">Bahamas</option>
              <option value="Bahrain">Bahrain</option>
              <option value="Bangladesh">Bangladesh</option>
              <option value="Belgium">Belgium</option>
              <option value="Brazil">Brazil</option>
              <option value="Bulgaria">Bulgaria</option>
              <option value="Cambodia">Cambodia</option>
              <option value="Canada">Canada</option>
              <option value="Chile">Chile</option>
              <option value="China">China</option>
              <option value="Colombia">Colombia</option>
              <option value="Costa Rica">Costa Rica</option>
              <option value="Croatia">Croatia</option>
              <option value="Cyprus">Cyprus</option>
              <option value="Czech Republic">Czech Republic</option>
              <option value="Denmark">Denmark</option>
              <option value="Dominican Republic">Dominican Republic</option>
              <option value="Ecuador">Ecuador</option>
              <option value="Egypt">Egypt</option>
              <option value="Estonia">Estonia</option>
              <option value="Ethiopia">Ethiopia</option>
              <option value="Finland">Finland</option>
              <option value="France">France</option>
              <option value="Germany">Germany</option>
              <option value="Ghana">Ghana</option>
              <option value="Greece">Greece</option>
              <option value="Guatemala">Guatemala</option>
              <option value="Hong Kong">Hong Kong</option>
              <option value="Hungary">Hungary</option>
              <option value="Iceland">Iceland</option>
              <option value="India">India</option>
              <option value="Indonesia">Indonesia</option>
              <option value="Iran">Iran</option>
              <option value="Iraq">Iraq</option>
              <option value="Ireland">Ireland</option>
              <option value="Israel">Israel</option>
              <option value="Italy">Italy</option>
              <option value="Jamaica">Jamaica</option>
              <option value="Japan">Japan</option>
              <option value="Jordan">Jordan</option>
              <option value="Kazakhstan">Kazakhstan</option>
              <option value="Kenya">Kenya</option>
              <option value="Kuwait">Kuwait</option>
              <option value="Latvia">Latvia</option>
              <option value="Lebanon">Lebanon</option>
              <option value="Libya">Libya</option>
              <option value="Lithuania">Lithuania</option>
              <option value="Luxembourg">Luxembourg</option>
              <option value="Malaysia">Malaysia</option>
              <option value="Malta">Malta</option>
              <option value="Mauritius">Mauritius</option>
              <option value="Mexico">Mexico</option>
              <option value="Morocco">Morocco</option>
              <option value="Namibia">Namibia</option>
              <option value="Nepal">Nepal</option>
              <option value="Netherlands">Netherlands</option>
              <option value="New Zealand">New Zealand</option>
              <option value="Nigeria">Nigeria</option>
              <option value="Norway">Norway</option>
              <option value="Oman">Oman</option>
              <option value="Pakistan">Pakistan</option>
              <option value="Panama">Panama</option>
              <option value="Peru">Peru</option>
              <option value="Philippines">Philippines</option>
              <option value="Poland">Poland</option>
              <option value="Portugal">Portugal</option>
              <option value="Qatar">Qatar</option>
              <option value="Romania">Romania</option>
              <option value="Russia">Russia</option>
              <option value="Saudi Arabia">Saudi Arabia</option>
              <option value="Senegal">Senegal</option>
              <option value="Singapore">Singapore</option>
              <option value="Slovakia">Slovakia</option>
              <option value="Slovenia">Slovenia</option>
              <option value="South Africa">South Africa</option>
              <option value="South Korea">South Korea</option>
              <option value="Spain">Spain</option>
              <option value="Sri Lanka">Sri Lanka</option>
              <option value="Sweden">Sweden</option>
              <option value="Switzerland">Switzerland</option>
              <option value="Taiwan">Taiwan</option>
              <option value="Tanzania">Tanzania</option>
              <option value="Thailand">Thailand</option>
              <option value="Tunisia">Tunisia</option>
              <option value="Turkey">Turkey</option>
              <option value="Uganda">Uganda</option>
              <option value="Ukraine">Ukraine</option>
              <option value="United Arab Emirates">United Arab Emirates</option>
              <option value="United Kingdom">United Kingdom</option>
              <option value="USA">United States</option>
              <option value="Uruguay">Uruguay</option>
              <option value="Vietnam">Vietnam</option>
              <option value="Zambia">Zambia</option>
              <option value="Zimbabwe">Zimbabwe</option>
            </select>
          </div>
          <div class="db-form-group">
            <label class="db-form-label" for="usercurrency">Account Currency *</label>
            <select name="usercurrency" id="usercurrency" class="db-form-select" required>
              <option value="">Select currency</option>
              <option value="USD">USD — United States Dollar</option>
              <option value="EUR">EUR — Euro</option>
              <option value="GBP">GBP — British Pound Sterling</option>
              <option value="JPY">JPY — Japanese Yen</option>
              <option value="CHF">CHF — Swiss Franc</option>
              <option value="CAD">CAD — Canadian Dollar</option>
              <option value="AUD">AUD — Australian Dollar</option>
              <option value="NZD">NZD — New Zealand Dollar</option>
              <option value="SGD">SGD — Singapore Dollar</option>
              <option value="HKD">HKD — Hong Kong Dollar</option>
              <option value="AED">AED — UAE Dirham</option>
              <option value="SAR">SAR — Saudi Riyal</option>
              <option value="QAR">QAR — Qatari Riyal</option>
              <option value="CNY">CNY — Chinese Yuan Renminbi</option>
              <option value="INR">INR — Indian Rupee</option>
              <option value="ZAR">ZAR — South African Rand</option>
              <option value="NGN">NGN — Nigerian Naira</option>
              <option value="KES">KES — Kenyan Shilling</option>
              <option value="SEK">SEK — Swedish Krona</option>
              <option value="NOK">NOK — Norwegian Krone</option>
              <option value="DKK">DKK — Danish Krone</option>
              <option value="PLN">PLN — Polish Zloty</option>
              <option value="TRY">TRY — Turkish Lira</option>
              <option value="RUB">RUB — Russian Ruble</option>
              <option value="BRL">BRL — Brazilian Real</option>
              <option value="MXN">MXN — Mexican Peso</option>
              <option value="ARS">ARS — Argentine Peso</option>
              <option value="CLP">CLP — Chilean Peso</option>
              <option value="COP">COP — Colombian Peso</option>
              <option value="PEN">PEN — Peruvian Sol</option>
              <option value="EGP">EGP — Egyptian Pound</option>
              <option value="PKR">PKR — Pakistani Rupee</option>
              <option value="BDT">BDT — Bangladeshi Taka</option>
              <option value="PHP">PHP — Philippine Peso</option>
              <option value="IDR">IDR — Indonesian Rupiah</option>
              <option value="MYR">MYR — Malaysian Ringgit</option>
              <option value="THB">THB — Thai Baht</option>
              <option value="VND">VND — Vietnamese Dong</option>
              <option value="KRW">KRW — South Korean Won</option>
              <option value="ILS">ILS — Israeli New Shekel</option>
              <option value="MAD">MAD — Moroccan Dirham</option>
              <option value="TND">TND — Tunisian Dinar</option>
              <option value="BHD">BHD — Bahraini Dinar</option>
              <option value="KWD">KWD — Kuwaiti Dinar</option>
              <option value="OMR">OMR — Omani Rial</option>
              <option value="JOD">JOD — Jordanian Dinar</option>
              <option value="RON">RON — Romanian Leu</option>
              <option value="BGN">BGN — Bulgarian Lev</option>
              <option value="CZK">CZK — Czech Koruna</option>
              <option value="HUF">HUF — Hungarian Forint</option>
              <option value="ISK">ISK — Icelandic Krona</option>
              <option value="HRK">HRK — Croatian Kuna</option>
              <option value="UAH">UAH — Ukrainian Hryvnia</option>
              <option value="DZD">DZD — Algerian Dinar</option>
              <option value="LBP">LBP — Lebanese Pound</option>
              <option value="IRR">IRR — Iranian Rial</option>
              <option value="IQD">IQD — Iraqi Dinar</option>
              <option value="XOF">XOF — West African CFA Franc</option>
              <option value="XAF">XAF — Central African CFA Franc</option>
              <option value="XPF">XPF — CFP Franc</option>
            </select>
          </div>
        </div>

        <!-- Account Type -->
        <div class="db-form-group">
          <label class="db-form-label" for="account_type">Account Type *</label>
          <select name="account_type" id="account_type" class="db-form-select" required>
            <option value="">Please select an account type</option>
            <option value="Checking Account">Checking Account</option>
            <option value="Savings Account">Savings Account</option>
            <option value="Current Account">Current Account</option>
            <option value="Fixed Deposit Account">Fixed Deposit Account</option>
            <option value="Business Account">Business Account</option>
            <option value="Corporate Business Account">Corporate Business Account</option>
            <option value="Non Resident Account">Non-Resident Account</option>
            <option value="Investment Account">Investment Account</option>
          </select>
        </div>

        <!-- Security -->
        <div class="db-form-row">
          <div class="db-form-group">
            <label class="db-form-label" for="password">Password *</label>
            <div class="db-password-field">
              <input type="password" class="db-form-input" name="password" id="password" placeholder="Create a strong password" required minlength="8">
              <button type="button" class="toggle-password" data-target="password" aria-label="Show password">
                <i class="fas fa-eye"></i>
              </button>
            </div>
            <p class="db-form-hint">Minimum 8 characters. Use a mix of letters, numbers, and symbols.</p>
          </div>
          <div class="db-form-group">
            <label class="db-form-label" for="re_password">Confirm Password *</label>
            <div class="db-password-field">
              <input type="password" class="db-form-input" name="re_password" id="re_password" placeholder="Re-enter your password" required minlength="8">
              <button type="button" class="toggle-password" data-target="re_password" aria-label="Show password">
                <i class="fas fa-eye"></i>
              </button>
            </div>
          </div>
        </div>

        <!-- Profile Picture -->
        <div class="db-form-group">
          <label class="db-form-label" for="profile_pic">Profile Picture *</label>
          <input type="file" class="db-form-input" name="profile_pic" id="profile_pic" accept="image/jpeg,image/png,image/jpg" required>
          <p class="db-form-hint">Accepted formats: JPG, JPEG, PNG. Maximum size: 2 MB.</p>
        </div>

        <!-- KYC Notice -->
        <div class="db-register-kyc-notice">
          <i class="fas fa-shield-alt"></i>
          <strong>Identity verification required.</strong> After registration, you will need to complete KYC (Know Your Customer) verification by providing a valid government-issued ID and proof of address. This is a regulatory requirement that helps protect your account.
        </div>

        <!-- Terms -->
        <div class="db-form-group">
          <label style="display:flex;align-items:flex-start;gap:var(--db-space-3);font-size:var(--db-text-sm);color:var(--db-gray-600);line-height:1.5;cursor:pointer;">
            <input type="checkbox" name="agree-term" id="agree-term" class="agree-term" required style="margin-top:3px;width:16px;height:16px;accent-color:var(--db-primary);">
            <span>I confirm that the information provided is accurate and I agree to the <a href="../terms.html" target="_blank" style="color:var(--db-primary);font-weight:600;">Terms of Service</a> and <a href="../privacy-policy.html" target="_blank" style="color:var(--db-primary);font-weight:600;">Privacy Policy</a>.</span>
          </label>
        </div>

        <!-- Submit -->
        <div class="db-form-group">
          <button type="button" name="submit" id="submit" class="db-btn db-btn-primary" style="width:100%;padding:0.875rem;font-size:var(--db-text-base);">
            <b>Create Account</b>
          </button>
        </div>

      </form>

      <p class="db-register-back">
        Already have an account? <a href="login">Sign in</a>
      </p>

      <div class="db-register-trust">
        <span><i class="fas fa-lock"></i> Bank-grade encryption</span>
        <span><i class="fas fa-shield-alt"></i> KYC verified</span>
        <span><i class="fas fa-key"></i> PIN protected</span>
      </div>

    </div>
  </div>

</div>

<!-- SweetAlert2 -->
<link href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-default@4/default.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.js"></script>
<script src="../plugin/jquery.min.js"></script>

<script>
  // ── Password visibility toggle ──────────────────────────────────────
  document.querySelectorAll('.toggle-password').forEach(function (btn) {
    btn.addEventListener('click', function () {
      var targetId = this.getAttribute('data-target');
      var input = document.getElementById(targetId);
      var icon = this.querySelector('i');
      if (input.type === 'password') {
        input.type = 'text';
        icon.classList.remove('fa-eye');
        icon.classList.add('fa-eye-slash');
      } else {
        input.type = 'password';
        icon.classList.remove('fa-eye-slash');
        icon.classList.add('fa-eye');
      }
    });
  });

  // ── Form submission ─────────────────────────────────────────────────
  document.getElementById('submit').addEventListener('click', function (e) {
    e.preventDefault();
    var form = document.getElementById('signup-form');
    var btn = this;

    // Client-side validation
    var firstname = form.querySelector('[name="firstname"]').value.trim();
    var lastname = form.querySelector('[name="lastname"]').value.trim();
    var email = form.querySelector('[name="email"]').value.trim();
    var phone = form.querySelector('[name="phone"]').value.trim();
    var dob = form.querySelector('[name="dob"]').value;
    var title = form.querySelector('[name="title"]').value;
    var gender = form.querySelector('[name="gender"]').value;
    var country = form.querySelector('[name="country"]').value;
    var usercurrency = form.querySelector('[name="usercurrency"]').value;
    var account_type = form.querySelector('[name="account_type"]').value;
    var password = form.querySelector('[name="password"]').value;
    var re_password = form.querySelector('[name="re_password"]').value;
    var profile_pic = form.querySelector('[name="profile_pic"]').value;
    var agreeTerm = form.querySelector('[name="agree-term"]').checked;

    function showToast(msg) {
      Swal.fire({
        toast: true,
        position: 'top-end',
        showConfirmButton: false,
        timer: 3000,
        icon: 'info',
        title: msg
      });
    }

    if (!firstname) { showToast('Please enter your first name.'); return; }
    if (!lastname) { showToast('Please enter your last name.'); return; }
    if (!title) { showToast('Please select your title.'); return; }
    if (!gender) { showToast('Please select your gender.'); return; }
    if (!dob) { showToast('Please enter your date of birth.'); return; }
    if (!email) { showToast('Please enter your email address.'); return; }
    if (!phone) { showToast('Please enter your phone number.'); return; }
    if (!country) { showToast('Please select your country.'); return; }
    if (!usercurrency) { showToast('Please select your account currency.'); return; }
    if (!account_type) { showToast('Please select an account type.'); return; }
    if (!password) { showToast('Please create a password.'); return; }
    if (password.length < 8) { showToast('Password must be at least 8 characters.'); return; }
    if (!re_password) { showToast('Please confirm your password.'); return; }
    if (password !== re_password) { showToast('Passwords do not match.'); return; }
    if (!profile_pic) { showToast('Please upload a profile picture.'); return; }
    if (!agreeTerm) { showToast('Please agree to the Terms of Service and Privacy Policy.'); return; }

    // Email format check
    var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(email)) { showToast('Please enter a valid email address.'); return; }

    // Disable button and show loading
    btn.disabled = true;
    btn.innerHTML = '<i class="fas fa-spinner fa-pulse"></i> Creating account…';
    Swal.showLoading();

    var formData = new FormData(form);
    $.ajax({
      url: '../apps/login/createaccount.php',
      type: 'POST',
      data: formData,
      processData: false,
      contentType: false,
      success: function (data) {
        var info = data.trim();
        if (info === 'success') {
          Swal.fire({
            toast: true,
            position: 'top-end',
            showConfirmButton: false,
            timer: 2000,
            icon: 'success',
            title: 'Account created successfully. Redirecting…'
          });
          setTimeout(function () {
            location.href = '../dashboard/dashboard.php';
          }, 2500);
        } else if (info === 'Already registered with this email.') {
          Swal.fire({
            icon: 'warning',
            title: 'Email Already Registered',
            text: 'An account with this email address already exists. Please sign in or use a different email.',
            showConfirmButton: true
          });
          resetButton();
        } else {
          console.log(data);
          Swal.fire({
            toast: true,
            position: 'top-end',
            showConfirmButton: false,
            timer: 4000,
            icon: 'error',
            title: 'Error: ' + info
          });
          resetButton();
        }
      },
      error: function (jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
        Swal.fire({
          icon: 'error',
          title: 'Request Failed',
          text: 'Could not reach the server. Please check your connection and try again.',
          showConfirmButton: true
        });
        resetButton();
      }
    });

    function resetButton() {
      btn.disabled = false;
      btn.innerHTML = '<b>Create Account</b>';
    }
  });
</script>
</body>
</html>
