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

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

function e($v){ return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8'); }

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) {
    die("Invalid doctor ID");
}

$message = '';
$error = '';

// fetch doctor
$stmt = $conn->prepare("SELECT * FROM doctors WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$doctor = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$doctor) {
    die("Doctor not found");
}

// update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $mobile = trim($_POST['mobile'] ?? '');
    $city = trim($_POST['city'] ?? '');
    $specialization = trim($_POST['specialization'] ?? '');

    if ($name === '' || $mobile === '') {
        $error = "Name and mobile are required.";
    } else {
        $stmt = $conn->prepare("
            UPDATE doctors
            SET name = ?, mobile = ?, city = ?, specialization = ?
            WHERE id = ?
        ");
        $stmt->bind_param("ssssi", $name, $mobile, $city, $specialization, $id);

        if ($stmt->execute()) {
            $message = "Doctor updated successfully.";
            $doctor['name'] = $name;
            $doctor['mobile'] = $mobile;
            $doctor['city'] = $city;
            $doctor['specialization'] = $specialization;
        } else {
            $error = "Update failed.";
        }
        $stmt->close();
    }
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Edit Doctor</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;
    --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,sans-serif;
    background:var(--bg);
}

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

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

.content{
    flex:1;
    padding:40px 30px;
}

.top-space{
    height:40px;
}

.container{
    max-width:900px;
    margin:auto;
}

.card{
    background:#fff;
    border-radius:18px;
    box-shadow:var(--shadow);
    border:1px solid var(--border);
    padding:24px;
}

.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:20px;
}

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

.page-header i.icon{
    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);
}

.input{
    width:100%;
    height:44px;
    padding:0 12px;
    border:1px solid #dbe3ee;
    border-radius:10px;
    margin-bottom:14px;
    box-sizing:border-box;
    font-size:14px;
    outline:none;
}

.input:focus{
    border-color:#2563eb;
    box-shadow:0 0 0 2px rgba(37,99,235,.08);
}

.btn-row{
    display:flex;
    gap:10px;
    flex-wrap:wrap;
    margin-top:8px;
}

.btn{
    height:42px;
    padding:0 18px;
    border:0;
    border-radius:10px;
    background:#374155;
    color:#fff;
    font-weight:700;
    cursor:pointer;
    display:inline-flex;
    align-items:center;
    gap:8px;
    text-decoration:none;
    font-size:14px;
}

.btn:hover{
    opacity:.92;
}

.btn.back{
    background:#e5e7eb;
    color:#111827;
}

.ok{
    background:#dcfce7;
    color:#166534;
    padding:10px 12px;
    border-radius:10px;
    margin-bottom:12px;
    font-weight:600;
}

.err{
    background:#fee2e2;
    color:#991b1b;
    padding:10px 12px;
    border-radius:10px;
    margin-bottom:12px;
    font-weight:600;
}

/* mobile */
@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;
    }

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

    .container{
        max-width:100%;
    }

    .btn-row{
        flex-direction:column;
    }

    .btn{
        width:100%;
        justify-content:center;
    }
}
</style>
</head>
<body>

<div class="main-wrapper">

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

    <div class="content">
        <?php if (file_exists('../header.php')) include '../header.php'; ?>

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

        <div class="container">
            <div class="page-header">
                <div class="header-left">
                    <i class="fa-solid fa-user-doctor icon"></i>
                    <div>
                        <h2>Edit Doctor</h2>
                        <p>Update doctor details</p>
                    </div>
                </div>
            </div>

            <div class="card">
                <?php if ($message): ?>
                    <div class="ok"><?php echo e($message); ?></div>
                <?php endif; ?>

                <?php if ($error): ?>
                    <div class="err"><?php echo e($error); ?></div>
                <?php endif; ?>

                <form method="post">
                    <input class="input" type="text" name="name" placeholder="Name" value="<?php echo e($doctor['name']); ?>">
                    <input class="input" type="text" name="mobile" placeholder="Mobile" value="<?php echo e($doctor['mobile']); ?>">
                    <input class="input" type="text" name="city" placeholder="City" value="<?php echo e($doctor['city']); ?>">
                    <input class="input" type="text" name="specialization" placeholder="Specialization" value="<?php echo e($doctor['specialization']); ?>">

                    <div class="btn-row">
                        <button type="submit" class="btn">
                            <i class="fa fa-save"></i> Update
                        </button>

                        <a href="javascript:history.back()" class="btn back">
    <i class="fa fa-arrow-left"></i> Back
</a>
                    </div>
                </form>
            </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");
            if (overlay) overlay.classList.toggle("show");
        });
    }

    if(overlay){
        overlay.addEventListener("click", function(){
            sidebar.classList.remove("active");
            overlay.classList.remove("show");
        });
    }
});
</script>
</html>