<?php
require_once __DIR__ . '/database.php';
include "helper.php";

// Fetch all accounts from DB
$accounts = getAccounts($mysqli);

// Convert PHP array to JS JSON
$accountsJson = json_encode($accounts);
// echo 'Data: <pre>' .print_r($accountsJson,true). '</pre>'; die;
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Upload Word Document</title>

    <!-- Bootstrap 5 Latest -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Select2 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />

    <!-- Select2 Bootstrap 5 Theme -->
    <link href="https://cdn.jsdelivr.net/npm/@ttskch/select2-bootstrap4-theme@1.5.2/dist/select2-bootstrap4.min.css" rel="stylesheet" />

  <style>
    body {
      background: #f7f9fc;
      font-family: 'Inter', sans-serif;
    }
    .card {
      border-radius: 12px;
      box-shadow: 0 4px 20px rgba(0,0,0,0.06);
    }
    .btn-primary {
      background: #3b82f6;
      border: none;
      font-weight: 600;
      padding: 10px 18px;
      border-radius: 8px;
    }
    .btn-primary:hover {
      background: #2563eb;
    }
    label {
      font-weight: 600;
    }
  </style>
</head>
<body>

<div class="container py-5">
  <div class="row justify-content-center">
    <div class="col-md-7 col-lg-6">

      <div class="card p-4">
        <h3 class="text-center mb-4">Upload Word Document</h3>

        <!-- 🔥 FORM STARTS HERE -->
        <form method="POST" action="/upload-handler.php" enctype="multipart/form-data">

        <!-- Account Name -->
        <div class="mb-3">
            <label class="form-label">Account Name</label>
            <select id="account_name" name="account_name" class="form-select" required>
                <option value="">-- Select Account --</option>
                <?php foreach ($accounts as $acc): ?>
                    <option value="<?= $acc['Account_Name'] ?>"><?= $acc['Account_Name'] ?></option>
                <?php endforeach; ?>
            </select>
        </div>


         <!-- Website -->
        <div class="mb-3">
            <label class="form-label">Website</label>
            <select id="website" name="website" class="form-select" required>
                <option value="">Select Account First</option>
            </select>
        </div>

          <!-- Content Type -->
          <div class="mb-3">
            <label for="content_type" class="form-label">Content Type</label>
            <select id="content_type" name="content_type" class="form-select" required>
              <option value="auto">Auto</option>
              <option value="page">Page</option>
              <option value="post">Post</option>
            </select>
          </div>

          <!-- Upload File -->
          <div class="mb-3">
            <label class="form-label" for="word_file">Upload Word File</label>
            <input type="file" class="form-control" id="word_file" name="word_file" accept=".doc,.docx" required />
          </div>

          <button type="submit" class="btn btn-primary w-100">
            Submit & Continue
          </button>
        </form>
        <!-- 🔥 FORM ENDS HERE -->

      </div>

    </div>
  </div>
</div>

<!-- jQuery (required for Select2) -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- Select2 JS -->
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

<script>
    // Enable Select2 on Account Name field
$(document).ready(function() {
    $('#account_name').select2({
        theme: 'bootstrap4',
        placeholder: "Search Account...",
        allowClear: true
    });
});

// PHP → JS accounts map
const accounts = <?= $accountsJson ?>;

const accountMap = {};
accounts.forEach(acc => {
    accountMap[acc.Account_Name] = acc.Website;
});

// Auto-fill Website when account selected
$('#account_name').on('change', function () {
    const selected = $(this).val();
    const websiteSelect = document.getElementById("website");

    websiteSelect.innerHTML = "";

    if (accountMap[selected]) {
        websiteSelect.innerHTML = `
            <option value="${accountMap[selected]}" selected>
                ${accountMap[selected]}
            </option>
        `;
    } else {
        websiteSelect.innerHTML = `<option value="">No Website Found</option>`;
    }
});

//   // Convert PHP array → JS array
//     const accounts = <?= $accountsJson ?>;

//     // Create a map: { Account_Name : Website }
//     const accountMap = {};
//     accounts.forEach(acc => {
//         accountMap[acc.Account_Name] = acc.Website;
//     });

//     // Auto-fill Website dropdown when selecting Account
//     document.getElementById("account_name").addEventListener("change", function () {
//         const selected = this.value;
//         const websiteSelect = document.getElementById("website");

//         websiteSelect.innerHTML = ""; // Clear old options

//         if (accountMap[selected]) {
//             // Insert one option, auto-selected
//             websiteSelect.innerHTML = `
//                 <option value="${accountMap[selected]}" selected>
//                     ${accountMap[selected]}
//                 </option>
//             `;
//         } else {
//             websiteSelect.innerHTML = `
//                 <option value="">No Website Found</option>
//             `;
//         }
//     });
</script>

</body>
</html>
