<?php
require_once __DIR__ . '/db.php';

// Handle status updates from form if posted
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) {
    $code = trim($_POST['order_code']);
    $newStatus = trim($_POST['status']);
    if ($code && $newStatus) {
        $stmt = $pdo->prepare("UPDATE orders SET status = :status WHERE order_code = :order_code");
        $stmt->execute([':status' => $newStatus, ':order_code' => $code]);
        $message = "የትዕዛዝ ቁጥር #$code ሁኔታ ወደ '$newStatus' ተቀይሯል!";
    }
}

// Fetch orders with optional search
$search = trim($_GET['q'] ?? '');
if ($search) {
    $stmt = $pdo->prepare("
        SELECT * FROM orders 
        WHERE order_code LIKE :q OR phone LIKE :q OR pickup LIKE :q OR dropoff LIKE :q
        ORDER BY id DESC
    ");
    $stmt->execute([':q' => "%$search%"]);
} else {
    $stmt = $pdo->query("SELECT * FROM orders ORDER BY id DESC LIMIT 150");
}
$orders = $stmt->fetchAll();

// Statistics
$totalCount = $pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn();
$pendingCount = $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'Pending'")->fetchColumn();
$inTransitCount = $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'In Transit' OR status = 'Picked Up'")->fetchColumn();
$deliveredCount = $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'Delivered'")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="am">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Yellow Delivery — Admin Dashboard</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&family=Noto+Sans+Ethiopic:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --yellow: #F59E0B;
            --yellow-light: #FEF3C7;
            --dark: #111827;
            --gray: #F3F4F6;
            --border: #E5E7EB;
        }
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: 'Plus Jakarta Sans', 'Noto Sans Ethiopic', sans-serif; background: #F8F9FB; color: #111827; }
        header { background: #FFFFFF; border-bottom: 1px solid var(--border); padding: 16px 5%; display: flex; align-items: center; justify-content: space-between; }
        .logo-box { display: flex; align-items: center; gap: 12px; }
        .logo-box img { width: 44px; height: 44px; border-radius: 8px; object-fit: contain; }
        .logo-box h1 { font-size: 1.3rem; font-weight: 800; }
        .nav-links a { color: #4B5563; text-decoration: none; font-weight: 600; font-size: 0.9rem; margin-left: 16px; }
        .nav-links a:hover { color: var(--yellow); }
        .container { max-width: 1280px; margin: 32px auto; padding: 0 20px; }
        .stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; margin-bottom: 32px; }
        .stat-card { background: #FFFFFF; padding: 22px; border-radius: 14px; border: 1px solid var(--border); }
        .stat-card.alert { border-left: 5px solid var(--yellow); }
        .stat-card h3 { font-size: 0.82rem; color: #6B7280; text-transform: uppercase; letter-spacing: 0.05em; font-weight: 700; margin-bottom: 6px; }
        .stat-card .num { font-size: 2rem; font-weight: 800; color: #111827; }
        .toolbar { background: #FFFFFF; padding: 16px 20px; border-radius: 12px; border: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; gap: 12px; flex-wrap: wrap; }
        .search-form { display: flex; gap: 8px; flex: 1; max-width: 450px; }
        .search-form input { flex: 1; padding: 10px 14px; border-radius: 8px; border: 1px solid var(--border); font-family: inherit; font-size: 0.9rem; }
        .btn-search { background: var(--dark); color: white; border: none; padding: 10px 18px; border-radius: 8px; cursor: pointer; font-weight: 700; }
        .table-wrap { background: #FFFFFF; border-radius: 14px; border: 1px solid var(--border); overflow-x: auto; box-shadow: 0 4px 12px rgba(0,0,0,0.03); }
        table { width: 100%; border-collapse: collapse; text-align: left; font-size: 0.9rem; }
        th { background: #F9FAFB; padding: 14px 18px; font-weight: 700; color: #4B5563; border-bottom: 1px solid var(--border); font-size: 0.8rem; text-transform: uppercase; }
        td { padding: 16px 18px; border-bottom: 1px solid var(--border); vertical-align: middle; }
        tr:hover { background: #FFFDF5; }
        .badge { display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 0.75rem; font-weight: 800; }
        .badge.Pending { background: #FEF3C7; color: #92400E; }
        .badge.In_Transit, .badge.Picked_Up { background: #DBEAFE; color: #1E40AF; }
        .badge.Delivered { background: #D1FAE5; color: #065F46; }
        .badge.Cancelled { background: #FEE2E2; color: #991B1B; }
        .action-select { padding: 6px 10px; border-radius: 6px; border: 1px solid var(--border); font-size: 0.82rem; font-family: inherit; }
        .btn-update { background: var(--yellow); color: #111827; border: none; padding: 6px 12px; border-radius: 6px; font-weight: 700; cursor: pointer; }
        .alert-bar { background: #D1FAE5; color: #065F46; padding: 12px 20px; border-radius: 8px; margin-bottom: 20px; font-weight: 600; }
        .call-link { color: #059669; font-weight: 700; text-decoration: none; display: inline-flex; align-items: center; gap: 4px; }
    </style>
</head>
<body>

<header>
    <div class="logo-box">
        <img src="yellow_logo.jpg" alt="Yellow Delivery" onerror="this.src='yellow_banner.jpg'" />
        <div>
            <h1>Yellow Delivery — Admin Control</h1>
            <p style="font-size:0.75rem; color:#6B7280;">Shashemene HQ • Office No 12</p>
        </div>
    </div>
    <div class="nav-links">
        <a href="index.html">🌐 ዋና ድረ-ገጽ (Main Site)</a>
        <a href="track.html">🔍 ትራኪንግ (Tracking)</a>
        <a href="order.html">📦 ትዕዛዝ መስጫ (Orders)</a>
    </div>
</header>

<div class="container">
    <?php if ($message): ?>
        <div class="alert-bar">✅ <?= htmlspecialchars($message) ?></div>
    <?php endif; ?>

    <!-- Summary Statistics -->
    <div class="stats-grid">
        <div class="stat-card">
            <h3>ጠቅላላ ትዕዛዞች (Total)</h3>
            <div class="num"><?= $totalCount ?></div>
        </div>
        <div class="stat-card alert">
            <h3>አዲስ ያልተነሱ (Pending)</h3>
            <div class="num"><?= $pendingCount ?></div>
        </div>
        <div class="stat-card">
            <h3>በመንገድ ላይ (In Transit)</h3>
            <div class="num"><?= $inTransitCount ?></div>
        </div>
        <div class="stat-card">
            <h3>የደረሱ (Delivered)</h3>
            <div class="num"><?= $deliveredCount ?></div>
        </div>
    </div>

    <!-- Search & Filter Toolbar -->
    <div class="toolbar">
        <form class="search-form" method="GET">
            <input type="text" name="q" placeholder="በትዕዛዝ ቁጥር (YD-...)፣ ስልክ ወይም አድራሻ ፈልግ..." value="<?= htmlspecialchars($search) ?>" />
            <button type="submit" class="btn-search">ፈልግ</button>
            <?php if ($search): ?>
                <a href="admin.php" style="padding:10px; font-size:0.85rem; color:#EF4444; text-decoration:none;">አጽዳ</a>
            <?php endif; ?>
        </form>
        <div style="font-size:0.88rem; color:#6B7280;">
            የሚታዩ ትዕዛዞች፡ <strong><?= count($orders) ?></strong>
        </div>
    </div>

    <!-- Orders Table -->
    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Tracking Code</th>
                    <th>አገልግሎት</th>
                    <th>መነሻ &rarr; መድረሻ</th>
                    <th>ስልክ ቁጥር</th>
                    <th>ዋጋ (Price)</th>
                    <th>ማስታወሻ</th>
                    <th>ቀን &amp; ሰዓት</th>
                    <th>ሁኔታ (Status)</th>
                    <th>አስተዳድር</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($orders)): ?>
                    <tr>
                        <td colspan="9" style="text-align:center; padding:36px; color:#9CA3AF;">
                            ምንም የተመዘገበ ትዕዛዝ አልተገኘም (No orders found).
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($orders as $o): ?>
                        <?php 
                            $statusClass = str_replace(' ', '_', $o['status']);
                        ?>
                        <tr>
                            <td>
                                <strong style="font-family:monospace; font-size:0.95rem; color:#111827;"><?= htmlspecialchars($o['order_code']) ?></strong>
                            </td>
                            <td>
                                <strong><?= htmlspecialchars($o['service']) ?></strong>
                            </td>
                            <td>
                                <div>📍 <strong>መነሻ:</strong> <?= htmlspecialchars($o['pickup']) ?></div>
                                <div style="color:#4B5563;">🏁 <strong>መድረሻ:</strong> <?= htmlspecialchars($o['dropoff']) ?></div>
                            </td>
                            <td>
                                <a href="tel:<?= htmlspecialchars($o['phone']) ?>" class="call-link">
                                    📞 <?= htmlspecialchars($o['phone']) ?>
                                </a>
                            </td>
                            <td>
                                <strong style="color:#059669; font-size:0.95rem;"><?= !empty($o['total_price']) ? htmlspecialchars($o['total_price']) . ' ETB' : '—' ?></strong>
                            </td>
                            <td style="max-width:220px; font-size:0.82rem; color:#4B5563;">
                                <?= htmlspecialchars($o['notes'] ?? '—') ?>
                            </td>
                            <td style="font-size:0.8rem; color:#6B7280; white-space:nowrap;">
                                <?= htmlspecialchars($o['created_at']) ?>
                            </td>
                            <td>
                                <span class="badge <?= $statusClass ?>">
                                    <?= htmlspecialchars($o['status']) ?>
                                </span>
                            </td>
                            <td>
                                <form method="POST" style="display:flex; gap:6px;">
                                    <input type="hidden" name="order_code" value="<?= htmlspecialchars($o['order_code']) ?>" />
                                    <input type="hidden" name="update_status" value="1" />
                                    <select name="status" class="action-select">
                                        <option value="Pending" <?= $o['status'] === 'Pending' ? 'selected' : '' ?>>Pending</option>
                                        <option value="Picked Up" <?= $o['status'] === 'Picked Up' ? 'selected' : '' ?>>Picked Up</option>
                                        <option value="In Transit" <?= $o['status'] === 'In Transit' ? 'selected' : '' ?>>In Transit</option>
                                        <option value="Delivered" <?= $o['status'] === 'Delivered' ? 'selected' : '' ?>>Delivered</option>
                                        <option value="Cancelled" <?= $o['status'] === 'Cancelled' ? 'selected' : '' ?>>Cancelled</option>
                                    </select>
                                    <button type="submit" class="btn-update">ቀይር</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

</body>
</html>



