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

if (isset($_POST['id'])) {
    $product_id = $_POST['id'];
    $row_number = isset($_POST['row_number']) ? intval($_POST['row_number']) : 1;

    // Fetch product details
    $query = $conn->prepare("
        SELECT 
            p.name AS product_name, 
            p.id AS product_id,
            p.color_id,
            p.size_id,
            p.barcode,
            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 p.business_id = 3 AND p.id = ?
    ");
    $query->execute([$product_id]);
    $product = $query->fetch();

    if ($product) {
        // Units dropdown
        $unitQuery = $conn->prepare("
            SELECT id, actual_name, short_name
            FROM units
            WHERE business_id = 3
        ");
        $unitQuery->execute();
        $units = $unitQuery->fetchAll();

        $unitOptions = '';
        foreach ($units as $unit) {
            $selected = ($unit['id'] == $product['unit_id']) ? 'selected' : '';
            $unitOptions .= '<option value="' . htmlspecialchars($unit['id']) . '" ' . $selected . '>'
                            . htmlspecialchars($unit['actual_name']) . ' (' 
                            . htmlspecialchars($unit['short_name']) . ')</option>';
        }

        // Colors dropdown
        $colorQuery = $conn->prepare("SELECT id, product_color FROM colors ORDER BY name ASC");
        $colorQuery->execute();
        $colors = $colorQuery->fetchAll();

        $colorOptions = '';
        foreach ($colors as $color) {
            $selected = ($color['id'] == $product['color_id']) ? 'selected' : '';
            $colorOptions = '<option value="' . htmlspecialchars($product['color_id']) . '" selected>'
                . htmlspecialchars($conn->query("SELECT product_color FROM colors WHERE id=" . (int)$product['color_id'])->fetchColumn())
                . '</option>';
        }

        // Sizes dropdown
        $sizeQuery = $conn->prepare("SELECT id, product_size FROM sizes ORDER BY name ASC");
        $sizeQuery->execute();
        $sizes = $sizeQuery->fetchAll();

        $sizeOptions = '';
        foreach ($sizes as $size) {
            $selected = ($size['id'] == $product['size_id']) ? 'selected' : '';
    $sizeOptions = '<option value="' . htmlspecialchars($product['size_id']) . '" selected>'
               . htmlspecialchars($conn->query("SELECT product_size FROM sizes WHERE id=" . (int)$product['size_id'])->fetchColumn())
               . '</option>';
        }

        // Output HTML row with separate TDs
        $html = '
        <tr>
            <td class="row-number">' . $row_number . '</td>
            <td style="display:none;">
                <input type="hidden" name="product_id[]" value="' . htmlspecialchars($product['product_id']) . '">
            </td>
                        <td style="display:none;"><input type="text" class="form-control drum" name="drum[]"></td>

            <td class="product-name">' . htmlspecialchars($product['product_name']) . '</td>
                        <td><input type="text" class="form-control" name="barcode[]" value="' . htmlspecialchars($product['barcode']) . '" readonly></td>
                   <td style="display:none;"><select name="color_id[]" class="form-control" disabled>' . $colorOptions . '</select></td>
    <td  style="display:none;"><select name="size_id[]" class="form-control" disabled>' . $sizeOptions . '</select></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><input type="text" class="form-control purchase-price" name="pp_without_discount[]" value="' . htmlspecialchars($product['purchase_price']) . '"></td>
            <td><input type="text" class="form-control unit-selling-price" name="sell_price[]" value="' . htmlspecialchars($product['sell_price']) . '"></td>
            <td style="display:none;"><input type="text" class="form-control fair_price" name="fair_price[]" value="0"></td>
            <td style="display:none;"><input type="text" class="form-control discount" name="discount_percent[]" value="0"></td>
            <td style="display:none;"><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">' . 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><ion-icon name="trash-outline" class="text-danger"></ion-icon></td>
        </tr>';

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