<?php
$pageTitle = 'Dashboard';
require_once 'includes/header.php';
require_once 'config/database.php';
requireLogin();

// Get statistics
$today = date('Y-m-d');
$user_id = $_SESSION['user_id'];

// Transaksi hari ini
$transaksiHariIni = query("SELECT COUNT(*) as total, COALESCE(SUM(total), 0) as pendapatan
                           FROM transaksi 
                           WHERE tanggal = ?", 
                           [$today], 's')->fetch_assoc();

// Total layanan aktif
$totalLayanan = query("SELECT COUNT(*) as total FROM layanan WHERE status = 'active'")->fetch_assoc();

// Pengeluaran hari ini (hanya admin)
$pengeluaranHariIni = ['total' => 0];
if (hasRole('admin')) {
    $pengeluaranHariIni = query("SELECT COALESCE(SUM(nominal), 0) as total 
                                FROM pengeluaran 
                                WHERE tanggal = ?", 
                                [$today], 's')->fetch_assoc();
}

// Recent transactions (10 terbaru)
$recentTransactions = query("SELECT t.id, t.tanggal, t.total, t.created_at, u.nama as kasir
                            FROM transaksi t
                            JOIN users u ON t.id_user = u.id
                            ORDER BY t.created_at DESC
                            LIMIT 10");
?>

<div class="px-4 sm:px-0">
    <h1 class="text-3xl font-bold text-gray-900 mb-8">Dashboard</h1>

    <!-- Quick Actions -->
    <?php if (hasRole('loket')): ?>
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
        <!-- Buat Struk -->
        <a href="struk.php" class="bg-gradient-to-r from-blue-500 to-blue-600 rounded-lg shadow-lg p-8 text-white hover:shadow-xl transform hover:-translate-y-1 transition duration-200">
            <div class="flex items-center justify-between">
                <div>
                    <h3 class="text-2xl font-bold mb-2">Buat Struk</h3>
                    <p class="text-blue-100">Catat transaksi penjualan baru</p>
                </div>
                <svg class="w-16 h-16 text-blue-200" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path>
                </svg>
            </div>
        </a>

        <!-- Lihat Riwayat Struk -->
        <a href="riwayat-struk.php" class="bg-gradient-to-r from-green-500 to-green-600 rounded-lg shadow-lg p-8 text-white hover:shadow-xl transform hover:-translate-y-1 transition duration-200">
            <div class="flex items-center justify-between">
                <div>
                    <h3 class="text-2xl font-bold mb-2">Riwayat Struk</h3>
                    <p class="text-green-100">Lihat struk yang sudah disimpan</p>
                </div>
                <svg class="w-16 h-16 text-green-200" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                </svg>
            </div>
        </a>
    </div>
    <?php endif; ?>

    <!-- Statistics -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
        <!-- Transaksi Hari Ini -->
        <div class="bg-white rounded-lg shadow-md p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-500 text-sm font-medium">Transaksi Hari Ini</p>
                    <p class="text-2xl font-bold text-gray-900 mt-2"><?php echo $transaksiHariIni['total']; ?></p>
                    <p class="text-green-600 text-sm mt-1"><?php echo formatRupiah($transaksiHariIni['pendapatan']); ?></p>
                </div>
                <div class="bg-blue-100 rounded-full p-3">
                    <svg class="w-8 h-8 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                    </svg>
                </div>
            </div>
        </div>

        <!-- Total Layanan -->
        <div class="bg-white rounded-lg shadow-md p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-500 text-sm font-medium">Total Layanan</p>
                    <p class="text-2xl font-bold text-gray-900 mt-2"><?php echo $totalLayanan['total']; ?></p>
                    <p class="text-gray-600 text-sm mt-1">Layanan aktif</p>
                </div>
                <div class="bg-purple-100 rounded-full p-3">
                    <svg class="w-8 h-8 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path>
                    </svg>
                </div>
            </div>
        </div>

        <?php if (hasRole('admin')): ?>
        <!-- Pengeluaran Hari Ini -->
        <div class="bg-white rounded-lg shadow-md p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-500 text-sm font-medium">Pengeluaran Hari Ini</p>
                    <p class="text-2xl font-bold text-red-600 mt-2"><?php echo formatRupiah($pengeluaranHariIni['total']); ?></p>
                    <p class="text-gray-600 text-sm mt-1">Total pengeluaran</p>
                </div>
                <div class="bg-red-100 rounded-full p-3">
                    <svg class="w-8 h-8 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"></path>
                    </svg>
                </div>
            </div>
        </div>
        <?php else: ?>
        <!-- Tanggal -->
        <div class="bg-white rounded-lg shadow-md p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-gray-500 text-sm font-medium">Tanggal</p>
                    <p class="text-xl font-bold text-gray-900 mt-2"><?php echo formatTanggal($today); ?></p>
                    <p class="text-gray-600 text-sm mt-1" id="currentTime"></p>
                </div>
                <div class="bg-orange-100 rounded-full p-3">
                    <svg class="w-8 h-8 text-orange-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                    </svg>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Recent Transactions -->
    <div class="bg-white rounded-lg shadow-md p-6">
        <h2 class="text-xl font-semibold text-gray-900 mb-4">Transaksi Terbaru</h2>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">ID</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Tanggal</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Total</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Kasir</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Aksi</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php if ($recentTransactions->num_rows > 0): ?>
                        <?php while ($row = $recentTransactions->fetch_assoc()): ?>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">#<?php echo str_pad($row['id'], 6, '0', STR_PAD_LEFT); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600">
                                <?php echo date('d/m/Y H:i', strtotime($row['created_at'])); ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold text-green-600">
                                <?php echo formatRupiah($row['total']); ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600">
                                <?php echo htmlspecialchars($row['kasir']); ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <a href="print-struk.php?id=<?php echo $row['id']; ?>" target="_blank" class="text-blue-600 hover:text-blue-800">
                                    Lihat Struk
                                </a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="5" class="px-6 py-4 text-center text-gray-500">Belum ada transaksi</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
// Update current time (jika user loket)
<?php if (hasRole('loket')): ?>
function updateTime() {
    const now = new Date();
    const timeString = now.toLocaleTimeString('id-ID', { hour: '2-digit', minute: '2-digit', second: '2-digit' });
    const timeEl = document.getElementById('currentTime');
    if (timeEl) timeEl.textContent = timeString;
}
updateTime();
setInterval(updateTime, 1000);
<?php endif; ?>
</script>

<?php require_once 'includes/footer.php'; ?>
