<div class="card-box">
    <div class="card-box-header">
        <h5><i class="fa-solid fa-shirt text-warning"></i> Tailoring Order Details</h5>
        <div class="d-flex gap-2">
            <a href="<?= site_url('tailoring_order/edit/'.$row->id) ?>" class="btn btn-sm btn-edit">
                <i class="fa-solid fa-pen-to-square me-1"></i> Edit
            </a>
            <a href="<?= site_url('tailoring_order') ?>" class="btn btn-sm" style="background:#f1f5f9;color:#374151;">
                <i class="fa-solid fa-arrow-left me-1"></i> Back
            </a>
        </div>
    </div>

    <?php
    $status_colors = [
        'Pending'     => ['bg' => '#fef3c7', 'color' => '#d97706'],
        'In Progress' => ['bg' => '#dbeafe', 'color' => '#2563eb'],
        'Ready'       => ['bg' => '#d1fae5', 'color' => '#059669'],
        'Delivered'   => ['bg' => '#dcfce7', 'color' => '#16a34a'],
        'Cancelled'   => ['bg' => '#fee2e2', 'color' => '#dc2626'],
    ];
    $sc = $status_colors[$row->order_status] ?? ['bg' => '#f1f5f9', 'color' => '#64748b'];
    ?>

    <div class="row mb-3">
        <div class="col-md-6">
            <table class="table table-bordered" style="font-size:13.5px;">
                <tr><th width="40%" class="bg-light">Order No</th><td><code><?= htmlspecialchars($row->order_no) ?></code></td></tr>
                <tr><th class="bg-light">Customer</th><td><strong><?= htmlspecialchars($row->customer_name) ?></strong></td></tr>
                <tr><th class="bg-light">Mobile</th><td><?= $row->mobile ?></td></tr>
                <tr><th class="bg-light">Order Date</th><td><?= date('d M Y', strtotime($row->order_date)) ?></td></tr>
                <tr><th class="bg-light">Delivery Date</th><td><?= date('d M Y', strtotime($row->delivery_date)) ?></td></tr>
                <tr><th class="bg-light">Status</th>
                    <td>
                        <span style="background:<?= $sc['bg'] ?>;color:<?= $sc['color'] ?>;padding:3px 12px;border-radius:12px;font-size:12px;font-weight:700;">
                            <?= $row->order_status ?>
                        </span>
                    </td>
                </tr>
                <tr><th class="bg-light">Notes</th><td><?= nl2br(htmlspecialchars($row->notes ?: '—')) ?></td></tr>
            </table>
        </div>
        <div class="col-md-4">
            <table class="table table-bordered" style="font-size:13.5px;">
                <tr><th class="bg-light">Total Charge</th><td><strong class="text-warning fs-6">₹<?= (int)($row->total_charge) ?></strong></td></tr>
                <tr><th class="bg-light">Advance Paid</th><td>₹<?= (int)($row->advance_paid) ?></td></tr>
                <tr><th class="bg-light">Balance Due</th>
                    <td><strong class="text-danger">₹<?= (int)($row->total_charge - $row->advance_paid) ?></strong></td>
                </tr>
            </table>
        </div>
    </div>

    <div class="p-3 rounded" style="background:#f8fafc;border:1px solid #e2e8f0;">
        <div class="fw-bold text-muted mb-2" style="font-size:12px;letter-spacing:.8px;text-transform:uppercase;">
            <i class="fa-solid fa-shirt me-1"></i> Garment Items
        </div>

        <?php if (empty($items)): ?>
            <div class="text-center text-muted py-3">No items found.</div>
        <?php else: $i = 1; foreach ($items as $item):
            $m = !empty($item->measurements) ? json_decode($item->measurements, true) : [];
            if (!is_array($m)) $m = [];
            $field_labels = [
                'height' => 'Height', 'waist' => 'Waist', 'seat' => 'Seat/Hip', 'thigh' => 'Thigh',
                'knee' => 'Knee', 'bottom' => 'Bottom', 'cross' => 'Cross', 'length' => 'Length',
                'style' => 'Fly/Pocket Style', 'chest' => 'Chest', 'stomach' => 'Stomach',
                'shoulder' => 'Shoulder', 'sleeve_length' => 'Sleeve Length', 'sleeve_type' => 'Sleeve Type',
                'collar' => 'Collar Style', 'front' => 'Front (F)', 'back' => 'Back (B)', 'notes' => 'Notes',
            ];
        ?>
        <div class="border rounded mb-3 bg-white">
            <div class="d-flex justify-content-between align-items-center p-2 border-bottom" style="background:#eef2ff;">
                <div>
                    <span class="badge bg-secondary me-2">#<?= $i++ ?></span>
                    <strong class="text-uppercase"><?= htmlspecialchars($item->garment_type) ?></strong>
                </div>
                <div class="text-end" style="font-size:13px;">
                    Qty: <strong><?= $item->qty ?></strong> &nbsp;|&nbsp;
                    Rate: <strong>₹<?= (int)($item->rate) ?></strong> &nbsp;|&nbsp;
                    Amount: <strong class="text-success">₹<?= (int)($item->amount) ?></strong>
                </div>
            </div>
            <div class="p-2">
                <?php if (!empty($m)): ?>
                <div class="row g-2" style="font-size:13px;">
                    <?php foreach ($m as $key => $val):
                        if ($val === '' || $val === null) continue;
                        $label = $field_labels[$key] ?? ucfirst(str_replace('_', ' ', $key));
                    ?>
                    <div class="col-6 col-md-3">
                        <div class="text-muted" style="font-size:11px;text-transform:uppercase;"><?= htmlspecialchars($label) ?></div>
                        <div class="fw-semibold"><?= htmlspecialchars($val) ?></div>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php elseif (!empty($item->description)): ?>
                    <div style="font-size:13px;"><?= nl2br(htmlspecialchars($item->description)) ?></div>
                <?php else: ?>
                    <div class="text-muted" style="font-size:13px;">—</div>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; endif; ?>

        <div class="d-flex justify-content-end p-2 bg-white rounded border">
            <div class="fw-bold" style="font-size:14px;">Grand Total: <span class="text-warning">₹<?= (int)($row->total_charge) ?></span></div>
        </div>
    </div>
</div>