<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit;
}

/* DELETE */
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {
    $id = (int)$_POST['delete_id'];

    $stmt = $conn->prepare("DELETE FROM doctors WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->close();

    header("Location: manage_doctors.php?msg=deleted");
    exit;
}

/* FETCH */
$result = $conn->query("SELECT * FROM doctors ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manage Doctors</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>
:root{
    --primary:#374155;
    --bg:#f4f7fb;
    --card:#ffffff;
    --border:#e2e8f0;
    --text:#0f172a;
    --muted:#64748b;
    --danger:#dc2626;
    --shadow:0 12px 30px rgba(0,0,0,.08);
}

*{
    box-sizing:border-box;
}

html, body{
    margin:0;
    padding:0;
    overflow-x:hidden;
}

body{
    font-family:Inter,Arial;
    background:var(--bg);
}

/* ===== LAYOUT ===== */
.main-wrapper{
    display:flex;
}

/* desktop sidebar space */
.sidebar-space{
    width:260px;
    flex-shrink:0;
}

/* open state */
.sidebar-space.active{
    left:0;
}

/* MAIN CONTENT */
.content{
    flex:1;
    padding:40px 30px;
}

/* TOP GAP FIX */
.top-space{
    height:40px;
}

/* CONTAINER */
.container{
    max-width:1150px;
    margin:auto;
}

/* ===== HEADER ===== */
.page-header{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:16px;
    background:#fff;
    padding:20px 24px;
    border-radius:16px;
    border:1px solid var(--border);
    box-shadow:var(--shadow);
    margin-bottom:30px;
}

.header-left{
    display:flex;
    align-items:center;
    gap:14px;
}

.page-header i{
    width:48px;
    height:48px;
    background:var(--primary);
    color:#fff;
    border-radius:14px;
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:18px;
}

.page-header h2{
    margin:0;
    font-size:22px;
    color:var(--primary);
    font-weight:700;
}

.page-header p{
    margin:3px 0 0;
    font-size:13px;
    color:var(--muted);
}

/* ===== ADD BUTTON ===== */
.add-btn{
    background:var(--primary);
    color:#fff;
    padding:12px 18px;
    border-radius:30px;
    text-decoration:none;
    font-size:14px;
    display:flex;
    align-items:center;
    gap:8px;
    font-weight:600;
    transition:.2s;
    white-space:nowrap;
}

.add-btn:hover{
    transform:translateY(-2px);
    box-shadow:0 8px 18px rgba(0,0,0,.15);
}

/* ===== MESSAGE ===== */
.msg{
    background:#fee2e2;
    color:#991b1b;
    padding:12px;
    border-radius:10px;
    margin-bottom:20px;
    font-weight:500;
}

/* ===== TABLE CARD ===== */
.card{
    background:#fff;
    border-radius:18px;
    box-shadow:var(--shadow);
    border:1px solid var(--border);
    padding:10px;
}

/* ===== TABLE ===== */
table{
    width:100%;
    border-collapse:separate;
    border-spacing:0 10px;
}

th{
    background:#374155;
    color:#fff;
    padding:14px;
    font-size:13px;
    text-align:left;
}

th:first-child{
    border-radius:10px 0 0 10px;
}
th:last-child{
    border-radius:0 10px 10px 0;
}

td{
    background:#f9fbfd;
    padding:14px;
    font-size:14px;
    border-top:1px solid var(--border);
    border-bottom:1px solid var(--border);
}

td:first-child{
    border-left:1px solid var(--border);
    border-radius:10px 0 0 10px;
}
td:last-child{
    border-right:1px solid var(--border);
    border-radius:0 10px 10px 0;
}

tr:hover td{
    background:#eef2f7;
}

/* ===== ACTION BUTTONS ===== */
.actions{
    display:flex;
    gap:8px;
    align-items:center;
}

.btn{
    padding:7px 10px;
    border-radius:8px;
    text-decoration:none;
    font-size:13px;
    color:#fff;
    display:flex;
    align-items:center;
    justify-content:center;
}

.edit{
    background:#2563eb;
}

.delete{
    background:var(--danger);
}

.btn:hover{
    opacity:.9;
}

/* ================= MOBILE RESPONSIVE ================= */
@media (max-width:768px){

    .sidebar-space{
        display:block;
        position:fixed;
        top:0;
        left:-260px;
        width:260px;
        height:100%;
        background:#fff;
        z-index:999;
        transition:0.3s;
    }

    .sidebar-space.active{
        left:0;
    }

    .content{
        padding:20px 12px;
    }

    .container{
        max-width:100%;
    }

    .page-header{
        flex-direction:column;
        align-items:flex-start;
        gap:10px;
        padding:16px;
    }

    .add-btn{
        width:100%;
        justify-content:center;
    }

    table thead{
        display:none;
    }

    table{
        border-spacing:0;
    }

    table tbody tr{
        display:block;
        margin-bottom:12px;
        border-radius:12px;
        border:1px solid #e2e8f0;
        padding:10px;
        background:#f9fbfd;
    }

    table tbody td{
        display:flex;
        justify-content:space-between;
        align-items:center;
        padding:6px 0;
        border:none;
        font-size:13px;
        gap:10px;
    }

    table tbody td::before{
        content:attr(data-label);
        font-weight:600;
        color:#64748b;
        flex:0 0 45%;
    }

    .actions{
        justify-content:flex-end;
    }
}
</style>
</head>

<body>

<div class="main-wrapper">

    <!-- SIDEBAR -->
    <div class="sidebar-space">
        <?php if (file_exists('rasidebar.php')) include 'rasidebar.php'; ?>
    </div>

    <!-- CONTENT -->
    <div class="content">

        <?php if (file_exists('../header.php')) include '../header.php'; ?>

        <div class="top-space"></div>

        <div class="container">

            <!-- HEADER -->
            <div class="page-header">
                <div class="header-left">
                    <i class="fa-solid fa-user-doctor"></i>
                    <div>
                        <h2>Manage Doctors</h2>
                        <p>View, edit and delete doctors</p>
                    </div>
                </div>

                <a href="add_doctor.php" class="add-btn">
                    <i class="fa-solid fa-plus"></i> Add
                </a>
            </div>

            <!-- MESSAGE -->
            <?php if (isset($_GET['msg']) && $_GET['msg']=='deleted'): ?>
                <div class="msg">Doctor deleted successfully</div>
            <?php endif; ?>

            <!-- TABLE -->
            <div class="card">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Mobile</th>
                            <th>City</th>
                            <th>Specialization</th>
                            <th>Action</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php $i=1; while($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= $i++ ?></td>
<td data-label="Name"><?= htmlspecialchars($row['name']) ?></td>
<td data-label="Mobile"><?= htmlspecialchars($row['mobile']) ?></td>
<td data-label="City"><?= htmlspecialchars($row['city']) ?></td>
<td data-label="Specialization"><?= htmlspecialchars($row['specialization']) ?></td>

<td data-label="Action">
    <div class="actions">
       <a href="edit_doctor.php?id=<?= (int)$row['id'] ?>" class="btn edit">
    <i class="fa fa-edit"></i>
</a>

<form method="post" style="display:inline;" onsubmit="return confirm('Delete this doctor?')">
    <input type="hidden" name="delete_id" value="<?= (int)$row['id'] ?>">
    <button type="submit" class="btn delete" style="border:none;cursor:pointer;">
        <i class="fa fa-trash"></i>
    </button>
</form>
    </div>
</td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>

                </table>
            </div>

        </div>

    </div>

</div>

</body>
<script>
document.addEventListener("DOMContentLoaded", function(){

    const toggle = document.querySelector(".mobile-toggle");
    const sidebar = document.querySelector(".sidebar-space");
    const overlay = document.querySelector(".overlay");

    if(toggle){
        toggle.addEventListener("click", function(){
            sidebar.classList.toggle("active");
            overlay.classList.toggle("show");
        });
    }

    if(overlay){
        overlay.addEventListener("click", function(){
            sidebar.classList.remove("active");
            overlay.classList.remove("show");
        });
    }

});
</script>
</html>