<div class="modal-content">
    <div class="modal-header bg-light text-dark">
        <h5 class="modal-title">Detail Riwayat 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">
        <div class="row">
            <div class="col-md-6">
                <table class="table table-sm">
                    <tr>
                        <th width="40%">No Order</th>
                        <td><?= $revisi->no_order_custom ?></td>
                    </tr>
                    <tr>
                        <th>Nama Customer</th>
                        <td><?= $revisi->customer_name ?></td>
                    </tr>
                    <tr>
                        <th>User Revisi</th>
                        <td><?= $revisi->user_name ?></td>
                    </tr>
                    <tr>
                        <th>Tanggal Revisi</th>
                        <td><?= date('d/m/Y H:i:s', strtotime($revisi->created_at)) ?></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <?php if ($perubahan): ?>
                    <div class="card card-outline card-secondary">
                        <div class="card-header bg-light text-dark">
                            <h3 class="card-title"><i class="fas fa-exchange-alt"></i> Perubahan Data</h3>
                        </div>
                        <div class="card-body p-2">
                            <table class="table table-sm table-borderless">
                                <?php if (isset($perubahan->old_nominal)): ?>
                                    <tr>
                                        <th width="50%">Nominal Lama:</th>
                                        <td class="text-danger">
                                            <del>Rp <?= number_format($perubahan->old_nominal, 0, ',', '.') ?></del>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>Nominal Baru:</th>
                                        <td class="text-success">
                                            <strong>Rp <?= number_format($perubahan->new_nominal, 0, ',', '.') ?></strong>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>Selisih:</th>
                                        <td class="<?= ($perubahan->new_nominal - $perubahan->old_nominal) >= 0 ? 'text-success' : 'text-danger' ?>">
                                            <strong>
                                                <?= ($perubahan->new_nominal - $perubahan->old_nominal) >= 0 ? '+' : '' ?>
                                                Rp <?= number_format($perubahan->new_nominal - $perubahan->old_nominal, 0, ',', '.') ?>
                                            </strong>
                                        </td>
                                    </tr>
                                <?php endif; ?>

                                <?php if (isset($perubahan->total_items)): ?>
                                    <tr>
                                        <th>Total Item:</th>
                                        <td><?= $perubahan->total_items ?></td>
                                    </tr>
                                <?php endif; ?>

                                <?php if (isset($perubahan->timestamp)): ?>
                                    <tr>
                                        <th>Waktu Perubahan:</th>
                                        <td><?= date('d/m/Y H:i:s', strtotime($perubahan->timestamp)) ?></td>
                                    </tr>
                                <?php endif; ?>
                            </table>
                        </div>
                    </div>
                <?php else: ?>
                    <div class="alert alert-light text-dark">
                        <i class="fas fa-info-circle"></i> Tidak ada detail perubahan data.
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <?php if ($perubahan && isset($perubahan->items_details) && !empty($perubahan->items_details)): ?>
            <hr>
            <h6 class="text-bold"><i class="fas fa-box-open"></i> Detail Item Revisi:</h6>
            <div class="accordion" id="accordionItems">
                <?php foreach ($perubahan->items_details as $idx => $item): ?>
                    <div class="card">
                        <div class="card-header p-2 bg-light text-dark" id="heading<?= $idx ?>">
                            <h2 class="mb-0">
                                <button class="btn btn-link btn-block text-left text-dark" type="button" data-toggle="collapse" data-target="#collapse<?= $idx ?>" aria-expanded="true" aria-controls="collapse<?= $idx ?>">
                                    <i class="fas fa-box"></i> Item #<?= $item->item_number ?>
                                    <?php if ($item->is_new): ?>
                                        <span class="badge badge-secondary">BARU</span>
                                    <?php endif; ?>
                                    <?php if (!empty($item->changes)): ?>
                                        <span class="badge badge-dark"><?= count($item->changes) ?> Perubahan</span>
                                    <?php endif; ?>
                                    - <?= $item->nama_model ?> / <?= $item->nama_jenis_kain ?> (<?= $item->warna ?>)
                                </button>
                            </h2>
                        </div>

                        <div id="collapse<?= $idx ?>" class="collapse <?= $idx === 0 ? 'show' : '' ?>" aria-labelledby="heading<?= $idx ?>" data-parent="#accordionItems">
                            <div class="card-body p-2">
                                <div class="row">
                                    <div class="col-md-6">
                                        <table class="table table-sm table-borderless">
                                            <tr>
                                                <th width="40%">Model:</th>
                                                <td><?= $item->nama_model ?> (<?= $item->kode_model ?>)</td>
                                            </tr>
                                            <tr>
                                                <th>Jenis Kain:</th>
                                                <td><?= $item->nama_jenis_kain ?> (<?= $item->kode_jenis_kain ?>)</td>
                                            </tr>
                                            <tr>
                                                <th>Warna:</th>
                                                <td><?= $item->warna ?></td>
                                            </tr>
                                            <tr>
                                                <th>Ukuran:</th>
                                                <td><?= $item->ukuran ?></td>
                                            </tr>
                                            <tr>
                                                <th>Jumlah:</th>
                                                <td><?= $item->jumlah_pcs ?> pcs</td>
                                            </tr>
                                            <tr>
                                                <th>Meter Kain:</th>
                                                <td><?= $item->meter_pemakaian ?> m</td>
                                            </tr>
                                        </table>
                                    </div>
                                    <div class="col-md-6">
                                        <table class="table table-sm table-borderless">
                                            <tr>
                                                <th width="50%">Harga Model:</th>
                                                <td>Rp <?= number_format($item->harga_model, 0, ',', '.') ?></td>
                                            </tr>
                                            <tr>
                                                <th>Harga Kain/m:</th>
                                                <td>Rp <?= number_format($item->harga_kain, 0, ',', '.') ?></td>
                                            </tr>
                                            <tr>
                                                <th>Subtotal Model:</th>
                                                <td>Rp <?= number_format($item->subtotal_model, 0, ',', '.') ?></td>
                                            </tr>
                                            <tr>
                                                <th>Subtotal Kain:</th>
                                                <td>Rp <?= number_format($item->subtotal_kain, 0, ',', '.') ?></td>
                                            </tr>
                                            <tr class="bg-light">
                                                <th>Total Item:</th>
                                                <td><strong>Rp <?= number_format($item->total_item, 0, ',', '.') ?></strong></td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>

                                <?php
                                // Check if item has custom measurements
                                $hasCustomSize = !empty($item->lingkar_dada) || !empty($item->lingkar_pinggang) ||
                                    !empty($item->lingkar_panggul) || !empty($item->lebar_bahu) ||
                                    !empty($item->panjang_lengan) || !empty($item->panjang_gamis);
                                ?>

                                <?php if ($hasCustomSize): ?>
                                    <hr class="mt-2 mb-2">
                                    <h6 class="text-bold mb-2"><i class="fas fa-ruler"></i> Ukuran Custom:</h6>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <table class="table table-sm table-borderless">
                                                <?php if (!empty($item->lingkar_dada)): ?>
                                                    <tr>
                                                        <th width="50%">Lingkar Dada:</th>
                                                        <td><?= $item->lingkar_dada ?> cm</td>
                                                    </tr>
                                                <?php endif; ?>
                                                <?php if (!empty($item->lingkar_pinggang)): ?>
                                                    <tr>
                                                        <th>Lingkar Pinggang:</th>
                                                        <td><?= $item->lingkar_pinggang ?> cm</td>
                                                    </tr>
                                                <?php endif; ?>
                                                <?php if (!empty($item->lingkar_panggul)): ?>
                                                    <tr>
                                                        <th>Lingkar Panggul:</th>
                                                        <td><?= $item->lingkar_panggul ?> cm</td>
                                                    </tr>
                                                <?php endif; ?>
                                            </table>
                                        </div>
                                        <div class="col-md-6">
                                            <table class="table table-sm table-borderless">
                                                <?php if (!empty($item->lebar_bahu)): ?>
                                                    <tr>
                                                        <th width="50%">Lebar Bahu:</th>
                                                        <td><?= $item->lebar_bahu ?> cm</td>
                                                    </tr>
                                                <?php endif; ?>
                                                <?php if (!empty($item->panjang_lengan)): ?>
                                                    <tr>
                                                        <th>Panjang Lengan:</th>
                                                        <td><?= $item->panjang_lengan ?> cm</td>
                                                    </tr>
                                                <?php endif; ?>
                                                <?php if (!empty($item->panjang_gamis)): ?>
                                                    <tr>
                                                        <th>Panjang Gamis:</th>
                                                        <td><?= $item->panjang_gamis ?> cm</td>
                                                    </tr>
                                                <?php endif; ?>
                                            </table>
                                        </div>
                                    </div>
                                <?php endif; ?>

                                <?php if (!empty($item->changes)): ?>
                                    <hr class="mt-2 mb-2">
                                    <div class="alert alert-light text-dark mb-0 p-2" style="border: 1px solid #dee2e6;">
                                        <strong><i class="fas fa-exclamation-triangle"></i> Perubahan:</strong>
                                        <ul class="mb-0 mt-1">
                                            <?php foreach ($item->changes as $change): ?>
                                                <li><?= $change ?></li>
                                            <?php endforeach; ?>
                                        </ul>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php elseif ($perubahan && isset($perubahan->items_changed)): ?>
            <hr>
            <h6 class="text-bold">Item yang Diubah:</h6>
            <div class="table-responsive">
                <table class="table table-sm table-bordered">
                    <thead class="bg-light">
                        <tr>
                            <th>No</th>
                            <th>Nama Item</th>
                            <th>Perubahan</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $no = 1; ?>
                        <?php foreach ($perubahan->items_changed as $item): ?>
                            <tr>
                                <td><?= $no++ ?></td>
                                <td><?= $item->nama ?? '-' ?></td>
                                <td>
                                    <?php if (isset($item->changes)): ?>
                                        <small>
                                            <?php foreach ($item->changes as $field => $change): ?>
                                                <div>
                                                    <strong><?= ucfirst($field) ?>:</strong>
                                                    <span class="text-danger"><?= $change->old ?? '-' ?></span>
                                                    <i class="fas fa-arrow-right"></i>
                                                    <span class="text-success"><?= $change->new ?? '-' ?></span>
                                                </div>
                                            <?php endforeach; ?>
                                        </small>
                                    <?php else: ?>
                                        -
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
    </div>
</div>