<?php
require_once __DIR__ . '/includes/lang.php';
require_once __DIR__ . '/config/db.php';
session_start();
if (!isset($_SESSION['user_id'])) { header('Location: auth/login.php'); exit; }

$dir  = t('dir');
$code = t('lang_code');
$uid  = (int)$_SESSION['user_id'];
$role = $_SESSION['role'] ?? 'tourist';

// Get provider info if applicable
$provider = null;
if ($role === 'provider') {
    $stmt = db()->prepare("SELECT * FROM providers WHERE user_id = ? LIMIT 1");
    $stmt->execute([$uid]);
    $provider = $stmt->fetch();
}

$act_icons = [
    'restaurant'=>'🍽️','hotel'=>'🏨','guide'=>'🗺️',
    'craft'=>'🛍️','agency'=>'✈️','transport'=>'🚲'
];
$act_labels = [
    'restaurant'=>t('act_restaurant'),'hotel'=>t('act_hotel'),
    'guide'=>t('act_guide'),'craft'=>t('act_craft'),
    'agency'=>t('act_agency'),'transport'=>t('act_transport')
];
?>
<!DOCTYPE html>
<html lang="<?= htmlspecialchars($code) ?>" dir="<?= htmlspecialchars($dir) ?>">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= htmlspecialchars(t('dashboard')) ?> — <?= htmlspecialchars(t('site_name')) ?></title>
  <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🌴</text></svg>">
  <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800;900&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="css/style.css">
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
</head>
<body>

<div class="dash-wrap">

  <!-- Header -->
  <div class="dash-header">
    <div class="dash-avatar"><?= $role === 'provider' ? '💼' : '👤' ?></div>
    <div class="dash-name"><?= htmlspecialchars(t('welcome_back')) ?>، <?= htmlspecialchars($_SESSION['username']) ?></div>
    <div class="dash-role"><?= $role === 'provider' ? htmlspecialchars(t('provider')) : htmlspecialchars(t('tourist')) ?></div>
  </div>

  <div class="dash-body">

    <?php if ($role === 'provider' && $provider): ?>

      <!-- Provider status -->
      <?php if ($provider['status'] === 'pending'): ?>
        <div class="status-wrap">
          <div class="status-icon">⏳</div>
          <div class="status-title"><?= htmlspecialchars(t('pending_approval')) ?></div>
          <div class="status-desc" style="margin-top:6px">سيتم مراجعة طلبك من قِبل الإدارة وإعلامك قريباً.</div>
        </div>
      <?php elseif ($provider['status'] === 'rejected'): ?>
        <div class="status-wrap" style="background:var(--error-bg);border-color:var(--error)">
          <div class="status-icon">❌</div>
          <div class="status-title" style="color:var(--error)">تم رفض طلبك</div>
          <div class="status-desc">تواصل مع الإدارة لمعرفة السبب.</div>
        </div>
      <?php elseif ($provider['status'] === 'approved'): ?>
        <div class="status-wrap" style="background:var(--g-pale);border-color:var(--g-mint)">
          <div class="status-icon">✅</div>
          <div class="status-title" style="color:var(--g-deep)">نشاطك مفعّل ومرئي للزوار!</div>
        </div>
      <?php endif; ?>

      <!-- Business card -->
      <div class="dash-card">
        <div class="dash-card-title">معلومات نشاطك</div>
        <div style="display:flex;gap:12px;align-items:flex-start">
          <?php if ($provider['image']): ?>
            <img src="<?= htmlspecialchars($provider['image']) ?>" style="width:64px;height:64px;border-radius:var(--r);object-fit:cover;flex-shrink:0">
          <?php else: ?>
            <div style="width:64px;height:64px;border-radius:var(--r);background:var(--g-pale);display:flex;align-items:center;justify-content:center;font-size:1.8rem;flex-shrink:0">
              <?= $act_icons[$provider['activity_type']] ?? '🏪' ?>
            </div>
          <?php endif; ?>
          <div>
            <div style="font-weight:800;font-size:1rem;color:var(--dark)"><?= htmlspecialchars($provider['business_name']) ?></div>
            <div style="font-size:.82rem;color:var(--muted);margin-top:3px">
              <?= $act_icons[$provider['activity_type']] ?? '' ?> <?= htmlspecialchars($act_labels[$provider['activity_type']] ?? $provider['activity_type']) ?>
            </div>
            <?php if ($provider['address']): ?>
              <div style="font-size:.8rem;color:var(--muted);margin-top:3px">📍 <?= htmlspecialchars($provider['address']) ?></div>
            <?php endif; ?>
            <div style="font-size:.8rem;color:var(--muted);margin-top:3px">📞 <?= htmlspecialchars($provider['phone']) ?></div>
          </div>
        </div>

        <?php if ($provider['lat'] && $provider['lng']): ?>
          <div style="margin-top:14px;border-radius:var(--r);overflow:hidden;height:160px" id="dashMap"></div>
        <?php endif; ?>
      </div>

    <?php else: ?>

      <!-- Tourist welcome card -->
      <div class="dash-card">
        <div class="dash-card-title">ابدأ استكشافك</div>
        <p style="font-size:.9rem;color:var(--muted);margin-bottom:16px">اكتشف أجمل أماكن المغير، الأنشطة، والخدمات المحلية.</p>
        <a href="index.php<?= $code!=='ar'?'?lang='.$code:'' ?>" class="btn btn-green btn-sm btn-inline" style="display:inline-flex">
          🌴 استكشف الموقع
        </a>
      </div>

    <?php endif; ?>

    <!-- Navigation card -->
    <div class="dash-card">
      <div class="dash-card-title">روابط سريعة</div>
      <div style="display:flex;flex-direction:column;gap:8px">
        <a href="index.php<?= $code!=='ar'?'?lang='.$code:'' ?>"
           style="display:flex;align-items:center;gap:10px;padding:12px;border-radius:var(--r-sm);background:var(--cream);font-size:.9rem;font-weight:600;color:var(--text);transition:var(--trans)"
           onmouseover="this.style.background='var(--g-pale)'" onmouseout="this.style.background='var(--cream)'">
          🏠 <?= htmlspecialchars(t('nav_home')) ?>
        </a>
        <a href="places.php<?= $code!=='ar'?'?lang='.$code:'' ?>"
           style="display:flex;align-items:center;gap:10px;padding:12px;border-radius:var(--r-sm);background:var(--cream);font-size:.9rem;font-weight:600;color:var(--text);transition:var(--trans)"
           onmouseover="this.style.background='var(--g-pale)'" onmouseout="this.style.background='var(--cream)'">
          🗺️ <?= htmlspecialchars(t('nav_places')) ?>
        </a>
        <a href="activities.php<?= $code!=='ar'?'?lang='.$code:'' ?>"
           style="display:flex;align-items:center;gap:10px;padding:12px;border-radius:var(--r-sm);background:var(--cream);font-size:.9rem;font-weight:600;color:var(--text);transition:var(--trans)"
           onmouseover="this.style.background='var(--g-pale)'" onmouseout="this.style.background='var(--cream)'">
          🏪 <?= htmlspecialchars(t('nav_activities')) ?>
        </a>
        <a href="auth/logout.php"
           style="display:flex;align-items:center;gap:10px;padding:12px;border-radius:var(--r-sm);background:var(--error-bg);font-size:.9rem;font-weight:600;color:var(--error)">
          🚪 <?= htmlspecialchars(t('logout')) ?>
        </a>
      </div>
    </div>

  </div>
</div>

<?php if ($role === 'provider' && $provider && $provider['lat'] && $provider['lng']): ?>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script>
var map = L.map('dashMap', {zoomControl:false, dragging:false, touchZoom:false, scrollWheelZoom:false})
  .setView([<?= (float)$provider['lat'] ?>, <?= (float)$provider['lng'] ?>], 14);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution:'© OpenStreetMap'}).addTo(map);
L.marker([<?= (float)$provider['lat'] ?>, <?= (float)$provider['lng'] ?>]).addTo(map);
</script>
<?php endif; ?>

</body>
</html>
