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

$adminPageTitle = 'Dashboard';
$db = getDB();

$totalProducts = $db->query("SELECT COUNT(*) FROM products")->fetchColumn();
$availableProducts = $db->query("SELECT COUNT(*) FROM products WHERE selling_status='available'")->fetchColumn();
$outOfStock = $db->query("SELECT COUNT(*) FROM products WHERE selling_status='out_of_stock'")->fetchColumn();
$comingSoon = $db->query("SELECT COUNT(*) FROM products WHERE selling_status='coming_soon'")->fetchColumn();

$recentProducts = $db->query(
    "SELECT p.*, (SELECT image_path FROM product_images WHERE product_id = p.id ORDER BY sort_order LIMIT 1) AS main_image
     FROM products p ORDER BY p.created_at DESC LIMIT 8"
)->fetchAll();

include __DIR__ . '/partials/admin_header.php';
?>

<!-- Stats Row -->
<div class="row g-4 mb-4">
    <div class="col-md-3 col-sm-6">
        <div class="stat-card d-flex align-items-center gap-3">
            <div class="stat-icon" style="background:#e8f0fe;"><i class="fas fa-box text-primary"></i></div>
            <div>
                <div class="fs-2 fw-bold"><?= $totalProducts ?></div>
                <div class="text-muted small">Total Products</div>
            </div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="stat-card d-flex align-items-center gap-3">
            <div class="stat-icon" style="background:#e8f8f0;"><i class="fas fa-check-circle text-success"></i></div>
            <div>
                <div class="fs-2 fw-bold text-success"><?= $availableProducts ?></div>
                <div class="text-muted small">Available</div>
            </div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="stat-card d-flex align-items-center gap-3">
            <div class="stat-icon" style="background:#fdecea;"><i class="fas fa-times-circle text-danger"></i></div>
            <div>
                <div class="fs-2 fw-bold text-danger"><?= $outOfStock ?></div>
                <div class="text-muted small">Out of Stock</div>
            </div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="stat-card d-flex align-items-center gap-3">
            <div class="stat-icon" style="background:#fff8e1;"><i class="fas fa-clock text-warning"></i></div>
            <div>
                <div class="fs-2 fw-bold text-warning"><?= $comingSoon ?></div>
                <div class="text-muted small">Coming Soon</div>
            </div>
        </div>
    </div>
</div>

<!-- Quick Actions -->
<div class="row g-3 mb-4">
    <div class="col-auto">
        <a href="<?= SITE_URL ?>/admin/product_add.php" class="btn btn-primary">
            <i class="fas fa-plus me-2"></i>Add New Product
        </a>
    </div>
    <div class="col-auto">
        <a href="<?= SITE_URL ?>/shop.php" target="_blank" class="btn btn-outline-secondary">
            <i class="fas fa-external-link-alt me-2"></i>View Shop
        </a>
    </div>
    <div class="col-auto">
        <a href="<?= SITE_URL ?>/admin/phones.php" class="btn btn-outline-info">
            <i class="fas fa-phone me-2"></i>Manage Phone Numbers
        </a>
    </div>
</div>

<!-- Recent Products Table -->
<div class="table-card">
    <div class="card-header d-flex align-items-center justify-content-between">
        <span><i class="fas fa-list me-2 text-primary"></i>Recent Products</span>
        <a href="<?= SITE_URL ?>/admin/products.php" class="btn btn-sm btn-outline-primary">View All</a>
    </div>
    <div class="table-responsive">
        <table class="table table-hover mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th style="width:60px;">Image</th>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Color</th>
                    <th>Status</th>
                    <th>Images</th>
                    <th>Added</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($recentProducts)): ?>
                <tr>
                    <td colspan="8" class="text-center py-4 text-muted">
                        <i class="fas fa-box-open fa-2x mb-2 d-block"></i>No products yet. <a href="<?= SITE_URL ?>/admin/product_add.php">Add one!</a>
                    </td>
                </tr>
                <?php else: ?>
                <?php foreach ($recentProducts as $p): ?>
                <tr>
                    <td>
                        <?php if ($p['main_image']): ?>
                            <img src="<?= UPLOAD_URL . sanitize($p['main_image']) ?>" class="product-thumb" alt="">
                        <?php else: ?>
                            <div class="product-thumb d-flex align-items-center justify-content-center">
                                <i class="fas fa-image text-muted"></i>
                            </div>
                        <?php endif; ?>
                    </td>
                    <td class="fw-semibold"><?= sanitize($p['name']) ?></td>
                    <td class="text-primary fw-bold"><?= formatPrice($p['price']) ?></td>
                    <td><?= sanitize($p['color'] ?: '—') ?></td>
                    <td><?= getStatusBadge($p['selling_status']) ?></td>
                    <td>
                        <?php
                        $imgCount = $db->prepare("SELECT COUNT(*) FROM product_images WHERE product_id = ?");
                        $imgCount->execute([$p['id']]);
                        echo $imgCount->fetchColumn();
                        ?>/<?= MAX_PRODUCT_IMAGES ?>
                    </td>
                    <td class="text-muted small"><?= date('d M Y', strtotime($p['created_at'])) ?></td>
                    <td>
                        <a href="<?= SITE_URL ?>/product.php?id=<?= $p['id'] ?>" class="btn btn-action btn-outline-info me-1" target="_blank" title="View">
                            <i class="fas fa-eye"></i>
                        </a>
                        <a href="<?= SITE_URL ?>/admin/product_edit.php?id=<?= $p['id'] ?>" class="btn btn-action btn-outline-primary me-1" title="Edit">
                            <i class="fas fa-edit"></i>
                        </a>
                        <a href="<?= SITE_URL ?>/admin/product_delete.php?id=<?= $p['id'] ?>"
                           class="btn btn-action btn-outline-danger"
                           onclick="return confirm('Delete this product and all its images?')"
                           title="Delete">
                            <i class="fas fa-trash"></i>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php include __DIR__ . '/partials/admin_footer.php'; ?>
