<?php
require_once '_auth.php';
adm_require_login();
require_once '_layout.php';

// Fetch stats
$totalCountries    = $pdo->query("SELECT COUNT(*) FROM countries")->fetchColumn();
$totalTrips        = $pdo->query("SELECT COUNT(*) FROM trips")->fetchColumn();
$totalPriceAvg     = $pdo->query("SELECT AVG(price) FROM trips")->fetchColumn();
$totalGallery      = count(get_tour_gallery());

// Recent trips
$recentTrips = $pdo->query(
    "SELECT t.*, c.name AS country_name, c.flag
     FROM trips t
     LEFT JOIN countries c ON t.country_id = c.id
     ORDER BY t.price DESC LIMIT 8"
)->fetchAll();

adm_layout_start('dashboard', 'Dashboard');
?>

<div class="adm-page-header">
  <div>
    <h1 class="adm-page-title">Dashboard</h1>
    <p class="adm-page-sub">Selamat datang kembali, <strong><?= htmlspecialchars($_SESSION['adm_user']) ?></strong>!</p>
  </div>
  <div style="display:flex;gap:10px;flex-wrap:wrap;">
    <a href="galeri.php" class="btn btn-outline btn-sm">
      <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>
      + Foto Galeri
    </a>
    <a href="destinasi.php?action=add" class="btn btn-outline btn-sm">
      <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
      + Destinasi
    </a>
    <a href="trip-form.php" class="btn btn-primary btn-sm">
      <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
      + Paket Trip
    </a>
  </div>
</div>

<!-- Stats -->
<div class="adm-stats-grid" style="grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));">
  <div class="adm-stat-card">
    <div class="adm-stat-icon blue">
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/>
        <path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/>
      </svg>
    </div>
    <div>
      <div class="adm-stat-lbl">Destinasi</div>
      <div class="adm-stat-val"><?= $totalCountries ?></div>
    </div>
  </div>
  <div class="adm-stat-card">
    <div class="adm-stat-icon green">
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/>
      </svg>
    </div>
    <div>
      <div class="adm-stat-lbl">Paket Trip</div>
      <div class="adm-stat-val"><?= $totalTrips ?></div>
    </div>
  </div>
  <div class="adm-stat-card">
    <div class="adm-stat-icon orange">
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <line x1="12" y1="1" x2="12" y2="23"/><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/>
      </svg>
    </div>
    <div>
      <div class="adm-stat-lbl">Harga Rata-rata</div>
      <div class="adm-stat-val" style="font-size:18px;letter-spacing:-0.5px;">
        Rp <?= number_format((int)$totalPriceAvg, 0, ',', '.') ?>
      </div>
    </div>
  </div>
</div>

<!-- 3-Step Workflow Guide -->
<div class="adm-card" style="margin-bottom:24px;border:1.5px solid #e2e8f0;background:#fafbfc;">
  <div class="adm-card-header" style="background:#fff;">
    <span class="adm-card-title" style="font-size:15px;display:flex;align-items:center;gap:8px;">
      <span>🚀</span> Alur Pengelolaan Konten (3 Tahap Terstruktur)
    </span>
  </div>
  <div class="adm-card-body" style="padding:20px 24px;">
    <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(240px, 1fr));gap:16px;">
      
      <!-- Step 1 -->
      <a href="destinasi.php" style="text-decoration:none;color:inherit;display:block;background:#fff;border:1.5px solid #e2e8f0;border-radius:10px;padding:18px;transition:all .2s;" onmouseover="this.style.borderColor='#3b82f6';this.style.boxShadow='0 4px 12px rgba(59,130,246,0.1)'" onmouseout="this.style.borderColor='#e2e8f0';this.style.boxShadow='none'">
        <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:10px;">
          <span style="font-size:12px;font-weight:800;background:#eff6ff;color:#2563eb;padding:3px 8px;border-radius:6px;">STEP 1</span>
          <span style="font-size:20px;">🌍</span>
        </div>
        <div style="font-weight:800;font-size:15px;color:#1e293b;margin-bottom:4px;">1. Destinasi (Negara)</div>
        <p style="font-size:12.5px;color:#64748b;margin:0;line-height:1.4;">
          Buat negara/destinasi utama, upload foto hero, pilih bendera, dan atur tampilan di Beranda.
        </p>
      </a>

      <!-- Step 2 -->
      <a href="trips.php" style="text-decoration:none;color:inherit;display:block;background:#fff;border:1.5px solid #e2e8f0;border-radius:10px;padding:18px;transition:all .2s;" onmouseover="this.style.borderColor='#10b981';this.style.boxShadow='0 4px 12px rgba(16,185,129,0.1)'" onmouseout="this.style.borderColor='#e2e8f0';this.style.boxShadow='none'">
        <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:10px;">
          <span style="font-size:12px;font-weight:800;background:#ecfdf5;color:#059669;padding:3px 8px;border-radius:6px;">STEP 2</span>
          <span style="font-size:20px;">✈️</span>
        </div>
        <div style="font-weight:800;font-size:15px;color:#1e293b;margin-bottom:4px;">2. Paket Trip (Isi Negara)</div>
        <p style="font-size:12.5px;color:#64748b;margin:0;line-height:1.4;">
          Buat paket perjalanan untuk destinasi tersebut (Kode trip, nama paket, tanggal, durasi, harga).
        </p>
      </a>

      <!-- Step 3 -->
      <a href="itinerary.php" style="text-decoration:none;color:inherit;display:block;background:#fff;border:1.5px solid #e2e8f0;border-radius:10px;padding:18px;transition:all .2s;" onmouseover="this.style.borderColor='#f59e0b';this.style.boxShadow='0 4px 12px rgba(245,158,11,0.1)'" onmouseout="this.style.borderColor='#e2e8f0';this.style.boxShadow='none'">
        <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:10px;">
          <span style="font-size:12px;font-weight:800;background:#fffbeb;color:#d97706;padding:3px 8px;border-radius:6px;">STEP 3</span>
          <span style="font-size:20px;">🗓️</span>
        </div>
        <div style="font-weight:800;font-size:15px;color:#1e293b;margin-bottom:4px;">3. Detail &amp; Itinerary</div>
        <p style="font-size:12.5px;color:#64748b;margin:0;line-height:1.4;">
          Lengkapi fasilitas Termasuk/Tidak Termasuk dan jadwal timeline harian dengan upload foto kegiatan.
        </p>
      </a>

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

<!-- Recent Trips -->
<div class="adm-card">
  <div class="adm-card-header">
    <span class="adm-card-title">Paket Trip Terbaru</span>
    <a href="trips.php" class="btn btn-outline btn-sm">Lihat Semua</a>
  </div>
  <div class="adm-table-wrap">
    <table class="adm-table">
      <thead>
        <tr>
          <th>Destinasi</th>
          <th>Nama Trip</th>
          <th>Kode</th>
          <th>Tanggal</th>
          <th>Harga</th>
          <th>Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($recentTrips as $t): ?>
        <tr>
          <td>
            <span style="margin-right:6px;"><?= render_flag($t['flag']) ?></span>
            <?= htmlspecialchars($t['country_name']) ?>
          </td>
          <td><strong><?= htmlspecialchars($t['name']) ?></strong></td>
          <td><span class="adm-badge adm-badge-blue"><?= htmlspecialchars($t['code']) ?></span></td>
          <td><?= htmlspecialchars($t['dateRange']) ?></td>
          <td>Rp <?= number_format($t['price'], 0, ',', '.') ?></td>
          <td>
            <a href="trip-form.php?id=<?= urlencode($t['id']) ?>" class="btn-icon btn-sm" title="Edit">
              <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
            </a>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($recentTrips)): ?>
        <tr><td colspan="6" style="text-align:center;padding:32px;color:#94a3b8;">Belum ada trip.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?php adm_layout_end(); ?>
