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

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

$query = $_GET['q'] ?? '';
$results = [];

if ($query) {
    $search = "%$query%";
    // Search Patients
    $sql = "SELECT p.patient_id, u.full_name, p.phone_number, 'Patient' as type 
            FROM patients p JOIN users u ON p.user_id = u.user_id 
            WHERE u.full_name LIKE ? OR p.phone_number LIKE ? OR p.patient_number LIKE ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sss", $search, $search, $search);
    $stmt->execute();
    $res = $stmt->get_result();
    while ($row = $res->fetch_assoc())
        $results[] = $row;

    // Search Invoices
    $sql2 = "SELECT invoice_id as id, total_amount as extra, 'Invoice' as type FROM invoices WHERE invoice_id LIKE ?";
    $stmt2 = $conn->prepare($sql2);
    $stmt2->bind_param("s", $search);
    $stmt2->execute();
    $res2 = $stmt2->get_result();
    while ($row = $res2->fetch_assoc()) {
        $row['full_name'] = "Invoice #" . $row['id'];
        $row['phone_number'] = "KES " . $row['extra'];
        $results[] = $row;
    }
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Record Search')); ?>
    <?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">Global Record Search</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row justify-content-center">
                        <div class="col-lg-8">
                            <div class="card">
                                <div class="card-body">
                                    <form method="GET" class="mb-4">
                                        <div class="input-group">
                                            <input type="text" name="q" class="form-control"
                                                placeholder="Search for patients, invoices, phone numbers..."
                                                value="<?php echo htmlspecialchars($query); ?>">
                                            <button class="btn btn-primary" type="submit"><i class="ri-search-line"></i>
                                                Search</button>
                                        </div>
                                    </form>

                                    <?php if ($query): ?>
                                        <h5>Results for "<?php echo htmlspecialchars($query); ?>"</h5>
                                        <?php if (count($results) > 0): ?>
                                            <div class="list-group">
                                                <?php foreach ($results as $r): ?>
                                                    <a href="#" class="list-group-item list-group-item-action">
                                                        <div class="d-flex w-100 justify-content-between">
                                                            <h5 class="mb-1"><?php echo htmlspecialchars($r['full_name']); ?></h5>
                                                            <small class="badge bg-info"><?php echo $r['type']; ?></small>
                                                        </div>
                                                        <p class="mb-1"><?php echo htmlspecialchars($r['phone_number']); ?></p>
                                                    </a>
                                                <?php endforeach; ?>
                                            </div>
                                        <?php else: ?>
                                            <div class="alert alert-warning">No results found.</div>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>