<?php
/*
 * Reusable "garment card" partial.
 * Now shows ALL THREE garment types (Pant, Shirt, Kurta/Blazer) together in one card —
 * no Garment Type dropdown. Each type has its OWN Qty / Rate / Amount + its own measurement chit.
 *
 * Expects: $idx, $pant, $shirt, $kurta (each: null OR stdClass with ->qty, ->rate, ->amount, ->measurements, ->description)
 */
if (!function_exists('gc_v')) {
    function gc_v($m, $k) { return htmlspecialchars($m[$k] ?? ''); }
}
if (!function_exists('gc_num')) {
    function gc_num($item, $k, $default = '') { return isset($item->$k) && $item->$k !== '' ? $item->$k : $default; }
}
if (!function_exists('gc_meas')) {
    function gc_meas($item) { return ($item && !empty($item->measurements)) ? json_decode($item->measurements, true) : []; }
}

$pant  = $pant  ?? null;
$shirt = $shirt ?? null;
$kurta = $kurta ?? null;

$p_qty    = gc_num($pant, 'qty', 1);
$p_rate   = gc_num($pant, 'rate', '');
$p_amount = gc_num($pant, 'amount', '0.00');
$p_m      = gc_meas($pant);

$s_qty    = gc_num($shirt, 'qty', 1);
$s_rate   = gc_num($shirt, 'rate', '');
$s_amount = gc_num($shirt, 'amount', '0.00');
$s_m      = gc_meas($shirt);

$k_qty    = gc_num($kurta, 'qty', 1);
$k_rate   = gc_num($kurta, 'rate', '');
$k_amount = gc_num($kurta, 'amount', '0.00');
$k_m      = gc_meas($kurta);

// Receipt No. shown on the chit = the order's own Order No (available via $order_no on create, $row on edit)
$receipt_no = isset($row) ? $row->order_no : ($order_no ?? '');

// Print the chit CSS only once, no matter how many garment cards are on the page
if (!defined('GARMENT_CHIT_STYLES_PRINTED')):
    define('GARMENT_CHIT_STYLES_PRINTED', true);
?>
<style>
    .chit-wrap{ margin-bottom:2px; }
    .chit-receipt-bar{ display:flex; justify-content:flex-end; align-items:baseline; gap:6px; padding:0 2px 2px; }
    .chit-receipt-lbl{ font-size:9px; text-transform:uppercase; color:#555; font-weight:700; letter-spacing:.4px; }
    .chit-receipt-val{ font-size:12.5px; font-weight:800; color:#000; }

    .chit{ display:flex; align-items:stretch; border:2px solid #000; border-radius:6px; background:#fffdf8; font-family:'Segoe UI',Arial,sans-serif; overflow:hidden; }

    /* ── Left vertical garment-name column (paper-chit book style) ── */
    .chit-label-col{
        flex-shrink:0;
        width:22px;
        background:#f3ede0;
        border-right:2px solid #000;
        position:relative;
        overflow:hidden;
    }
    .chit-label-col span{
        position:absolute;
        top:50%; left:50%;
        transform:translate(-50%,-50%) rotate(-90deg);
        white-space:nowrap;
        font-weight:900; text-transform:uppercase; letter-spacing:2px;
        font-size:12px; color:#000;
    }

    .chit-body{ flex:1; min-width:0; display:flex; flex-direction:column; }

    .chit-qra{ display:flex; gap:0; border-bottom:2px solid #000; background:#faf7f0; }
    .chit-qra-item{ flex:1; min-width:0; padding:4px 8px; border-right:1px solid #000; }
    .chit-qra-item:last-child{ border-right:none; }
    .chit-qra-item label{ display:block; font-size:9px; text-transform:uppercase; color:#333; font-weight:700; margin-bottom:2px; letter-spacing:.3px; }
    .chit-qra-item input{ width:100%; border:none; border-bottom:1px dashed #999; background:transparent; padding:2px 1px; font-size:13px; text-align:center; color:#111; }
    .chit-qra-item input:focus{ outline:none; border-bottom:1px solid #7c3aed; }
    .chit-qra-item input[readonly]{ color:#000; font-weight:700; background:transparent; }

    .chit-row{ display:flex; flex-wrap:wrap; border-bottom:1.5px solid #000; margin:0; }
    .chit-row:last-child{ border-bottom:none; }
    .chit-cell{ flex:1; min-width:70px; border-right:1.5px solid #000; padding:4px 7px; }
    .chit-cell:last-child{ border-right:none; }
    .chit-cell.chit-wide{ flex:2; }
    .chit-cell.chit-narrow{ flex:0 0 70px; }
    .chit-cell label, .chit-arrow{ display:block; font-size:9px; text-transform:uppercase; color:#333; font-weight:700; margin-bottom:2px; letter-spacing:.3px; }
    .chit-cell input[type=text], .chit-cell select{
        width:100%; border:none; border-bottom:1px dashed #999; background:transparent;
        padding:2px 1px; font-size:13px; text-align:center; color:#111;
    }
    .chit-cell input[type=text]:focus, .chit-cell select:focus{ outline:none; border-bottom:1px solid #7c3aed; }

    .chit-line-row{ display:flex; align-items:center; gap:6px; padding:4px 8px; border-bottom:1.5px solid #000; font-size:11px; }
    .chit-line-row .chit-lbl{ font-weight:700; color:#000; white-space:nowrap; }
    .chit-line-row .chit-dash{ flex:1; border-bottom:1px dashed #999; height:1px; margin:0 4px; }
    .chit-line-row input[type=text]{ border:none; border-bottom:1px dashed #999; background:transparent; text-align:center; font-size:13px; width:70px; }
    .chit-line-row input[type=text]:focus{ outline:none; border-bottom:1px solid #7c3aed; }

    .chit-fbrow{ display:flex; gap:16px; padding:4px 8px; border-bottom:1.5px solid #000; align-items:center; }
    .chit-fbrow .chit-fb{ display:flex; align-items:center; gap:5px; font-size:12px; font-weight:700; color:#000; }
    .chit-fbrow .chit-fb input[type=text]{ border:none; border-bottom:1px dashed #999; background:transparent; text-align:center; font-size:13px; width:80px; }
    .chit-fbrow .chit-fb input[type=text]:focus{ outline:none; border-bottom:1px solid #7c3aed; }

    .chit-notes{ padding:4px 8px; }
    .chit-notes label{ font-size:9px; text-transform:uppercase; color:#333; font-weight:700; letter-spacing:.3px; display:block; margin-bottom:2px; }
    .chit-notes input[type=text]{ width:100%; border:none; border-bottom:1px dashed #999; background:transparent; padding:2px 1px; font-size:13px; }
    .chit-notes input[type=text]:focus{ outline:none; border-bottom:1px solid #7c3aed; }

    .chit-sleeve-wrap select{ text-align:left !important; }
</style>
<?php endif; ?>

<div class="garment-card border rounded mb-3 bg-white text-dark">
    <div class="d-flex justify-content-between align-items-center p-2 border-bottom bg-light rounded-top">
        <div class="fw-bold text-muted row-num">#<?= $idx + 1 ?></div>
        <button type="button" class="btn btn-sm btn-danger remove-row no-loader"><i class="fa-solid fa-times"></i></button>
    </div>

    <!-- PANT -->
    <div class="type-block p-2">
        <div class="chit-wrap">
            <div class="chit-receipt-bar">
                <div class="chit-receipt-lbl">Receipt No.</div>
                <div class="chit-receipt-val"><?= htmlspecialchars($receipt_no) ?></div>
            </div>
            <div class="chit">
                <div class="chit-label-col"><span>Pant</span></div>
                <div class="chit-body">
                    <div class="chit-qra">
                        <div class="chit-qra-item"><label>Qty</label><input type="number" min="1" name="qty[]" class="qty-inp" value="<?= $p_qty ?>"></div>
                        <div class="chit-qra-item"><label>Rate (₹)</label><input type="number" step="0.01" min="0" name="rate[]" class="rate-inp" value="<?= $p_rate ?>"></div>
                        <div class="chit-qra-item"><label>Amount (₹)</label><input type="text" class="amt-out" readonly value="<?= $p_amount ?>"></div>
                    </div>

                    <div class="chit-row">
                        <div class="chit-cell"><label>Height</label><input type="text" class="m-fld" data-key="height" value="<?= gc_v($p_m,'height') ?>"></div>
                        <div class="chit-cell"><label>Waist</label><input type="text" class="m-fld" data-key="waist" value="<?= gc_v($p_m,'waist') ?>"></div>
                        <div class="chit-cell"><label>Length (T)</label><input type="text" class="m-fld" data-key="length" value="<?= gc_v($p_m,'length') ?>"></div>
                    </div>

                    <div class="chit-row">
                        <div class="chit-cell"><label>Seat / Hip (F)</label><input type="text" class="m-fld" data-key="seat" value="<?= gc_v($p_m,'seat') ?>"></div>
                        <div class="chit-cell"><label>Thigh</label><input type="text" class="m-fld" data-key="thigh" value="<?= gc_v($p_m,'thigh') ?>"></div>
                        <div class="chit-cell"><label>Knee</label><input type="text" class="m-fld" data-key="knee" value="<?= gc_v($p_m,'knee') ?>"></div>
                    </div>

                    <div class="chit-line-row">
                        <span class="chit-lbl">French Fly</span>
                        <span class="chit-dash"></span>
                        <span class="chit-lbl">Bottom</span>
                        <input type="text" class="m-fld" data-key="bottom" value="<?= gc_v($p_m,'bottom') ?>">
                    </div>

                    <div class="chit-row">
                        <div class="chit-cell chit-narrow"><label>Cross</label><input type="text" class="m-fld" data-key="cross" value="<?= gc_v($p_m,'cross') ?>"></div>
                        <div class="chit-cell chit-wide"><label>Fly / Pocket Style</label><input type="text" class="m-fld" data-key="style" placeholder="V.P.C / P.S / 2 P.C" value="<?= gc_v($p_m,'style') ?>"></div>
                    </div>

                    <div class="chit-notes"><label>Other Notes</label><input type="text" class="m-fld" data-key="notes" value="<?= gc_v($p_m,'notes') ?>"></div>
                </div>
            </div>
        </div>
        <input type="hidden" name="garment_type[]" value="Pant">
        <input type="hidden" name="measurements[]" class="measurements-json" value="">
        <input type="hidden" name="description[]" class="description-hidden" value="">
    </div>

    <!-- SHIRT -->
    <div class="type-block p-2">
        <div class="chit-wrap">
            <div class="chit-receipt-bar">
                <div class="chit-receipt-lbl">Receipt No.</div>
                <div class="chit-receipt-val"><?= htmlspecialchars($receipt_no) ?></div>
            </div>
            <div class="chit">
                <div class="chit-label-col"><span>Shirt</span></div>
                <div class="chit-body">
                    <div class="chit-qra">
                        <div class="chit-qra-item"><label>Qty</label><input type="number" min="1" name="qty[]" class="qty-inp" value="<?= $s_qty ?>"></div>
                        <div class="chit-qra-item"><label>Rate (₹)</label><input type="number" step="0.01" min="0" name="rate[]" class="rate-inp" value="<?= $s_rate ?>"></div>
                        <div class="chit-qra-item"><label>Amount (₹)</label><input type="text" class="amt-out" readonly value="<?= $s_amount ?>"></div>
                    </div>

                    <div class="chit-row">
                        <div class="chit-cell"><label>Height</label><input type="text" class="m-fld" data-key="height" value="<?= gc_v($s_m,'height') ?>"></div>
                        <div class="chit-cell"><label>Chest</label><input type="text" class="m-fld" data-key="chest" value="<?= gc_v($s_m,'chest') ?>"></div>
                        <div class="chit-cell"><label>Waist</label><input type="text" class="m-fld" data-key="waist" value="<?= gc_v($s_m,'waist') ?>"></div>
                        <div class="chit-cell"><label>Stomach</label><input type="text" class="m-fld" data-key="stomach" value="<?= gc_v($s_m,'stomach') ?>"></div>
                        <div class="chit-cell"><label>Shoulder</label><input type="text" class="m-fld" data-key="shoulder" value="<?= gc_v($s_m,'shoulder') ?>"></div>
                    </div>

                    <div class="chit-row">
                        <div class="chit-cell"><label>Sleeve Length</label><input type="text" class="m-fld" data-key="sleeve_length" value="<?= gc_v($s_m,'sleeve_length') ?>"></div>
                        <div class="chit-cell chit-sleeve-wrap">
                            <label>Sleeve Type</label>
                            <select class="form-select form-select-sm m-fld select2 sleeve-type-sel" data-key="sleeve_type">
                                <option value="">--</option>
                                <option value="Full Sleeves" <?= (($s_m['sleeve_type'] ?? '') === 'Full Sleeves') ? 'selected' : '' ?>>Full Sleeves</option>
                                <option value="Half Sleeves" <?= (($s_m['sleeve_type'] ?? '') === 'Half Sleeves') ? 'selected' : '' ?>>Half Sleeves</option>
                            </select>
                        </div>
                        <div class="chit-cell chit-wide"><label>Collar Style</label><input type="text" class="m-fld" data-key="collar" placeholder="M / X / T / C / R.C" value="<?= gc_v($s_m,'collar') ?>"></div>
                    </div>

                    <div class="chit-fbrow">
                        <div class="chit-fb">F: <input type="text" class="m-fld" data-key="front" value="<?= gc_v($s_m,'front') ?>"></div>
                        <div class="chit-fb">B: <input type="text" class="m-fld" data-key="back" value="<?= gc_v($s_m,'back') ?>"></div>
                    </div>

                    <div class="chit-notes"><label>Other Notes</label><input type="text" class="m-fld" data-key="notes" value="<?= gc_v($s_m,'notes') ?>"></div>
                </div>
            </div>
        </div>
        <input type="hidden" name="garment_type[]" value="Shirt">
        <input type="hidden" name="measurements[]" class="measurements-json" value="">
        <input type="hidden" name="description[]" class="description-hidden" value="">
    </div>

    <!-- KURTA/BLAZER -->
    <div class="type-block p-2">
        <div class="chit-wrap">
            <div class="chit-receipt-bar">
                <div class="chit-receipt-lbl">Receipt No.</div>
                <div class="chit-receipt-val"><?= htmlspecialchars($receipt_no) ?></div>
            </div>
            <div class="chit">
                <div class="chit-label-col"><span>Kurta/Blazer</span></div>
                <div class="chit-body">
                    <div class="chit-qra">
                        <div class="chit-qra-item"><label>Qty</label><input type="number" min="1" name="qty[]" class="qty-inp" value="<?= $k_qty ?>"></div>
                        <div class="chit-qra-item"><label>Rate (₹)</label><input type="number" step="0.01" min="0" name="rate[]" class="rate-inp" value="<?= $k_rate ?>"></div>
                        <div class="chit-qra-item"><label>Amount (₹)</label><input type="text" class="amt-out" readonly value="<?= $k_amount ?>"></div>
                    </div>

                    <div class="chit-row">
                        <div class="chit-cell"><label>Height</label><input type="text" class="m-fld" data-key="height" value="<?= gc_v($k_m,'height') ?>"></div>
                        <div class="chit-cell"><label>Chest</label><input type="text" class="m-fld" data-key="chest" value="<?= gc_v($k_m,'chest') ?>"></div>
                        <div class="chit-cell"><label>Waist</label><input type="text" class="m-fld" data-key="waist" value="<?= gc_v($k_m,'waist') ?>"></div>
                        <div class="chit-cell"><label>Stomach</label><input type="text" class="m-fld" data-key="stomach" value="<?= gc_v($k_m,'stomach') ?>"></div>
                        <div class="chit-cell"><label>Shoulder</label><input type="text" class="m-fld" data-key="shoulder" value="<?= gc_v($k_m,'shoulder') ?>"></div>
                        <div class="chit-cell"><label>Length</label><input type="text" class="m-fld" data-key="length" value="<?= gc_v($k_m,'length') ?>"></div>
                    </div>

                    <div class="chit-row">
                        <div class="chit-cell"><label>Sleeve Length</label><input type="text" class="m-fld" data-key="sleeve_length" value="<?= gc_v($k_m,'sleeve_length') ?>"></div>
                        <div class="chit-cell chit-sleeve-wrap">
                            <label>Sleeve Type</label>
                            <select class="form-select form-select-sm m-fld select2 sleeve-type-sel" data-key="sleeve_type">
                                <option value="">--</option>
                                <option value="Full Sleeves" <?= (($k_m['sleeve_type'] ?? '') === 'Full Sleeves') ? 'selected' : '' ?>>Full Sleeves</option>
                                <option value="Half Sleeves" <?= (($k_m['sleeve_type'] ?? '') === 'Half Sleeves') ? 'selected' : '' ?>>Half Sleeves</option>
                            </select>
                        </div>
                        <div class="chit-cell chit-wide"><label>Collar Style</label><input type="text" class="m-fld" data-key="collar" placeholder="M / X / T / C / R.C" value="<?= gc_v($k_m,'collar') ?>"></div>
                    </div>

                    <div class="chit-fbrow">
                        <div class="chit-fb">F: <input type="text" class="m-fld" data-key="front" value="<?= gc_v($k_m,'front') ?>"></div>
                        <div class="chit-fb">B: <input type="text" class="m-fld" data-key="back" value="<?= gc_v($k_m,'back') ?>"></div>
                    </div>

                    <div class="chit-notes"><label>Other Notes</label><input type="text" class="m-fld" data-key="notes" value="<?= gc_v($k_m,'notes') ?>"></div>
                </div>
            </div>
        </div>
        <input type="hidden" name="garment_type[]" value="Kurta/Blazer">
        <input type="hidden" name="measurements[]" class="measurements-json" value="">
        <input type="hidden" name="description[]" class="description-hidden" value="">
    </div>
</div>