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

if (isset($_GET['ref_no'])) {
    $refNo = htmlspecialchars($_GET['ref_no']);

    // Fetch purchase details
    $query = "SELECT 
                p.product_id,
                pr.name AS ProductName,
                p.quantity,
                p.pp_without_discount,
                p.discount_percent,
                p.purchase_price,
                p.sell_price,
                p.Profit_margin,
                p.purchase_date,
                p.ref_no,
                p.purchase_status,
                p.discount_amount,
                p.shipping_detail,
                p.shipping_charges,
                p.note,
                p.discount_type
              FROM purchase_lines p
              JOIN products pr ON p.product_id = pr.id
              WHERE p.ref_no = :ref_no";

    $stmt = $conn->prepare($query);
    $stmt->bindParam(':ref_no', $refNo);
    $stmt->execute();
    $details = $stmt->fetchAll();

    if (!$details) {
        echo "<p>No purchase details found for Reference No: $refNo</p>";
        exit;
    }

    // Generate Output
    $subtotalBeforeTax = 0;
    $totalAfterTax = 0;

    $output = '<h5>Reference No: ' . htmlspecialchars($details[0]['ref_no']) . '</h5>';
    $output .= '<p>Date: ' . htmlspecialchars($details[0]['purchase_date']) . '</p>';
    $output .= '<p>Supplier: N/A</p>';
    $output .= '<p>Address: N/A</p>';
    $output .= '<p>Mobile: N/A</p>';

    $output .= '<table class="table table-bordered">';
    $output .= '<thead>
                    <tr>
                        <th>#</th>
                        <th>Product <br> Name</th>
                        <th>Purchase <br> Quantity</th>
                        <th>Unit Cost <br> (Before Discount)</th>
                        <th>Discount <br> Percent</th>
                        <th>Subtotal <br> (Before Tax)</th>
                        <th>Unit Cost <br> Price (After Tax)</th>
                        <th>Profit <br> Margin</th>
                        <th>Unit Selling <br> Price</th>
                        <th>Subtotal</th>
                    </tr>
                </thead>';
    $output .= '<tbody>';

    foreach ($details as $index => $row) {
        $subtotal = $row['purchase_price'] * $row['quantity'];
        $output .= '<tr>';
        $output .= '<td>' . ($index + 1) . '</td>';
        $output .= '<td>' . htmlspecialchars($row['ProductName']) . '</td>';
        $output .= '<td>' . number_format($row['quantity'], 2) . '</td>';
        $output .= '<td>' . number_format($row['pp_without_discount'], 2) . '</td>';
        $output .= '<td>' . number_format($row['discount_percent'], 2) . '</td>';
        $output .= '<td>' . number_format($subtotal, 2) . '</td>';
        $output .= '<td>' . number_format($row['purchase_price'], 2) . '</td>';
        $output .= '<td>' . number_format($row['Profit_margin'], 2) . ' %</td>';
        $output .= '<td>' . number_format($row['sell_price'], 2) . '</td>';
        $output .= '<td>' . number_format($subtotal, 2) . '</td>';
        $output .= '</tr>';

        $subtotalBeforeTax += $subtotal;
        $totalAfterTax += $row['purchase_price'] * $row['quantity'];
    }

    $output .= '</tbody></table>';
    $output .= '<p><strong>Subtotal (Before Tax): </strong>' . number_format($subtotalBeforeTax, 2) . '</p>';
    $output .= '<p><strong>Grand Total: </strong>' . number_format($totalAfterTax, 2) . '</p>';

    echo $output;
}
?>
