<?php
// po-header.php
include 'koneksi.php';

if (isset($_POST['id_supplier'])) {
    $id_supplier = mysqli_real_escape_string($conn, $_POST['id_supplier']);
    $id_cabang   = $_POST['id_cabang'];
    $user_input  = $_POST['user_input'];
    $no_po       = "PO-" . date('YmdHis');
    $tgl_po      = date('Y-m-d');

    $query = "INSERT INTO purchase_order (no_po, id_supplier, tgl_po, id_cabang, user_input) 
              VALUES ('$no_po', '$id_supplier', '$tgl_po', '$id_cabang', '$user_input')";

    if (mysqli_query($conn, $query)) {
        $id_po_baru = mysqli_insert_id($conn);
        // Redirect ke halaman PO dengan ID baru agar bisa lanjut tambah barang
        header("Location: purchase-order.php?id_po=" . $id_po_baru);
        exit();
    } else {
        echo "Gagal membuat PO: " . mysqli_error($conn);
    }
}
?>