<?php
require_once 'init.php';

$subsStats = $subscription->getSubscriptionStats($_SESSION['user_id']);
$todaySales = $transaction->getDailySales($_SESSION['user_id'], date(DATE_FORMAT));
$totalProducts = $product->getCount($_SESSION['user_id']);
$lowStockProducts = $product->getLowStock($_SESSION['user_id'], 10);

$pageTitle = 'Dashboard - KasirMurah.biz.id';
?>
<?php include 'layout/header.php'; ?>
    <?php include 'layout/navbar.php'; ?>

    <div class="container-fluid mt-4 px-3 px-md-4" style="max-width:1100px;margin-left:auto;margin-right:auto;">

        <!-- Header -->
        <div class="page-header mb-3">
            <div>
                <h1>Dashboard</h1>
                <p class="text-muted mb-0" style="font-size:14px">Selamat datang, <?php echo htmlspecialchars($_SESSION['name']); ?></p>
            </div>
            <a href="pos/index.php" class="btn btn-primary btn-lg">
                <i class="fas fa-cash-register me-1"></i> Buka Kasir
            </a>
        </div>

        <!-- Subscription banner -->
        <?php if (!empty($subsStats['subscription']['status']) && $subsStats['subscription']['status'] === 'trial'): ?>
        <div class="sub-banner trial mb-4">
            <i class="fas fa-crown"></i>
            <div>
                <strong>Periode Trial</strong>
                <div style="font-size:13px">Sisa <strong><?php echo $subsStats['days_remaining']; ?> hari</strong> trial.
                    <a href="subscription/upgrade.php" class="ms-2" style="color:var(--primary);font-weight:600">Upgrade sekarang →</a>
                </div>
            </div>
        </div>
        <?php elseif (!empty($subsStats['subscription']['status']) && $subsStats['subscription']['status'] === 'active'): ?>
        <div class="sub-banner active mb-4">
            <i class="fas fa-check-circle"></i>
            <div>
                <strong>Langganan Aktif</strong>
                <div style="font-size:13px">Berakhir dalam <strong><?php echo $subsStats['days_remaining']; ?> hari</strong></div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Stats -->
        <div class="row g-3 mb-4">
            <div class="col-6 col-md-3">
                <div class="stat-card purple">
                    <div class="stat-icon"><i class="fas fa-receipt"></i></div>
                    <div>
                        <div class="stat-value"><?php echo $todaySales['total_transactions']; ?></div>
                        <div class="stat-label">Transaksi Hari Ini</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="stat-card green">
                    <div class="stat-icon"><i class="fas fa-dollar-sign"></i></div>
                    <div>
                        <div class="stat-value" style="font-size:18px"><?php echo Helper::formatCurrency($todaySales['total_sales']); ?></div>
                        <div class="stat-label">Penjualan Hari Ini</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="stat-card orange">
                    <div class="stat-icon"><i class="fas fa-box"></i></div>
                    <div>
                        <div class="stat-value"><?php echo $totalProducts; ?></div>
                        <div class="stat-label">Total Produk</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="stat-card red">
                    <div class="stat-icon"><i class="fas fa-exclamation-triangle"></i></div>
                    <div>
                        <div class="stat-value"><?php echo count($lowStockProducts); ?></div>
                        <div class="stat-label">Stok Rendah</div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row g-3">
            <!-- Quick actions -->
            <div class="col-md-6">
                <div class="card h-100">
                    <div class="card-header"><i class="fas fa-bolt me-2" style="color:var(--primary)"></i>Menu Cepat</div>
                    <div class="card-body">
                        <div class="row g-2">
                            <div class="col-6">
                                <a href="pos/index.php" class="quick-btn">
                                    <i class="fas fa-shopping-cart"></i> Penjualan
                                </a>
                            </div>
                            <div class="col-6">
                                <a href="products/index.php" class="quick-btn">
                                    <i class="fas fa-box"></i> Produk
                                </a>
                            </div>
                            <div class="col-6">
                                <a href="subscription/payments.php" class="quick-btn">
                                    <i class="fas fa-money-bill"></i> Pembayaran
                                </a>
                            </div>
                            <div class="col-6">
                                <a href="reports/index.php" class="quick-btn">
                                    <i class="fas fa-chart-bar"></i> Laporan
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Low stock -->
            <div class="col-md-6">
                <div class="card h-100">
                    <div class="card-header"><i class="fas fa-exclamation-triangle me-2" style="color:var(--danger)"></i>Stok Rendah</div>
                    <div class="card-body p-0">
                        <?php if (!empty($lowStockProducts)): ?>
                        <div class="table-responsive">
                            <table class="table mb-0">
                                <thead>
                                    <tr>
                                        <th>Produk</th>
                                        <th class="text-end">Stok</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($lowStockProducts as $prod): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars(Helper::truncate($prod['name'], 32)); ?></td>
                                        <td class="text-end">
                                            <span class="badge bg-danger"><?php echo $prod['quantity']; ?></span>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        <?php else: ?>
                        <div class="p-4 text-center text-muted">
                            <i class="fas fa-check-circle fa-2x mb-2 d-block" style="color:var(--success)"></i>
                            Semua stok produk aman
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <?php include 'layout/footer.php'; ?>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
