<?php
session_start();

if (!isset($_SESSION['clinic_logged_in']) || $_SESSION['clinic_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}

// Optional: access admin ID like this
$admin_id = $_SESSION['clinic_id'];
include 'dbconn.php';

// Get the ID from the URL
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// Fetch the record for this ID
$sql = "SELECT * FROM site_facilities WHERE id = $id";
$result = $mysqli->query($sql);
$row = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CRA - Clinical Research Analysis
    </title>

    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/bootstrap.css">

    <link rel="stylesheet" href="assets/vendors/iconly/bold.css">

    <link rel="stylesheet" href="assets/vendors/perfect-scrollbar/perfect-scrollbar.css">
    <link rel="stylesheet" href="assets/vendors/bootstrap-icons/bootstrap-icons.css">
    <link rel="stylesheet" href="assets/css/app.css">
    <link rel="shortcut icon" href="assets/images/favicon.svg" type="image/x-icon">
</head>

<body>
    <div id="app">
        <div id="sidebar" class="active">
            <div class="sidebar-wrapper active">
                <div class="sidebar-header">
                    <div class="d-flex justify-content-between">
                        <div class="logo">
                              <a href="dashboard.php"><img src="img/logo.png" alt="Logo" style="width: 150px; height:50px" srcset=""></a>
                        </div>
                        <div class="toggler">
                            <a href="#" class="sidebar-hide d-xl-none d-block"><i class="bi bi-x bi-middle"></i></a>
                        </div>
                    </div>
                </div>
                  <div class="sidebar-menu">
                    <ul class="menu">
                      
                        <li class="sidebar-item  ">
                            <a href="dashboard.php" class='sidebar-link'>
                                <i class="bi bi-grid-fill"></i>
                                <span>Dashboard</span>
                            </a>
                        </li>

                        <li class="sidebar-item  ">
                            <a href="basic_information.php" class='sidebar-link'>
                                <i class="bi bi-collection-fill"></i>
                                <span>Basic Information</span>
                            </a>
                           
                        </li>

                        <li class="sidebar-item ">
                            <a href="principal_investigators.php" class='sidebar-link'>
                                <i class="bi bi-grid-1x2-fill"></i>
                                <span>Principal Investigator</span>
                            </a>
                        </li>
                        <li class="sidebar-item  ">
                            <a href="study_design.php" class='sidebar-link'>
                                <i class="bi bi-hexagon-fill"></i>
                                <span>Study Design</span>
                            </a>
                        </li>

                        <li class="sidebar-item active  ">
                            <a href="site_facility.php" class='sidebar-link'>
                                <i class="bi bi-file-earmark-medical-fill"></i>
                                <span>Site & Facility</span>
                            </a>
                        </li>

                        <li class="sidebar-item  ">
                            <a href="investigational_products.php" class='sidebar-link'>
                                <i class="bi bi-pen-fill"></i>
                                <span>Investigational Product</span>
                            </a>
                        </li>

                        <li class="sidebar-item  ">
                            <a href="requirement_datacollection.php" class='sidebar-link'>
                                <i class="bi bi-grid-1x2-fill"></i>
                                <span>Recruitment & Data<br> Collection</span>
                            </a>
                        </li>
                        
                        <li class="sidebar-item  ">
                            <a href="logout.php" class='sidebar-link'>
                                <i class="bi bi-x-octagon-fill"></i>
                                <span>Log Out</span>
                            </a>
                           
                        </li>

                        

                    </ul>
                </div>
                <button class="sidebar-toggler btn x"><i data-feather="x"></i></button>
            </div>
        </div>
        <div id="main">
            <header class="mb-3">
                <a href="#" class="burger-btn d-block d-xl-none">
                    <i class="bi bi-justify fs-3"></i>
                </a>
            </header>

            <div class="page-heading">
                <h3>Clinics Admin</h3>
            </div>
            <div class="page-content">
                <section class="row">
                      <div class="col-md-12 col-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title">Edit Site Details</h4>
                                </div>
                                <div class="card-content">
                                    <div class="card-body">
                                   <form action="update_site_details.php" method="POST" class="row g-3">
                                    <input type="hidden" name="id" value="<?= $row['id'] ?>">

                                    <!-- Site Name -->
                                        <div class="col-md-12">
                                            <label for="site_name" class="form-label">Site Name</label>
                                            <input type="text" class="form-control" id="site_name" name="site_name" value="<?= htmlspecialchars($row['site_name']) ?>" required>
                                        </div>

                                        <!-- Site Address -->
                                        <div class="col-md-12">
                                            <label for="site_address" class="form-label">Site Address</label>
                                            <input type="text" class="form-control" id="site_address" name="site_address" value="<?= htmlspecialchars($row['site_address']) ?>" required>
                                        </div>

                                        <!-- Site Type -->
                                        <div class="col-md-12">
                                            <label for="site_type" class="form-label">Site Type</label>
                                            <select class="form-select" id="site_type" name="site_type" required>
                                                <option value="">-- Select Type --</option>
                                                <?php
                                                $types = ['Hospital', 'Clinic', 'University', 'Private Practice', 'Other'];
                                                foreach ($types as $type) {
                                                    $selected = ($row['site_type'] === $type) ? 'selected' : '';
                                                    echo "<option value=\"$type\" $selected>$type</option>";
                                                }
                                                ?>
                                            </select>
                                        </div>

                                        <!-- Site Readiness -->
                                        <div class="col-md-12">
                                            <label for="site_readiness" class="form-label">Site Readiness Status</label>
                                            <select class="form-select" id="site_readiness" name="site_readiness" required>
                                                <option value="">-- Select Status --</option>
                                                <option value="Ready" <?= $row['site_readiness'] === 'Ready' ? 'selected' : '' ?>>Ready</option>
                                                <option value="Not Ready" <?= $row['site_readiness'] === 'Not Ready' ? 'selected' : '' ?>>Not Ready</option>
                                            </select>
                                        </div>

                                        <!-- IRB/IEC Approval -->
                                        <div class="col-md-12">
                                            <label for="irb_approval" class="form-label">IRB/IEC Approval Status</label>
                                            <select class="form-select" id="irb_approval" name="irb_approval" required>
                                                <option value="">-- Select Approval --</option>
                                                <option value="Approved" <?= $row['irb_approval'] === 'Approved' ? 'selected' : '' ?>>Approved</option>
                                                <option value="Pending" <?= $row['irb_approval'] === 'Pending' ? 'selected' : '' ?>>Pending</option>
                                                <option value="Rejected" <?= $row['irb_approval'] === 'Rejected' ? 'selected' : '' ?>>Rejected</option>
                                            </select>
                                        </div>

                                        <!-- IRB/IEC Contact -->
                                        <div class="col-md-12">
                                            <label for="irb_contact" class="form-label">IRB/IEC Contact Details</label>
                                            <input type="text" class="form-control" id="irb_contact" name="irb_contact" value="<?= htmlspecialchars($row['irb_contact']) ?>">
                                        </div>

                                        <!-- Site Infrastructure (Checklist) -->
                                        <?php
                                        $infra = explode(',', $row['infrastructure']); // convert to array
                                        function is_checked($value, $infra) {
                                            return in_array(trim($value), $infra) ? 'checked' : '';
                                        }
                                        ?>
                                        <div class="col-12">
                                            <label class="form-label">Site Infrastructure Available</label>
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox" id="lab" name="infrastructure[]" value="Lab" <?= is_checked('Lab', $infra) ?>>
                                                <label class="form-check-label" for="lab">Laboratory</label>
                                            </div>
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox" id="pharmacy" name="infrastructure[]" value="Pharmacy" <?= is_checked('Pharmacy', $infra) ?>>
                                                <label class="form-check-label" for="pharmacy">Pharmacy</label>
                                            </div>
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox" id="ecg" name="infrastructure[]" value="ECG" <?= is_checked('ECG', $infra) ?>>
                                                <label class="form-check-label" for="ecg">ECG</label>
                                            </div>
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox" id="imaging" name="infrastructure[]" value="Imaging" <?= is_checked('Imaging', $infra) ?>>
                                                <label class="form-check-label" for="imaging">Imaging Facilities</label>
                                            </div>
                                        </div>


                                    <!-- Submit -->
                                    <div class="col-12 mt-3">
                                        <button type="submit" class="btn btn-primary">Save Site Details</button>
                                    </div>
                                    </form>


                                    </div>
                                </div>
                            </div>
                        </div>
                </section>
            </div>

            <footer>
                <div class="footer clearfix mb-0 text-muted">
                    <div class="float-start">
                        <p>2025 &copy; Phaseon Clinical Research</p>
                    </div>
                    <div class="float-end">
                        <p>Design by PoojaInfotech</p>
                    </div>
                </div>
            </footer>
        </div>
    </div>
    <script src="assets/vendors/perfect-scrollbar/perfect-scrollbar.min.js"></script>
    <script src="assets/js/bootstrap.bundle.min.js"></script>

    <script src="assets/vendors/apexcharts/apexcharts.js"></script>
    <script src="assets/js/pages/dashboard.js"></script>

    <script src="assets/js/main.js"></script>
</body>

</html>