<?php
require_once 'auth.php';
requireLogin();

$q = trim($_GET['q'] ?? '');
$results = ['devices' => [], 'customers' => [], 'hospitals' => [], 'clinics' => []];

if (strlen($q) >= 2) {
    $s = "%$q%";
    $st = $conn->prepare("SELECT d.id, d.invoice_number, d.hospital_name, dn.name as device_name, c.name as customer_name FROM devices d JOIN device_names dn ON d.device_name_id = dn.id JOIN customers c ON d.customer_id = c.id WHERE d.invoice_number LIKE ? OR d.hospital_name LIKE ? OR dn.name LIKE ? OR c.name LIKE ? LIMIT 10");
    $st->bind_param("ssss", $s, $s, $s, $s);
    $st->execute();
    $r = $st->get_result();
    while ($row = $r->fetch_assoc()) $results['devices'][] = $row;

    $st2 = $conn->prepare("SELECT c.id, c.name, c.hospital_name FROM customers c WHERE c.name LIKE ? OR c.hospital_name LIKE ? LIMIT 5");
    $st2->bind_param("ss", $s, $s);
    $st2->execute();
    $r2 = $st2->get_result();
    while ($row = $r2->fetch_assoc()) $results['customers'][] = $row;

    $st3 = $conn->prepare("SELECT h.id, h.name, g.name_ar as gov_name FROM hospitals h JOIN governorates g ON h.governorate_id = g.id WHERE h.name LIKE ? OR g.name_ar LIKE ? LIMIT 5");
    $st3->bind_param("ss", $s, $s);
    $st3->execute();
    $r3 = $st3->get_result();
    while ($row = $r3->fetch_assoc()) $results['hospitals'][] = $row;

    $st4 = $conn->prepare("SELECT c.id, c.name, g.name_ar as gov_name FROM clinics c JOIN governorates g ON c.governorate_id = g.id WHERE c.name LIKE ? OR g.name_ar LIKE ? LIMIT 5");
    $st4->bind_param("ss", $s, $s);
    $st4->execute();
    $r4 = $st4->get_result();
    while ($row = $r4->fetch_assoc()) $results['clinics'][] = $row;
}
$hasResults = !empty($results['devices']) || !empty($results['customers']) || !empty($results['hospitals']) || !empty($results['clinics']);
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <?php include 'includes/favicon.php'; ?>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>بحث - نظام صيانة</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
    <?php include 'includes/header.php'; ?>
    
    <div class="main-content">
        <div class="container py-4">
            <h2 class="page-title mb-4"><i class="bi bi-search"></i> البحث</h2>
            
            <form method="GET" class="mb-4">
                <div class="input-group input-group-lg" style="max-width: 500px;">
                    <input type="text" name="q" class="form-control" placeholder="ابحث عن جهاز، زبون، مستشفى..." value="<?= htmlspecialchars($q) ?>" autofocus>
                    <button type="submit" class="btn btn-primary"><i class="bi bi-search"></i> بحث</button>
                </div>
            </form>
            
            <?php if ($q && !$hasResults): ?>
            <div class="alert alert-info d-flex align-items-center">
                <i class="bi bi-info-circle me-2 fs-4"></i>
                <span>لا توجد نتائج لـ "<strong><?= htmlspecialchars($q) ?></strong>"</span>
            </div>
            <?php elseif (!$q): ?>
            <div class="text-center py-5">
                <div class="mb-3"><i class="bi bi-search text-muted" style="font-size: 4rem;"></i></div>
                <h5 class="text-muted">أدخل كلمة بحث للبدء</h5>
                <p class="text-muted small">ابحث في الأجهزة، الزبائن، والمستشفيات</p>
            </div>
            <?php elseif ($hasResults): ?>
            <div class="row g-4">
                <?php if (!empty($results['devices'])): ?>
                <div class="col-12">
                    <div class="card">
                        <div class="card-header"><h5 class="mb-0"><i class="bi bi-cpu me-2"></i>الأجهزة</h5></div>
                        <div class="list-group list-group-flush">
                            <?php foreach ($results['devices'] as $d): ?>
                            <a href="device_view.php?id=<?= $d['id'] ?>" class="list-group-item list-group-item-action">
                                <?= htmlspecialchars($d['invoice_number']) ?> - <?= htmlspecialchars($d['device_name']) ?> - <?= htmlspecialchars($d['hospital_name']) ?>
                            </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                <?php if (!empty($results['customers'])): ?>
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header"><h5 class="mb-0"><i class="bi bi-people me-2"></i>الزبائن</h5></div>
                        <div class="list-group list-group-flush">
                            <?php foreach ($results['customers'] as $c): ?>
                            <a href="customers.php?q=<?= urlencode($q) ?>" class="list-group-item list-group-item-action">
                                <?= htmlspecialchars($c['name']) ?> - <?= htmlspecialchars($c['hospital_name'] ?? '') ?>
                            </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                <?php if (!empty($results['hospitals'])): ?>
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header"><h5 class="mb-0"><i class="bi bi-hospital me-2"></i>المستشفيات</h5></div>
                        <div class="list-group list-group-flush">
                            <?php foreach ($results['hospitals'] as $h): ?>
                            <a href="hospitals.php" class="list-group-item list-group-item-action">
                                <?= htmlspecialchars($h['name']) ?> - <?= htmlspecialchars($h['gov_name']) ?>
                            </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                <?php if (!empty($results['clinics'])): ?>
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header"><h5 class="mb-0"><i class="bi bi-building me-2"></i>العيادات</h5></div>
                        <div class="list-group list-group-flush">
                            <?php foreach ($results['clinics'] as $c): ?>
                            <a href="hospitals.php?t=clinic" class="list-group-item list-group-item-action">
                                <?= htmlspecialchars($c['name']) ?> - <?= htmlspecialchars($c['gov_name']) ?>
                            </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
    <?php include 'includes/footer.php'; ?>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
