<?php
session_start();
if (!isset($_SESSION['admin_id'])) { header("Location: login.php"); exit(); }
include '../include/config.php';

// Handle add/edit/delete
$msg = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['add'])) {
        $nurse_number = $_POST['nurse_number'];
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $specialization = $_POST['specialization'];
        $ward = $_POST['ward'];
        $password = password_hash('nurse123', PASSWORD_DEFAULT);
        $conn->query("INSERT INTO nurses (nurse_number, first_name, last_name, email, phone, specialization, ward, login_password, status) 
                      VALUES ('$nurse_number', '$first_name', '$last_name', '$email', '$phone', '$specialization', '$ward', '$password', 'active')");
        $msg = "Nurse added successfully!";
    } elseif (isset($_POST['delete'])) {
        $id = $_POST['nurse_id'];
        $conn->query("DELETE FROM nurses WHERE nurse_id = $id");
        $msg = "Nurse deleted.";
    }
}

$nurses = $conn->query("SELECT * FROM nurses ORDER BY created_at DESC");
?>
<!DOCTYPE html>
<html>
<head><title>Manage Nurses</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"></head>
<body>
<div class="container mt-4">
    <h2>Manage Nurses</h2>
    <?php if($msg) echo "<div class='alert alert-success'>$msg</div>"; ?>
    <button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#addModal">+ Add Nurse</button>
    <table class="table table-bordered">
        <thead><tr><th>ID</th><th>Nurse Number</th><th>Name</th><th>Email</th><th>Specialization</th><th>Ward</th><th>Status</th><th>Action</th></tr></thead>
        <tbody>
        <?php while($n = $nurses->fetch_assoc()): ?>
        <tr>
            <td><?= $n['nurse_id'] ?></td>
            <td><?= $n['nurse_number'] ?></td>
            <td><?= $n['first_name'].' '.$n['last_name'] ?></td>
            <td><?= $n['email'] ?></td>
            <td><?= $n['specialization'] ?></td>
            <td><?= $n['ward'] ?></td>
            <td><?= $n['status'] ?></td>
            <td>
                <form method="POST" onsubmit="return confirm('Delete?');" style="display:inline;">
                    <input type="hidden" name="nurse_id" value="<?= $n['nurse_id'] ?>">
                    <button name="delete" class="btn btn-danger btn-sm">Delete</button>
                </form>
            </td>
        </tr>
        <?php endwhile; ?>
        </tbody>
    </table>
    <a href="dashboard.php" class="btn btn-secondary">Back</a>
</div>

<div class="modal fade" id="addModal" tabindex="-1">
    <div class="modal-dialog"><div class="modal-content">
        <div class="modal-header bg-success text-white"><h5>Add Nurse</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
        <form method="POST">
            <div class="modal-body">
                <input type="text" name="nurse_number" class="form-control mb-2" placeholder="Nurse Number (e.g., NURSE001)" required>
                <input type="text" name="first_name" class="form-control mb-2" placeholder="First Name" required>
                <input type="text" name="last_name" class="form-control mb-2" placeholder="Last Name" required>
                <input type="email" name="email" class="form-control mb-2" placeholder="Email">
                <input type="text" name="phone" class="form-control mb-2" placeholder="Phone">
                <input type="text" name="specialization" class="form-control mb-2" placeholder="Specialization">
                <input type="text" name="ward" class="form-control mb-2" placeholder="Ward">
            </div>
            <div class="modal-footer"><button type="submit" name="add" class="btn btn-primary">Add Nurse</button></div>
        </form>
    </div></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>