<?php
// ── Shared JV grid-row renderer ──────────────────────────────────────────────
// Included by both jv.php (initial render / edit mode) and jv_add.php (AJAX
// "Add Row"), so the two stay pixel- and field-identical. Caller must set,
// before including this file:
//   $conn    - mysqli connection (already on the correct year DB)
//   $idx     - zero-based row index (drives the party_/debit_/credit_ field names)
//   $accode  - existing account code for this row (0 for a blank new row)
//   $dr      - existing debit amount (0 for a blank new row)
//   $cr      - existing credit amount (0 for a blank new row)
//
// Account list is restricted to grpcode > '002', i.e. everything except
// Bank (001) and Cash (002) — those two must go through Bank/Cash
// Receipt/Payment or Contra, not a Journal Voucher line.
$__jvacc = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode` > '002' ORDER BY `name`") or die(mysqli_error($conn));
?>
<tr>
    <td class="jv-sr" style="text-align:center;color:#94a3b8;font-weight:600;"><?php echo ($idx + 1); ?></td>
    <td>
        <select name="party_<?php echo (int)$idx; ?>" class="finp form jvacc select2" style="width:100%;">
            <option value="">-- Select Account --</option>
            <?php while ($__a = mysqli_fetch_assoc($__jvacc)):
                $__sel = ($accode > 0 && (int)$accode === (int)$__a['accode']) ? 'selected' : '';
            ?>
            <option value="<?php echo (int)$__a['accode']; ?>" <?php echo $__sel; ?>><?php echo htmlspecialchars($__a['name']); ?></option>
            <?php endwhile; ?>
        </select>
    </td>
    <td><input type="number" step="any" name="debit_<?php echo (int)$idx; ?>" class="finp form text-right jvdebit"
            value="<?php echo $dr > 0 ? rtrim(rtrim(number_format((float)$dr, 4, '.', ''), '0'), '.') : ''; ?>"
            <?php echo $cr > 0 ? 'readonly' : ''; ?>></td>
    <td><input type="number" step="any" name="credit_<?php echo (int)$idx; ?>" class="finp form text-right jvcredit"
            value="<?php echo $cr > 0 ? rtrim(rtrim(number_format((float)$cr, 4, '.', ''), '0'), '.') : ''; ?>"
            <?php echo $dr > 0 ? 'readonly' : ''; ?>></td>
    <td class="jv-rm" style="text-align:center;">
        <button type="button" class="row-del form jvremove" title="Remove Row"><i class="fa fa-times"></i></button>
    </td>
</tr>
