<div class="card-box">
    <div class="card-box-header">
        <h5><i class="fa-solid fa-shirt text-warning"></i> Tailoring Orders</h5>
        <a href="<?= site_url('tailoring_order/create') ?>" class="btn btn-primary btn-sm">
            <i class="fa-solid fa-plus me-1"></i> Add Tailoring Order
        </a>
    </div>

    <?php if($this->session->flashdata('success')): ?>
    <div class="alert alert-success alert-dismissible fade show py-2">
        <i class="fa-solid fa-circle-check me-2"></i><?= $this->session->flashdata('success') ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <?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'],
    ];
    ?>

    <div class="table-responsive">
        <table class="table table-bordered table-hover dt-table mb-0" style="font-size:13px;">
            <thead>
                <tr>
                    <th width="40">#</th>
                    <th>Order No</th>
                    <th>Order Date</th>
                    <th>Delivery Date</th>
                    <th>Customer</th>
                    <th>Mobile</th>
                    <th>Garment(s)</th>
                    <th class="text-end">Total (₹)</th>
                    <th class="text-end">Advance (₹)</th>
                    <th class="text-center">Status</th>
                    <th width="130" class="text-center" style="white-space:nowrap;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($rows)): $i = 1; foreach($rows as $row):
                    $sc = $status_colors[$row->order_status] ?? ['bg' => '#f1f5f9', 'color' => '#374151'];
                ?>
                <tr>
                    <td class="py-1"><?= $i++ ?></td>
                    <td class="py-1"><code style="font-size:12px;"><?= htmlspecialchars($row->order_no) ?></code></td>
                    <td class="py-1" style="white-space:nowrap;"><?= date('d M Y', strtotime($row->order_date)) ?></td>
                    <td class="py-1" style="white-space:nowrap;"><?= date('d M Y', strtotime($row->delivery_date)) ?></td>
                    <td class="py-1" style="white-space:nowrap;"><strong><?= htmlspecialchars($row->customer_name) ?></strong></td>
                    <td class="py-1"><?= htmlspecialchars($row->mobile) ?></td>
                    <td class="py-1"><?= htmlspecialchars($row->garment_type ?: '—') ?></td>
                    <td class="py-1 text-end fw-bold text-primary">₹<?= number_format((float)$row->total_charge, 2) ?></td>
                    <td class="py-1 text-end">₹<?= number_format((float)$row->advance_paid, 2) ?></td>
                    <td class="py-1 text-center">
                        <span style="background:<?= $sc['bg'] ?>;color:<?= $sc['color'] ?>;padding:3px 10px;border-radius:20px;font-size:11px;font-weight:600;white-space:nowrap;">
                            <?= htmlspecialchars($row->order_status) ?>
                        </span>
                    </td>
                    <td class="py-1 text-center" style="white-space:nowrap;">
                        <a href="<?= site_url('tailoring_order/view/'.$row->id) ?>" class="btn btn-sm btn-view me-1" title="View"><i class="fa-solid fa-eye"></i></a>
                        <a href="<?= site_url('tailoring_order/edit/'.$row->id) ?>" class="btn btn-sm btn-edit me-1" title="Edit"><i class="fa-solid fa-pen-to-square"></i></a>
                        <!--<a href="<?= site_url('tailoring_order/delete/'.$row->id) ?>" class="btn btn-sm btn-delete btn-delete-confirm" title="Delete"><i class="fa-solid fa-trash"></i></a>-->
                    </td>
                </tr>
                <?php endforeach; else: ?>
                <tr><td colspan="11" class="text-center py-4 text-muted">
                    No tailoring orders found. <a href="<?= site_url('tailoring_order/create') ?>">Add first order</a>
                </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
