<?php
include 'includes/config.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Fetch form data
    $supplier_id = $_POST['supplier_id'];
    $purchase_date = $_POST['purchase_date'];
    $ref_no = $_POST['ref_no'];
    $purchase_status = $_POST['purchase_status'];
    $discount_type = $_POST['discount_type'];
    $discount_amount = $_POST['discount_amount'];
    $shipping_detail = $_POST['shipping_detail'];
    $shipping_charges = $_POST['shipping_charges'];
    $note = $_POST['note'];

    // Handle file upload
    $attach_document = '';
    if (isset($_FILES['attach_document']) && $_FILES['attach_document']['error'] == 0) {
        $upload_dir = 'purchase_upload/';
        $file_name = basename($_FILES['attach_document']['name']);
        $target_file = $upload_dir . $file_name;

        // Move uploaded file to the designated folder
        if (move_uploaded_file($_FILES['attach_document']['tmp_name'], $target_file)) {
            $attach_document = $target_file; // Save the file path
        } else {
            echo "Error uploading the file.";
            exit;
        }
    }

    // Prepare the SQL query for inserting purchase data
    $query = "INSERT INTO purchase_lines (supplier_id, product_id, quantity, pp_without_discount, discount_percent, purchase_price, profit_margin, sell_price, purchase_date, ref_no, purchase_status, attach_document, discount_type, discount_amount, shipping_detail, shipping_charges, note) 
              VALUES (:supplier_id, :product_id, :quantity, :pp_without_discount, :discount_percent, :purchase_price, :profit_margin, :sell_price, :purchase_date, :ref_no, :purchase_status, :attach_document, :discount_type, :discount_amount, :shipping_detail, :shipping_charges, :note)";

    $stmt = $conn->prepare($query);

    // Loop through products to insert each one
    foreach ($_POST['product_id'] as $index => $product_id) {
        $quantity = $_POST['quantity'][$index];
        $pp_without_discount = $_POST['pp_without_discount'][$index];
        $discount_percent = $_POST['discount_percent'][$index];
        $purchase_price = $_POST['purchase_price'][$index];
        $profit_margin = $_POST['profit_margin'][$index];
        $sell_price = $_POST['sell_price'][$index];

        // Execute the insert statement
        $stmt->execute([
            ':supplier_id' => $supplier_id,
            ':product_id' => $product_id,
            ':quantity' => $quantity,
            ':pp_without_discount' => $pp_without_discount,
            ':discount_percent' => $discount_percent,
            ':purchase_price' => $purchase_price,
            ':profit_margin' => $profit_margin,
            ':sell_price' => $sell_price,
            ':purchase_date' => $purchase_date,
            ':ref_no' => $ref_no,
            ':purchase_status' => $purchase_status,
            ':attach_document' => $attach_document,
            ':discount_type' => $discount_type,
            ':discount_amount' => $discount_amount,
            ':shipping_detail' => $shipping_detail,
            ':shipping_charges' => $shipping_charges,
            ':note' => $note
        ]);
    }

    echo "Purchase data has been successfully inserted.";
}
?>
