<div class="row">
    <div class="col-sm-10">
        <div class="page-title-box">
            <h2 class="page-title">Create Internal Indent</h2>
        </div>
    </div>
    <div class="col-sm-2">
        <div class="page-title-box">
            <h2 class="page-title">
                <a href="internal_indent_list">
                    <button class='btn btn-danger' type="button" style="float:right">Back</button>
                </a>
            </h2>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-sm-12">
        <div class="card">
            <div class="card-body">
                <?php
                echo $this->Form->create(false, ['id' => 'internalIndentForm']);

                $options = ['' => 'Select location'];
                foreach ($locations as $location) {
                    $options[$location['Location']['id']] = h($location['Location']['name']);
                }

                echo $this->Form->input('location', [
                    'div' => ['class' => 'form-group'],
                    'label' => 'Select Location',
                    'class' => 'form-control',
                    'options' => $options,
                    'id' => 'location',
                    'required' => true
                ]);
                ?>

                <table id="example" class="display table table-hover table-condensed" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Brand</th>
                            <th>Unit Name</th>
                            <th>Price</th>
                            <th>Tax %</th>
                            <th>Quantity</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $x = 1;
                        foreach ($users as $user):
                            $sku = $user['Inventory_sku']; ?>
                            <tr>
                                <td><?= $x; ?></td>
                                <td><?= h($sku['name']); ?></td>
                                <td><?= h($sku['brand']); ?></td>
                                <td><?= h($sku['unit_name']); ?></td>
                                <td><?= h($sku['price']); ?></td>
                                <td><?= h($user[0]['total_tax']); ?> %</td>
                                <td>
                                    <?= $this->Form->input("quantity[{$sku['id']}]", [
                                        'class' => 'form-control',
                                        'label' => false,
                                        'type' => 'number',
                                        'style' => 'width:auto',
                                        'value' => 0,
                                        'min' => 0,
                                        'id' => 'quantity_' . $sku['id']
                                    ]); ?>
                                </td>
                                <input type="hidden" name="sku_id[<?= $sku['id']; ?>]" value="<?= $sku['id']; ?>">
                                <input type="hidden" name="sku_name[<?= $sku['id']; ?>]" id="sku_name_<?= $sku['id']; ?>" value="<?= h($sku['name']); ?>">
                                <input type="hidden" name="sku_brand[<?= $sku['id']; ?>]" id="sku_brand_<?= $sku['id']; ?>" value="<?= h($sku['brand']); ?>">
                                <input type="hidden" name="sku_price[<?= $sku['id']; ?>]" id="sku_price_<?= $sku['id']; ?>" value="<?= h($sku['price']); ?>">
                                <input type="hidden" name="unit_name[<?= $sku['id']; ?>]" id="unit_name_<?= $sku['id']; ?>" value="<?= h($sku['unit_name']); ?>">
                                <input type="hidden" name="unit_id[<?= $sku['id']; ?>]" id="unit_id_<?= $sku['id']; ?>" value="<?= h($sku['unit']); ?>">
                                <input type="hidden"
                                   id="sku_tax_<?= $sku['id']; ?>"
                                   value="<?= h($user[0]['total_tax']); ?>">
                            </tr>
                        <?php $x++; endforeach; ?>
                    </tbody>
                </table><br>

                <div class="row">
                    <div class="col-md-12">
                        <button type="button" onclick="getCheckedCheckboxes()" class="btn btn-success" style="float:right;">Save</button>
                        <a href="internal_indent_list">
                            <button class='btn btn-danger' type="button" style="float:right; margin-right: 10px;">Cancel</button>
                        </a>
                    </div>
                </div>
                <?= $this->Form->end(); ?>
            </div>
        </div>
    </div>
</div>

<script>

function showInternalIndentConfirmation(items, onConfirm) {

    let html = `
        <table class="table table-bordered table-sm text-start">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Brand</th>
                    <th>Unit</th>
                    <th>Price</th>
                    <th>Tax %</th>
                    <th>Qty</th>
                    <th>Amount</th>
                    <th>Tax Amt</th>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody>
    `;

    let subtotal = 0;
    let totalTax = 0;

    items.forEach(item => {

        const price = parseFloat(item.price) || 0;
        const qty   = parseFloat(item.sku_quantity) || 0;
        const taxPct = parseFloat(item.tax) || 0;

        const baseAmount = price * qty;
        const taxValue   = (baseAmount * taxPct) / 100;
        const total      = baseAmount + taxValue;

        subtotal += baseAmount;
        totalTax += taxValue;

        html += `
            <tr>
                <td>${item.sku_name}</td>
                <td>${item.sku_brand}</td>
                <td>${item.unit_name}</td>
                <td>₹${price.toFixed(2)}</td>
                <td>${taxPct}%</td>
                <td><b>${qty}</b></td>
                <td>₹${baseAmount.toFixed(2)}</td>
                <td>₹${taxValue.toFixed(2)}</td>
                <td><b>₹${total.toFixed(2)}</b></td>
            </tr>
        `;
    });

    const grandTotal = subtotal + totalTax;

    html += `
            </tbody>
        </table>

        <div class="text-end mt-3">
            <b>Subtotal :</b> ₹${subtotal.toFixed(2)}<br>
            <b>Total Tax :</b> ₹${totalTax.toFixed(2)}<br>
            <h5 class="mt-2"><b>Grand Total : ₹${grandTotal.toFixed(2)}</b></h5>
        </div>
    `;

    Swal.fire({
        title: 'Confirm Internal Indent',
        html: html,
        icon: 'warning',
        width: '1100px',
        showCancelButton: true,
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        reverseButtons: true
    }).then(result => {
        if (result.isConfirmed) {
            onConfirm();
        }
    });
}

// Store all selected SKUs across pages
let selectedSkus = {};
// Track changes in quantity inputs
$(document).on('change', "input[type='number'][id^='quantity_']", function () {
    const quantity = $(this).val().trim();
    const skuId = this.id.replace('quantity_', '');
    if (quantity !== "" && quantity !== "0") {
        selectedSkus[skuId] = {
            id: skuId,
            sku_name: $('#sku_name_' + skuId).val(),
            sku_brand: $('#sku_brand_' + skuId).val(),
            unit_name: $('#unit_name_' + skuId).val(),
            unit_id: $('#unit_id_' + skuId).val(),
            sku_quantity: quantity,
            price: $('#sku_price_' + skuId).val(),
            tax: $('#sku_tax_' + skuId).val()   // ✅ NEW
        };

    } else {
        delete selectedSkus[skuId];
    }
});
/*
function getCheckedCheckboxes() {
    const locationId = $('#location').val();
    if (!locationId) {
        Swal.fire({
            icon: 'warning',
            title: 'Location not selected',
            text: 'Please select a location before saving.',
            confirmButtonColor: '#d33',
        });
        return;
    }
    const checkedData = Object.values(selectedSkus);
    // const checkedData = [];
    // $("input[type='number'][id^='quantity_']").each(function () {
    //     const quantity = $(this).val().trim();
    //     const skuId = this.id.replace('quantity_', '');
    //     if (quantity !== "" && quantity !== "0") {
    //         checkedData.push({
    //             id: skuId,
    //             sku_name: $('#sku_name_' + skuId).val(),
    //             sku_brand: $('#sku_brand_' + skuId).val(),
    //             unit_name: $('#unit_name_' + skuId).val(),
    //             unit_id: $('#unit_id_' + skuId).val(),
    //             sku_quantity: quantity,
    //             price: $('#sku_price_' + skuId).val()
    //         });
    //     }
    // });
    if (checkedData.length === 0) {
        Swal.fire({
            icon: 'warning',
            title: 'No SKUs selected',
            text: 'Please enter quantity for at least one SKU.',
            confirmButtonColor: '#d33',
        });
        return;
    }
    $.ajax({
        url: '<?= $this->Html->url(['controller' => 'dashboard', 'action' => 'add_internal_indent']); ?>',
        type: 'POST',
        data: {
            location_id: locationId,
            checkedData: JSON.stringify(checkedData)
        },
        success: function (response) {
            Swal.fire({
                icon: 'success',
                title: 'Saved successfully!',
                showConfirmButton: false,
                timer: 1200
            }).then(() => {
                window.location.href = 'internal_indent_list';
            });
        },
        error: function (xhr, status, error) {
            Swal.fire({
                icon: 'error',
                title: 'An error occurred',
                text: error,
                confirmButtonColor: '#d33',
            });
        }
    });
}
*/

function getCheckedCheckboxes() {

    const locationId = $('#location').val();

    if (!locationId) {
        Swal.fire({
            icon: 'warning',
            title: 'Location not selected',
            text: 'Please select a location before saving.'
        });
        return;
    }

    const checkedData = Object.values(selectedSkus);

    if (checkedData.length === 0) {
        Swal.fire({
            icon: 'warning',
            title: 'No SKUs selected',
            text: 'Please enter quantity for at least one SKU.'
        });
        return;
    }

    // 🔥 SHOW ITEM SUMMARY POPUP
    showInternalIndentConfirmation(checkedData, function () {

        // ✅ AJAX only after confirm
        $.ajax({
            url: '<?= $this->Html->url(['controller' => 'dashboard', 'action' => 'add_internal_indent']); ?>',
            type: 'POST',
            data: {
                location_id: locationId,
                checkedData: JSON.stringify(checkedData)
            },
            success: function () {
                Swal.fire({
                    icon: 'success',
                    title: 'Saved successfully!',
                    timer: 1200,
                    showConfirmButton: false
                }).then(() => {
                    window.location.href = 'internal_indent_list';
                });
            }
        });

    });
}

</script>

