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

if (isset($_POST['id'])) {
    $product_id = $_POST['id'];

    // Query to get product details and units
    $query = $conn->prepare("
        SELECT 
            p.name AS product_name, 
            p.id AS product_id,
            v.default_purchase_price AS purchase_price,
            v.dpp_inc_tax AS unit_cost_before_tax,
            v.profit_percent AS profit_margin,
            v.sell_price_inc_tax AS sell_price,
            p.unit_id AS unit_id
        FROM products p
        INNER JOIN variations v ON p.id = v.product_id
        WHERE business_id = 3 AND p.id = ?
    ");
    $query->execute([$product_id]);
    $product = $query->fetch();

    if ($product) {
        // Query to get units if the unit_id matches
        $unitQuery = $conn->prepare("
            SELECT `id`, `business_id`, `actual_name`, `short_name`
            FROM units
            WHERE `business_id` = 3 AND `id` = ?
        ");
        $unitQuery->execute([$product['unit_id']]);
        $units = $unitQuery->fetchAll();

        // Generate options for the select box
        $unitOptions = '';
        foreach ($units as $unit) {
            $unitOptions .= '<option value="' . htmlspecialchars($unit['id']) . '">'
                            . htmlspecialchars($unit['actual_name']) . ' (' 
                            . htmlspecialchars($unit['short_name']) . ')</option>';
        }

        // Prepare HTML output
        $html = '
        <tr>
            <td>1</td>
            <td style="display:none;"><input type="text" name="product_id[]" class="form-control" value="' . htmlspecialchars($product['product_id']) . '"></td>
            <td class="product-name">' . htmlspecialchars($product['product_name']) . '</td>
            <td><input type="text" class="form-control quantity" name="quantity[]" value="1"><br> <select name="unit_id[]" class="form-control">' . $unitOptions . '</select></td>
            <td style="display:none;"><input type="text" class="form-control purchase-price" name="pp_without_discount[]" value="' . htmlspecialchars($product['purchase_price']) . '"></td>
            <td style="display:none;"><input type="text" class="form-control discount" name="discount_percent[]" value="0"></td>
            <td><input type="text" class="form-control unit-cost-before-tax" name="purchase_price[]" value="' . htmlspecialchars($product['unit_cost_before_tax']) . '"></td>
            <td class="line-total" style="display:none;">' . number_format($product['unit_cost_before_tax'], 2) . '</td>
            <td style="display:none;"><input type="text" class="form-control profit-margin" name="profit_margin[]" value="' . htmlspecialchars($product['profit_margin']) . '"></td>
            <td style="display:none;"><input type="text" class="form-control unit-selling-price" name="sell_price[]" value="' . htmlspecialchars($product['sell_price']) . '"></td>
           
            <td>
                <ion-icon name="trash-outline"></ion-icon>
            </td>
        </tr>';

        echo $html;
    } else {
        echo '<tr><td colspan="11">Product not found.</td></tr>';
    }
}
?>
