<?php
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requireLogin();
$page_title = 'Dashboard';

include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper">
    <div class="container-fluid">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h4 class="page-title mb-0"><i class="fas fa-tachometer-alt me-2"></i>Dashboard</h4>
            <div class="d-flex align-items-center gap-2">
                <small class="text-muted">Last updated: <span id="dashboard-last-updated">--</span></small>
                <button id="refresh-dashboard-btn" class="btn btn-sm btn-outline-primary"><i class="fas fa-sync-alt me-1"></i>Refresh Now</button>
            </div>
        </div>

        <div id="dashboard-error"></div>

        <!-- Stats Cards -->
        <div class="row">
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-primary-gradient">
                    <div class="stat-icon"><i class="fas fa-shopping-cart"></i></div>
                    <div class="stat-value" id="stat-today-sales">--</div>
                    <div class="stat-label">Today's Sales</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-success-gradient">
                    <div class="stat-icon"><i class="fas fa-tools"></i></div>
                    <div class="stat-value" id="stat-repair-income">--</div>
                    <div class="stat-label">Repair Income Today</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-warning-gradient">
                    <div class="stat-icon"><i class="fas fa-clock"></i></div>
                    <div class="stat-value" id="stat-pending-repairs">--</div>
                    <div class="stat-label">Pending Repairs</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-info-gradient">
                    <div class="stat-icon"><i class="fas fa-check-circle"></i></div>
                    <div class="stat-value" id="stat-completed-repairs">--</div>
                    <div class="stat-label">Completed Repairs</div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-danger-gradient">
                    <div class="stat-icon"><i class="fas fa-exclamation-triangle"></i></div>
                    <div class="stat-value" id="stat-low-stock">--</div>
                    <div class="stat-label">Low Stock Items</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-purple-gradient">
                    <div class="stat-icon"><i class="fas fa-users"></i></div>
                    <div class="stat-value" id="stat-total-customers">--</div>
                    <div class="stat-label">Total Customers</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-teal-gradient">
                    <div class="stat-icon"><i class="fas fa-file-invoice"></i></div>
                    <div class="stat-value" id="stat-total-invoices">--</div>
                    <div class="stat-label">Total Invoices</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-pink-gradient">
                    <div class="stat-icon"><i class="fas fa-chart-line"></i></div>
                    <div class="stat-value" id="stat-net-profit">--</div>
                    <div class="stat-label">Estimated Net Profit (Today)</div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-secondary-gradient">
                    <div class="stat-icon"><i class="fas fa-boxes"></i></div>
                    <div class="stat-value" id="stat-total-products">--</div>
                    <div class="stat-label">Total Products</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-orange-gradient">
                    <div class="stat-icon"><i class="fas fa-truck"></i></div>
                    <div class="stat-value" id="stat-today-purchases">--</div>
                    <div class="stat-label">Today Purchases</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-danger-gradient">
                    <div class="stat-icon"><i class="fas fa-hand-holding-usd"></i></div>
                    <div class="stat-value" id="stat-supplier-due">--</div>
                    <div class="stat-label">Supplier Due</div>
                </div>
            </div>
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-warning-gradient">
                    <div class="stat-icon"><i class="fas fa-users-cog"></i></div>
                    <div class="stat-value" id="stat-customer-due">--</div>
                    <div class="stat-label">Customer Due</div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="stat-card bg-info-gradient">
                    <div class="stat-icon"><i class="fas fa-money-bill-wave"></i></div>
                    <div class="stat-value" id="stat-today-expenses">--</div>
                    <div class="stat-label">Today Expenses</div>
                </div>
            </div>
        </div>

        <!-- Charts Row -->
        <div class="row">
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header"><i class="fas fa-chart-line me-2"></i>Monthly Sales <?= date('Y') ?></div>
                    <div class="card-body">
                        <canvas id="monthlySalesChart" height="250"></canvas>
                    </div>
                </div>
            </div>
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header"><i class="fas fa-chart-pie me-2"></i>Repair Status</div>
                    <div class="card-body">
                        <canvas id="repairStatusChart" height="250"></canvas>
                    </div>
                </div>
            </div>
        </div>

        <!-- Tables Row -->
        <div class="row">
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header"><i class="fas fa-file-invoice me-2"></i>Recent Sales</div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-sm no-datatable">
                                <thead><tr><th>Invoice</th><th>Customer</th><th>Amount</th><th>Status</th><th>Date</th></tr></thead>
                                <tbody id="recent-sales-tbody">
                                    <tr><td colspan="5" class="text-muted">Loading...</td></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header"><i class="fas fa-tools me-2"></i>Recent Repairs</div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-sm no-datatable">
                                <thead><tr><th>Job #</th><th>Customer</th><th>Status</th><th>Date</th></tr></thead>
                                <tbody id="recent-repairs-tbody">
                                    <tr><td colspan="4" class="text-muted">Loading...</td></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header"><i class="fas fa-exclamation-triangle me-2"></i>Low Stock Products</div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-sm no-datatable">
                                <thead><tr><th>Product</th><th>Current Stock</th><th>Min Stock</th></tr></thead>
                                <tbody id="low-stock-tbody">
                                    <tr><td colspan="3" class="text-muted">Loading...</td></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
var BASE_URL = '<?= BASE_URL ?>';
</script>
<script src="<?= BASE_URL ?>assets/js/dashboard.js"></script>

<?php include __DIR__ . '/../includes/footer.php'; ?>
