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

// Handle add/edit/delete
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['add'])) {
        $name = $_POST['name'];
        $specialization = $_POST['specialization'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $uhd_id = $_POST['uhd_id'];
        $conn->query("INSERT INTO doctors (name, specialization, email, phone, uhd_id, status) VALUES ('$name', '$specialization', '$email', '$phone', '$uhd_id', 'active')");
        $msg = "Doctor added.";
    } elseif (isset($_POST['delete'])) {
        $id = $_POST['doctor_id'];
        $conn->query("DELETE FROM doctors WHERE doctor_id=$id");
        $msg = "Doctor removed.";
    }
}

$doctors = $conn->query("SELECT * FROM doctors ORDER BY name");
?>
<!DOCTYPE html>
<html><head><title>Manage Doctors</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"></head>
<body><nav class="navbar navbar-dark bg-dark"><div class="container"><span class="navbar-brand">Manage Doctors</span><a href="dashboard.php" class="btn btn-light btn-sm">Back</a></div></nav>
<div class="container mt-4"><div class="card"><div class="card-header bg-success text-white">Doctors List</div><div class="card-body">
<?php if(isset($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 New Doctor</button>
<div class="table-responsive"><table class="table"><thead><tr><th>Name</th><th>Specialization</th><th>UHD ID</th><th>Email</th><th>Action</th></tr></thead><tbody>
<?php while($d=$doctors->fetch_assoc()): ?>
<tr><td><?= $d['name'] ?></td><td><?= $d['specialization'] ?></td><td><?= $d['uhd_id'] ?></td><td><?= $d['email'] ?></td>
<td><form method="POST" onsubmit="return confirm('Delete this doctor?');"><input type="hidden" name="doctor_id" value="<?= $d['doctor_id'] ?>"><button name="delete" class="btn btn-sm btn-danger">Delete</button></form></td></tr>
<?php endwhile; ?>
</tbody></table></div>
</div></div></div>

<div class="modal fade" id="addModal"><div class="modal-dialog"><div class="modal-content"><div class="modal-header bg-success text-white"><h5>Add Doctor</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
<form method="POST"><div class="modal-body"><input type="text" name="name" class="form-control mb-2" placeholder="Full Name" required>
<input type="text" name="specialization" class="form-control mb-2" placeholder="Specialization" required>
<input type="email" name="email" class="form-control mb-2" placeholder="Email" required>
<input type="text" name="phone" class="form-control mb-2" placeholder="Phone">
<input type="text" name="uhd_id" class="form-control mb-2" placeholder="UHD ID" required></div>
<div class="modal-footer"><button type="submit" name="add" class="btn btn-primary">Add</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>