<?php
session_start();
include 'koneksi.php';
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";
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar PO - <?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 */
        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 */
        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 { background: white; padding: 25px; border-radius: 18px; box-shadow: 0 10px 30px rgba(0,0,0,0.08); }
        
        /* SEARCH BOX */
        .search-box { margin-bottom: 15px; display: flex; justify-content: flex-end; }
        .search-box input { padding: 10px 15px; width: 300px; border: 1px solid #ddd; border-radius: 8px; outline: none; font-size: 13px; }

        /* TABLE STYLING */
        table { width: 100%; border-collapse: collapse; margin-top: 10px; table-layout: auto; }
        th { background: #333; color: white; padding: 15px; text-align: left; font-size: 13px; text-transform: uppercase; }
        td { padding: 12px 15px; border-bottom: 1px solid #eee; font-size: 13px; color: #444; vertical-align: middle; }
        tr:hover { background-color: #f9f9f9; }
        
        /* BUTTONS STYLING */
        .btn-group { display: flex; gap: 5px; justify-content: center; }
        .btn { 
            padding: 7px 10px; 
            border-radius: 6px; 
            text-decoration: none; 
            color: white; 
            font-size: 11px; 
            display: inline-flex; 
            align-items: center; 
            gap: 5px; 
            font-weight: 700; 
            transition: 0.3s;
            border: none;
            cursor: pointer;
        }
        .btn:hover { opacity: 0.8; transform: translateY(-1px); }
        .btn-print { background: #007bff; }
        .btn-view { background: #17a2b8; }
        .btn-danger { background: #ff5252; }
        
        .badge-rilis { background: #2ecc71; color: white; padding: 5px 12px; border-radius: 20px; font-size: 11px; font-weight: 600; }
    </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"><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 active"><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;">Daftar Transaksi - <?php echo $nama_pt; ?></div>
            <div>User: <strong style="color:var(--accent-red)"><?php echo $_SESSION['username'] ?? 'superadmin'; ?></strong></div>
        </header>

        <div class="content-body">
            <?php
            if (isset($_SESSION['pesan_sukses'])) {
                echo '<div style="background: #d4edda; color: #155724; padding: 15px; border-radius: 12px; margin-bottom: 20px; border: 1px solid #c3e6cb; font-weight: 600; display: flex; align-items: center; gap: 10px;">
                        <i class="fa-solid fa-circle-check"></i> ' . $_SESSION['pesan_sukses'] . '
                      </div>';
                unset($_SESSION['pesan_sukses']); // Hapus pesan setelah ditampilkan
            }
            ?>
            
            <div class="card">
                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;">
                    <h3 style="margin:0; font-size: 18px;"><i class="fa-solid fa-file-invoice"></i> Daftar Purchase Order</h3>
                    <div class="search-box">
                        <input type="text" id="cariPO" onkeyup="cariData()" placeholder="Cari No. PO atau Supplier...">
                    </div>
                </div>
                
                <table id="tabelPO">
                
            
                    <thead>
                        <tr>
                            <th width="15%">No. PO</th>
                            <th width="15%">Tanggal Rilis</th>
                            <th width="20%">Supplier</th>
                            <th width="15%">Total Amount</th>
                            <th width="10%">Status</th>
                            <th width="25%" style="text-align: center;">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $res = mysqli_query($koneksi, "SELECT p.*, s.nama_supplier FROM po_utama p JOIN supplier s ON p.id_supplier = s.id ORDER BY p.tanggal_po DESC");
                        if(mysqli_num_rows($res) > 0) {
                            while($d = mysqli_fetch_array($res)) {
                        ?>
                        <tr>
                            <td><strong><?php echo $d['no_po']; ?></strong></td>
                            <td><?php echo date('d M Y, H:i', strtotime($d['tanggal_po'])); ?></td>
                            <td><?php echo $d['nama_supplier']; ?></td>
                            <td>Rp <?php echo number_format($d['total'], 0, ',', '.'); ?></td>
                            <td><span class="badge-rilis">Rilis</span></td>
                            <td>
                                <div class="btn-group">
                                    <a href="print_po.php?no=<?php echo $d['no_po']; ?>" target="_blank" class="btn btn-print">
                                        <i class="fa-solid fa-print"></i> PRINT
                                    </a>
                                    <a href="view_po.php?no=<?php echo $d['no_po']; ?>" class="btn btn-view">
                                        <i class="fa-solid fa-eye"></i> VIEW
                                    </a>
                                    <a href="hapus_po.php?no=<?php echo $d['no_po']; ?>" 
                                    class="btn btn-danger" 
                                    onclick="return confirm('Apakah Anda yakin ingin menghapus PO dengan nomor <?php echo $d['no_po']; ?>? Data yang dihapus tidak dapat dikembalikan.');">
                                    HAPUS
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <?php 
                            }
                        } else {
                            echo "<tr><td colspan='6' align='center'>Belum ada data PO.</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </main>

    <script>
    function cariData() {
        var input = document.getElementById("cariPO");
        var filter = input.value.toUpperCase();
        var table = document.getElementById("tabelPO");
        var tr = table.getElementsByTagName("tr");
        for (var i = 1; i < tr.length; i++) {
            var tdNo = tr[i].getElementsByTagName("td")[0];
            var tdSupp = tr[i].getElementsByTagName("td")[2];
            if (tdNo || tdSupp) {
                var txtValue = (tdNo.textContent || tdNo.innerText) + (tdSupp.textContent || tdSupp.innerText);
                tr[i].style.display = (txtValue.toUpperCase().indexOf(filter) > -1) ? "" : "none";
            }
        }
    }
    </script>
</body>
</html>