<?php
// Statistics
$total_items = $db->query("SELECT COUNT(*) FROM inventory_items WHERE is_active = 1")->fetchColumn();

$total_value_stmt = $db->query("SELECT SUM(quantity * cost_price) FROM inventory_batches");
$total_value = $total_value_stmt->fetchColumn() ?: 0;

// Low Stock
$low_stock_count = $db->query("
    SELECT COUNT(*) 
    FROM inventory_items i 
    WHERE (SELECT SUM(quantity) FROM inventory_batches b WHERE b.item_id = i.id) <= i.min_level
")->fetchColumn();

// Expiring Soon (30 days)
$expiring_count = $db->query("
    SELECT COUNT(*) 
    FROM inventory_batches 
    WHERE expiry_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 30 DAY) 
    AND quantity > 0
")->fetchColumn();

// Recent Transactions
$recent_transactions = $db->query("
    SELECT t.*, i.name_en as item_name 
    FROM inventory_transactions t 
    JOIN inventory_items i ON t.item_id = i.id 
    ORDER BY t.transaction_date DESC 
    LIMIT 5
")->fetchAll(PDO::FETCH_ASSOC);

?>

<h2 class="mb-4 fw-bold text-dark"><?php echo __('inventory_dashboard'); ?></h2>

<div class="row g-4 mb-4">
    <div class="col-md-3">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-body text-center">
                <div class="text-primary mb-2"><i class="bi bi-box-seam fs-1"></i></div>
                <h5 class="card-title text-muted"><?php echo __('total_items'); ?></h5>
                <h2 class="fw-bold"><?php echo number_format($total_items); ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-body text-center">
                <div class="text-success mb-2"><i class="bi bi-cash-stack fs-1"></i></div>
                <h5 class="card-title text-muted"><?php echo __('total_value'); ?></h5>
                <h2 class="fw-bold"><?php echo number_format($total_value, 2); ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-body text-center">
                <div class="text-warning mb-2"><i class="bi bi-exclamation-triangle fs-1"></i></div>
                <h5 class="card-title text-muted"><?php echo __('low_stock_items'); ?></h5>
                <h2 class="fw-bold"><?php echo number_format($low_stock_count); ?></h2>
                <a href="inventory_items.php" class="small text-decoration-none stretched-link"><?php echo __('view_details'); ?></a>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-body text-center">
                <div class="text-danger mb-2"><i class="bi bi-calendar-x fs-1"></i></div>
                <h5 class="card-title text-muted"><?php echo __('expiring_soon'); ?></h5>
                <h2 class="fw-bold"><?php echo number_format($expiring_count); ?></h2>
                <a href="inventory_reports.php?report=expiry" class="small text-decoration-none stretched-link"><?php echo __('view_details'); ?></a>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card shadow-sm border-0">
            <div class="card-header bg-white">
                <h5 class="mb-0 fw-bold text-dark"><?php echo __('recent_transactions'); ?></h5>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0 align-middle">
                        <thead class="table-light">
                            <tr>
                                <th><?php echo __('date'); ?></th>
                                <th><?php echo __('item'); ?></th>
                                <th><?php echo __('type'); ?></th>
                                <th><?php echo __('qty'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recent_transactions as $t): ?>
                            <tr>
                                <td><?php echo date('M d, H:i', strtotime($t['transaction_date'])); ?></td>
                                <td><?php echo htmlspecialchars($t['item_name']); ?></td>
                                <td>
                                    <?php if ($t['transaction_type'] === 'in'): ?>
                                        <span class="badge bg-success-subtle text-success">IN</span>
                                    <?php else: ?>
                                        <span class="badge bg-danger-subtle text-danger">OUT</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $t['quantity']; ?></td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if (empty($recent_transactions)): ?>
                            <tr><td colspan="4" class="text-center text-muted py-3"><?php echo __('no_recent_transactions'); ?></td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="card-footer bg-white text-center">
                <a href="inventory_transactions.php" class="text-decoration-none"><?php echo __('view_all_transactions'); ?></a>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <!-- Quick Actions -->
        <div class="card shadow-sm border-0 mb-4">
            <div class="card-header bg-white">
                <h5 class="mb-0 fw-bold text-dark"><?php echo __('quick_actions'); ?></h5>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="inventory_items.php" class="btn btn-outline-primary"><i class="bi bi-plus-lg me-2"></i> <?php echo __('add_new_item'); ?></a>
                    <a href="inventory_transactions.php" class="btn btn-outline-success"><i class="bi bi-box-arrow-in-down me-2"></i> <?php echo __('receive_stock'); ?></a>
                    <a href="inventory_transactions.php" class="btn btn-outline-danger"><i class="bi bi-box-arrow-up me-2"></i> <?php echo __('issue_stock'); ?></a>
                </div>
            </div>
        </div>
    </div>
</div>
