<?php
/**
 * Nephro Link - Dashboard Module (UI-First)
 * - TR/EN via core/i18n.php (t('key'))
 * - Mobile friendly
 * - DB yoksa placeholder
 */

// Not: i18n.php zaten dashboard.php içinde require ediliyor.
// Yine de direkt açılırsa diye güvene alalım:
if (!function_exists('t')) {
  require_once __DIR__ . '/../core/i18n.php';
}

if (session_status() === PHP_SESSION_NONE) { session_start(); }

if (!function_exists('h')) {
  function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
}

$lang = $_SESSION['lang'] ?? ($_COOKIE['lang'] ?? 'tr');
$lang = in_array($lang, ['tr','en'], true) ? $lang : 'tr';

$role = $_SESSION['role'] ?? 'staff';
$full = $_SESSION['full_name'] ?? 'User';

// --------- module dictionary (yalnız dashboard'a özel) ----------
$GLOBALS['I18N_DICT']['tr'] = array_merge($GLOBALS['I18N_DICT']['tr'] ?? [], [
  'dash_hello'          => 'Merhaba',
  'dash_subtitle'       => 'Bugünkü operasyon özeti ve hızlı aksiyonlar',
  'dash_quick'          => 'Hızlı Aksiyonlar',
  'dash_stats'          => 'Genel İstatistikler',
  'dash_today'          => 'Bugün',
  'dash_week'           => 'Bu Hafta',
  'dash_pending'        => 'Bekleyen',
  'dash_active_sessions'=> 'Aktif Seanslar',
  'dash_today_appt'     => 'Bugünkü Randevular',
  'dash_patients'       => 'Toplam Hasta',
  'dash_files'          => 'Yüklenen Dosyalar',
  'dash_alerts'         => 'Uyarılar',
  'dash_alerts_sub'     => 'Önemli hatırlatmalar',
  'dash_recent'         => 'Son Hareketler',
  'dash_recent_sub'     => 'Sistemdeki son aktiviteler',
  'dash_go_patient_add' => 'Yeni Hasta Kaydı',
  'dash_go_patient_list'=> 'Hasta Listesi',
  'dash_go_calendar'    => 'Seans Takvimi',
  'dash_go_active'      => 'Aktif Seans Takibi',
  'dash_go_logs'        => 'Sistem Logları',
  'dash_hint_db'        => 'Not: Bu ekran şu an demo değerlerle çalışıyor. Veritabanı tablolarını bağlayınca sayılar otomatik gerçek veriye dönecek.',
  'dash_chip_role'      => 'Rol',
  'dash_warn_maint'     => 'Bugün 2 cihaz bakım tarihi yaklaşıyor.',
  'dash_info_conflict'  => 'Seans planında 1 çakışma tespit edildi.',
  'dash_recent_newpat'  => 'Yeni hasta kaydı oluşturuldu',
  'dash_recent_appt'    => 'Randevu güncellendi',
  'dash_recent_file'    => 'Hasta dosyası yüklendi',
  'time_10m'            => '10 dk önce',
  'time_32m'            => '32 dk önce',
  'time_1h'             => '1 saat önce',
]);

$GLOBALS['I18N_DICT']['en'] = array_merge($GLOBALS['I18N_DICT']['en'] ?? [], [
  'dash_hello'          => 'Hello',
  'dash_subtitle'       => 'Today’s operational summary and quick actions',
  'dash_quick'          => 'Quick Actions',
  'dash_stats'          => 'Overview Stats',
  'dash_today'          => 'Today',
  'dash_week'           => 'This Week',
  'dash_pending'        => 'Pending',
  'dash_active_sessions'=> 'Active Sessions',
  'dash_today_appt'     => 'Today’s Appointments',
  'dash_patients'       => 'Total Patients',
  'dash_files'          => 'Uploaded Files',
  'dash_alerts'         => 'Alerts',
  'dash_alerts_sub'     => 'Important reminders',
  'dash_recent'         => 'Recent Activity',
  'dash_recent_sub'     => 'Latest events in the system',
  'dash_go_patient_add' => 'New Patient',
  'dash_go_patient_list'=> 'Patient List',
  'dash_go_calendar'    => 'Session Calendar',
  'dash_go_active'      => 'Active Sessions',
  'dash_go_logs'        => 'System Logs',
  'dash_hint_db'        => 'Note: This screen is running with demo values. Once DB tables are wired, numbers will reflect real data.',
  'dash_chip_role'      => 'Role',
  'dash_warn_maint'     => '2 device maintenance dates are near today.',
  'dash_info_conflict'  => '1 scheduling conflict detected in sessions plan.',
  'dash_recent_newpat'  => 'New patient created',
  'dash_recent_appt'    => 'Appointment updated',
  'dash_recent_file'    => 'Patient file uploaded',
  'time_10m'            => '10 min ago',
  'time_32m'            => '32 min ago',
  'time_1h'             => '1 hour ago',
]);

// --------- PLACEHOLDER DATA (DB bağlanınca query ile değişecek) ----------
$stats = [
  'active_sessions' => 3,
  'today_appt'      => 18,
  'patients_total'  => 412,
  'files_total'     => 1270,
];

$alerts = [
  ['type'=>'warn', 'text'=> t('dash_warn_maint') ],
  ['type'=>'info', 'text'=> t('dash_info_conflict') ],
];

$recent = [
  ['icon'=>'bi-person-plus',        'title'=> t('dash_recent_newpat'), 'meta'=> t('time_10m')],
  ['icon'=>'bi-calendar-check',     'title'=> t('dash_recent_appt'),   'meta'=> t('time_32m')],
  ['icon'=>'bi-file-earmark-arrow-up','title'=> t('dash_recent_file'),  'meta'=> t('time_1h')],
];

$roleLabel = ucfirst((string)$role);
?>

<style>
/* Dashboard module styles - mevcut tasarımla aynı dil */
.nl-container{ animation: fadeIn .35s ease both; }

.nl-hero{
  display:flex;
  align-items:flex-start;
  justify-content:space-between;
  gap: 14px;
  margin-bottom: 18px;
}
.nl-hero .h-left{ min-width:0; }
.nl-hero .h-title{
  font-size: 1.25rem;
  font-weight: 1000;
  color:#0f172a;
  margin: 0;
  letter-spacing: -0.2px;
}
.nl-hero .h-sub{
  margin-top: 6px;
  font-size: .88rem;
  font-weight: 800;
  color:#94a3b8;
}

.nl-chip{
  display:inline-flex;
  align-items:center;
  gap:10px;
  border:1px solid var(--border, #e2e8f0);
  background:#fff;
  border-radius: 14px;
  padding: 10px 12px;
  box-shadow: 0 10px 25px rgba(15,23,42,0.04);
  white-space: nowrap;
}
.nl-chip .dot{
  width: 10px; height: 10px;
  border-radius: 999px;
  background: var(--accent, #4361ee);
}
.nl-chip .k{ font-weight: 1000; font-size: .72rem; color:#94a3b8; text-transform: uppercase; letter-spacing: 1px; }
.nl-chip .v{ font-weight: 1000; font-size: .9rem; color:#0f172a; }

.nl-grid{
  display:grid;
  grid-template-columns: 1.1fr .9fr;
  gap: 18px;
}
@media (max-width: 992px){
  .nl-grid{ grid-template-columns: 1fr; }
}

/* Cards */
.nl-card{
  background:#fff;
  border:1px solid var(--border, #e2e8f0);
  border-radius: 18px;
  box-shadow: 0 10px 25px rgba(15,23,42,0.04);
  overflow:hidden;
}
.nl-card .ch{
  padding: 14px 16px;
  border-bottom: 1px solid #f1f5f9;
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 10px;
}
.nl-card .ch .ttl{
  font-weight: 1000;
  color:#0f172a;
  font-size: .95rem;
}
.nl-card .ch .sub{
  font-weight: 900;
  color:#94a3b8;
  font-size: .78rem;
  margin-top: 4px;
}
.nl-card .cb{ padding: 16px; }

/* Stats cards */
.stat-grid{
  display:grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}
@media (max-width: 1200px){
  .stat-grid{ grid-template-columns: repeat(2, 1fr); }
}
.stat{
  border:1px solid #f1f5f9;
  background:#fff;
  border-radius: 16px;
  padding: 14px 14px;
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 10px;
}
.stat .l{ min-width:0; }
.stat .k{
  font-weight: 1000;
  font-size: .74rem;
  color:#94a3b8;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.stat .v{
  margin-top: 6px;
  font-weight: 1100;
  font-size: 1.25rem;
  color:#0f172a;
}
.stat .ico{
  width: 42px; height: 42px;
  border-radius: 14px;
  background: #eff6ff;
  border: 1px solid #dbeafe;
  display:flex;
  align-items:center;
  justify-content:center;
  color: var(--accent, #4361ee);
  font-size: 1.15rem;
}

/* Quick actions */
.qa{
  display:grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
@media (max-width: 576px){
  .qa{ grid-template-columns: 1fr; }
}
.qa a{
  text-decoration:none;
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 16px;
  border: 1px solid #f1f5f9;
  background:#fff;
  transition: .15s;
}
.qa a:hover{
  background:#f8fafc;
  border-color:#dbeafe;
}
.qa .q-left{
  display:flex;
  align-items:center;
  gap: 10px;
  min-width:0;
}
.qa .q-ico{
  width: 42px; height: 42px;
  border-radius: 14px;
  background:#f8fafc;
  border:1px solid #f1f5f9;
  display:flex;
  align-items:center;
  justify-content:center;
  color:#64748b;
  flex: 0 0 auto;
}
.qa .q-txt{
  font-weight: 1000;
  color:#0f172a;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.qa .q-arrow{ color:#94a3b8; }

/* Alerts */
.alert-item{
  display:flex;
  align-items:flex-start;
  gap: 12px;
  padding: 12px 12px;
  border:1px solid #f1f5f9;
  border-radius: 16px;
  margin-bottom: 10px;
  background:#fff;
}
.alert-ico{
  width: 38px; height: 38px;
  border-radius: 14px;
  display:flex;
  align-items:center;
  justify-content:center;
  border:1px solid #f1f5f9;
  background:#f8fafc;
  color:#64748b;
}
.alert-item.warn .alert-ico{ background:#fff7ed; border-color:#fed7aa; color:#f59e0b; }
.alert-item.info .alert-ico{ background:#eff6ff; border-color:#dbeafe; color: var(--accent, #4361ee); }
.alert-text{
  font-weight: 900;
  color:#0f172a;
  font-size: .9rem;
  line-height: 1.25;
}

/* Recent list */
.r-item{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 12px;
  padding: 12px 12px;
  border:1px solid #f1f5f9;
  border-radius: 16px;
  margin-bottom: 10px;
  background:#fff;
}
.r-left{
  display:flex;
  align-items:center;
  gap: 12px;
  min-width:0;
}
.r-ico{
  width: 40px; height: 40px;
  border-radius: 14px;
  background:#f8fafc;
  border:1px solid #f1f5f9;
  display:flex;
  align-items:center;
  justify-content:center;
  color:#64748b;
  flex: 0 0 auto;
}
.r-title{
  font-weight: 1000;
  color:#0f172a;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.r-meta{
  font-weight: 900;
  color:#94a3b8;
  font-size: .78rem;
  white-space: nowrap;
}

.nl-note{
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: 16px;
  border: 1px dashed #dbeafe;
  background: #eff6ff;
  color:#1e40af;
  font-weight: 900;
  font-size: .86rem;
}
</style>

<div class="nl-container">
  <div class="nl-hero">
    <div class="h-left">
      <h1 class="h-title"><?= h(t('dash_hello')) ?>, <?= h($full) ?> 👋</h1>
      <div class="h-sub"><?= h(t('dash_subtitle')) ?></div>
    </div>

    <div class="nl-chip" title="<?= h(t('dash_chip_role')) ?>">
      <span class="dot"></span>
      <div>
        <div class="k"><?= h(t('dash_chip_role')) ?></div>
        <div class="v"><?= h($roleLabel) ?></div>
      </div>
    </div>
  </div>

  <!-- STATS -->
  <div class="nl-card" style="margin-bottom:18px;">
    <div class="ch">
      <div>
        <div class="ttl"><?= h(t('dash_stats')) ?></div>
        <div class="sub"><?= h(t('dash_today')) ?> / <?= h(t('dash_week')) ?> / <?= h(t('dash_pending')) ?></div>
      </div>
    </div>

    <div class="cb">
      <div class="stat-grid">
        <div class="stat">
          <div class="l">
            <div class="k"><?= h(t('dash_active_sessions')) ?></div>
            <div class="v"><?= (int)$stats['active_sessions'] ?></div>
          </div>
          <div class="ico"><i class="bi bi-broadcast"></i></div>
        </div>

        <div class="stat">
          <div class="l">
            <div class="k"><?= h(t('dash_today_appt')) ?></div>
            <div class="v"><?= (int)$stats['today_appt'] ?></div>
          </div>
          <div class="ico"><i class="bi bi-calendar-check"></i></div>
        </div>

        <div class="stat">
          <div class="l">
            <div class="k"><?= h(t('dash_patients')) ?></div>
            <div class="v"><?= (int)$stats['patients_total'] ?></div>
          </div>
          <div class="ico"><i class="bi bi-people"></i></div>
        </div>

        <div class="stat">
          <div class="l">
            <div class="k"><?= h(t('dash_files')) ?></div>
            <div class="v"><?= (int)$stats['files_total'] ?></div>
          </div>
          <div class="ico"><i class="bi bi-folder2-open"></i></div>
        </div>
      </div>

      <div class="nl-note">
        <?= h(t('dash_hint_db')) ?>
      </div>
    </div>
  </div>

  <div class="nl-grid">

    <!-- LEFT -->
    <div>
      <div class="nl-card" style="margin-bottom:18px;">
        <div class="ch">
          <div>
            <div class="ttl"><?= h(t('dash_quick')) ?></div>
            <div class="sub"><?= h(t('dash_subtitle')) ?></div>
          </div>
        </div>
        <div class="cb">
          <div class="qa">
            <a href="dashboard.php?sayfa=hasta-ekle">
              <div class="q-left">
                <div class="q-ico"><i class="bi bi-person-plus"></i></div>
                <div class="q-txt"><?= h(t('dash_go_patient_add')) ?></div>
              </div>
              <i class="bi bi-arrow-right-short q-arrow"></i>
            </a>

            <a href="dashboard.php?sayfa=hasta-liste">
              <div class="q-left">
                <div class="q-ico"><i class="bi bi-people"></i></div>
                <div class="q-txt"><?= h(t('dash_go_patient_list')) ?></div>
              </div>
              <i class="bi bi-arrow-right-short q-arrow"></i>
            </a>

            <a href="dashboard.php?sayfa=seans-takvimi">
              <div class="q-left">
                <div class="q-ico"><i class="bi bi-calendar-week"></i></div>
                <div class="q-txt"><?= h(t('dash_go_calendar')) ?></div>
              </div>
              <i class="bi bi-arrow-right-short q-arrow"></i>
            </a>

            <a href="dashboard.php?sayfa=aktif-seans-takip">
              <div class="q-left">
                <div class="q-ico"><i class="bi bi-broadcast"></i></div>
                <div class="q-txt"><?= h(t('dash_go_active')) ?></div>
              </div>
              <i class="bi bi-arrow-right-short q-arrow"></i>
            </a>

            <?php if ($role === 'admin'): ?>
            <a href="dashboard.php?sayfa=log-listesi">
              <div class="q-left">
                <div class="q-ico"><i class="bi bi-journal-text"></i></div>
                <div class="q-txt"><?= h(t('dash_go_logs')) ?></div>
              </div>
              <i class="bi bi-arrow-right-short q-arrow"></i>
            </a>
            <?php endif; ?>
          </div>
        </div>
      </div>

      <div class="nl-card">
        <div class="ch">
          <div>
            <div class="ttl"><?= h(t('dash_recent')) ?></div>
            <div class="sub"><?= h(t('dash_recent_sub')) ?></div>
          </div>
        </div>

        <div class="cb">
          <?php foreach($recent as $r): ?>
            <div class="r-item">
              <div class="r-left">
                <div class="r-ico"><i class="bi <?= h($r['icon']) ?>"></i></div>
                <div class="r-title"><?= h($r['title']) ?></div>
              </div>
              <div class="r-meta"><?= h($r['meta']) ?></div>
            </div>
          <?php endforeach; ?>
        </div>
      </div>
    </div>

    <!-- RIGHT -->
    <div>
      <div class="nl-card">
        <div class="ch">
          <div>
            <div class="ttl"><?= h(t('dash_alerts')) ?></div>
            <div class="sub"><?= h(t('dash_alerts_sub')) ?></div>
          </div>
        </div>

        <div class="cb">
          <?php foreach($alerts as $a): ?>
            <div class="alert-item <?= h($a['type']) ?>">
              <div class="alert-ico">
                <?php if ($a['type']==='warn'): ?>
                  <i class="bi bi-exclamation-triangle"></i>
                <?php else: ?>
                  <i class="bi bi-info-circle"></i>
                <?php endif; ?>
              </div>
              <div class="alert-text"><?= h($a['text']) ?></div>
            </div>
          <?php endforeach; ?>
        </div>
      </div>
    </div>

  </div>
</div>
