<?php
include '../includes/db.php';
include 'header.php';
// Handle deletion
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $conn->query("DELETE FROM home_sampling_requests WHERE id=$id");
    header("Location: ".$_SERVER['PHP_SELF']); // reload after deletion
    exit;
}

// Fetch requests
$result = $conn->query("
    SELECT * FROM home_sampling_requests
    ORDER BY created_at DESC
");
?>

<h2>Home Sampling Requests</h2>

<table class="requests-table">
<thead>
<tr>
    <th>Patient</th>
    <th>Contact</th>
    <th>Prescription</th>
    <th>Map</th>
    <th>Status</th>
    <th>Update</th>
    <th>Action</th>
</tr>
</thead>
<tbody>
<?php while($r = $result->fetch_assoc()): ?>
<tr>
    <td><?= htmlspecialchars($r['patient_name']) ?></td>

    <td>
        📞 <a href="tel:<?= $r['phone'] ?>"><?= $r['phone'] ?></a><br>
        📱 <a target="_blank"
              href="https://wa.me/<?= preg_replace('/[^0-9]/','',$r['phone']) ?>">
              WhatsApp
            </a><br>
        ✉️ <?= $r['email'] ?: '-' ?>
    </td>

    <td>
        <?php if($r['prescription_file']): ?>
            <a href="../uploads/prescriptions/<?= $r['prescription_file'] ?>" target="_blank">
                View File
            </a>
        <?php else: ?>
            —
        <?php endif; ?>
    </td>

    <td>
        <?= htmlspecialchars($r['address']) ?><br>
        <?php if(!empty($r['address'])): ?>
            <a target="_blank" href="https://www.google.com/maps?q=<?= urlencode($r['address']) ?>">
                🗺️ Open Map
            </a>
        <?php endif; ?>
    </td>

    <td><strong><?= htmlspecialchars($r['status']) ?></strong></td>

    <td>
        <form method="post" action="update-status.php">
            <input type="hidden" name="id" value="<?= $r['id'] ?>">
            <select name="status">
                <option value="new" <?= $r['status']=='new'?'selected':'' ?>>New</option>
                <option value="contacted" <?= $r['status']=='contacted'?'selected':'' ?>>Contacted</option>
                <option value="completed" <?= $r['status']=='completed'?'selected':'' ?>>Completed</option>
            </select>
            <button type="submit">Update</button>
        </form>
    </td>

    <td>
        <a href="?delete=<?= $r['id'] ?>" class="btn-delete" onclick="return confirm('Are you sure you want to delete this request?')">
            Delete
        </a>
    </td>
</tr>
<?php endwhile; ?>
</tbody>
</table>

<style>
.requests-table {
    width: 95%;
    max-width: 1000px;
    margin: 20px auto 50px;
    border-collapse: collapse;
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 5px 20px rgba(0,0,0,0.1);
}

.requests-table th, .requests-table td {
    padding: 12px 10px;
    text-align: left;
    font-size: 14px;
    border-bottom: 1px solid #e0e0e0;
}

.requests-table th {
    background: #1a237e;
    color: #fff;
}

.requests-table tr:hover {
    background: #f0f4ff;
}

.requests-table a {
    text-decoration: none;
    font-weight: 500;
}

.btn-delete {
    background: #dc3545;
    color: #fff;
    padding: 5px 10px;
    border-radius: 6px;
    display: inline-block;
    transition: 0.3s;
}
.btn-delete:hover {
    background: #a71d2a;
}

/* Responsive */
@media(max-width:768px){
    .requests-table, .requests-table thead, .requests-table tbody, .requests-table th, .requests-table td, .requests-table tr {
        display: block;
    }
    .requests-table tr {
        margin-bottom: 20px;
        border-bottom: 2px solid #ddd;
        padding-bottom: 10px;
    }
    .requests-table th {
        display: none;
    }
    .requests-table td {
        position: relative;
        padding-left: 50%;
        text-align: left;
    }
    .requests-table td:before {
        position: absolute;
        top: 50%;
        left: 15px;
        transform: translateY(-50%);
        font-weight: bold;
        color: #1a237e;
    }
    .requests-table td:nth-of-type(1):before { content: "Patient"; }
    .requests-table td:nth-of-type(2):before { content: "Contact"; }
    .requests-table td:nth-of-type(3):before { content: "Prescription"; }
    .requests-table td:nth-of-type(4):before { content: "Map"; }
    .requests-table td:nth-of-type(5):before { content: "Status"; }
    .requests-table td:nth-of-type(6):before { content: "Update"; }
    .requests-table td:nth-of-type(7):before { content: "Action"; }
}
</style>
