<?php
session_start();
include 'koneksi.php';

$query_supplier = mysqli_query($koneksi, "SELECT * FROM supplier WHERE status='Aktif' ORDER BY nama_supplier ASC");
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Create Order - BEST</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        body { font-family: 'Inter', sans-serif; background-color: #f8f9fa; margin: 0; display: flex; height: 100vh; }
        main { flex-grow: 1; display: flex; align-items: center; justify-content: center; padding: 20px; }
        .select-card { background: white; padding: 40px; border-radius: 16px; box-shadow: 0 4px 25px rgba(0,0,0,0.1); width: 100%; max-width: 600px; text-align: center; }
        .form-control { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 8px; font-size: 15px; box-sizing: border-box; }
        .btn-next { background: #eee; width: 100%; padding: 15px; border: 1px solid #ccc; border-radius: 8px; cursor: pointer; font-weight: 600; margin-top: 20px; transition: 0.3s; }
        .btn-next:hover { background: #ddd; }
    </style>
</head>
<body>
    <main>
        <div class="select-card">
            <i class="fa-solid fa-truck-fast" style="font-size: 60px; color: #b71c1c; margin-bottom: 20px;"></i>
            <h2 style="margin: 0 0 10px 0;">Buat Pesanan Baru</h2>
            <p style="color: #666; margin-bottom: 30px;">Silahkan pilih supplier terdaftar untuk memulai Purchase Order</p>
            
            <div style="text-align: left;">
                <label style="font-size: 14px; font-weight: 600; margin-bottom: 8px; display: block;">Pilih Supplier</label>
                <select id="select_supplier" class="form-control">
                    <option value="">-- Pilih Mitra Supplier --</option>
                    <?php while($s = mysqli_fetch_array($query_supplier)) { ?>
                        <option value='<?php echo json_encode($s); ?>'><?php echo $s['nama_supplier']; ?></option>
                    <?php } ?>
                </select>
            </div>
            
            <button type="button" class="btn-next" onclick="startOrder()">
                Lanjutkan ke Form Order <i class="fa-solid fa-arrow-right"></i>
            </button>
        </div>
    </main>

    <?php include 'modal_po_snippet.php'; ?>

    <script>
    function validateAndOpen() {
    const select = document.getElementById('select_supplier');
    if (select.value === "") {
        alert("Silahkan pilih supplier terlebih dahulu.");
        return;
    }
    const data = JSON.parse(select.value);
    openOrderModal(data); // Fungsi ini ada di dalam modal_po_snippet.php
}
    </script>
</body>
</html>