<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

/**
 * MODERN HASTA DETAY SAYFASI
 * Ferah, temiz ve profesyonel tasarım
 */

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

$root = realpath(__DIR__ . '/..');
if (!$root) { $root = realpath(__DIR__); }

/* Auth resolve */
$authCandidates = [
  $root . '/auth.php',
  $root . '/core/auth.php',
  $root . '/includes/auth.php',
  $root . '/inc/auth.php',
  $root . '/middleware/auth.php',
];
$authLoaded = false;
foreach ($authCandidates as $p) {
  if (is_file($p)) { require_once $p; $authLoaded = true; break; }
}
if (!$authLoaded) {
  $uid = (int)($_SESSION['user_id'] ?? 0);
  if ($uid <= 0) { header('Location: login.php'); exit; }
}

/* DB include */
$dbCandidates = [
  $root . '/core/db.php',
  $root . '/db.php',
  $root . '/config/db.php',
  $root . '/includes/db.php',
];
$dbLoaded = false;
foreach ($dbCandidates as $p) {
  if (is_file($p)) { require_once $p; $dbLoaded = true; break; }
}
if (!$dbLoaded) {
  http_response_code(500);
  exit("DB include bulunamadı. core/db.php yolunu kontrol et.");
}

/* PDO resolve */
global $pdo, $db, $conn;
if (!isset($pdo) || !($pdo instanceof PDO)) {
  if (isset($db) && $db instanceof PDO) $pdo = $db;
  elseif (isset($conn) && $conn instanceof PDO) $pdo = $conn;
}
if (!isset($pdo) || !($pdo instanceof PDO)) {
  http_response_code(500);
  exit("PDO bulunamadı. core/db.php içinde \$pdo tanımlı olmalı.");
}

/* Helpers */
if (!function_exists('e')) {
  function e(?string $s): string { return htmlspecialchars((string)($s ?? ''), ENT_QUOTES, 'UTF-8'); }
}
function table_exists(PDO $pdo, string $table): bool {
  $st = $pdo->prepare("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?");
  $st->execute([$table]);
  return (int)$st->fetchColumn() > 0;
}
function column_exists(PDO $pdo, string $table, string $col): bool {
  $st = $pdo->prepare("SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?");
  $st->execute([$table, $col]);
  return (int)$st->fetchColumn() > 0;
}
function pick_col(PDO $pdo, string $table, array $candidates): ?string {
  foreach ($candidates as $c) {
    if (column_exists($pdo, $table, $c)) return $c;
  }
  return null;
}
function age_from_date(?string $date): string {
  if (!$date) return '-';
  $d = date_create($date);
  if (!$d) return '-';
  $now = new DateTime();
  return (string)$now->diff($d)->y;
}
function badge_class(string $durum): string {
  return match($durum){
    'aktif' => 'status-active',
    'pasif' => 'status-inactive',
    'nakil' => 'status-transfer',
    'vefat' => 'status-deceased',
    'arsiv' => 'status-archive',
    default => 'status-unknown',
  };
}
function format_phone($phone): string {
  if (!$phone) return '-';
  $phone = preg_replace('/\D/', '', $phone);
  if (strlen($phone) === 10) {
    return sprintf('(%s) %s %s %s', 
      substr($phone, 0, 3),
      substr($phone, 3, 3),
      substr($phone, 6, 2),
      substr($phone, 8, 2)
    );
  }
  if (strlen($phone) === 11) {
    return sprintf('%s %s %s %s', 
      substr($phone, 0, 4),
      substr($phone, 4, 3),
      substr($phone, 7, 2),
      substr($phone, 9, 2)
    );
  }
  return $phone;
}

/* Input */
$hasta_id = (int)($_GET['id'] ?? 0);
if ($hasta_id <= 0) { http_response_code(400); exit("Geçersiz hasta."); }

/* Tables */
$hasAdres   = table_exists($pdo, 'hasta_adresleri');
$hasYakin   = table_exists($pdo, 'hasta_yakinlari');
$hasSigorta = table_exists($pdo, 'hasta_sigorta');
$hasKlinik  = table_exists($pdo, 'hasta_klinik_ozet');
$hasDiyaliz = table_exists($pdo, 'hasta_diyaliz');
$hasDosya   = table_exists($pdo, 'hasta_dosyalar');
$hasDokuman = table_exists($pdo, 'hasta_dokumanlar');
$hasSgkTip  = table_exists($pdo, 'sgk_tipleri');
$hasKurum   = table_exists($pdo, 'kurumlar');
$hasDamar   = table_exists($pdo, 'damar_yolu_tanimlari');

/* SIGORTA kolon eşlemesi */
$sig_table = 'hasta_sigorta';
$col_sgk_tipi   = $hasSigorta ? pick_col($pdo, $sig_table, ['sgk_tipi_id','sgk_tip_id']) : null;
$col_kurum      = $hasSigorta ? pick_col($pdo, $sig_table, ['kurum_id']) : null;
$col_sicil      = $hasSigorta ? pick_col($pdo, $sig_table, ['sicil_no','sgk_sicil_no']) : null;
$col_provizyon  = $hasSigorta ? pick_col($pdo, $sig_table, ['provizyon_no']) : null;
$col_baslangic  = $hasSigorta ? pick_col($pdo, $sig_table, ['baslangic_tarihi']) : null;
$col_bitis      = $hasSigorta ? pick_col($pdo, $sig_table, ['bitis_tarihi']) : null;
$col_sigorta_not = $hasSigorta ? pick_col($pdo, $sig_table, ['notlar','aciklama','not']) : null;

$HS = function(?string $col, string $alias): string {
  if ($col) return "hs.`{$col}` AS {$alias}";
  return "NULL AS {$alias}";
};

/* Address columns (default) */
$addr_table = 'hasta_adresleri';
$col_il        = $hasAdres ? pick_col($pdo, $addr_table, ['il']) : null;
$col_ilce      = $hasAdres ? pick_col($pdo, $addr_table, ['ilce']) : null;
$col_mahalle   = $hasAdres ? pick_col($pdo, $addr_table, ['mahalle']) : null;
$col_posta     = $hasAdres ? pick_col($pdo, $addr_table, ['posta_kodu','postakodu']) : null;
$col_acikadres = $hasAdres ? pick_col($pdo, $addr_table, ['acik_adres','adres']) : null;
$col_adrestip  = $hasAdres ? pick_col($pdo, $addr_table, ['adres_tip']) : null;

$A = function(?string $col, string $alias): string {
  if ($col) return "a.`{$col}` AS {$alias}";
  return "NULL AS {$alias}";
};

/* Main query */
$sql = "
SELECT
  h.*,

  " . ($hasAdres
      ? ($A($col_il,'adres_il') . "," . $A($col_ilce,'adres_ilce') . "," . $A($col_mahalle,'adres_mahalle') . "," . $A($col_posta,'adres_posta') . "," . $A($col_acikadres,'adres_acik') . "," . $A($col_adrestip,'adres_tip'))
      : "NULL AS adres_il, NULL AS adres_ilce, NULL AS adres_mahalle, NULL AS adres_posta, NULL AS adres_acik, NULL AS adres_tip"
  ) . ",

  " . ($hasSigorta ? $HS($col_sgk_tipi,'sgk_tipi_id') : "NULL AS sgk_tipi_id") . ",
  " . ($hasSigorta ? $HS($col_kurum,'kurum_id') : "NULL AS kurum_id") . ",
  " . ($hasSigorta ? $HS($col_sicil,'sicil_no') : "NULL AS sicil_no") . ",
  " . ($hasSigorta ? $HS($col_provizyon,'provizyon_no') : "NULL AS provizyon_no") . ",
  " . ($hasSigorta ? $HS($col_baslangic,'baslangic_tarihi') : "NULL AS baslangic_tarihi") . ",
  " . ($hasSigorta ? $HS($col_bitis,'bitis_tarihi') : "NULL AS bitis_tarihi") . ",

  " . ($hasSigorta
      ? ($col_sigorta_not ? "hs.`{$col_sigorta_not}` AS sigorta_not" : "NULL AS sigorta_not")
      : "NULL AS sigorta_not"
  ) . ",

  " . ($hasSgkTip ? "st.ad AS sgk_tipi_ad" : "NULL AS sgk_tipi_ad") . ",
  " . ($hasKurum ? "k.ad AS kurum_ad" : "NULL AS kurum_ad") . "

  " . ($hasDiyaliz ? ",
      d.modality, d.baslangic_tarihi, d.damar_yolu_id,
      " . ($hasDamar ? "dy.ad AS damar_yolu_ad" : "NULL AS damar_yolu_ad") . ",
      d.kuru_agirlik, d.hedef_uf, d.seans_suresi_dk, d.damar_yolu_lokasyon, d.heparin_not, d.ozel_uyarilar
    " : ",
      NULL as modality, NULL as diyaliz_baslangic, NULL as damar_yolu_id,
      NULL as damar_yolu_ad, NULL as kuru_agirlik, NULL as hedef_uf, NULL as seans_suresi_dk, NULL as damar_lokasyon, NULL as heparin_not, NULL as ozel_uyarilar
    ") . "

FROM hastalar h
" . ($hasAdres ? "LEFT JOIN hasta_adresleri a ON a.hasta_id=h.id AND a.is_default=1" : "") . "
" . ($hasSigorta ? "LEFT JOIN hasta_sigorta hs ON hs.hasta_id=h.id" : "") . "
" . ($hasSgkTip && $hasSigorta && $col_sgk_tipi ? "LEFT JOIN sgk_tipleri st ON st.id = hs.`{$col_sgk_tipi}`" : "") . "
" . ($hasKurum  && $hasSigorta && $col_kurum ? "LEFT JOIN kurumlar k ON k.id = hs.`{$col_kurum}`" : "") . "
" . ($hasDiyaliz ? "LEFT JOIN hasta_diyaliz d ON d.hasta_id=h.id" : "") . "
" . ($hasDamar && $hasDiyaliz ? "LEFT JOIN damar_yolu_tanimlari dy ON dy.id = d.damar_yolu_id" : "") . "
WHERE h.id=?
LIMIT 1
";

$st = $pdo->prepare($sql);
$st->execute([$hasta_id]);
$hasta = $st->fetch(PDO::FETCH_ASSOC);
if (!$hasta) { http_response_code(404); exit("Hasta bulunamadı."); }

/* Lists */
$adresler = [];
if ($hasAdres) {
  $st = $pdo->prepare("SELECT * FROM hasta_adresleri WHERE hasta_id=? ORDER BY is_default DESC, id DESC");
  $st->execute([$hasta_id]);
  $adresler = $st->fetchAll(PDO::FETCH_ASSOC) ?: [];
}

$yakinlar = [];
if ($hasYakin) {
  $st = $pdo->prepare("SELECT * FROM hasta_yakinlari WHERE hasta_id=? ORDER BY is_acil DESC, id DESC");
  $st->execute([$hasta_id]);
  $yakinlar = $st->fetchAll(PDO::FETCH_ASSOC) ?: [];
}

$klinik = null;
if ($hasKlinik) {
  $st = $pdo->prepare("SELECT * FROM hasta_klinik_ozet WHERE hasta_id=? LIMIT 1");
  $st->execute([$hasta_id]);
  $klinik = $st->fetch(PDO::FETCH_ASSOC) ?: null;
}

$dosyalar = [];
$fileTable = null;
if ($hasDosya) {
  $fileTable = 'hasta_dosyalar';
  $st = $pdo->prepare("SELECT * FROM hasta_dosyalar WHERE hasta_id=? ORDER BY id DESC");
  $st->execute([$hasta_id]);
  $dosyalar = $st->fetchAll(PDO::FETCH_ASSOC) ?: [];
} elseif ($hasDokuman) {
  $fileTable = 'hasta_dokumanlar';
  $st = $pdo->prepare("SELECT * FROM hasta_dokumanlar WHERE hasta_id=? ORDER BY id DESC");
  $st->execute([$hasta_id]);
  $dosyalar = $st->fetchAll(PDO::FETCH_ASSOC) ?: [];
}

/* File columns */
$file_path_col = $fileTable ? pick_col($pdo, $fileTable, ['dosya_yolu','file_path','path','url','dosya','dosya_url']) : null;
$file_name_col = $fileTable ? pick_col($pdo, $fileTable, ['dosya_adi','file_name','name','orjinal_ad','orijinal_ad']) : null;
$file_desc_col = $fileTable ? pick_col($pdo, $fileTable, ['aciklama','description','not','notlar']) : null;
$file_date_col = $fileTable ? pick_col($pdo, $fileTable, ['created_at','olusturma_tarihi','tarih','createdOn']) : null;

/* Flash */
$flash_ok  = $_SESSION['flash_ok']  ?? null;
$flash_err = $_SESSION['flash_err'] ?? null;
unset($_SESSION['flash_ok'], $_SESSION['flash_err']);

/* TOPBAR include */
$current_page = 'hastaListe';
$topbarPath = null;
$topbarCandidates = [
  __DIR__ . '/topbar.php',
  $root . '/topbar.php',
  $root . '/includes/topbar.php',
  $root . '/partials/topbar.php',
  $root . '/layout/topbar.php',
  $root . '/components/topbar.php',
];
foreach ($topbarCandidates as $p) {
  if (is_file($p)) { $topbarPath = $p; break; }
}

/* Small helpers for UI */
function first_existing(array $row, array $keys): ?string {
  foreach ($keys as $k) if (array_key_exists($k, $row) && (string)$row[$k] !== '') return (string)$row[$k];
  return null;
}
?>
<!doctype html>
<html lang="tr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title><?= e(($hasta['ad'] ?? '').' '.($hasta['soyad'] ?? '')) ?> | Hasta Detay</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">

  <style>
  :root {
    --primary-color: #2563eb;
    --primary-light: #dbeafe;
    --primary-dark: #1d4ed8;
    --secondary-color: #475569;
    --accent-color: #0ea5e9;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --danger-color: #ef4444;
    --light-color: #f8fafc;
    --dark-color: #1e293b;
    --border-color: #e2e8f0;
    --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    --hover-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --transition: all 0.2s ease-in-out;
    --radius: 8px;
  }
  
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }
  
  body {
    font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
    background-color: #f1f5f9;
    color: #334155;
    line-height: 1.5;
    min-height: 100vh;
  }
  
  a {
    text-decoration: none;
    color: inherit;
  }
  
  .container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;
  }
  
  /* Header */
  .page-header {
    background: white;
    border-bottom: 1px solid var(--border-color);
    padding: 1.5rem 0;
    margin-bottom: 2rem;
  }
  
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
  }
  
  .page-title {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--dark-color);
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }
  
  .page-title i {
    color: var(--primary-color);
  }
  
  .patient-subtitle {
    color: var(--secondary-color);
    font-size: 0.95rem;
    margin-top: 0.25rem;
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: center;
  }
  
  .patient-subtitle span {
    display: flex;
    align-items: center;
    gap: 0.25rem;
  }
  
  .patient-subtitle i {
    font-size: 0.875rem;
  }
  
  .header-actions {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
  }
  
  .btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.625rem 1.25rem;
    border-radius: var(--radius);
    font-weight: 500;
    font-size: 0.875rem;
    cursor: pointer;
    transition: var(--transition);
    border: 1px solid transparent;
  }
  
  .btn-outline {
    background: white;
    border-color: var(--border-color);
    color: var(--secondary-color);
  }
  
  .btn-outline:hover {
    background: #f8fafc;
    border-color: #cbd5e1;
  }
  
  .btn-primary {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
  }
  
  .btn-primary:hover {
    background: var(--primary-dark);
    box-shadow: var(--hover-shadow);
  }
  
  /* Alert */
  .alert {
    padding: 1rem;
    border-radius: var(--radius);
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }
  
  .alert-success {
    background-color: #d1fae5;
    color: #065f46;
    border-left: 4px solid var(--success-color);
  }
  
  .alert-error {
    background-color: #fee2e2;
    color: #991b1b;
    border-left: 4px solid var(--danger-color);
  }
  
  /* Status badges */
  .status-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 500;
    text-transform: capitalize;
  }
  
  .status-active {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #a7f3d0;
  }
  
  .status-inactive {
    background: #f1f5f9;
    color: #475569;
    border: 1px solid #e2e8f0;
  }
  
  .status-transfer {
    background: #dbeafe;
    color: #1e40af;
    border: 1px solid #bfdbfe;
  }
  
  .status-deceased {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fecaca;
  }
  
  .status-archive {
    background: #fef3c7;
    color: #92400e;
    border: 1px solid #fde68a;
  }
  
  /* Tab Navigation */
  .tab-navigation {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 2rem;
    overflow-x: auto;
    padding-bottom: 0.5rem;
  }
  
  .tab-button {
    padding: 0.75rem 1.25rem;
    background: white;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    font-weight: 500;
    font-size: 0.875rem;
    color: var(--secondary-color);
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
  }
  
  .tab-button:hover {
    background: #f8fafc;
    border-color: #cbd5e1;
  }
  
  .tab-button.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
  }
  
  /* Main Layout */
  .main-layout {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: 1.5rem;
  }
  
  @media (max-width: 1024px) {
    .main-layout {
      grid-template-columns: 1fr;
    }
  }
  
  /* Content Sections */
  .content-section {
    display: none;
  }
  
  .content-section.active {
    display: block;
    animation: fadeIn 0.3s ease-in-out;
  }
  
  @keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
  }
  
  /* Cards */
  .card {
    background: white;
    border-radius: var(--radius);
    box-shadow: var(--card-shadow);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
  }
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 1rem;
  }
  
  .card-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--dark-color);
  }
  
  .card-subtitle {
    color: var(--secondary-color);
    font-size: 0.875rem;
    margin-top: 0.25rem;
  }
  
  /* Grid System */
  .grid {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: 1rem;
  }
  
  .col-12 { grid-column: span 12; }
  .col-6 { grid-column: span 6; }
  .col-4 { grid-column: span 4; }
  .col-3 { grid-column: span 3; }
  
  @media (max-width: 768px) {
    .col-6, .col-4, .col-3 {
      grid-column: span 12;
    }
  }
  
  /* Info Boxes */
  .info-box {
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 1rem;
  }
  
  .info-label {
    font-size: 0.75rem;
    color: var(--secondary-color);
    font-weight: 500;
    margin-bottom: 0.25rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }
  
  .info-value {
    font-size: 1rem;
    font-weight: 600;
    color: var(--dark-color);
  }
  
  /* List Items */
  .list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }
  
  .list-item {
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 1rem;
  }
  
  .list-item-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 0.5rem;
  }
  
  .list-item-title {
    font-weight: 600;
    color: var(--dark-color);
    font-size: 1rem;
  }
  
  .list-item-subtitle {
    color: var(--secondary-color);
    font-size: 0.875rem;
    margin-top: 0.25rem;
  }
  
  .list-item-content {
    color: var(--secondary-color);
    font-size: 0.875rem;
    line-height: 1.4;
  }
  
  /* Sidebar */
  .sidebar {
    position: sticky;
    top: 1.5rem;
  }
  
  @media (max-width: 1024px) {
    .sidebar {
      position: static;
    }
  }
  
  /* Quick Stats */
  .quick-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 1.5rem;
  }
  
  .stat-box {
    background: white;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 1rem;
    text-align: center;
  }
  
  .stat-label {
    font-size: 0.75rem;
    color: var(--secondary-color);
    font-weight: 500;
    margin-bottom: 0.25rem;
  }
  
  .stat-value {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--dark-color);
  }
  
  /* Action Buttons */
  .action-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-top: 1.5rem;
  }
  
  .action-buttons .btn {
    justify-content: center;
    width: 100%;
  }
  
  /* Divider */
  .divider {
    height: 1px;
    background: var(--border-color);
    margin: 1.5rem 0;
  }
  
  /* Empty State */
  .empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--secondary-color);
  }
  
  .empty-state i {
    font-size: 3rem;
    color: #cbd5e1;
    margin-bottom: 1rem;
  }
  
  .empty-state h3 {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
  }
  
  .empty-state p {
    font-size: 0.875rem;
  }
  
  /* File Items */
  .file-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1rem;
    background: #f8fafc;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
  }
  
  .file-icon {
    width: 40px;
    height: 40px;
    background: var(--primary-light);
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary-color);
  }
  
  .file-info {
    flex: 1;
  }
  
  .file-name {
    font-weight: 600;
    color: var(--dark-color);
    margin-bottom: 0.25rem;
  }
  
  .file-meta {
    font-size: 0.75rem;
    color: var(--secondary-color);
  }
  
  /* Utility Classes */
  .text-muted {
    color: var(--secondary-color);
  }
  
  .text-small {
    font-size: 0.875rem;
  }
  
  .mb-1 { margin-bottom: 0.5rem; }
  .mb-2 { margin-bottom: 1rem; }
  .mb-3 { margin-bottom: 1.5rem; }
  .mt-1 { margin-top: 0.5rem; }
  .mt-2 { margin-top: 1rem; }
  .mt-3 { margin-top: 1.5rem; }
  
  .d-flex {
    display: flex;
  }
  
  .align-center {
    align-items: center;
  }
  
  .justify-between {
    justify-content: space-between;
  }
  
  .gap-1 {
    gap: 0.5rem;
  }
  
  .gap-2 {
    gap: 1rem;
  }
  </style>
</head>

<body>
  
  <!-- TOPBAR -->
  <?php if ($topbarPath): ?>
    <?php include $topbarPath; ?>
  <?php endif; ?>
  
  <div class="page-header">
    <div class="container">
      <div class="header-content">
        <div>
          <h1 class="page-title">
            <i class="bi bi-person-circle"></i> <?= e(($hasta['ad'] ?? '').' '.($hasta['soyad'] ?? '')) ?>
            <span class="status-badge <?= e(badge_class((string)($hasta['durum'] ?? ''))) ?>">
              <?= e(ucfirst((string)($hasta['durum'] ?? '-'))) ?>
            </span>
          </h1>
          <div class="patient-subtitle">
            <span><i class="bi bi-file-text"></i> Dosya: <b><?= e((string)($hasta['dosya_no'] ?? '-') ?: '-') ?></b></span>
            <span><i class="bi bi-credit-card"></i> TC: <b><?= e(substr((string)($hasta['tc_no'] ?? ''), -4) ?: '-') ?></b></span>
            <span><i class="bi bi-calendar3"></i> Yaş: <b><?= e(age_from_date($hasta['dogum_tarihi'] ?? null)) ?></b></span>
            <span><i class="bi bi-telephone"></i> <b><?= e(format_phone($hasta['telefon1'] ?? '')) ?></b></span>
          </div>
        </div>
        <div class="header-actions">
          <a href="hastaListe.php" class="btn btn-outline">
            <i class="bi bi-arrow-left"></i> Listeye Dön
          </a>
          <a href="hastaDuzenle.php?id=<?= (int)$hasta_id ?>" class="btn btn-primary">
            <i class="bi bi-pencil-square"></i> Bilgileri Düzenle
          </a>
        </div>
      </div>
    </div>
  </div>
  
  <div class="container">
    <?php if ($flash_ok): ?>
      <div class="alert alert-success">
        <i class="bi bi-check-circle-fill"></i> <?= e($flash_ok) ?>
      </div>
    <?php endif; ?>
    
    <?php if ($flash_err): ?>
      <div class="alert alert-error">
        <i class="bi bi-exclamation-triangle-fill"></i> <?= e($flash_err) ?>
      </div>
    <?php endif; ?>
    
    <!-- Tab Navigation -->
    <div class="tab-navigation">
      <button class="tab-button active" data-tab="genel">Genel Bilgiler</button>
      <button class="tab-button" data-tab="iletisim">İletişim</button>
      <button class="tab-button" data-tab="yakin">Yakınlar</button>
      <button class="tab-button" data-tab="sigorta">Sigorta</button>
      <button class="tab-button" data-tab="klinik">Klinik</button>
      <button class="tab-button" data-tab="diyaliz">Diyaliz</button>
      <button class="tab-button" data-tab="belgeler">Belgeler</button>
    </div>
    
    <div class="main-layout">
      <!-- Main Content -->
      <div class="main-content">
        
        <!-- General Information -->
        <section class="content-section active" id="section-genel">
          <div class="card">
            <div class="card-header">
              <div>
                <h2 class="card-title">Genel Bilgiler</h2>
                <p class="card-subtitle">Hastanın temel kimlik ve iletişim bilgileri</p>
              </div>
            </div>
            
            <div class="grid">
              <div class="info-box col-6">
                <div class="info-label">Ad</div>
                <div class="info-value"><?= e((string)($hasta['ad'] ?? '-')) ?></div>
              </div>
              
              <div class="info-box col-6">
                <div class="info-label">Soyad</div>
                <div class="info-value"><?= e((string)($hasta['soyad'] ?? '-')) ?></div>
              </div>
              
              <div class="info-box col-4">
                <div class="info-label">Cinsiyet</div>
                <div class="info-value"><?= e((string)($hasta['cinsiyet'] ?? '-')) ?></div>
              </div>
              
              <div class="info-box col-4">
                <div class="info-label">Doğum Tarihi</div>
                <div class="info-value"><?= e((string)($hasta['dogum_tarihi'] ?? '-') ?: '-') ?></div>
              </div>
              
              <div class="info-box col-4">
                <div class="info-label">Yaş</div>
                <div class="info-value"><?= e(age_from_date($hasta['dogum_tarihi'] ?? null)) ?></div>
              </div>
              
              <div class="info-box col-6">
                <div class="info-label">Telefon 1</div>
                <div class="info-value"><?= e(format_phone($hasta['telefon1'] ?? '')) ?></div>
              </div>
              
              <div class="info-box col-6">
                <div class="info-label">Telefon 2</div>
                <div class="info-value"><?= e(format_phone($hasta['telefon2'] ?? '')) ?></div>
              </div>
              
              <div class="info-box col-12">
                <div class="info-label">E-posta</div>
                <div class="info-value"><?= e((string)($hasta['email'] ?? '-') ?: '-') ?></div>
              </div>
              
              <div class="info-box col-12">
                <div class="info-label">Varsayılan Adres</div>
                <div class="info-value">
                  <?php
                    $addr = trim((string)($hasta['adres_acik'] ?? ''));
                    $m    = trim((string)($hasta['adres_mahalle'] ?? ''));
                    $ilce = trim((string)($hasta['adres_ilce'] ?? ''));
                    $il   = trim((string)($hasta['adres_il'] ?? ''));
                    $full = trim($addr . ' ' . $m . ' ' . $ilce . ' / ' . $il);
                    echo e($full ?: '-');
                  ?>
                </div>
              </div>
            </div>
          </div>
        </section>
        
        <!-- Communication -->
        <section class="content-section" id="section-iletisim">
          <div class="card">
            <div class="card-header">
              <div>
                <h2 class="card-title">İletişim Bilgileri</h2>
                <p class="card-subtitle">Telefonlar ve adres kayıtları</p>
              </div>
              <a href="hastaDuzenle.php?id=<?= (int)$hasta_id ?>#iletisim" class="btn btn-primary">
                <i class="bi bi-pencil-square"></i> Düzenle
              </a>
            </div>
            
            <div class="grid mb-3">
              <div class="info-box col-6">
                <div class="info-label">Telefon 1</div>
                <div class="info-value"><?= e(format_phone($hasta['telefon1'] ?? '')) ?></div>
              </div>
              
              <div class="info-box col-6">
                <div class="info-label">Telefon 2</div>
                <div class="info-value"><?= e(format_phone($hasta['telefon2'] ?? '')) ?></div>
              </div>
              
              <div class="info-box col-12">
                <div class="info-label">E-posta</div>
                <div class="info-value"><?= e((string)($hasta['email'] ?? '-') ?: '-') ?></div>
              </div>
            </div>
            
            <div class="divider"></div>
            
            <h3 class="mb-2">Adresler</h3>
            
            <?php if (!$hasAdres): ?>
              <div class="empty-state">
                <i class="bi bi-geo-alt"></i>
                <h3>Adres Tablosu Bulunamadı</h3>
                <p>hasta_adresleri tablosu sistemde tanımlı değil.</p>
              </div>
            <?php elseif (!$adresler): ?>
              <div class="empty-state">
                <i class="bi bi-geo-alt"></i>
                <h3>Adres Kaydı Bulunamadı</h3>
                <p>Bu hasta için kayıtlı adres bulunmuyor.</p>
              </div>
            <?php else: ?>
              <div class="list">
                <?php foreach ($adresler as $a): ?>
                  <?php
                    $tip = first_existing($a, ['adres_tip','tip']) ?? '-';
                    $il  = first_existing($a, ['il']) ?? '';
                    $ilce= first_existing($a, ['ilce']) ?? '';
                    $mah = first_existing($a, ['mahalle']) ?? '';
                    $pk  = first_existing($a, ['posta_kodu','postakodu']) ?? '';
                    $acik= first_existing($a, ['acik_adres','adres']) ?? '';
                    $isDef = (int)($a['is_default'] ?? 0) === 1;
                  ?>
                  <div class="list-item">
                    <div class="list-item-header">
                      <div class="list-item-title">
                        <?= e(ucfirst($tip)) ?> Adresi
                        <?php if ($isDef): ?>
                          <span class="status-badge status-transfer" style="margin-left: 0.5rem;">Varsayılan</span>
                        <?php endif; ?>
                      </div>
                    </div>
                    <div class="list-item-content">
                      <?php if ($acik): ?>
                        <div><?= e($acik) ?></div>
                      <?php endif; ?>
                      <?php if ($mah || $ilce || $il): ?>
                        <div class="text-muted text-small">
                          <?= trim(e($mah . ' ' . $ilce)) ?>
                          <?php if ($il): ?>
                            / <?= e($il) ?>
                          <?php endif; ?>
                          <?php if ($pk): ?>
                            (<?= e($pk) ?>)
                          <?php endif; ?>
                        </div>
                      <?php endif; ?>
                    </div>
                  </div>
                <?php endforeach; ?>
              </div>
            <?php endif; ?>
          </div>
        </section>
        
        <!-- Relatives -->
        <section class="content-section" id="section-yakin">
          <div class="card">
            <div class="card-header">
              <div>
                <h2 class="card-title">Yakınlar / Acil Durum</h2>
                <p class="card-subtitle">Acil durumda ulaşılacak kişiler</p>
              </div>
              <a href="hastaDuzenle.php?id=<?= (int)$hasta_id ?>#yakin" class="btn btn-primary">
                <i class="bi bi-pencil-square"></i> Düzenle
              </a>
            </div>
            
            <?php if (!$hasYakin): ?>
              <div class="empty-state">
                <i class="bi bi-people"></i>
                <h3>Yakınlar Tablosu Bulunamadı</h3>
                <p>hasta_yakinlari tablosu sistemde tanımlı değil.</p>
              </div>
            <?php elseif (!$yakinlar): ?>
              <div class="empty-state">
                <i class="bi bi-people"></i>
                <h3>Yakın Kaydı Bulunamadı</h3>
                <p>Bu hasta için kayıtlı yakın bulunmuyor.</p>
              </div>
            <?php else: ?>
              <div class="list">
                <?php foreach ($yakinlar as $y): ?>
                  <?php
                    $adsoy = first_existing($y, ['ad_soyad','yakin_ad_soyad','adsoyad','ad']) ?? '-';
                    $yak   = first_existing($y, ['yakinlik','iliski','relation']) ?? '-';
                    $t1    = first_existing($y, ['telefon','telefon1','yakin_telefon']) ?? '';
                    $t2    = first_existing($y, ['telefon2','yakin_telefon2']) ?? '';
                    $mail  = first_existing($y, ['email','yakin_email']) ?? '';
                    $not   = first_existing($y, ['not','notlar','aciklama','yakin_not']) ?? '';
                    $isAcil= (int)($y['is_acil'] ?? 0) === 1 || (int)($y['yakin_is_acil'] ?? 0) === 1;
                    $isVasi= (int)($y['is_vasi'] ?? 0) === 1 || (int)($y['yakin_is_vasi'] ?? 0) === 1;
                  ?>
                  <div class="list-item">
                    <div class="list-item-header">
                      <div class="list-item-title"><?= e($adsoy) ?></div>
                      <div class="d-flex gap-1">
                        <?php if ($isAcil): ?>
                          <span class="status-badge status-active">Acil</span>
                        <?php endif; ?>
                        <?php if ($isVasi): ?>
                          <span class="status-badge status-deceased">Vasi</span>
                        <?php endif; ?>
                      </div>
                    </div>
                    <div class="list-item-subtitle"><?= e(ucfirst($yak)) ?></div>
                    <div class="list-item-content">
                      <div class="d-flex flex-wrap gap-2 mb-1">
                        <?php if ($t1): ?>
                          <span><i class="bi bi-telephone"></i> <?= e(format_phone($t1)) ?></span>
                        <?php endif; ?>
                        <?php if ($t2): ?>
                          <span><i class="bi bi-telephone-plus"></i> <?= e(format_phone($t2)) ?></span>
                        <?php endif; ?>
                        <?php if ($mail): ?>
                          <span><i class="bi bi-envelope"></i> <?= e($mail) ?></span>
                        <?php endif; ?>
                      </div>
                      <?php if ($not): ?>
                        <div class="text-muted text-small"><?= e($not) ?></div>
                      <?php endif; ?>
                    </div>
                  </div>
                <?php endforeach; ?>
              </div>
            <?php endif; ?>
          </div>
        </section>
        
        <!-- Insurance -->
        <section class="content-section" id="section-sigorta">
          <div class="card">
            <div class="card-header">
              <div>
                <h2 class="card-title">Sigorta Bilgileri</h2>
                <p class="card-subtitle">SGK ve kurum bilgileri</p>
              </div>
              <a href="hastaDuzenle.php?id=<?= (int)$hasta_id ?>#sigorta" class="btn btn-primary">
                <i class="bi bi-pencil-square"></i> Düzenle
              </a>
            </div>
            
            <?php if(!$hasSigorta): ?>
              <div class="empty-state">
                <i class="bi bi-shield-check"></i>
                <h3>Sigorta Tablosu Bulunamadı</h3>
                <p>hasta_sigorta tablosu sistemde tanımlı değil.</p>
              </div>
            <?php else: ?>
              <div class="grid">
                <div class="info-box col-6">
                  <div class="info-label">SGK Tipi</div>
                  <div class="info-value"><?= e((string)($hasta['sgk_tipi_ad'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-6">
                  <div class="info-label">Kurum</div>
                  <div class="info-value"><?= e((string)($hasta['kurum_ad'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-4">
                  <div class="info-label">Sicil No</div>
                  <div class="info-value"><?= e((string)($hasta['sicil_no'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-4">
                  <div class="info-label">Provizyon No</div>
                  <div class="info-value"><?= e((string)($hasta['provizyon_no'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-4">
                  <div class="info-label">Sigorta Not</div>
                  <div class="info-value"><?= e((string)($hasta['sigorta_not'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-6">
                  <div class="info-label">Başlangıç Tarihi</div>
                  <div class="info-value"><?= e((string)($hasta['baslangic_tarihi'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-6">
                  <div class="info-label">Bitiş Tarihi</div>
                  <div class="info-value"><?= e((string)($hasta['bitis_tarihi'] ?? '-') ?: '-') ?></div>
                </div>
              </div>
            <?php endif; ?>
          </div>
        </section>
        
        <!-- Clinical -->
        <section class="content-section" id="section-klinik">
          <div class="card">
            <div class="card-header">
              <div>
                <h2 class="card-title">Klinik Bilgiler</h2>
                <p class="card-subtitle">Klinik özet ve notlar</p>
              </div>
              <a href="hastaDuzenle.php?id=<?= (int)$hasta_id ?>#klinik" class="btn btn-primary">
                <i class="bi bi-pencil-square"></i> Düzenle
              </a>
            </div>
            
            <?php if (!$hasKlinik): ?>
              <div class="empty-state">
                <i class="bi bi-clipboard-pulse"></i>
                <h3>Klinik Tablosu Bulunamadı</h3>
                <p>hasta_klinik_ozet tablosu sistemde tanımlı değil.</p>
              </div>
            <?php elseif (!$klinik): ?>
              <div class="empty-state">
                <i class="bi bi-clipboard-pulse"></i>
                <h3>Klinik Kaydı Bulunamadı</h3>
                <p>Bu hasta için klinik kaydı bulunmuyor.</p>
              </div>
            <?php else: ?>
              <?php
                $ilaclar = first_existing($klinik, ['ilaclar','medications']) ?? '';
                $serbest = first_existing($klinik, ['serbest_not','not','notlar','aciklama']) ?? '';
              ?>
              
              <?php if ($ilaclar): ?>
                <div class="info-box mb-2">
                  <div class="info-label">İlaçlar</div>
                  <div class="info-value"><?= e($ilaclar) ?></div>
                </div>
              <?php endif; ?>
              
              <?php if ($serbest): ?>
                <div class="info-box mb-3">
                  <div class="info-label">Klinik Not</div>
                  <div class="info-value"><?= e($serbest) ?></div>
                </div>
              <?php endif; ?>
              
              <div class="divider"></div>
              
              <h3 class="mb-2">Diğer Klinik Bilgiler</h3>
              
              <div class="grid">
                <?php 
                  $displayed = ['ilaclar', 'medications', 'serbest_not', 'not', 'notlar', 'aciklama', 'id', 'hasta_id'];
                  foreach ($klinik as $k => $v):
                    if (in_array((string)$k, $displayed, true)) continue;
                    $vv = (string)$v;
                    if ($vv === '') continue;
                ?>
                  <div class="info-box col-6">
                    <div class="info-label"><?= e(ucfirst(str_replace('_', ' ', $k))) ?></div>
                    <div class="info-value"><?= e($vv) ?></div>
                  </div>
                <?php endforeach; ?>
              </div>
            <?php endif; ?>
          </div>
        </section>
        
        <!-- Dialysis -->
        <section class="content-section" id="section-diyaliz">
          <div class="card">
            <div class="card-header">
              <div>
                <h2 class="card-title">Diyaliz Bilgileri</h2>
                <p class="card-subtitle">Tedavi parametreleri ve notlar</p>
              </div>
              <a href="hastaDuzenle.php?id=<?= (int)$hasta_id ?>#diyaliz" class="btn btn-primary">
                <i class="bi bi-pencil-square"></i> Düzenle
              </a>
            </div>
            
            <?php if (!$hasDiyaliz): ?>
              <div class="empty-state">
                <i class="bi bi-droplet"></i>
                <h3>Diyaliz Tablosu Bulunamadı</h3>
                <p>hasta_diyaliz tablosu sistemde tanımlı değil.</p>
              </div>
            <?php else: ?>
              <div class="grid">
                <div class="info-box col-4">
                  <div class="info-label">Başlangıç Tarihi</div>
                  <div class="info-value"><?= e((string)($hasta['diyaliz_baslangic'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-4">
                  <div class="info-label">Modalite</div>
                  <div class="info-value"><?= e((string)($hasta['modality'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-4">
                  <div class="info-label">Damar Yolu</div>
                  <div class="info-value"><?= e((string)($hasta['damar_yolu_ad'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-3">
                  <div class="info-label">Kuru Ağırlık</div>
                  <div class="info-value"><?= e((string)($hasta['kuru_agirlik'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-3">
                  <div class="info-label">Hedef UF</div>
                  <div class="info-value"><?= e((string)($hasta['hedef_uf'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-3">
                  <div class="info-label">Seans Süresi</div>
                  <div class="info-value"><?= e((string)($hasta['seans_suresi_dk'] ?? '-') ?: '-') ?></div>
                </div>
                
                <div class="info-box col-3">
                  <div class="info-label">Damar Lokasyon</div>
                  <div class="info-value"><?= e((string)($hasta['damar_lokasyon'] ?? '-') ?: '-') ?></div>
                </div>
                
                <?php if ($hasta['heparin_not']): ?>
                  <div class="info-box col-12">
                    <div class="info-label">Heparin Notu</div>
                    <div class="info-value"><?= e((string)($hasta['heparin_not'] ?? '-')) ?></div>
                  </div>
                <?php endif; ?>
                
                <?php if ($hasta['ozel_uyarilar']): ?>
                  <div class="info-box col-12">
                    <div class="info-label">Özel Uyarılar</div>
                    <div class="info-value"><?= e((string)($hasta['ozel_uyarilar'] ?? '-')) ?></div>
                  </div>
                <?php endif; ?>
              </div>
            <?php endif; ?>
          </div>
        </section>
        
        <!-- Documents -->
        <section class="content-section" id="section-belgeler">
          <div class="card">
            <div class="card-header">
              <div>
                <h2 class="card-title">Belgeler</h2>
                <p class="card-subtitle">Hasta dosyaları ve dokümanlar</p>
              </div>
              <a href="hastaDuzenle.php?id=<?= (int)$hasta_id ?>#belgeler" class="btn btn-primary">
                <i class="bi bi-upload"></i> Belge Yönetimi
              </a>
            </div>
            
            <?php if (!$fileTable): ?>
              <div class="empty-state">
                <i class="bi bi-folder"></i>
                <h3>Belge Tablosu Bulunamadı</h3>
                <p>hasta_dosyalar veya hasta_dokumanlar tablosu sistemde tanımlı değil.</p>
              </div>
            <?php elseif (!$dosyalar): ?>
              <div class="empty-state">
                <i class="bi bi-folder"></i>
                <h3>Belge Bulunamadı</h3>
                <p>Bu hasta için kayıtlı belge bulunmuyor.</p>
              </div>
            <?php else: ?>
              <div class="list">
                <?php foreach ($dosyalar as $f): ?>
                  <?php
                    $p = $file_path_col ? (string)($f[$file_path_col] ?? '') : '';
                    $n = $file_name_col ? (string)($f[$file_name_col] ?? '') : '';
                    $d = $file_desc_col ? (string)($f[$file_desc_col] ?? '') : '';
                    $t = $file_date_col ? (string)($f[$file_date_col] ?? '') : '';
                    $label = $n ?: ($p ? basename($p) : 'Belge');
                    
                    // Determine file type icon
                    $ext = strtolower(pathinfo($label, PATHINFO_EXTENSION));
                    $icon = 'bi-file-earmark';
                    if (in_array($ext, ['pdf'])) $icon = 'bi-file-earmark-pdf';
                    elseif (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) $icon = 'bi-file-earmark-image';
                    elseif (in_array($ext, ['doc', 'docx'])) $icon = 'bi-file-earmark-word';
                    elseif (in_array($ext, ['xls', 'xlsx'])) $icon = 'bi-file-earmark-excel';
                  ?>
                  <div class="file-item">
                    <div class="file-icon">
                      <i class="bi <?= $icon ?>"></i>
                    </div>
                    <div class="file-info">
                      <div class="file-name"><?= e($label) ?></div>
                      <div class="file-meta">
                        <?php if ($d): ?>
                          <span><?= e($d) ?></span>
                        <?php endif; ?>
                        <?php if ($t): ?>
                          <span> • <?= e($t) ?></span>
                        <?php endif; ?>
                      </div>
                    </div>
                    <?php if ($p): ?>
                      <a href="<?= e($p) ?>" target="_blank" rel="noopener" class="btn btn-outline">
                        <i class="bi bi-box-arrow-up-right"></i> Aç
                      </a>
                    <?php else: ?>
                      <span class="status-badge status-inactive">Yol yok</span>
                    <?php endif; ?>
                  </div>
                <?php endforeach; ?>
              </div>
            <?php endif; ?>
          </div>
        </section>
      </div>
      
      <!-- Sidebar -->
      <div class="sidebar">
        <div class="card">
          <h3 class="card-title mb-2">Hızlı Özet</h3>
          
          <div class="quick-stats">
            <div class="stat-box">
              <div class="stat-label">Yaş</div>
              <div class="stat-value"><?= e(age_from_date($hasta['dogum_tarihi'] ?? null)) ?></div>
            </div>
            
            <div class="stat-box">
              <div class="stat-label">Durum</div>
              <div class="stat-value"><?= e(ucfirst((string)($hasta['durum'] ?? '-'))) ?></div>
            </div>
            
            <div class="stat-box">
              <div class="stat-label">Modalite</div>
              <div class="stat-value"><?= e((string)($hasta['modality'] ?? '-') ?: '-') ?></div>
            </div>
            
            <div class="stat-box">
              <div class="stat-label">SGK Tipi</div>
              <div class="stat-value"><?= e((string)($hasta['sgk_tipi_ad'] ?? '-') ?: '-') ?></div>
            </div>
          </div>
          
          <div class="action-buttons">
            <a href="hastaDuzenle.php?id=<?= (int)$hasta_id ?>" class="btn btn-primary">
              <i class="bi bi-pencil-square"></i> Bilgileri Düzenle
            </a>
            <a href="hastaListe.php" class="btn btn-outline">
              <i class="bi bi-arrow-left"></i> Listeye Dön
            </a>
          </div>
          
          <div class="divider"></div>
          
          <div class="text-muted text-small">
            <p><i class="bi bi-info-circle"></i> Hasta bilgilerini düzenlemek için "Bilgileri Düzenle" butonuna tıklayın.</p>
          </div>
        </div>
        
        <div class="card mt-2">
          <h3 class="card-title mb-2">İletişim</h3>
          
          <div class="list-item-content">
            <div class="mb-1">
              <div class="info-label">Telefon 1</div>
              <div class="info-value"><?= e(format_phone($hasta['telefon1'] ?? '')) ?></div>
            </div>
            
            <?php if ($hasta['telefon2']): ?>
              <div class="mb-1">
                <div class="info-label">Telefon 2</div>
                <div class="info-value"><?= e(format_phone($hasta['telefon2'] ?? '')) ?></div>
              </div>
            <?php endif; ?>
            
            <?php if ($hasta['email']): ?>
              <div class="mb-1">
                <div class="info-label">E-posta</div>
                <div class="info-value"><?= e($hasta['email']) ?></div>
              </div>
            <?php endif; ?>
            
            <?php if ($hasta['adres_il'] || $hasta['adres_ilce']): ?>
              <div>
                <div class="info-label">Konum</div>
                <div class="info-value">
                  <?= e(trim((string)($hasta['adres_ilce'] ?? '').' / '.(string)($hasta['adres_il'] ?? ''))) ?>
                </div>
              </div>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>
  </div>

<script>
document.addEventListener('DOMContentLoaded', function() {
  // Tab functionality
  const tabButtons = document.querySelectorAll('.tab-button');
  const contentSections = document.querySelectorAll('.content-section');
  
  function switchTab(tabId) {
    // Update tab buttons
    tabButtons.forEach(button => {
      button.classList.toggle('active', button.dataset.tab === tabId);
    });
    
    // Update content sections
    contentSections.forEach(section => {
      section.classList.toggle('active', section.id === 'section-' + tabId);
    });
    
    // Save active tab to session storage
    sessionStorage.setItem('activeTab', tabId);
  }
  
  // Tab click handlers
  tabButtons.forEach(button => {
    button.addEventListener('click', () => {
      switchTab(button.dataset.tab);
    });
  });
  
  // Restore active tab from session storage
  const savedTab = sessionStorage.getItem('activeTab');
  if (savedTab) {
    switchTab(savedTab);
  }
  
  // Add subtle hover effects to cards
  const cards = document.querySelectorAll('.card');
  cards.forEach(card => {
    card.addEventListener('mouseenter', () => {
      card.style.transform = 'translateY(-2px)';
      card.style.transition = 'transform 0.2s ease, box-shadow 0.2s ease';
      card.style.boxShadow = '0 10px 15px -3px rgba(0, 0, 0, 0.1)';
    });
    
    card.addEventListener('mouseleave', () => {
      card.style.transform = 'translateY(0)';
      card.style.boxShadow = '';
    });
  });
  
  // Format phone numbers on the page
  function formatPhoneNumbers() {
    const phoneElements = document.querySelectorAll('.info-value');
    phoneElements.forEach(el => {
      const text = el.textContent.trim();
      if (text.length >= 10 && !text.includes('(')) {
        const phone = text.replace(/\D/g, '');
        if (phone.length === 10) {
          el.textContent = '(' + phone.substring(0, 3) + ') ' + phone.substring(3, 6) + ' ' + 
                           phone.substring(6, 8) + ' ' + phone.substring(8, 10);
        } else if (phone.length === 11) {
          el.textContent = phone.substring(0, 4) + ' ' + phone.substring(4, 7) + ' ' + 
                           phone.substring(7, 9) + ' ' + phone.substring(9, 11);
        }
      }
    });
  }
  
  // Initialize phone formatting
  formatPhoneNumbers();
});
</script>

</body>
</html>