<?php

$pageTitle = 'reserve_tag';

require_once 'includes/header.php';

$db = getDB();

$success = false;
$error   = '';

// ─────────────────────────────────────────────
// Handle Reservation
// ─────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $name      = trim($_POST['customer_name'] ?? '');
    $phone     = trim($_POST['customer_phone'] ?? '');
    $email     = trim($_POST['customer_email'] ?? '');
    $partySize = (int)($_POST['party_size'] ?? 0);
    $date      = $_POST['date'] ?? '';
    $time      = $_POST['time'] ?? '';
    $notes     = trim($_POST['notes'] ?? '');

    if (
        !$name ||
        !$phone ||
        !$date ||
        !$time ||
        $partySize < 1
    ) {

        $error = $isRTL
            ? 'يرجى تعبئة جميع الحقول المطلوبة.'
            : 'Please fill in all required fields.';

    } else {

        $stmt = $db->prepare("
            INSERT INTO reservations (
                customer_name,
                customer_phone,
                customer_email,
                party_size,
                date,
                time,
                notes,
                status
            )
            VALUES (
                ?, ?, ?, ?, ?, ?, ?, 'pending'
            )
        ");

        $stmt->execute([
            $name,
            $phone,
            $email ?: null,
            $partySize,
            $date,
            $time,
            $notes ?: null
        ]);

        $success = true;
    }
}

// ─────────────────────────────────────────────
// Time Slots
// ─────────────────────────────────────────────
$times = [];

for ($h = 12; $h <= 22; $h++) {

    $times[] = sprintf('%02d:00', $h);

    if ($h < 22) {
        $times[] = sprintf('%02d:30', $h);
    }
}

?>

<div class="pt-page"
     style="padding-bottom:80px;min-height:100vh">

  <div class="container">

    <!-- Header -->
    <div style="
      padding:40px 0 36px;
      border-bottom:1px solid var(--border);
      margin-bottom:48px
    ">

      <span class="section-tag">
        <?= t('reserve_tag') ?>
      </span>

      <h1 class="section-title">
        <?= t('reserve_title') ?>
      </h1>

      <p class="section-desc">
        <?= t('reserve_desc') ?>
      </p>

    </div>

    <!-- Success -->
    <?php if ($success): ?>

    <div style="
      text-align:center;
      padding:60px 0
    ">

      <div style="
        font-size:4rem;
        margin-bottom:20px
      ">
        🎉
      </div>

      <h2 style="
        font-family:var(--font-serif);
        font-size:2.5rem;
        margin-bottom:12px;
        color:var(--primary)
      ">

        <?= t('res_confirmed') ?>

      </h2>

      <p style="
        color:var(--fg60);
        margin-bottom:32px
      ">

        <?= $isRTL
            ? 'سيتواصل معك فريقنا قريباً لتأكيد الحجز.'
            : 'Our team will contact you shortly to confirm your reservation.'
        ?>

      </p>

      <a href="reservations.php"
         class="btn btn-primary">

         <?= t('book_another') ?>

      </a>

    </div>

    <?php else: ?>

    <div class="res-grid">

      <!-- LEFT -->
      <div>

        <!-- Error -->
        <?php if ($error): ?>

        <div class="alert alert-error">
          <?= e($error) ?>
        </div>

        <?php endif; ?>

        <!-- Form Card -->
        <div class="card"
             style="padding:32px">

          <div style="
            font-size:10px;
            font-weight:700;
            letter-spacing:.2em;
            text-transform:uppercase;
            color:var(--primary);
            border-bottom:1px solid var(--border);
            padding-bottom:14px;
            margin-bottom:22px
          ">

            <?= t('res_details') ?>

          </div>

          <!-- FORM -->
          <form method="POST"
                action="reservations.php">

            <!-- Name + Phone -->
            <div class="grid-2">

              <div class="form-group">

                <label class="form-label">
                  <?= t('full_name') ?>
                </label>

                <input
                  class="form-input"
                  type="text"
                  name="customer_name"
                  placeholder="<?= $isRTL ? 'محمد أحمد' : 'John Doe' ?>"
                  required
                >

              </div>

              <div class="form-group">

                <label class="form-label">
                  <?= t('phone_number') ?>
                </label>

                <input
                  class="form-input"
                  type="tel"
                  name="customer_phone"
                  placeholder="+1 (555) 000-0000"
                  required
                >

              </div>

            </div>

            <!-- Email -->
            <div class="form-group">

              <label class="form-label">
                <?= t('email_opt') ?>
              </label>

              <input
                class="form-input"
                type="email"
                name="customer_email"
                placeholder="email@example.com"
              >

            </div>

            <!-- Guests + Date -->
            <div class="grid-2">

              <div class="form-group">

                <label class="form-label">
                  <?= t('guests_lbl') ?>
                </label>

                <select
                  class="form-select"
                  name="party_size"
                  required
                >

                  <?php for ($i = 1; $i <= 12; $i++): ?>

                  <option value="<?= $i ?>">

                    <?= $i ?>
                    <?= $isRTL ? 'أشخاص' : 'Guests' ?>

                  </option>

                  <?php endfor; ?>

                </select>

              </div>

              <div class="form-group">

                <label class="form-label">
                  <?= t('date_lbl') ?>
                </label>

                <input
                  class="form-input"
                  type="date"
                  name="date"
                  min="<?= date('Y-m-d', strtotime('+1 day')) ?>"
                  required
                >

              </div>

            </div>

            <!-- Time -->
            <div class="form-group">

              <label class="form-label">
                <?= t('time_lbl') ?>
              </label>

              <select
                class="form-select"
                name="time"
                required
              >

                <option value="">
                  <?= t('selectTime') ?>
                </option>

                <?php foreach ($times as $t_val): ?>

                <option value="<?= $t_val ?>">
                  <?= $t_val ?>
                </option>

                <?php endforeach; ?>

              </select>

            </div>

            <!-- Notes -->
            <div class="form-group">

              <label class="form-label">
                <?= t('special_req') ?>
              </label>

              <textarea
                class="form-textarea"
                name="notes"
                placeholder="<?= $isRTL
                    ? 'الحساسية الغذائية، المناسبات الخاصة...'
                    : 'Dietary requirements, special occasions...'
                ?>"
              ></textarea>

            </div>

            <!-- Submit -->
            <button type="submit"
                    class="btn btn-primary"
                    style="
                      width:100%;
                      justify-content:center
                    ">

              <?= t('confirm_res') ?>

            </button>

          </form>

        </div>

      </div>

      <!-- RIGHT -->
      <div>

        <!-- Hours -->
        <div class="info-card">

          <h4>
            🕐 <?= t('hours_lbl') ?>
          </h4>

          <p><?= t('mon_thu') ?></p>
          <p><?= t('fri_sat') ?></p>
          <p><?= t('sunday') ?></p>

        </div>

        <!-- Events -->
        <div class="info-card">

          <h4>
            🎉
            <?= $isRTL
                ? 'المناسبات الخاصة'
                : 'Private Events'
            ?>
          </h4>

          <p>

            <?= $isRTL
                ? 'نوفر خدمات خاصة للمناسبات والاحتفالات والمجموعات الكبيرة.'
                : 'We offer premium private event services for celebrations and large groups.'
            ?>

          </p>

        </div>

        <!-- Dress Code -->
        <div class="info-card">

          <h4>
            👔
            <?= $isRTL
                ? 'قواعد اللباس'
                : 'Dress Code'
            ?>
          </h4>

          <p>

            <?= $isRTL
                ? 'اللباس الأنيق مفضل لضمان تجربة راقية.'
                : 'Smart casual to formal attire preferred.'
            ?>

          </p>

        </div>

        <!-- Delivery -->
        <div class="info-card">

          <h4>
            🚚 <?= t('delivery') ?>
          </h4>

          <p>

            <?= $isRTL
                ? 'استمتع بوجباتنا الفاخرة في منزلك عبر خدمة التوصيل.'
                : 'Enjoy our fine dining experience at home with delivery service.'
            ?>

          </p>

          <a href="delivery.php"
             style="
               color:var(--primary);
               font-size:13px;
               margin-top:8px;
               display:inline-block
             ">

             <?= t('start_order') ?> →

          </a>

        </div>

      </div>

    </div>

    <?php endif; ?>

  </div>

</div>

<?php require_once 'includes/footer.php'; ?>