<?php
// Güvenlik Kontrolü
if (!defined('PDO::ATTR_ERRMODE')) { echo "Direct access denied."; exit; }
global $pdo;

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

// Tanımları çek
$hepatitler = $pdo->query("SELECT id, ad FROM hepatit_tanimlari WHERE aktif=1 ORDER BY sira, ad")->fetchAll(PDO::FETCH_ASSOC);
$damarlar   = $pdo->query("SELECT id, ad FROM damar_yolu_tanimlari WHERE aktif=1 ORDER BY sira, ad")->fetchAll(PDO::FETCH_ASSOC);
$kurumlar   = $pdo->query("SELECT id, kurum_adi, kurum_tipi FROM kurumlar WHERE aktif=1 ORDER BY kurum_tipi, kurum_adi")->fetchAll(PDO::FETCH_ASSOC);
$sgkler     = $pdo->query("SELECT id, ad FROM sgk_tipleri WHERE aktif=1 ORDER BY sira, ad")->fetchAll(PDO::FETCH_ASSOC);

$alerjiler  = $pdo->query("SELECT id, ad FROM alerji_tanimlari WHERE aktif=1 ORDER BY sira, ad")->fetchAll(PDO::FETCH_ASSOC);
$kronikler  = $pdo->query("SELECT id, ad FROM kronik_hastalik_tanimlari WHERE aktif=1 ORDER BY sira, ad")->fetchAll(PDO::FETCH_ASSOC);
$riskler    = $pdo->query("SELECT id, ad FROM risk_uyari_tanimlari WHERE aktif=1 ORDER BY sira, ad")->fetchAll(PDO::FETCH_ASSOC);
?>

<style>
  .form-wrapper{max-width:980px;margin:0 auto;padding-bottom:40px;}
  .patient-card{background:#fff;border-radius:24px;box-shadow:0 10px 40px rgba(0,0,0,.04);padding:40px;border:1px solid #f1f5f9;}
  .section-title{font-size:.7rem;font-weight:900;color:#2563eb;text-transform:uppercase;letter-spacing:1.5px;margin:30px 0 15px;border-bottom:2px solid #f8fafc;padding-bottom:8px;display:block}
  .section-title:first-child{margin-top:0}
  .f-group{margin-bottom:20px}
  .f-group label{display:block;font-size:.85rem;font-weight:700;color:#475569;margin-bottom:8px}
  .input-aura{width:100%;padding:14px 18px;border:2px solid #f1f5f9;border-radius:14px;font-size:.95rem;color:#0f172a;transition:.2s;outline:none;box-sizing:border-box;background:#fdfdfe}
  .input-aura:focus{border-color:#3b82f6;background:#fff;box-shadow:0 0 0 4px rgba(59,130,246,.08)}
  .grid-row{display:grid;grid-template-columns:1fr 1fr;gap:20px}
  .grid-row-3{display:grid;grid-template-columns:1fr 1fr 1fr;gap:20px}
  @media(max-width:768px){.grid-row,.grid-row-3{grid-template-columns:1fr}.patient-card{padding:22px}}
  .btn-submit{width:100%;padding:18px;background:#2563eb;color:#fff;border:none;border-radius:14px;font-size:1rem;font-weight:800;cursor:pointer;transition:.2s;margin-top:20px;box-shadow:0 4px 15px rgba(37,99,235,.2);display:flex;align-items:center;justify-content:center;gap:10px}
  .btn-submit:hover{background:#1d4ed8;transform:translateY(-1px);box-shadow:0 6px 20px rgba(37,99,235,.28)}
  .mini-note{color:#64748b;font-size:.9rem;margin:6px 0 0}
  .chips{display:flex;flex-wrap:wrap;gap:10px}
  .chip{display:inline-flex;align-items:center;gap:8px;padding:10px 12px;border:1px solid #e2e8f0;border-radius:999px;background:#f8fafc;font-size:.9rem}
  .chip input{transform:scale(1.1)}
  .soft-box{background:#f8fafc;padding:18px;border-radius:18px;border:1px solid #f1f5f9}
</style>

<div class="form-wrapper">
  <div class="patient-card">
    <div style="text-align:center;margin-bottom:35px;">
      <h2 style="margin:0;color:#0f172a;font-weight:900;font-size:1.85rem;">Yeni Hasta Tanımla</h2>
      <p style="color:#94a3b8;font-size:1rem;margin-top:6px;">Kimlik, kurum/SGK ve klinik özet bilgilerini kaydedin.</p>
    </div>

    <form action="islem.php" method="POST">

      <span class="section-title">Kimlik Bilgileri</span>

      <div class="grid-row">
        <div class="f-group">
          <label>T.C. Kimlik Numarası</label>
          <input type="number" name="tc_no" class="input-aura" id="tcField" placeholder="11 Hane" required>
          <div class="mini-note">Sadece rakam.</div>
        </div>
        <div class="f-group">
          <label>Kan Grubu</label>
          <select name="kan_grubu" class="input-aura">
            <option value="">Seçiniz...</option>
            <option>A Rh(+)</option><option>A Rh(-)</option>
            <option>B Rh(+)</option><option>B Rh(-)</option>
            <option>AB Rh(+)</option><option>AB Rh(-)</option>
            <option>0 Rh(+)</option><option>0 Rh(-)</option>
          </select>
        </div>
      </div>

      <div class="grid-row">
        <div class="f-group"><label>Adı</label><input type="text" name="ad" class="input-aura" required></div>
        <div class="f-group"><label>Soyadı</label><input type="text" name="soyad" class="input-aura" required></div>
      </div>

      <div class="grid-row">
        <div class="f-group">
          <label>Cinsiyet</label>
          <select name="cinsiyet" class="input-aura" required>
            <option value="">Seçiniz...</option>
            <option value="Erkek">Erkek</option>
            <option value="Kadın">Kadın</option>
          </select>
        </div>
        <div class="f-group">
          <label>Doğum Tarihi</label>
          <input type="date" name="dogum_tarihi" class="input-aura" required>
        </div>
      </div>

      <span class="section-title">Kurum & SGK</span>

      <div class="grid-row">
        <div class="f-group">
          <label>Kurum</label>
          <select name="kurum_id" class="input-aura">
            <option value="">Seçiniz...</option>
            <?php foreach($kurumlar as $k): ?>
              <option value="<?= (int)$k['id'] ?>"><?= h($k['kurum_adi']) ?> (<?= h($k['kurum_tipi']) ?>)</option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="f-group">
          <label>SGK Tipi</label>
          <select name="sgk_tipi_id" class="input-aura">
            <option value="">Seçiniz...</option>
            <?php foreach($sgkler as $s): ?>
              <option value="<?= (int)$s['id'] ?>"><?= h($s['ad']) ?></option>
            <?php endforeach; ?>
          </select>
        </div>
      </div>

      <div class="grid-row">
        <div class="f-group">
          <label>SGK / Sicil No</label>
          <input type="text" name="sgk_no" class="input-aura" placeholder="Örn: sicil / provizyon / dosya no">
        </div>
        <div class="f-group">
          <label>Tanı / Klinik Özet</label>
          <input type="text" name="tani_ozet" class="input-aura" placeholder="Örn: KBY, HT, DM, ...">
        </div>
      </div>

      <span class="section-title">Klinik Veriler</span>

      <div class="grid-row-3">
        <div class="f-group">
          <label>Hepatit</label>
          <select name="hepatit_id" class="input-aura">
            <option value="">Seçiniz...</option>
            <?php foreach($hepatitler as $hpt): ?>
              <option value="<?= (int)$hpt['id'] ?>"><?= h($hpt['ad']) ?></option>
            <?php endforeach; ?>
          </select>
        </div>

        <div class="f-group">
          <label>Damar Yolu</label>
          <select name="damar_yolu_id" class="input-aura">
            <option value="">Seçiniz...</option>
            <?php foreach($damarlar as $d): ?>
              <option value="<?= (int)$d['id'] ?>"><?= h($d['ad']) ?></option>
            <?php endforeach; ?>
          </select>
        </div>

        <div class="f-group">
          <label>Erişim Tarafı</label>
          <select name="erisim_tarafi" class="input-aura">
            <option value="Bilinmiyor">Bilinmiyor</option>
            <option value="Sol">Sol</option>
            <option value="Sağ">Sağ</option>
            <option value="Bilateral">Bilateral</option>
          </select>
        </div>
      </div>

      <div class="grid-row-3">
        <div class="f-group">
          <label>Diyabet</label>
          <select name="diyabet_durumu" class="input-aura">
            <option value="0">Hayır</option>
            <option value="1">Evet</option>
          </select>
        </div>
        <div class="f-group">
          <label>Kuru Ağırlık (kg)</label>
          <input type="number" step="0.1" name="kuru_agirlik" class="input-aura" placeholder="Örn: 72.5">
        </div>
        <div class="f-group">
          <label>Durum</label>
          <select name="durum" class="input-aura">
            <option value="aktif" selected>aktif</option>
            <option value="pasif">pasif</option>
            <option value="vefat">vefat</option>
            <option value="nakil">nakil</option>
          </select>
        </div>
      </div>

      <span class="section-title">Alerjiler, Kronikler, Risk Uyarıları</span>

      <div class="soft-box">
        <div class="f-group">
          <label>Alerjiler</label>
          <div class="chips">
            <?php if(!$alerjiler): ?><div class="mini-note">Tanım yok. Ayarlar → Tanımlar’dan ekle.</div><?php endif; ?>
            <?php foreach($alerjiler as $a): ?>
              <label class="chip"><input type="checkbox" name="alerjiler[]" value="<?= (int)$a['id'] ?>"> <?= h($a['ad']) ?></label>
            <?php endforeach; ?>
          </div>
        </div>

        <div class="f-group">
          <label>Kronik Hastalıklar</label>
          <div class="chips">
            <?php if(!$kronikler): ?><div class="mini-note">Tanım yok. Ayarlar → Tanımlar’dan ekle.</div><?php endif; ?>
            <?php foreach($kronikler as $k): ?>
              <label class="chip"><input type="checkbox" name="kronikler[]" value="<?= (int)$k['id'] ?>"> <?= h($k['ad']) ?></label>
            <?php endforeach; ?>
          </div>
        </div>

        <div class="f-group" style="margin-bottom:0;">
          <label>Risk Uyarıları</label>
          <div class="chips">
            <?php if(!$riskler): ?><div class="mini-note">Tanım yok. Ayarlar → Tanımlar’dan ekle.</div><?php endif; ?>
            <?php foreach($riskler as $r): ?>
              <label class="chip"><input type="checkbox" name="riskler[]" value="<?= (int)$r['id'] ?>"> <?= h($r['ad']) ?></label>
            <?php endforeach; ?>
          </div>
        </div>
      </div>

      <span class="section-title">İletişim & Yakınlar</span>

      <div class="f-group">
        <label>Hasta Telefon</label>
        <input type="tel" name="telefon" class="input-aura" placeholder="05xx xxx xx xx">
      </div>

      <div class="f-group">
        <label>İkamet Adresi</label>
        <textarea name="adres" class="input-aura" style="min-height:80px;"></textarea>
      </div>

      <div class="soft-box">
        <p style="margin:0 0 12px 0;font-size:.75rem;font-weight:900;color:#2563eb;">ACİL DURUM YAKINI</p>
        <div class="grid-row">
          <div class="f-group"><label>Ad Soyad</label><input type="text" name="yakin_ad_soyad" class="input-aura"></div>
          <div class="f-group"><label>Yakınlık Derecesi</label><input type="text" name="yakin_yakinlik_derecesi" class="input-aura" placeholder="Örn: Eşi"></div>
        </div>
        <div class="f-group" style="margin-bottom:0;"><label>Yakın Telefon</label><input type="tel" name="yakin_telefon" class="input-aura"></div>
      </div>

      <button type="submit" name="hasta_kaydet" class="btn-submit">
        <i class="fas fa-save"></i> HASTA KAYDINI TAMAMLA
      </button>

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

<script>
  const tc = document.getElementById('tcField');
  tc?.addEventListener('input', function(){ if(this.value.length>11) this.value=this.value.slice(0,11); });
</script>
