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

use App\Auth;
use App\Csrf;
use App\Database;

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $action = $_POST['action'] ?? '';
  if (!Csrf::verify($_POST['_csrf'] ?? '')) {
    $errors[] = 'Geçersiz CSRF';
  } else {
    try {
      if ($action === 'add_lab_to_institution') {
        $institution_id = (int)($_POST['institution_id'] ?? 0);
        $external_lab_id = (int)($_POST['external_lab_id'] ?? 0);
        
        if (!$institution_id) $errors[] = 'Kurum seçimi gerekli';
        if (!$external_lab_id) $errors[] = 'Laboratuvar seçimi gerekli';
        
        if (empty($errors)) {
          // Check if already linked
          $check = $pdo->prepare('SELECT id FROM institution_external_labs WHERE institution_id=:inst AND external_lab_id=:lab');
          $check->execute([':inst' => $institution_id, ':lab' => $external_lab_id]);
          
          if ($check->fetch()) {
            $errors[] = 'Bu laboratuvar zaten kuruma bağlı';
          } else {
            $st = $pdo->prepare('INSERT INTO institution_external_labs (institution_id, external_lab_id, status) VALUES (:inst,:lab,:status)');
            $st->execute([
              ':inst' => $institution_id,
              ':lab' => $external_lab_id,
              ':status' => 'active'
            ]);
            $info = 'Laboratuvar kuruma başarıyla eklendi';
          }
        }
      } elseif ($action === 'remove_lab_from_institution') {
        $id = (int)($_POST['id'] ?? 0);
        if (!$id) {
          $errors[] = 'Geçersiz işlem';
        } else {
          $del = $pdo->prepare('DELETE FROM institution_external_labs WHERE id=:id');
          $del->execute([':id' => $id]);
          $info = 'Laboratuvar kurumdan başarıyla kaldırıldı';
        }
      } elseif ($action === 'set_default') {
        $id = (int)($_POST['id'] ?? 0);
        if (!$id) {
          $errors[] = 'Geçersiz işlem';
        } else {
          $st = $pdo->prepare('SELECT institution_id FROM institution_external_labs WHERE id=:id');
          $st->execute([':id' => $id]);
          $link = $st->fetch();
          
          if ($link) {
            $upd = $pdo->prepare('UPDATE institution_external_labs SET is_default=0 WHERE institution_id=:inst');
            $upd->execute([':inst' => $link['institution_id']]);
            
            $upd = $pdo->prepare('UPDATE institution_external_labs SET is_default=1 WHERE id=:id');
            $upd->execute([':id' => $id]);
            $info = 'Varsayılan laboratuvar başarıyla değiştirildi';
          }
        }
      } elseif ($action === 'toggle_status') {
        $id = (int)($_POST['id'] ?? 0);
        if (!$id) {
          $errors[] = 'Geçersiz işlem';
        } else {
          $st = $pdo->prepare('SELECT status FROM institution_external_labs WHERE id=:id');
          $st->execute([':id' => $id]);
          $link = $st->fetch();
          
          if ($link) {
            $new_status = ($link['status'] === 'active') ? 'inactive' : 'active';
            $upd = $pdo->prepare('UPDATE institution_external_labs SET status=:status WHERE id=:id');
            $upd->execute([':status' => $new_status, ':id' => $id]);
            $info = 'Laboratuvar durumu güncellendi';
          }
        }
      }
    } catch (\Throwable $e) {
      $errors[] = 'İşlem başarısız: ' . $e->getMessage();
    }
  }
}

$csrf = App\Csrf::token();

// Get all institutions
$institutions = $pdo->query('SELECT id, name FROM institutions WHERE status="active" ORDER BY name')->fetchAll();
$selected_institution = (int)($_GET['institution'] ?? $_POST['institution_id'] ?? 0);

// Get all active laboratories
$laboratories = $pdo->query('SELECT id, name FROM external_laboratories WHERE status="active" ORDER BY name')->fetchAll();

// Get linked laboratories for selected institution
$sql = "SELECT 
          iel.id, 
          iel.institution_id, 
          iel.external_lab_id, 
          iel.is_default, 
          iel.status,
          iel.created_at,
          el.name as lab_name
        FROM institution_external_labs iel
        JOIN external_laboratories el ON el.id = iel.external_lab_id
        WHERE iel.institution_id=:inst_id
        ORDER BY iel.is_default DESC, el.name ASC";
$st = $pdo->prepare($sql);
$st->execute([':inst_id' => $selected_institution]);
$linked_labs = $st->fetchAll();

include __DIR__ . '/../header.php';
?>
<style>
/* ✅ Full Width */
.container,
.container-sm,
.container-md,
.container-lg,
.container-xl,
.container-xxl {
  max-width: 100% !important;
  width: 100% !important;
}

main,
#app,
#content,
.content,
.page,
.wrapper {
  max-width: 100% !important;
}

:root {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-light: #e0e7ff;
  --secondary: #7c3aed;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --info: #06b6d4;
  --light: #f8fafc;
  --dark: #0f172a;
  --gray-50: #f8fafc;
  --gray-100: #f1f5f9;
  --gray-200: #e2e8f0;
  --gray-300: #cbd5e1;
  --gray-400: #94a3b8;
  --gray-500: #64748b;
  --gray-600: #475569;
  --gray-700: #334155;
  --gray-800: #1e293b;
  --gray-900: #0f172a;
  --border-radius: 12px;
  --border-radius-lg: 16px;
  --border-radius-xl: 20px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow: 0 4px 20px rgba(0,0,0,0.06);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.10);
  --shadow-xl: 0 20px 50px rgba(0,0,0,0.15);
  --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  color: var(--gray-800);
  line-height: 1.6;
  min-height: 100vh;
}

.page-container {
  max-width: 100% !important;
  width: 100% !important;
  margin: 0 !important;
  padding: 20px 24px;
}

@media (max-width: 768px) {
  .page-container {
    padding: 14px;
  }
}

.page-header {
  background: white;
  color: var(--gray-900);
  padding: 32px;
  margin-bottom: 32px;
  position: relative;
  overflow: hidden;
  border-radius: var(--border-radius-lg);
  box-shadow: var(--shadow);
  border-left: 6px solid var(--primary);
}

.page-header::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 300px;
  height: 300px;
  background: linear-gradient(135deg, var(--primary-light) 0%, transparent 70%);
  border-radius: 50%;
  opacity: 0.3;
  pointer-events: none;
}

.page-header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--gray-200), transparent);
}

.page-title-wrapper {
  position: relative;
  z-index: 2;
  max-width: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 24px;
}

.page-title h1 {
  font-weight: 800;
  font-size: 36px;
  color: var(--gray-900);
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.page-title p {
  color: var(--gray-600);
  font-size: 16px;
  font-weight: 400;
}

.btn {
  padding: 14px 28px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 14px;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  transition: var(--transition);
  text-decoration: none;
  position: relative;
  overflow: hidden;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  font-family: 'Inter', sans-serif;
}

.btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.6s ease;
}

.btn:hover::before {
  left: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
  color: white;
  box-shadow: 0 8px 24px rgba(79, 70, 229, 0.25);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(79, 70, 229, 0.35);
}

.btn-secondary {
  background: white;
  color: var(--primary);
  border: 2px solid var(--primary);
  box-shadow: var(--shadow-sm);
}

.btn-secondary:hover {
  background: var(--primary-light);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.filter-section {
  background: white;
  padding: 28px;
  border-radius: var(--border-radius-lg);
  margin-bottom: 28px;
  box-shadow: var(--shadow);
  border: 1px solid var(--gray-200);
}

.filter-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.filter-header h3 {
  font-size: 18px;
  font-weight: 700;
  color: var(--gray-900);
  margin: 0;
}

.filter-header i {
  color: var(--primary);
  font-size: 24px;
}

.filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
}

.form-label {
  font-weight: 600;
  font-size: 14px;
  color: var(--gray-700);
  margin-bottom: 10px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.form-control,
.form-select {
  padding: 12px 16px;
  border: 1px solid var(--gray-300);
  border-radius: 10px;
  font-size: 14px;
  font-family: 'Inter', sans-serif;
  background: white;
  color: var(--gray-900);
  transition: var(--transition);
}

.form-control:focus,
.form-select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
  background: white;
}

.form-actions {
  display: flex;
  gap: 12px;
  margin-top: 24px;
  flex-wrap: wrap;
}

.main-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.alert {
  padding: 16px 20px;
  border-radius: 12px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  animation: slideDown 0.4s ease;
  border-left: 4px solid;
}

.alert i {
  font-size: 20px;
  flex-shrink: 0;
  margin-top: 2px;
}

.alert-success {
  background: linear-gradient(135deg, rgba(16, 185, 129, 0.1) 0%, rgba(5, 150, 105, 0.08) 100%);
  color: #065f46;
  border-left-color: var(--success);
}

.alert-danger {
  background: linear-gradient(135deg, rgba(239, 68, 68, 0.1) 0%, rgba(220, 38, 38, 0.08) 100%);
  color: #7f1d1d;
  border-left-color: var(--danger);
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.table-container {
  background: white;
  border-radius: var(--border-radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow);
  border: 1px solid var(--gray-200);
}

.table {
  width: 100%;
  border-collapse: collapse;
}

.table thead {
  background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
  color: white;
}

.table th {
  padding: 16px 20px;
  text-align: left;
  font-weight: 700;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  border-bottom: 2px solid rgba(255, 255, 255, 0.1);
}

.table td {
  padding: 16px 20px;
  border-bottom: 1px solid var(--gray-200);
  vertical-align: middle;
}

.table tbody tr {
  transition: var(--transition);
}

.table tbody tr:hover {
  background: var(--gray-50);
}

.table tbody tr:last-child td {
  border-bottom: none;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.badge-success {
  background: linear-gradient(135deg, var(--success) 0%, #059669 100%);
  color: white;
}

.badge-danger {
  background: linear-gradient(135deg, var(--danger) 0%, #dc2626 100%);
  color: white;
}

.badge-warning {
  background: linear-gradient(135deg, var(--warning) 0%, #d97706 100%);
  color: white;
}

.default-badge {
  background: linear-gradient(135deg, #fbbf24 0%, var(--warning) 100%);
  color: white;
  display: inline-block;
  margin-left: 8px;
  padding: 4px 10px;
  font-size: 10px;
}

.action-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn-sm {
  padding: 8px 14px;
  font-size: 12px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: var(--transition);
  text-decoration: none;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.btn-sm-primary {
  background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.2);
}

.btn-sm-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(79, 70, 229, 0.3);
}

.btn-sm-warning {
  background: linear-gradient(135deg, var(--warning) 0%, #d97706 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.2);
}

.btn-sm-warning:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(245, 158, 11, 0.3);
}

.btn-sm-danger {
  background: linear-gradient(135deg, var(--danger) 0%, #dc2626 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.2);
}

.btn-sm-danger:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(239, 68, 68, 0.3);
}

.btn-sm-success {
  background: linear-gradient(135deg, var(--success) 0%, #059669 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.2);
}

.btn-sm-success:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(16, 185, 129, 0.3);
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--gray-500);
}

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

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

.add-lab-card {
  background: white;
  padding: 28px;
  border-radius: var(--border-radius-lg);
  margin-bottom: 28px;
  box-shadow: var(--shadow);
  border: 1px solid var(--gray-200);
}

.add-lab-card h5 {
  font-size: 18px;
  font-weight: 700;
  color: var(--gray-900);
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.add-lab-card i {
  color: var(--primary);
  font-size: 24px;
}

.add-lab-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 16px;
  align-items: flex-end;
}

@media (max-width: 768px) {
  .add-lab-grid {
    grid-template-columns: 1fr;
  }

  .page-title h1 {
    font-size: 24px;
  }

  .filter-grid {
    grid-template-columns: 1fr;
  }

  .action-buttons {
    flex-direction: column;
  }

  .btn-sm {
    width: 100%;
    justify-content: center;
  }
}
</style>

<!-- Navigation Links -->
<div style="margin-bottom: 20px; display: flex; gap: 10px; justify-content: center;">
  <a href="/SistemModulu/kurum-tanimlari.php" class="btn btn-secondary" style="background: transparent; color: #4f46e5; border: 1px solid #4f46e5; text-decoration: none; padding: 10px 16px; border-radius: 6px; font-size: 14px; font-weight: 600;">
    <i class="bi bi-building" style="margin-right: 6px;"></i>
    Kurum Tanımlamaları
  </a>
  <a href="/SistemModulu/dislab-tanimlari.php" class="btn btn-secondary" style="background: transparent; color: #4f46e5; border: 1px solid #4f46e5; text-decoration: none; padding: 10px 16px; border-radius: 6px; font-size: 14px; font-weight: 600;">
    <i class="bi bi-flask-fill" style="margin-right: 6px;"></i>
    Dış Laboratuvar Tanımlamaları
  </a>
  <a href="/SistemModulu/kurum-lab-iliskilendirme.php" class="btn btn-primary" style="background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%); color: white; text-decoration: none; padding: 10px 16px; border-radius: 6px; font-size: 14px; font-weight: 600;">
    <i class="bi bi-diagram-3" style="margin-right: 6px;"></i>
    Kurum-Lab İlişkilendirmesi
  </a>
</div>

<div class="page-container">
  <div class="main-content">
    <!-- Page Header -->
    <div class="page-header">
      <div class="page-title-wrapper">
        <div class="page-title">
          <h1>
            <i class="bi bi-diagram-3" style="margin-right: 12px; color: var(--primary);"></i>
            Kurum-Laboratuvar İlişkilendirmesi
          </h1>
          <p>Kurumlar ile dış laboratuvarları ilişkilendirerek tanımlamalar yapın</p>
        </div>
        <div style="flex-shrink: 0; display: flex; align-items: center;">
          <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addLabToInstitutionModal">
            <i class="bi bi-plus-lg"></i>
            LABORATUVAR EKLE
          </button>
        </div>
      </div>
    </div>

    <!-- Alerts -->
    <?php if($info): ?>
      <div class="alert alert-success">
        <i class="bi bi-check-circle-fill"></i>
        <div><?= htmlspecialchars($info) ?></div>
      </div>
    <?php endif; ?>

    <?php if($errors): ?>
      <div class="alert alert-danger">
        <i class="bi bi-exclamation-circle-fill"></i>
        <div>
          <?php foreach($errors as $e): ?>
            <div><?= htmlspecialchars($e) ?></div>
          <?php endforeach; ?>
        </div>
      </div>
    <?php endif; ?>

    <!-- Filter Section -->
    </div>

    <!-- Linked Laboratories Table -->
    <div class="table-container">
      <table class="table">
        <thead>
          <tr>
            <th>LABORATUVAR ADI</th>
            <th>DURUM</th>
            <th>KAYIT TARİHİ</th>
            <th>İŞLEMLER</th>
          </tr>
        </thead>
        <tbody>
          <?php if(empty($linked_labs)): ?>
            <tr>
              <td colspan="4">
                <div class="empty-state">
                  <i class="bi bi-inbox"></i>
                  <p>Bu kuruma henüz laboratuvar bağlanmamıştır</p>
                </div>
              </td>
            </tr>
          <?php else: ?>
            <?php foreach($linked_labs as $link): ?>
              <tr>
                <td>
                  <div style="display: flex; align-items: center; gap: 10px;">
                    <i class="bi bi-flask" style="color: var(--primary); font-size: 20px;"></i>
                    <div>
                      <div style="font-weight: 600; color: var(--gray-900);">
                        <?= htmlspecialchars($link['lab_name']) ?>
                      </div>
                      <?php if($link['is_default']): ?>
                        <span class="default-badge">
                          <i class="bi bi-star-fill"></i> VARSAYILAN
                        </span>
                      <?php endif; ?>
                    </div>
                  </div>
                </td>
                <td>
                  <span class="badge <?= $link['status'] === 'active' ? 'badge-success' : 'badge-danger' ?>">
                    <i class="bi <?= $link['status'] === 'active' ? 'bi-check-circle-fill' : 'bi-x-circle-fill' ?>"></i>
                    <?= $link['status'] === 'active' ? 'AKTİF' : 'PASİF' ?>
                  </span>
                </td>
                <td style="color: var(--gray-600); font-size: 13px;">
                  <?= date('d.m.Y', strtotime($link['created_at'])) ?>
                </td>
                <td>
                  <div class="action-buttons">
                    <?php if(!$link['is_default']): ?>
                      <form method="POST" style="display: inline;">
                        <input type="hidden" name="_csrf" value="<?= htmlspecialchars($csrf) ?>">
                        <input type="hidden" name="action" value="set_default">
                        <input type="hidden" name="id" value="<?= $link['id'] ?>">
                        <button type="submit" class="btn-sm btn-sm-warning" title="Varsayılan Yap">
                          <i class="bi bi-star"></i> VARSAYILAN YAP
                        </button>
                      </form>
                    <?php endif; ?>

                    <form method="POST" style="display: inline;">
                      <input type="hidden" name="_csrf" value="<?= htmlspecialchars($csrf) ?>">
                      <input type="hidden" name="action" value="toggle_status">
                      <input type="hidden" name="id" value="<?= $link['id'] ?>">
                      <button type="submit" class="btn-sm <?= $link['status'] === 'active' ? 'btn-sm-danger' : 'btn-sm-success' ?>">
                        <?php if($link['status'] === 'active'): ?>
                          <i class="bi bi-x-circle"></i> PASİFLEŞTİR
                        <?php else: ?>
                          <i class="bi bi-check-circle"></i> AKTİFLEŞTİR
                        <?php endif; ?>
                      </button>
                    </form>

                    <form method="POST" style="display: inline;" onsubmit="return confirm('Bu laboratuvarı kaldırmak istediğinize emin misiniz?');">
                      <input type="hidden" name="_csrf" value="<?= htmlspecialchars($csrf) ?>">
                      <input type="hidden" name="action" value="remove_lab_from_institution">
                      <input type="hidden" name="id" value="<?= $link['id'] ?>">
                      <button type="submit" class="btn-sm btn-sm-danger" title="Kaldır">
                        <i class="bi bi-trash"></i> KALDIR
                      </button>
                    </form>
                  </div>
                </td>
              </tr>
            <?php endforeach; ?>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<!-- Add Laboratory to Institution Modal -->
<div class="modal fade" id="addLabToInstitutionModal" tabindex="-1" aria-labelledby="addLabToInstitutionModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="addLabToInstitutionModalLabel">
          <i class="bi bi-plus-circle" style="margin-right: 8px;"></i> Kurum-Laboratuvar İlişkisi Ekle
        </h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <form method="POST">
        <div class="modal-body">
          <input type="hidden" name="_csrf" value="<?= htmlspecialchars($csrf) ?>">
          <input type="hidden" name="action" value="add_lab_to_institution">

          <div class="mb-3">
            <label class="form-label">Kurum *</label>
            <select name="institution_id" class="form-control" required>
              <option value="">-- Kurum Seçiniz --</option>
              <?php foreach($institutions as $inst): ?>
                <option value="<?= $inst['id'] ?>">
                  <?= htmlspecialchars($inst['name']) ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>

          <div class="mb-3">
            <label class="form-label">Dış Laboratuvar *</label>
            <select name="external_lab_id" class="form-control" required>
              <option value="">-- Laboratuvar Seçiniz --</option>
              <?php foreach($laboratories as $lab): ?>
                <option value="<?= $lab['id'] ?>">
                  <?= htmlspecialchars($lab['name']) ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>
          <button type="submit" class="btn btn-primary">
            <i class="bi bi-check-circle" style="margin-right: 6px;"></i> Ekle
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

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