<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

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

$patient_id = $_GET['id'] ?? null;
$msg = "";
$patient = null;

if ($patient_id) {
    // Fetch patient details
    $sql = "SELECT p.*, u.full_name, u.email FROM patients p JOIN users u ON p.user_id = u.user_id WHERE p.patient_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $patient_id);
    $stmt->execute();
    $patient = $stmt->get_result()->fetch_assoc();
}

if (isset($_POST['update_patient'])) {
    $pid = $_POST['patient_id'];
    $uid = $_POST['user_id'];
    $full_name = htmlspecialchars($_POST['full_name']);
    $email = htmlspecialchars($_POST['email']);
    $phone = htmlspecialchars($_POST['phone']);
    $address = htmlspecialchars($_POST['address']);
    $dob = htmlspecialchars($_POST['dob']);

    $conn->begin_transaction();
    try {
        $stmt_u = $conn->prepare("UPDATE users SET full_name = ?, email = ? WHERE user_id = ?");
        $stmt_u->bind_param("ssi", $full_name, $email, $uid);
        $stmt_u->execute();

        $stmt_p = $conn->prepare("UPDATE patients SET phone_number = ?, address = ?, date_of_birth = ? WHERE patient_id = ?");
        $stmt_p->bind_param("sssi", $phone, $address, $dob, $pid);
        $stmt_p->execute();

        $conn->commit();
        $msg = '<div class="alert alert-success">Patient details updated successfully.</div>';
        // Refresh details
        $patient['full_name'] = $full_name;
        $patient['email'] = $email;
        $patient['phone_number'] = $phone;
        $patient['address'] = $address;
        $patient['date_of_birth'] = $dob;

    } catch (Exception $e) {
        $conn->rollback();
        $msg = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
    }
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Update Patient')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Update Patient Information</h4>
                            </div>
                        </div>
                    </div>

                    <?php if (!$patient): ?>
                        <div class="alert alert-warning">No patient selected. Please use <a
                                href="patient_search.php">Patient Search</a> to find a patient first.</div>
                        <div class="card">
                            <div class="card-body">
                                <form action="patient_search.php" method="GET">
                                    <div class="input-group">
                                        <input type="text" class="form-control" name="query"
                                            placeholder="Search by name or phone...">
                                        <button class="btn btn-primary" type="submit">Search</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="row justify-content-center">
                            <div class="col-lg-8">
                                <div class="card">
                                    <div class="card-body">
                                        <?php echo $msg; ?>
                                        <form method="POST" action="">
                                            <input type="hidden" name="patient_id"
                                                value="<?php echo $patient['patient_id']; ?>">
                                            <input type="hidden" name="user_id" value="<?php echo $patient['user_id']; ?>">

                                            <div class="mb-3">
                                                <label class="form-label">Full Name</label>
                                                <input type="text" name="full_name" class="form-control"
                                                    value="<?php echo htmlspecialchars($patient['full_name']); ?>" required>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Email</label>
                                                    <input type="email" name="email" class="form-control"
                                                        value="<?php echo htmlspecialchars($patient['email']); ?>">
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Phone</label>
                                                    <input type="text" name="phone" class="form-control"
                                                        value="<?php echo htmlspecialchars($patient['phone_number']); ?>">
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Date of Birth</label>
                                                    <input type="date" name="dob" class="form-control"
                                                        value="<?php echo $patient['date_of_birth']; ?>">
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Address</label>
                                                    <input type="text" name="address" class="form-control"
                                                        value="<?php echo htmlspecialchars($patient['address']); ?>">
                                                </div>
                                            </div>

                                            <div class="mt-3 text-end">
                                                <button type="submit" name="update_patient" class="btn btn-primary">Update
                                                    Details</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>