<?php
session_start();
// Pastikan koneksi terhubung
include 'koneksi.php';

$profil = []; // Inisialisasi awal agar tidak error jika database kosong
$query_profil = mysqli_query($koneksi, "SELECT * FROM profil_perusahaan LIMIT 1");
if ($query_profil && mysqli_num_rows($query_profil) > 0) {
    $profil = mysqli_fetch_array($query_profil);
}

if (!isset($_SESSION['status']) || $_SESSION['status'] != "login") {
    header("Location: login.php?pesan=belum_login");
    exit();
}

// --- 1. DATA PROFIL PERUSAHAAN ---
$nama_pt = "BEST PROCUREMENT";
$logo_pt = "assets/logosys.png";

$query_profil = mysqli_query($koneksi, "SELECT * FROM profil_perusahaan LIMIT 1");
if ($query_profil && mysqli_num_rows($query_profil) > 0) {
    $profil = mysqli_fetch_array($query_profil);
    $nama_pt = $profil['nama_perusahaan'];
    $logo_pt = (!empty($profil['logo'])) ? $profil['logo'] : "assets/logosys.png";
}

// --- 2. DATA STATISTIK DASAR ---
$count_supplier = 0;
$res_supp = mysqli_query($koneksi, "SELECT id FROM supplier");
if ($res_supp) { $count_supplier = mysqli_num_rows($res_supp); }

$count_po = 0;
$res_po = mysqli_query($koneksi, "SELECT no_po FROM po_utama");
if ($res_po) { $count_po = mysqli_num_rows($res_po); }

// --- 3. DATA BAR CHART (X: Bulan, Y: Nilai Transaksi) ---
$label_bulan = ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agt", "Sep", "Okt", "Nov", "Des"];
$data_bulanan = array_fill(0, 12, 0);

$query_grafik = mysqli_query($koneksi, "SELECT MONTH(tanggal_po) as bulan, SUM(total) as total 
                                       FROM po_utama 
                                       WHERE YEAR(tanggal_po) = YEAR(CURDATE()) 
                                       GROUP BY MONTH(tanggal_po)");
if ($query_grafik) {
    while($row = mysqli_fetch_array($query_grafik)) {
        $idx = (int)$row['bulan'] - 1;
        $data_bulanan[$idx] = (float)$row['total'];
    }
}

// --- 4. DATA PIE CHART (Supplier) ---
$nama_supp_pie = []; $nilai_transaksi_pie = [];
$query_pie = mysqli_query($koneksi, "SELECT s.nama_supplier, SUM(p.total) as total_nilai 
                                     FROM po_utama p 
                                     JOIN supplier s ON p.id_supplier = s.id 
                                     GROUP BY s.id ORDER BY total_nilai DESC LIMIT 5");
if ($query_pie) {
    while($p = mysqli_fetch_array($query_pie)) {
        $nama_supp_pie[] = $p['nama_supplier'];
        $nilai_transaksi_pie[] = (float)$p['total_nilai'];
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - <?php echo $nama_pt; ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        :root { 
            --bg-sidebar: linear-gradient(180deg, #e60000 0%, #000000 100%); 
            --accent-red: #b71c1c; 
            --text-gray: #b0b0b0;
        }
        
        body { margin: 0; font-family: 'Inter', sans-serif; display: flex; background-color: #f4f7f6; height: 100vh; overflow: hidden; }
        
        /* SIDEBAR FIX */
        aside { 
            width: 260px; 
            background: var(--bg-sidebar); 
            color: white; 
            display: flex; 
            flex-direction: column; 
            flex-shrink: 0; 
            z-index: 100;
        }
        
        .brand { 
            padding: 30px 25px; 
            text-align: center; 
            border-bottom: 1px solid rgba(255,255,255,0.1); 
            margin-bottom: 10px;
        }
        
        .brand img { max-width: 200px; }
        
        .nav-category { 
            padding: 20px 25px 5px; 
            font-size: 11px; 
            color: rgba(255,255,255,0.5); 
            text-transform: uppercase; 
            font-weight: bold; 
            letter-spacing: 1px;
        }
        
        .nav-item { 
            padding: 14px 25px; 
            display: flex; 
            align-items: center; 
            gap: 15px; 
            text-decoration: none; 
            color: var(--text-gray); 
            font-size: 14px; 
            transition: 0.3s; 
            border-left: 4px solid transparent; 
        }
        
        .nav-item:hover, .nav-item.active { 
            background: rgba(255,255,255,0.1); 
            color: white; 
            border-left: 4px solid #ffffff; 
        }
        
        .logout-area { 
            margin-top: auto; 
            padding-bottom: 20px; 
            border-top: 1px solid rgba(255,255,255,0.1); 
        }

        /* CONTENT LAYOUT */
        main { flex-grow: 1; display: flex; flex-direction: column; overflow-y: auto; }
        header { 
            background: white; 
            padding: 18px 30px; 
            display: flex; 
            justify-content: space-between; 
            align-items: center; 
            box-shadow: 0 2px 5px rgba(0,0,0,0.05); 
        }
        
        .content-body { padding: 30px; }

        /* CARD & CHART */
        .grid-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 25px; margin-bottom: 30px; }
        .card { 
            background: white; 
            padding: 25px; 
            border-radius: 15px; 
            display: flex; 
            align-items: center; 
            gap: 20px; 
            box-shadow: 0 4px 15px rgba(0,0,0,0.05); 
            border-left: 6px solid var(--accent-red); 
        }
        
        .chart-container { display: grid; grid-template-columns: 1.6fr 1fr; gap: 25px; }
        .chart-box { 
            background: white; 
            padding: 25px; 
            border-radius: 18px; 
            box-shadow: 0 10px 30px rgba(0,0,0,0.08); 
        }
        
        h3 { margin: 0; font-size: 24px; color: #333; }
        p { margin: 0; color: #888; font-size: 13px; }
    </style>
</head>
<body>

    <aside>
        <div class="brand"><img src="assets/logosys.png" alt="Logo"></div>
    
    
        
        <div class="nav-category">Menu Utama</div>
        <a href="dashboard.php" class="nav-item active"><i class="fa-solid fa-gauge"></i> Dashboard</a>
        <a href="supplier.php" class="nav-item"><i class="fa-solid fa-truck-field"></i> Supplier</a>
        
        <div class="nav-category">Laporan</div>
        <a href="daftar_po.php" class="nav-item"><i class="fa-solid fa-file-invoice"></i> Daftar PO</a>
        
        <div class="nav-category">Admin</div>
        <a href="profil_perusahaan.php" class="nav-item"><i class="fa-solid fa-building-shield"></i> Profil Perusahaan</a>
        
        <div class="logout-area">
            <a href="logout.php" class="nav-item" style="color: #ff5252;"><i class="fa-solid fa-power-off"></i> Logout</a>
        </div>
    </aside>

    <main>
        <header>
            <div style="font-weight: 600;">Dashboard Panel - <?php echo $nama_pt; ?></div>
            <div style="font-size: 13px;">User: <strong style="color:var(--accent-red)"><?php echo $_SESSION['username'] ?? 'Admin'; ?></strong></div>
        </header>

        <div class="content-body">
            <div class="grid-stats">
                <div class="card"><i class="fa-solid fa-users" style="color:#ddd; font-size:30px;"></i><div><h3><?php echo $count_supplier; ?></h3><p>Total Supplier</p></div></div>
                <div class="card" style="border-left-color: #007bff;"><i class="fa-solid fa-file-invoice-dollar" style="color:#ddd; font-size:30px;"></i><div><h3><?php echo $count_po; ?></h3><p>Purchase Order</p></div></div>
            </div>

            <div class="chart-container">
                <div class="chart-box">
                    <h4><i class="fa-solid fa-chart-column"></i> Tren Transaksi Bulanan</h4>
                    <div style="height: 300px;"><canvas id="barChart"></canvas></div>
                </div>
                <div class="chart-box">
                    <h4><i class="fa-solid fa-chart-pie"></i> Per Supplier</h4>
                    <div style="height: 300px;"><canvas id="pieChart"></canvas></div>
                </div>
            </div>
        </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script>
        // BAR CHART
        new Chart(document.getElementById('barChart'), {
            type: 'bar',
            data: {
                labels: <?php echo json_encode($label_bulan); ?>,
                datasets: [{
                    label: 'Total Rp',
                    data: <?php echo json_encode($data_bulanan); ?>,
                    backgroundColor: '#e60000',
                    borderRadius: 5
                }]
            },
            options: { responsive: true, maintainAspectRatio: false }
        });

        // PIE CHART
        new Chart(document.getElementById('pieChart'), {
            type: 'pie',
            data: {
                labels: <?php echo json_encode($nama_supp_pie); ?>,
                datasets: [{
                    data: <?php echo json_encode($nilai_transaksi_pie); ?>,
                    backgroundColor: ['#b71c1c', '#0056b3', '#1e5631', '#d39e00', '#495057']
                }]
            },
            options: { responsive: true, maintainAspectRatio: false }
        });
    </script>
</body>
</html>