<div class="modal-header">
    <h4 class="modal-title">Detail Pesanan Custom</h4>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">×</span>
    </button>
</div>
<div class="modal-body">
    <?php
    // Check if meter pemakaian is missing (skip items with bawa_kain_sendiri)
    $hasMissingMeter = false;
    foreach ($detail_model as $detail) {
        // Skip validation if customer brings their own fabric
        if ($detail->bawa_kain_sendiri == 1) {
            continue;
        }
        // Check if meter is missing or zero
        if (empty($detail->meter_pemakaian) || $detail->meter_pemakaian == 0) {
            $hasMissingMeter = true;
            break;
        }
    }

    if ($data->status_pesanan == 'menunggu_konfirmasi' && auth()->user()->role == "admin") {
        $hasMissingMeter = false;
    }


    ?>

    <?php if ($hasMissingMeter): ?>
        <div class="alert alert-danger alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h5><i class="icon fas fa-exclamation-triangle"></i> Meter Pemakaian Belum Diinputkan!</h5>
            Terdapat item yang belum diinputkan meter pemakaiannya. Status pesanan: <strong>Menunggu Input Meter Pemakaian</strong>.
            <?php if (auth()->user()->role == 'kasir'): ?>
                <br>Silakan gunakan tombol "Edit Meter" di halaman utama untuk menginputkan meter pemakaian.
            <?php else: ?>
                <br>Silakan hubungi kasir untuk menginputkan meter pemakaian terlebih dahulu.
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <div class="row">
        <div class="col-sm-6">
            <table cellpadding="5">
                <tr>
                    <td><b>No Order</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td><?= $data->no_order_custom  ?></td>
                </tr>
                <tr>
                    <td><b>Customer</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td><?= $data->nama ?></td>
                </tr>
                <tr>
                    <td><b>Tanggal Order</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td><?= $data->tanggal_order ? date('d/m/Y H:i', strtotime($data->tanggal_order)) : '-'  ?></td>
                </tr>
                <tr>
                    <td><b>Tanggal Pengambilan</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td><?= $data->tanggal_pengambilan ? date('d/m/Y', strtotime($data->tanggal_pengambilan)) : '-' ?></td>
                </tr>
                <tr>
                    <td><b>Status Pesanan</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td>
                        <?php if (!empty($data->status_pesanan)): ?>
                            <?php
                            $statusLabels = [
                                'menunggu_konfirmasi' => '<span class="badge badge-secondary">Menunggu Konfirmasi</span>',
                                'diproses' => '<span class="badge badge-info">Pesanan Diproses</span>',
                                'selesai' => '<span class="badge badge-success">Pesanan Selesai</span>',
                                'siap_diambil' => '<span class="badge badge-primary">Siap Diambil di Toko</span>',
                                'dikirim' => '<span class="badge badge-warning">Dikirim ke Alamat</span>'
                            ];
                            echo $statusLabels[$data->status_pesanan] ?? ucfirst($data->status_pesanan);
                            ?>
                        <?php else: ?>
                            -
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td><b>Tanggal Pelunasan</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td><?= $data->tanggal_pelunasan ? date('d/m/Y H:i', strtotime($data->tanggal_pelunasan)) : '-' ?></td>
                </tr>
                <tr>
                    <td><b>Total Order</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td><?= $data->total_order  ?></td>
                </tr>
                <tr>
                    <td><b>DP Pembayaran</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td>Rp. <?= number_format($data->dp_pembayaran, 0, ",", ".") ?></td>
                </tr>
                <tr>
                    <td><b>Sisa Pembayaran</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td>Rp. <?= number_format($data->sisa_pembayaran, 0, ",", ".") ?></td>
                </tr>
                <tr>
                    <td><b>Nominal Pelunasan</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td>Rp. <?= number_format($data->nominal_pelunasan, 0, ",", ".") ?></td>
                </tr>
                <tr>
                    <td><b>Status Pelunasan</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td>
                        <?php if ($data->status_pelunasan == 2): ?>
                            <span class="badge badge-success">Sudah Dibayar</span>
                        <?php elseif ($data->status_pelunasan == 1): ?>
                            <span class="badge badge-warning">DP</span>
                        <?php else: ?>
                            <span class="badge badge-danger">Belum dibayar</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td><b>Metode Pembayaran</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td><?= $data->metode_pembayaran ? ucfirst($data->metode_pembayaran) : '-' ?></td>
                </tr>
                <tr>
                    <td><b>Alamat Pengiriman</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td>
                        <?= $data->alamat_pengiriman ?? '-' ?>

                        <?php if (isset($data->alamat_locked) && $data->alamat_locked == 1): ?>
                            <span class="badge badge-warning ml-2">
                                <i class="fas fa-lock"></i> Terkunci
                            </span>
                        <?php endif; ?>

                        <?php if (isset($data->alamat_change_request) && $data->alamat_change_request): ?>
                            <br><br>
                            <div class="alert alert-warning mt-2 mb-0">
                                <strong><i class="fas fa-exclamation-triangle"></i> Permintaan Perubahan Alamat:</strong><br>
                                <?= $data->alamat_change_request ?>
                                <br><br>
                                <strong>Status:</strong>
                                <?php if ($data->alamat_change_approved === null): ?>
                                    <span class="badge badge-warning">Menunggu Approval</span>
                                <?php elseif ($data->alamat_change_approved == 1): ?>
                                    <span class="badge badge-success">Disetujui</span>
                                    <?php if ($data->alamat_change_note): ?>
                                        <br><small>Catatan: <?= $data->alamat_change_note ?></small>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <span class="badge badge-danger">Ditolak</span>
                                    <?php if ($data->alamat_change_note): ?>
                                        <br><small>Catatan: <?= $data->alamat_change_note ?></small>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td><b>Status Pengiriman</b></td>
                    <td style="width: 20px; text-align: center;">:</td>
                    <td>
                        <?php if ($data->status_pengiriman == 2): ?>
                            <span class="badge badge-success">Dikirim ke Alamat</span>
                        <?php elseif ($data->status_pengiriman == 1): ?>
                            <span class="badge badge-info">Ambil Ditoko</span>
                        <?php else: ?>
                            -
                        <?php endif; ?>
                    </td>
                </tr>
            </table>
        </div>
        <div class="col-sm-6">
            <table cellpadding="5">
                <?php if ($data->status_pelunasan != 0): ?>
                    <?php if ($data->bukti_pembayaran_dp != null): ?>
                        <tr>
                            <td>
                                <b>Bukti Pembayaran DP</b><br><br>
                                <a href="<?= base_url($data->bukti_pembayaran_dp) ?>" target="_blank"><img src="<?= base_url($data->bukti_pembayaran_dp) ?>" alt="Bukti DP" width="150" class="img-fluid" /></a>
                            </td>
                        </tr>
                    <?php endif; ?>
                    <?php if ($data->bukti_pembayaran != null): ?>
                        <tr>
                            <td>
                                <b>Bukti Pembayaran Lunas/Pelunasan</b><br><br>
                                <a href="<?= base_url($data->bukti_pembayaran) ?>" target="_blank"><img src="<?= base_url($data->bukti_pembayaran) ?>" alt="Bukti Lunas" width="150" class="img-fluid" /></a>
                            </td>
                        </tr>
                    <?php endif; ?>
                <?php endif; ?>
            </table>
        </div>
        <div class="col-sm-12 mt-4">
            <h5 class="mb-3"><i class="fas fa-box-open"></i> Detail Item Pesanan</h5>

            <?php if (empty($detail_model) || count($detail_model) == 0): ?>
                <div class="alert alert-info text-center py-4">
                    <i class="fas fa-info-circle fa-2x mb-2"></i>
                    <p class="mb-0">Tidak ada item pesanan ditemukan untuk order ini.</p>
                </div>
            <?php else: ?>

                <?php
                $itemNumber = 1;
                foreach ($detail_model as $detail):
                    $riwayat = $riwayatPerubahanModel
                        ->where('id_detail_order', $detail->id_detail_order)
                        ->where('status', 1)
                        ->orderBy('created_at', 'DESC')
                        ->first();

                    $message = $riwayat
                        ? $riwayat->keterangan_permintaan_revisi
                        : 'Tidak ada revisi';
                ?>
                    <!-- ITEM CARD -->
                    <?php if (auth()->user()->role == 'customer'): ?>
                        <div class="card mb-3" style="border: 2px solid #d1d5db; border-radius: 8px;">
                            <div class="card-header" style="background: #e5e7eb; color: #111827; border-bottom: 2px solid #d1d5db;">
                                <h6 class="mb-0">
                                    <i class="fas fa-shopping-bag"></i>
                                    <span>Item #<?= $itemNumber++ ?></span>
                                    <span class="float-right">
                                        <?php if ($detail->upload_model_sendiri == 1): ?>
                                            <span class="badge badge-info mr-1">
                                                <i class="fas fa-upload"></i> Model Sendiri
                                            </span>
                                        <?php endif; ?>
                                        <?php if ($detail->bawa_kain_sendiri == 1): ?>
                                            <span class="badge badge-info mr-1">
                                                <i class="fas fa-hand-holding"></i> Kain Sendiri
                                            </span>
                                        <?php endif; ?>
                                        <?php if ($detail->bawa_kain_sendiri != 1 && (empty($detail->meter_pemakaian) || $detail->meter_pemakaian == 0)): ?>
                                            <span class="badge badge-warning">
                                                <i class="fas fa-exclamation-triangle"></i> Meter Belum Diinput
                                            </span>
                                        <?php endif; ?>
                                    </span>
                                </h6>
                            </div>
                        <?php else: ?>
                            <div class="card mb-3 border-<?= ($detail->bawa_kain_sendiri != 1 && (empty($detail->meter_pemakaian) || $detail->meter_pemakaian == 0)) ? 'danger' : 'primary' ?>">
                                <div class="card-header bg-<?= ($detail->bawa_kain_sendiri != 1 && (empty($detail->meter_pemakaian) || $detail->meter_pemakaian == 0)) ? 'danger' : 'primary' ?> text-white">
                                    <h6 class="mb-0">
                                        <i class="fas fa-shopping-bag"></i>
                                        <span style="color: white;">Item #<?= $itemNumber++ ?></span>
                                        <span class="float-right">
                                            <?php if ($detail->upload_model_sendiri == 1): ?>
                                                <span class="badge badge-light text-dark mr-1">
                                                    <i class="fas fa-upload"></i> Model Sendiri
                                                </span>
                                            <?php endif; ?>
                                            <?php if ($detail->bawa_kain_sendiri == 1): ?>
                                                <span class="badge badge-light text-dark mr-1">
                                                    <i class="fas fa-hand-holding"></i> Kain Sendiri
                                                </span>
                                            <?php endif; ?>
                                            <?php if ($detail->bawa_kain_sendiri != 1 && (empty($detail->meter_pemakaian) || $detail->meter_pemakaian == 0)): ?>
                                                <span class="badge badge-warning">
                                                    <i class="fas fa-exclamation-triangle"></i> Meter Belum Diinput
                                                </span>
                                            <?php endif; ?>
                                        </span>
                                    </h6>
                                </div>
                            <?php endif; ?>
                            <div class="card-body">
                                <div class="row">
                                    <!-- MODEL SECTION -->
                                    <div class="col-md-6 mb-3 mb-md-0">
                                        <div class="card h-100">
                                            <?php if (auth()->user()->role == 'customer'): ?>
                                                <div class="card-header" style="background: #e5e7eb; color: #111827; border-bottom: 2px solid #d1d5db;">
                                                    <strong><i class="fas fa-shirt"></i> Model Gamis</strong>
                                                </div>
                                            <?php else: ?>
                                                <div class="card-header bg-success text-white">
                                                    <strong><i class="fas fa-shirt"></i> Model Gamis</strong>
                                                </div>
                                            <?php endif; ?>
                                            <div class="card-body">
                                                <?php if (!empty($detail->foto_model)): ?>
                                                    <div class="text-center mb-3">
                                                        <img src="<?= base_url($detail->foto_model) ?>" alt="Model" class="img-fluid rounded" style="max-height: 200px; object-fit: cover;" />
                                                    </div>
                                                <?php elseif (!empty($detail->gambar_referensi)): ?>
                                                    <div class="text-center mb-3">
                                                        <img src="<?= base_url($detail->gambar_referensi) ?>" alt="Model Referensi" class="img-fluid rounded" style="max-height: 200px; object-fit: cover;" />
                                                    </div>
                                                <?php else: ?>
                                                    <div class="text-center mb-3">
                                                        <div class="bg-light rounded d-flex align-items-center justify-content-center" style="height: 200px;">
                                                            <div class="text-center">
                                                                <i class="fas fa-upload fa-3x text-info mb-2"></i>
                                                                <p class="mb-0 text-muted"><strong>Model Upload Sendiri</strong></p>
                                                                <small class="text-muted">Customer membawa desain sendiri</small>
                                                            </div>
                                                        </div>
                                                    </div>
                                                <?php endif; ?>
                                                <?php if ($detail->upload_model_sendiri == 1): ?>
                                                    <div class="alert alert-info mb-3">
                                                        <i class="fas fa-info-circle"></i> <strong>Informasi:</strong> Customer mengupload model/desain sendiri untuk item ini.
                                                    </div>
                                                <?php endif; ?>
                                                <table class="table table-sm table-borderless">
                                                    <tr>
                                                        <th width="50%">Nama Model:</th>
                                                        <td>
                                                            <?php if (!empty($detail->nama_model)): ?>
                                                                <?= $detail->nama_model ?>
                                                            <?php elseif ($detail->upload_model_sendiri == 1): ?>
                                                                <span class="badge badge-info"><i class="fas fa-upload"></i> Model Upload Sendiri</span>
                                                            <?php else: ?>
                                                                <span class="text-muted">-</span>
                                                            <?php endif; ?>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>Ukuran:</th>
                                                        <td><?= $detail->ukuran_gamis ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Jumlah (Pcs):</th>
                                                        <td><?= $detail->jumlah_pcs ?></td>
                                                    </tr>
                                                    <tr>
                                                        <th>Ongkos Pengerjaan:</th>
                                                        <td><strong>Rp. <?= number_format($detail->subtotal_detail_order, 0, ",", ".") ?></strong></td>
                                                    </tr>
                                                </table>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- KAIN SECTION -->
                                    <div class="col-md-6">
                                        <div class="card h-100">
                                            <?php if (auth()->user()->role == 'customer'): ?>
                                                <div class="card-header" style="background: #e5e7eb; color: #111827; border-bottom: 2px solid #d1d5db;">
                                                    <strong><i class="fas fa-fabric"></i> Jenis Kain</strong>
                                                </div>
                                            <?php else: ?>
                                                <div class="card-header bg-warning text-dark">
                                                    <strong><i class="fas fa-fabric"></i> Jenis Kain</strong>
                                                </div>
                                            <?php endif; ?>
                                            <div class="card-body">
                                                <?php
                                                // Check if there are additional fabrics
                                                $additionalFabrics = [];
                                                if (!empty($detail->additional_fabrics)) {
                                                    $additionalFabrics = json_decode($detail->additional_fabrics, true);
                                                    if (!is_array($additionalFabrics)) {
                                                        $additionalFabrics = [];
                                                    }
                                                }

                                                $fabricCount = count($additionalFabrics);
                                                $totalFabrics = $fabricCount + 1; // Primary + additional
                                                ?>

                                                <?php if ($fabricCount > 0): ?>
                                                    <div class="alert alert-info mb-3">
                                                        <i class="fas fa-info-circle"></i> <strong>Kombinasi Kain:</strong> <?= $totalFabrics ?> jenis kain
                                                    </div>
                                                <?php endif; ?>

                                                <!-- PRIMARY FABRIC -->
                                                <?php if (!empty($detail->kode_jenis_kain) && $detail->bawa_kain_sendiri != 1): ?>
                                                    <div class="mb-3 pb-3 <?= $fabricCount > 0 ? 'border-bottom' : '' ?>">
                                                        <h6 class="text-primary">
                                                            <i class="fas fa-star"></i> Kain Utama
                                                        </h6>
                                                        <?php if (!empty($detail->foto_jenis_kain)): ?>
                                                            <div class="text-center mb-3">
                                                                <img src="<?= base_url($detail->foto_jenis_kain) ?>" alt="Kain" class="img-fluid rounded" style="max-height: 150px; object-fit: cover;" />
                                                            </div>
                                                        <?php else: ?>
                                                            <div class="text-center mb-3">
                                                                <div class="bg-light rounded d-flex align-items-center justify-content-center" style="height: 150px;">
                                                                    <div class="text-center">
                                                                        <i class="fas fa-hand-holding fa-2x text-info mb-2"></i>
                                                                        <p class="mb-0 text-muted"><strong>Bawa Kain Sendiri</strong></p>
                                                                        <small class="text-muted">Customer menyediakan kain</small>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        <?php endif; ?>
                                                        <table class="table table-sm table-borderless">
                                                            <tr>
                                                                <th width="50%">Jenis Kain:</th>
                                                                <td><?= $detail->nama_jenis_kain ?></td>
                                                            </tr>
                                                            <tr>
                                                                <th>Warna:</th>
                                                                <td><?= $detail->warna ?></td>
                                                            </tr>
                                                            <?php if ($detail->bawa_kain_sendiri != 1): ?>
                                                                <tr>
                                                                    <th>Meter Pemakaian:</th>
                                                                    <td>
                                                                        <?php if (empty($detail->meter_pemakaian) || $detail->meter_pemakaian == 0): ?>
                                                                            <span class="badge badge-danger">
                                                                                <i class="fas fa-exclamation-circle"></i> Belum Diinputkan
                                                                            </span>
                                                                        <?php else: ?>
                                                                            <?= $detail->meter_pemakaian ?> meter
                                                                        <?php endif; ?>
                                                                    </td>
                                                                </tr>
                                                            <?php endif; ?>
                                                            <tr>
                                                                <th>Harga:</th>
                                                                <td><strong>Rp. <?= number_format($detail->harga_per_meter * ($detail->meter_pemakaian ?? 0), 0, ",", ".") ?></strong></td>
                                                            </tr>
                                                        </table>
                                                    </div>
                                                <?php else: ?>
                                                    <div class="mb-3 pb-3 <?= $fabricCount > 0 ? 'border-bottom' : '' ?>">
                                                        <div class="text-center mb-3">
                                                            <div class="bg-light rounded d-flex align-items-center justify-content-center" style="height: 150px;">
                                                                <div class="text-center">
                                                                    <i class="fas fa-hand-holding fa-3x text-info mb-2"></i>
                                                                    <p class="mb-0 text-muted"><strong>Bawa Kain Sendiri</strong></p>
                                                                    <small class="text-muted">Customer menyediakan kain sendiri</small>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="alert alert-info">
                                                            <i class="fas fa-info-circle"></i> <strong>Informasi:</strong> Customer membawa kain sendiri untuk item ini. Tidak ada pembelian kain dari toko.
                                                        </div>
                                                    </div>
                                                <?php endif; ?>

                                                <!-- ADDITIONAL FABRICS -->
                                                <?php if ($fabricCount > 0): ?>
                                                    <?php foreach ($additionalFabrics as $idx => $fabric): ?>
                                                        <div class="mb-3 pb-3 <?= ($idx < $fabricCount - 1) ? 'border-bottom' : '' ?>">
                                                            <h6 class="text-secondary">
                                                                <i class="fas fa-plus-circle"></i> Kain Tambahan #<?= ($idx + 1) ?>
                                                            </h6>
                                                            <div class="text-center mb-2">
                                                                <img src="<?= base_url($fabric['foto_jenis_kain'] ?? 'assets/images/placeholder.png') ?>" alt="Kain" class="img-fluid rounded" style="max-height: 100px; object-fit: cover;" />
                                                            </div>
                                                            <table class="table table-sm table-borderless">
                                                                <tr>
                                                                    <th width="50%">Jenis Kain:</th>
                                                                    <td><?= $fabric['nama_jenis_kain'] ?? '-' ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <th>Warna:</th>
                                                                    <td><?= $fabric['warna'] ?? '-' ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <th>Meter Pemakaian:</th>
                                                                    <td>
                                                                        <?php if (empty($fabric['meter_pemakaian']) || $fabric['meter_pemakaian'] == 0): ?>
                                                                            <span class="badge badge-danger">
                                                                                <i class="fas fa-exclamation-circle"></i> Belum Diinputkan
                                                                            </span>
                                                                        <?php else: ?>
                                                                            <?= $fabric['meter_pemakaian'] ?> meter
                                                                        <?php endif; ?>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <th>Harga:</th>
                                                                    <td><strong>Rp. <?= number_format(($fabric['harga_per_meter'] ?? 0) * ($fabric['meter_pemakaian'] ?? 0), 0, ",", ".") ?></strong></td>
                                                                </tr>
                                                            </table>
                                                        </div>
                                                    <?php endforeach; ?>

                                                    <div class="alert alert-success mb-0">
                                                        <strong>Total Harga Kain:</strong>
                                                        <span class="float-right">Rp. <?= number_format($detail->subtotal_detail_kain, 0, ",", ".") ?></span>
                                                    </div>
                                                <?php else: ?>
                                                    <div class="alert alert-success mb-0">
                                                        <strong>Total Harga Kain:</strong>
                                                        <span class="float-right">Rp. <?= number_format($detail->subtotal_detail_kain, 0, ",", ".") ?></span>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- CUSTOM MEASUREMENTS SECTION -->
                                <?php
                                $hasCustomMeasurements = false;
                                $customMeasurements = [];

                                // Check and skip if value is 0, '0', or empty
                                if (!empty($detail->lingkar_dada) && $detail->lingkar_dada != 0 && $detail->lingkar_dada != '0') {
                                    $customMeasurements['Lingkar Dada'] = $detail->lingkar_dada . ' cm';
                                    $hasCustomMeasurements = true;
                                }
                                if (!empty($detail->lingkar_pinggang) && $detail->lingkar_pinggang != 0 && $detail->lingkar_pinggang != '0') {
                                    $customMeasurements['Lingkar Pinggang'] = $detail->lingkar_pinggang . ' cm';
                                    $hasCustomMeasurements = true;
                                }
                                if (!empty($detail->lingkar_panggul) && $detail->lingkar_panggul != 0 && $detail->lingkar_panggul != '0') {
                                    $customMeasurements['Lingkar Panggul'] = $detail->lingkar_panggul . ' cm';
                                    $hasCustomMeasurements = true;
                                }
                                if (!empty($detail->lebar_bahu) && $detail->lebar_bahu != 0 && $detail->lebar_bahu != '0') {
                                    $customMeasurements['Lebar Bahu'] = $detail->lebar_bahu . ' cm';
                                    $hasCustomMeasurements = true;
                                }
                                if (!empty($detail->panjang_lengan) && $detail->panjang_lengan != 0 && $detail->panjang_lengan != '0') {
                                    $customMeasurements['Panjang Lengan'] = $detail->panjang_lengan . ' cm';
                                    $hasCustomMeasurements = true;
                                }
                                if (!empty($detail->panjang_gamis) && $detail->panjang_gamis != 0 && $detail->panjang_gamis != '0') {
                                    $customMeasurements['Panjang Gamis'] = $detail->panjang_gamis . ' cm';
                                    $hasCustomMeasurements = true;
                                }
                                if (!empty($detail->catatan_detail)) {
                                    $customMeasurements['Catatan'] = $detail->catatan_detail;
                                    $hasCustomMeasurements = true;
                                }
                                ?>

                                <?php if ($hasCustomMeasurements): ?>
                                    <div class="mt-3">
                                        <div class="card">
                                            <div class="card-header bg-info text-white">
                                                <strong><i class="fas fa-ruler-combined"></i> Ukuran Custom</strong>
                                            </div>
                                            <div class="card-body">
                                                <table class="table table-sm table-borderless">
                                                    <?php foreach ($customMeasurements as $label => $value): ?>
                                                        <tr>
                                                            <th width="50%"><?= $label ?>:</th>
                                                            <td><?= $value ?></td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                <?php endif; ?>

                                <!-- REVISI INFO -->
                                <?php if ($riwayat): ?>
                                    <div class="mt-3">
                                        <div class="alert alert-info mb-0">
                                            <strong><i class="fas fa-info-circle"></i> Keterangan Revisi:</strong>
                                            <p class="mb-0 mt-2"><?= $message ?></p>
                                            <small class="text-muted">Tanggal: <?= date('d/m/Y H:i', strtotime($riwayat->tanggal_permintaan_revisi)) ?></small>
                                        </div>
                                    </div>
                                <?php endif; ?>
                            </div>
                            </div>
                        <?php endforeach; ?>

                    <?php endif; // End of check for empty detail_model 
                    ?>
                        </div>
        </div>

        <!-- REVISION HISTORY SECTION -->
        <?php if (isset($revisions) && !empty($revisions)): ?>
            <div class="mt-4">
                <div class="card">
                    <div class="card-header bg-warning text-dark">
                        <h5 class="mb-0"><i class="fas fa-history"></i> Riwayat Pesanan</h5>
                    </div>
                    <div class="card-body">
                        <div>
                            <?php foreach ($revisions as $rev): ?>
                                <?php
                                $changesData = json_decode($rev['perubahan_data'], true);
                                $difference = $changesData['difference'] ?? 0;
                                $oldNominal = $changesData['old_nominal'] ?? 0;
                                $newNominal = $changesData['new_nominal'] ?? 0;
                                ?>
                                <div class="timeline-item mb-3 pb-3 border-bottom">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div>
                                            <h6 class="mb-1">
                                                <i class="fas fa-user-edit text-primary"></i>
                                                <strong><?= esc($rev['user_name']) ?></strong>
                                            </h6>
                                            <small class="text-muted">
                                                <i class="fas fa-clock"></i>
                                                <?= date('d/m/Y H:i', strtotime($rev['created_at'])) ?>
                                            </small>
                                        </div>
                                        <?php if ($difference != 0): ?>
                                            <span class="badge badge-<?= $difference > 0 ? 'success' : 'danger' ?> badge-pill">
                                                <?= $difference > 0 ? '+' : '' ?>Rp. <?= number_format($difference, 0, ',', '.') ?>
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                    <div class="mt-2">
                                        <table class="table table-sm table-borderless mb-0">
                                            <tr>
                                                <td width="40%"><strong>Nominal Lama:</strong></td>
                                                <td>Rp. <?= number_format($oldNominal, 0, ',', '.') ?></td>
                                            </tr>
                                            <tr>
                                                <td><strong>Nominal Baru:</strong></td>
                                                <td>Rp. <?= number_format($newNominal, 0, ',', '.') ?></td>
                                            </tr>
                                            <tr>
                                                <td><strong>Total Item:</strong></td>
                                                <td><?= $changesData['total_items'] ?? '-' ?> item</td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>

        <?php
        $role = auth()->user()->role;
        $isPending = ($data->status_admin_konfirmasi == 0 || $data->status_pesanan == 'menunggu_konfirmasi');
        $isConfirmed = ($data->status_admin_konfirmasi == 1);
        $isLunas = ($data->status_pelunasan == 2);
        $isKasir = ($role == 'kasir');
        $isAdmin = $role == 'admin';

        ?>

        <!-- KASIR CONFIRMATION FORM -->
        <?php if ($isKasir && $isPending && $isLunas): ?>
            <div class="mt-4">
                <div class="card border-success">
                    <div class="card-header bg-success text-white">
                        <h5 class="mb-0"><i class="fas fa-check-circle"></i> Form Konfirmasi Pesanan (Kasir)</h5>
                    </div>
                    <form action="<?= base_url('permintaan_custom/confirmAsKasir/' . $data->kode_permintaan_custom) ?>" method="post">
                        <?= csrf_field() ?>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-calendar-alt"></i> Tanggal Pengambilan <span class="text-danger">*</span></label>
                                        <input type="date"
                                            class="form-control"
                                            name="tanggal_pengambilan"
                                            value="<?= $data->tanggal_pengambilan ? date('Y-m-d', strtotime($data->tanggal_pengambilan)) : '' ?>"
                                            required>
                                        <small class="form-text text-muted">Perkiraan tanggal pesanan siap diambil</small>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-tasks"></i> Status Pesanan <span class="text-danger">*</span></label>
                                        <select class="form-control" name="status_pesanan" required>
                                            <option value="">-- Pilih Status --</option>
                                            <option value="menunggu" <?= ($data->status_pesanan ?? '') == 'menunggu' ? 'selected' : '' ?>>Menunggu</option>
                                            <option value="diproses" <?= ($data->status_pesanan ?? '') == 'diproses' ? 'selected' : '' ?>>Pesanan Diproses</option>
                                        </select>
                                        <small class="form-text text-muted">Status awal pesanan setelah konfirmasi</small>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label><i class="fas fa-clock"></i> Estimasi Pengerjaan</label>
                                <textarea class="form-control"
                                    name="estimasi_pengerjaan"
                                    rows="2"><?= $data->estimasi_pengerjaan ?? 'Estimasi pengerjaan 7-14 hari tergantung desain & proses' ?></textarea>
                                <small class="form-text text-muted">Informasi estimasi waktu pengerjaan untuk customer</small>
                            </div>
                            <div class="alert alert-info mb-0">
                                <i class="fas fa-info-circle"></i> <strong>Catatan:</strong> Setelah konfirmasi, pesanan akan masuk ke proses pengerjaan dan customer akan menerima notifikasi.
                            </div>
                        </div>
                        <div class="card-footer">
                            <button type="submit" class="btn btn-success btn-lg">
                                <i class="fas fa-check-circle"></i> Konfirmasi Pesanan
                            </button>
                            <button type="button" class="btn btn-secondary btn-lg" data-dismiss="modal">Batal</button>
                        </div>
                    </form>
                </div>
            </div>
        <?php endif; ?>

        <!-- KASIR UPDATE STATUS (AFTER CONFIRMATION & LUNAS) -->
        <?php if ($isKasir && $isConfirmed && $isLunas): ?>
            <div class="mt-4">
                <div class="card border-primary">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-edit"></i> Update Status Pesanan</h5>
                    </div>
                    <form action="<?= base_url('permintaan_custom/updateStatus/' . $data->kode_permintaan_custom) ?>" method="post">
                        <?= csrf_field() ?>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6 d-none">
                                    <div class="form-group">
                                        <label><i class="fas fa-calendar-alt"></i> Tanggal Pengambilan</label>
                                        <input type="date"
                                            class="form-control"
                                            name="tanggal_pengambilan"
                                            value="<?= $data->tanggal_pengambilan ? date('Y-m-d', strtotime($data->tanggal_pengambilan)) : '' ?>">
                                        <small class="form-text text-muted">Tanggal pesanan siap diambil</small>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-tasks"></i> Status Pesanan <span class="text-danger">*</span></label>
                                        <select class="form-control" name="status_pesanan" required>
                                            <option value="menunggu" <?= ($data->status_pesanan ?? '') == 'menunggu' ? 'selected' : '' ?>>Menunggu</option>
                                            <option value="diproses" <?= ($data->status_pesanan ?? '') == 'diproses' ? 'selected' : '' ?>>Pesanan Diproses</option>
                                            <option value="selesai" <?= ($data->status_pesanan ?? '') == 'selesai' ? 'selected' : '' ?>>Pesanan Selesai</option>
                                            <option value="siap_diambil" <?= ($data->status_pesanan ?? '') == 'siap_diambil' ? 'selected' : '' ?>>Siap Diambil di Toko</option>
                                            <option value="dikirim" <?= ($data->status_pesanan ?? '') == 'dikirim' ? 'selected' : '' ?>>Dikirim ke Alamat</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label><i class="fas fa-clock"></i> Estimasi Pengerjaan</label>
                                <textarea class="form-control"
                                    name="estimasi_pengerjaan"
                                    rows="2"><?= $data->estimasi_pengerjaan ?? 'Estimasi pengerjaan 7-14 hari tergantung desain & proses' ?></textarea>
                            </div>
                        </div>
                        <div class="card-footer">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> Update Status
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        <?php endif; ?>

        <!-- DISPLAY ESTIMASI FOR NON-KASIR -->
        <?php if (!$isKasir && $isConfirmed): ?>
            <div class="mt-4">
                <div class="alert alert-info">
                    <h6><i class="fas fa-clock"></i> Estimasi Pengerjaan:</h6>
                    <p class="mb-0"><?= $data->estimasi_pengerjaan ?? 'Estimasi pengerjaan 7-14 hari tergantung desain & proses' ?></p>
                </div>
            </div>
        <?php endif; ?>

        <div class="modal-footer">
            <?php if (($title == 'Konfirmasi Pesanan' || $title == 'Detail Model') && $role == 'admin' && $isPending): ?>
                <!-- Admin Confirmation Section -->
                <form action="<?= base_url('permintaan_custom/confirmAsAdmin/' . $data->kode_permintaan_custom) ?>" method="post" style="display:inline;">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success">
                        <i class="fas fa-check"></i> Konfirmasi
                    </button>
                </form>
            <?php endif; ?>

            <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
        </div>
    </div>

    <!-- Modal Reject Reason -->
    <div class="modal fade" id="modalRejectReason" tabindex="-1" role="dialog" aria-labelledby="modalRejectLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form action="<?= base_url('permintaan_custom/rejectAsAdmin/' . $data->kode_permintaan_custom) ?>" method="post">
                    <div class="modal-header">
                        <h5 class="modal-title" id="modalRejectLabel">Alasan Penolakan</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="alasan_penolakan">Alasan Penolakan Pesanan</label>
                            <textarea class="form-control" id="alasan_penolakan" name="catatan_toko" placeholder="Jelaskan alasan penolakan..." rows="3" required></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-danger">Tolak Pesanan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Modal Create Revision -->
    <div class="modal fade" id="modalCreateRevision" tabindex="-1" role="dialog" aria-labelledby="modalRevisionLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form action="<?= base_url('riwayat_perubahan/createRevision') ?>" method="post">
                    <div class="modal-header">
                        <h5 class="modal-title" id="modalRevisionLabel">Buat Permintaan Revisi</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="id_detail_order" value="<?= $data->kode_permintaan_custom ?>">
                        <div class="form-group">
                            <label for="tipe_revisi">Tipe Revisi</label>
                            <select class="custom-select" id="tipe_revisi" name="tipe_revisi" required>
                                <option value="">--- Pilih Tipe Revisi ---</option>
                                <option value="ukuran">Ukuran</option>
                                <option value="warna">Warna</option>
                                <option value="material">Material/Kain</option>
                                <option value="desain">Desain</option>
                                <option value="lainnya">Lainnya</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="keterangan_revisi">Keterangan Revisi</label>
                            <textarea class="form-control" id="keterangan_revisi" name="keterangan" placeholder="Jelaskan perubahan yang diminta..." rows="3" required></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary">Buat Revisi</button>
                    </div>
                </form>
            </div>
        </div>
    </div>