<?php
require_once __DIR__ . '/../../vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..' . '/../');
$dotenv->safeLoad();
require_once __DIR__ . '/../i18n.php';

use App\Auth;
use App\Database;

Auth::init();
Auth::requireRole(['admin','staff']);
$pdo = Database::getConnection();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
  http_response_code(400);
  echo 'Geçersiz hasta';
  exit;
}

// Fetch patient
$st = $pdo->prepare("SELECT p.*, r.doctor_id, r.institution_id, r.referral_reason, r.referral_date,
  d.full_name AS doctor_name, i.name AS institution_name
  FROM patients p
  LEFT JOIN patient_referrals r ON r.patient_id = p.id
  LEFT JOIN doctors d ON d.id = r.doctor_id
  LEFT JOIN institutions i ON i.id = r.institution_id
  WHERE p.id = :id");
$st->execute([':id' => $id]);
$p = $st->fetch();
if (!$p) {
  http_response_code(404);
  echo 'Hasta bulunamadı';
  exit;
}

// Relatives
$rels = $pdo->prepare('SELECT relation, full_name, phone, email, address FROM patient_relatives WHERE patient_id = :id');
$rels->execute([':id' => $id]);
$relatives = $rels->fetchAll();

include __DIR__ . '/../header.php';
?>
<style>
/* Reset ve Temel Stiller */
.container { max-width: 1400px; margin: 0 auto; padding: 0 20px; }
.page { padding: 24px 0 40px; font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif; }

/* Başlık ve Aksiyonlar */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
  flex-wrap: wrap;
  gap: 16px;
}

.page-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.page-title h1 {
  font-size: 28px;
  font-weight: 700;
  color: #1e293b;
  margin: 0;
}

.page-title .patient-id {
  background: #e2e8f0;
  color: #64748b;
  font-size: 14px;
  font-weight: 500;
  padding: 4px 12px;
  border-radius: 20px;
}

.actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 14px;
  border: none;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
}

.btn-outline {
  background: white;
  color: #475569;
  border: 1px solid #cbd5e1;
}

.btn-outline:hover {
  background: #f8fafc;
  border-color: #94a3b8;
}

.btn-primary {
  background: #3b82f6;
  color: white;
  border: 1px solid #3b82f6;
}

.btn-primary:hover {
  background: #2563eb;
  border-color: #2563eb;
}

.btn-secondary {
  background: #10b981;
  color: white;
  border: 1px solid #10b981;
}

.btn-secondary:hover {
  background: #059669;
  border-color: #059669;
}

/* Grid Layout */
.patient-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 24px;
}

@media (max-width: 1200px) {
  .patient-grid {
    grid-template-columns: 1fr;
  }
}

/* Kart Stilleri */
.card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  border: 1px solid #e2e8f0;
  overflow: hidden;
}

.card-header {
  padding: 20px 24px;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.card-body {
  padding: 24px;
}

/* Kimlik Bilgileri */
.identity-info {
  grid-column: span 4;
}

.patient-profile {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 32px;
  font-weight: 600;
}

.patient-name h2 {
  font-size: 24px;
  font-weight: 700;
  color: #1e293b;
  margin: 0 0 4px;
}

.patient-name .id-number {
  font-size: 14px;
  color: #64748b;
  font-weight: 500;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.info-label {
  font-size: 13px;
  color: #64748b;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.info-value {
  font-size: 16px;
  color: #1e293b;
  font-weight: 600;
}

.info-value.small {
  font-size: 14px;
  font-weight: 500;
}

/* İletişim Bilgileri */
.contact-info {
  grid-column: span 4;
}

.contact-item {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 14px 0;
  border-bottom: 1px solid #f1f5f9;
}

.contact-item:last-child {
  border-bottom: none;
}

.contact-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: #f0f9ff;
  color: #0ea5e9;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.contact-content {
  flex: 1;
}

.contact-title {
  font-size: 14px;
  color: #64748b;
  margin-bottom: 4px;
}

.contact-detail {
  font-size: 16px;
  color: #1e293b;
  font-weight: 600;
}

/* Sevk Bilgileri */
.referral-info {
  grid-column: span 4;
}

.status-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.status-active {
  background: #d1fae5;
  color: #065f46;
}

.status-inactive {
  background: #fef3c7;
  color: #92400e;
}

/* Yakın Bilgileri */
.relatives-info {
  grid-column: span 12;
  margin-top: 16px;
}

.relatives-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.relative-card {
  background: #f8fafc;
  border-radius: 10px;
  padding: 20px;
  border: 1px solid #e2e8f0;
  transition: all 0.2s ease;
}

.relative-card:hover {
  border-color: #cbd5e1;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.relative-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.relative-name {
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
  margin: 0;
}

.relation-badge {
  background: #e0e7ff;
  color: #3730a3;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.relative-details {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.relative-detail {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: #475569;
}

.relative-detail i {
  color: #94a3b8;
  width: 16px;
}

/* Boş Durum Stili */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #94a3b8;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state p {
  font-size: 16px;
  margin: 0;
}

/* Responsive Tasarım */
@media (max-width: 1024px) {
  .identity-info,
  .contact-info,
  .referral-info {
    grid-column: span 6;
  }
}

@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .actions {
    width: 100%;
    justify-content: flex-start;
  }
  
  .identity-info,
  .contact-info,
  .referral-info {
    grid-column: span 12;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .relatives-grid {
    grid-template-columns: 1fr;
  }
}

/* Yardımcı Sınıflar */
.mt-4 { margin-top: 24px; }
.mb-2 { margin-bottom: 16px; }
.text-muted { color: #64748b; }
.text-center { text-align: center; }
.d-flex { display: flex; }
.align-center { align-items: center; }
.justify-between { justify-content: space-between; }
</style>

<div class="container page">
  <!-- Sayfa Başlığı -->
  <div class="page-header">
    <div class="page-title">
      <h1>Hasta Detayları</h1>
      <span class="patient-id">ID: <?= htmlspecialchars($p['id']) ?></span>
    </div>
    
    <div class="actions">
      <a href="/HastaModulu/hastaListe.php" class="btn btn-outline">
        <i class="bi bi-arrow-left"></i> Listeye Dön
      </a>
      <a href="/HastaModulu/onamArsiv.php?patient_id=<?= (int)$p['id'] ?>" class="btn btn-outline">
        <i class="bi bi-folder-fill"></i> Belgeler
      </a>
      <a href="/HastaModulu/hastaDuzenle.php?id=<?= (int)$p['id'] ?>" class="btn btn-primary">
        <i class="bi bi-pencil-square"></i> Düzenle
      </a>
      <a href="#" class="btn btn-secondary">
        <i class="bi bi-printer"></i> Yazdır
      </a>
    </div>
  </div>
  
  <!-- Hasta Bilgileri Grid -->
  <div class="patient-grid">
    <!-- Kimlik Bilgileri -->
    <div class="card identity-info">
      <div class="card-header">
        <h3><i class="bi bi-person-badge-fill" style="color: #3b82f6;"></i> Kimlik Bilgileri</h3>
      </div>
      <div class="card-body">
        <div class="patient-profile">
          <div class="avatar">
            <?= strtoupper(substr($p['first_name'] ?? '', 0, 1) . substr($p['last_name'] ?? '', 0, 1)) ?>
          </div>
          <div class="patient-name">
            <h2><?= htmlspecialchars(($p['first_name'] ?? '').' '.($p['last_name'] ?? '')) ?></h2>
            <div class="id-number">TCKN: <?= htmlspecialchars($p['national_id'] ?? '') ?></div>
          </div>
        </div>
        
        <div class="info-grid">
          <div class="info-item">
            <span class="info-label">Doğum Tarihi</span>
            <span class="info-value"><?= htmlspecialchars($p['birth_date'] ?? '') ?></span>
          </div>
          <div class="info-item">
            <span class="info-label">Yaş</span>
            <span class="info-value">
              <?php 
              if (!empty($p['birth_date'])) {
                $birthDate = new DateTime($p['birth_date']);
                $today = new DateTime();
                $age = $today->diff($birthDate)->y;
                echo $age . ' yaş';
              } else {
                echo '-';
              }
              ?>
            </span>
          </div>
          <div class="info-item">
            <span class="info-label">Cinsiyet</span>
            <span class="info-value"><?= htmlspecialchars($p['gender'] ?? '') ?></span>
          </div>
          <div class="info-item">
            <span class="info-label">Kan Grubu</span>
            <span class="info-value"><?= htmlspecialchars($p['blood_type'] ?? '') ?></span>
          </div>
          <div class="info-item">
            <span class="info-label">Kayıt Tarihi</span>
            <span class="info-value small"><?= htmlspecialchars($p['created_at'] ?? '') ?></span>
          </div>
          <div class="info-item">
            <span class="info-label">Son Güncelleme</span>
            <span class="info-value small"><?= htmlspecialchars($p['updated_at'] ?? $p['created_at'] ?? '') ?></span>
          </div>
        </div>
      </div>
    </div>
    
    <!-- İletişim Bilgileri -->
    <div class="card contact-info">
      <div class="card-header">
        <h3><i class="bi bi-telephone-fill" style="color: #10b981;"></i> İletişim Bilgileri</h3>
      </div>
      <div class="card-body">
        <div class="contact-item">
          <div class="contact-icon">
            <i class="bi bi-telephone"></i>
          </div>
          <div class="contact-content">
            <div class="contact-title">Telefon Numarası</div>
            <div class="contact-detail"><?= htmlspecialchars($p['phone'] ?? '') ?></div>
          </div>
        </div>
        
        <div class="contact-item">
          <div class="contact-icon">
            <i class="bi bi-envelope"></i>
          </div>
          <div class="contact-content">
            <div class="contact-title">E-posta Adresi</div>
            <div class="contact-detail"><?= htmlspecialchars($p['email'] ?? '') ?></div>
          </div>
        </div>
        
        <div class="contact-item">
          <div class="contact-icon">
            <i class="bi bi-geo-alt"></i>
          </div>
          <div class="contact-content">
            <div class="contact-title">Adres</div>
            <div class="contact-detail">
              <?= htmlspecialchars($p['address'] ?? '') ?><br>
              <span class="text-muted"><?= htmlspecialchars($p['district'] ?? '') ?> / <?= htmlspecialchars($p['city'] ?? '') ?></span>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- Geliş / Sevk Bilgileri -->
    <div class="card referral-info">
      <div class="card-header">
        <h3><i class="bi bi-hospital-fill" style="color: #8b5cf6;"></i> Geliş & Sevk Bilgileri</h3>
      </div>
      <div class="card-body">
        <div class="info-grid mb-2">
          <div class="info-item">
            <span class="info-label">Sevk Eden Doktor</span>
            <span class="info-value"><?= htmlspecialchars($p['doctor_name'] ?? 'Belirtilmemiş') ?></span>
          </div>
          <div class="info-item">
            <span class="info-label">Kurum</span>
            <span class="info-value"><?= htmlspecialchars($p['institution_name'] ?? 'Belirtilmemiş') ?></span>
          </div>
          <div class="info-item">
            <span class="info-label">Sevk Tarihi</span>
            <span class="info-value"><?= htmlspecialchars($p['referral_date'] ?? '') ?></span>
          </div>
          <div class="info-item">
            <span class="info-label">Servis İhtiyacı</span>
            <span class="info-value">
              <?php if((int)($p['service_needed'] ?? 0)===1): ?>
                <span class="status-badge status-active">Gerekli</span>
              <?php else: ?>
                <span class="status-badge status-inactive">Gerekli Değil</span>
              <?php endif; ?>
            </span>
          </div>
        </div>
        
        <div class="info-item mt-4">
          <span class="info-label">Sevk Nedeni</span>
          <span class="info-value small"><?= htmlspecialchars($p['referral_reason'] ?? 'Belirtilmemiş') ?></span>
        </div>
        
        <div class="info-item mt-4">
          <span class="info-label">Notlar</span>
          <span class="info-value small"><?= htmlspecialchars($p['notes'] ?? 'Not bulunmamaktadır.') ?></span>
        </div>
      </div>
    </div>
    
    <!-- Yakın Bilgileri -->
    <div class="card relatives-info">
      <div class="card-header d-flex align-center justify-between">
        <h3><i class="bi bi-people-fill" style="color: #f59e0b;"></i> Yakın Bilgileri</h3>
        <span class="text-muted"><?= count($relatives) ?> kayıt</span>
      </div>
      <div class="card-body">
        <?php if(!$relatives): ?>
          <div class="empty-state">
            <i class="bi bi-person-x"></i>
            <p>Kayıtlı yakın bulunmamaktadır.</p>
          </div>
        <?php else: ?>
          <div class="relatives-grid">
            <?php foreach($relatives as $rr): ?>
              <div class="relative-card">
                <div class="relative-header">
                  <h4 class="relative-name"><?= htmlspecialchars($rr['full_name'] ?? '') ?></h4>
                  <span class="relation-badge"><?= htmlspecialchars($rr['relation'] ?? '') ?></span>
                </div>
                
                <div class="relative-details">
                  <?php if(!empty($rr['phone'])): ?>
                    <div class="relative-detail">
                      <i class="bi bi-telephone"></i>
                      <span><?= htmlspecialchars($rr['phone']) ?></span>
                    </div>
                  <?php endif; ?>
                  
                  <?php if(!empty($rr['email'])): ?>
                    <div class="relative-detail">
                      <i class="bi bi-envelope"></i>
                      <span><?= htmlspecialchars($rr['email']) ?></span>
                    </div>
                  <?php endif; ?>
                  
                  <?php if(!empty($rr['address'])): ?>
                    <div class="relative-detail">
                      <i class="bi bi-geo-alt"></i>
                      <span><?= htmlspecialchars($rr['address']) ?></span>
                    </div>
                  <?php endif; ?>
                </div>
              </div>
            <?php endforeach; ?>
          </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>

<?php include __DIR__ . '/../footer.php'; ?>