<?php
include("../../classes/manager.class.php");
$m = new Manager();

if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
    $id = (int)$_REQUEST['id'];
    $qry = IngSetup::GetIngSetupById($id);
    if ($qry) {
        ?>
        <tr id="tr<?= $id ?>">
            <!-- S No -->
            <?php if (!isset($_GET['edit'])): ?>
  <td class="sn"></td>
<?php endif; ?>

            <!-- Delete -->
            <td>
                <i id="trashing<?= $id ?>" style="cursor:pointer;font-size:18px;margin-top:7px;"
                   class="mdi-action-delete mdi mdi-delete"></i>
            </td>

            <!-- NEW (deductible): one radio group across all ingredient rows -->
<?php $rid = 'ded_ing_' . (int)$qry->id; ?>
<td>
  <input
    type="radio"
    id="<?= $rid ?>"
    name="deductible_ing_id"
    class="with-gap ded-radio"
    value="<?= (int)$qry->id ?>"
    required
  >
  <label for="<?= $rid ?>" style="margin:0;">&nbsp;</label>
</td>
            <!-- Item Name -->
            <td>
                <input class="form-control" style="width:100%!important;" type="text" readonly
                       value="<?= htmlspecialchars($qry->name, ENT_QUOTES) ?>">
                <input type="hidden" name="item[]" value="<?= $qry->id ?>">
            </td>

            <!-- Qty -->
            <td>
                <input style="width:100%!important;"
                       required
                       maxlength="15"
                       onkeypress="return isNumberKey(event)"
                       class="form-control per-unit"
                       type="text"
                       name="per_unit[]">
            </td>
          
          
        </tr>

        <script>
            // NEW (deductible): bind delete for this row and keep group 'required' logic correct
            $('#trashing<?= $id ?>').on('click', function () {
                const $tbody = $('#addNewIngredient');
                $(this).closest('tr').remove();
                // reuse global helpers from the parent page (already defined there)
                if (typeof updateSN === 'function') updateSN();                   // keeps S.No correct
                if (typeof ensureDeductibleRequired === 'function')               // keeps radio group 'required' correct
                    ensureDeductibleRequired($tbody);
            });

            // NEW (deductible): when a radio is chosen, relax 'required' on the group (use parent helper)
            $(document).on('change', 'input[name="deductible_ing_id"]', function () {
                if (typeof ensureDeductibleRequired === 'function')
                    ensureDeductibleRequired($('#addNewIngredient'));
            });

            // NEW (deductible): initial pass after appending this row
            if (typeof ensureDeductibleRequired === 'function')
                ensureDeductibleRequired($('#addNewIngredient'));
            if (typeof updateSN === 'function') updateSN();
        </script>
        <?php
    }
}
?>
