<?= $this->extend('layouts/admin_layout') ?>

<?= $this->section('content') ?>

<style>
    /* ===== MODERN SAAS DESIGN SYSTEM TOKENS ===== */
    :root {
        --bg-body: #f8fafc;
        --bg-card: #ffffff;
        --bg-accent: #f1f5f9;
        --bg-input: #ffffff;
        --border-color: #e2e8f0;
        --border-hover: #cbd5e1;
        --text-main: #0f172a;
        --text-sub: #64748b;
        --primary: #4f46e5;
        --primary-hover: #4338ca;
        --primary-light: rgba(79, 70, 229, 0.08);
        --success: #10b981;
        --warning: #f59e0b;
        --danger: #ef4444;
        --info: #06b6d4;
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.05);
        --radius-lg: 16px;
        --radius-md: 12px;
        --radius-sm: 8px;
        --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @media (prefers-color-scheme: dark) {
        :root {
            --bg-body: #0b0f19;
            --bg-card: #151e2e;
            --bg-accent: #1e293b;
            --bg-input: #0f172a;
            --border-color: #243044;
            --border-hover: #334155;
            --text-main: #f8fafc;
            --text-sub: #94a3b8;
            --primary-light: rgba(79, 70, 229, 0.18);
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
            --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
            --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
        }
    }

    [data-bs-theme="dark"] {
        --bg-body: #0b0f19;
        --bg-card: #151e2e;
        --bg-accent: #1e293b;
        --bg-input: #0f172a;
        --border-color: #243044;
        --border-hover: #334155;
        --text-main: #f8fafc;
        --text-sub: #94a3b8;
        --primary-light: rgba(79, 70, 229, 0.18);
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
    }

    body, .content-wrapper, .main-content, .content, .page-content, main, .order-detail-workspace {
        background-color: var(--bg-body) !important;
        color: var(--text-main) !important;
        transition: var(--transition);
    }

    /* Card Premium Styling */
    .card-premium {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-sm);
        transition: var(--transition);
    }

    .card-premium:hover {
        border-color: var(--border-hover);
    }

    .card-header-clean {
        background: transparent;
        border-bottom: 1px solid var(--border-color);
        padding: 1.1rem 1.25rem;
    }

    /* Stat Mini Cards */
    .metric-card {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        padding: 1rem 1.25rem;
        transition: var(--transition);
    }

    .metric-card:hover {
        transform: translateY(-2px);
        box-shadow: var(--shadow-md);
    }

    /* Avatar & Profile */
    .avatar-lg {
        width: 48px;
        height: 48px;
        border-radius: 50%;
        background: var(--primary-light);
        color: var(--primary);
        font-weight: 700;
        font-size: 1.1rem;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    /* Table Design */
    .custom-table {
        margin-bottom: 0;
        width: 100%;
        color: var(--text-main);
    }

    .custom-table thead {
        background: var(--bg-accent);
        border-bottom: 1px solid var(--border-color);
    }

    .custom-table thead th {
        font-size: 0.72rem;
        text-transform: uppercase;
        letter-spacing: 0.08em;
        font-weight: 700;
        color: var(--text-sub);
        padding: 0.85rem 1rem;
        border: none;
    }

    .custom-table tbody tr {
        border-bottom: 1px solid var(--border-color);
        transition: var(--transition);
    }

    .custom-table tbody tr:last-child {
        border-bottom: none;
    }

    .custom-table td {
        padding: 0.9rem 1rem !important;
        vertical-align: middle;
    }

    /* Soft Badges */
    .badge-soft-success { background: rgba(16, 185, 129, 0.12); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
    .badge-soft-warning { background: rgba(245, 158, 11, 0.12); color: #f59e0b; border: 1px solid rgba(245, 158, 11, 0.2); }
    .badge-soft-danger  { background: rgba(239, 68, 68, 0.12); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
    .badge-soft-primary { background: rgba(79, 70, 229, 0.12); color: #4f46e5; border: 1px solid rgba(79, 70, 229, 0.2); }
    .badge-soft-secondary { background: rgba(100, 116, 139, 0.12); color: #64748b; border: 1px solid rgba(100, 116, 139, 0.2); }

    /* Notes Box Callout */
    .notes-box {
        background: var(--bg-accent);
        border-left: 4px solid var(--primary);
        border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
        padding: 1rem 1.25rem;
    }
</style>

<div class="container-fluid py-4 order-detail-workspace">
    
    <?php 
        // Helper Phone & WA Link
        $phone = preg_replace('/[^0-9]/', '', $order['customer_phone'] ?? '');
        $waLink = $phone ? 'https://wa.me/' . (str_starts_with($phone, '0') ? '62' . substr($phone, 1) : $phone) : null;
        $customerInitial = strtoupper(substr(trim($order['customer_name'] ?? 'C'), 0, 1));

        // Status Colors & Badges
        $payStatus = strtolower($order['payment_status'] ?? 'unpaid');
        $payClass  = $payStatus === 'paid' ? 'badge-soft-success' : ($payStatus === 'partial' ? 'badge-soft-warning' : 'badge-soft-danger');
        
        $orderType = strtolower($order['order_type'] ?? 'ready');
        $typeClass = $orderType === 'ready' ? 'badge-soft-success' : 'badge-soft-warning';
    ?>

    <!-- PAGE HEADER & ACTIONS -->
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center mb-4 gap-3">
        <div>
            <div class="d-flex align-items-center gap-2 mb-1">
                <a href="<?= base_url('admin/orders') ?>" class="btn btn-sm btn-outline-secondary rounded-2 px-2 py-1" title="Kembali ke daftar">
                    <i class="bi bi-arrow-left"></i>
                </a>
                <h3 class="fw-bold mb-0 text-main">Order #<?= esc($order['order_number']) ?></h3>
                <span class="badge <?= $payClass ?> rounded-pill px-2.5 py-1 font-monospace"><?= strtoupper($payStatus) ?></span>
                <span class="badge <?= $typeClass ?> rounded-pill px-2.5 py-1 font-monospace"><?= strtoupper($orderType) ?></span>
            </div>
            <p class="text-sub small mb-0">Detail lengkap pesanan, invoice, serta riwayat transaksi pembayaran.</p>
        </div>

        <div class="d-flex align-items-center gap-2 flex-wrap">
            <a href="<?= base_url('admin/orders/edit/' . $order['id_order']) ?>" class="btn btn-outline-secondary rounded-3 px-3 py-2 fw-semibold d-inline-flex align-items-center gap-1.5 fs-7">
                <i class="bi bi-pencil"></i> Edit Order
            </a>
            <a href="<?= base_url('admin/payments/create/' . $order['id_order']) ?>" class="btn btn-primary rounded-3 px-3.5 py-2 fw-semibold d-inline-flex align-items-center gap-1.5 fs-7 shadow-sm">
                <i class="bi bi-wallet2"></i> Catat Bayar
            </a>
        </div>
    </div>

    <!-- FLASH MESSAGES -->
    <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
            <div class="d-flex align-items-center gap-2">
                <i class="bi bi-check-circle-fill fs-5"></i>
                <div><?= session()->getFlashdata('success') ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <!-- RINGKASAN METRIK FINANSIAL -->
    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3">
            <div class="metric-card shadow-sm">
                <div class="small fw-semibold text-sub mb-1" style="font-size: 0.75rem;">TOTAL ORDER</div>
                <div class="h4 fw-bolder text-main mb-0">Rp <?= number_format((float)($order['total'] ?? 0), 0, ',', '.') ?></div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="metric-card shadow-sm">
                <div class="small fw-semibold text-sub mb-1" style="font-size: 0.75rem;">SUDAH DIBAYAR</div>
                <div class="h4 fw-bolder text-success mb-0">Rp <?= number_format((float)($order['paid_amount'] ?? 0), 0, ',', '.') ?></div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="metric-card shadow-sm">
                <div class="small fw-semibold text-sub mb-1" style="font-size: 0.75rem;">SISA TAGIHAN</div>
                <div class="h4 fw-bolder <?= ((float)($order['remaining_amount'] ?? 0) > 0) ? 'text-danger' : 'text-sub' ?> mb-0">
                    Rp <?= number_format((float)($order['remaining_amount'] ?? 0), 0, ',', '.') ?>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="metric-card shadow-sm">
                <div class="small fw-semibold text-sub mb-1" style="font-size: 0.75rem;">TARGET SELESAI</div>
                <div class="h4 fw-bolder text-main mb-0 d-flex align-items-center gap-1.5">
                    <i class="bi bi-calendar3 text-primary opacity-75 fs-6"></i>
                    <span style="font-size: 1.05rem;"><?= !empty($order['target_date']) ? esc($order['target_date']) : '-' ?></span>
                </div>
            </div>
        </div>
    </div>

    <?php if (($order['order_type'] ?? '') === 'po' && !empty($productionSchedules)): ?>
        <div class="card-premium shadow-sm mb-4">
            <div class="card-header-clean d-flex align-items-center gap-2">
                <i class="bi bi-calendar-range text-primary"></i>
                <h6 class="fw-bold text-main mb-0">Jadwal Produksi Bulanan</h6>
            </div>
            <div class="card-body py-3">
                <div class="row g-2">
                    <?php foreach ($productionSchedules as $schedule): ?>
                        <div class="col-6 col-md-3">
                            <div class="border rounded-3 p-2 bg-light h-100">
                                <div class="small text-muted">Batch <?= esc(date('m/Y', strtotime($schedule['scheduled_month']))) ?></div>
                                <div class="fw-bold text-primary"><?= (int) $schedule['quantity'] ?> unit</div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <!-- MAIN CONTENT GRID -->
    <div class="row g-4">
        
        <!-- LEFT COLUMN: ITEM PESANAN & CATATAN -->
        <div class="col-12 col-lg-8">
            
            <!-- CARD ITEM PESANAN -->
            <div class="card-premium shadow-sm mb-4">
                <div class="card-header-clean d-flex align-items-center justify-content-between">
                    <h6 class="fw-bold text-main mb-0 d-flex align-items-center gap-2">
                        <i class="bi bi-box-seam text-primary"></i> Rincian Item Pesanan
                    </h6>
                    <span class="badge badge-soft-secondary rounded-pill px-2.5 py-1">
                        <?= count($items ?? []) ?> Item
                    </span>
                </div>
                <div class="table-responsive">
                    <table class="table custom-table align-middle">
                        <thead>
                            <tr>
                                <th class="ps-3">Produk / Layanan</th>
                                <th class="text-center" style="width: 90px;">Qty</th>
                                <th class="text-end" style="width: 150px;">Harga Satuan</th>
                                <th class="text-end pe-3" style="width: 160px;">Subtotal</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($items)): ?>
                                <?php foreach ($items as $item): ?>
                                    <tr>
                                        <td class="ps-3">
                                            <div class="fw-semibold text-main">
                                                <?= esc($item['product_name'] ?? $item['nama_product'] ?? 'Item Tanpa Nama') ?>
                                            </div>
                                        </td>
                                        <td class="text-center font-monospace fw-medium">
                                            <?= esc($item['qty']) ?>
                                        </td>
                                        <td class="text-end text-sub">
                                            Rp <?= number_format((float)($item['price'] ?? 0), 0, ',', '.') ?>
                                        </td>
                                        <td class="text-end pe-3 fw-bold text-main">
                                            Rp <?= number_format((float)($item['subtotal'] ?? 0), 0, ',', '.') ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="4" class="text-center text-sub py-4">
                                        <i class="bi bi-inbox fs-3 d-block opacity-40 mb-1"></i>
                                        Tidak ada rincian item untuk order ini.
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                        <tfoot>
                            <tr class="bg-accent">
                                <td colspan="3" class="text-end fw-bold text-sub ps-3">Grand Total:</td>
                                <td class="text-end pe-3 fw-bolder text-primary fs-6">
                                    Rp <?= number_format((float)($order['total'] ?? 0), 0, ',', '.') ?>
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>

            <!-- CARD CATATAN / INSTRUKSI -->
            <div class="card-premium shadow-sm">
                <div class="card-header-clean">
                    <h6 class="fw-bold text-main mb-0 d-flex align-items-center gap-2">
                        <i class="bi bi-sticky text-warning"></i> Catatan & Instruksi Khusus
                    </h6>
                </div>
                <div class="p-3.5">
                    <?php if (!empty($order['notes'])): ?>
                        <div class="notes-box">
                            <p class="mb-0 text-main small" style="white-space: pre-line; line-height: 1.6;">
                                <?= esc($order['notes']) ?>
                            </p>
                        </div>
                    <?php else: ?>
                        <p class="text-sub small mb-0 italic">Tidak ada catatan khusus untuk order ini.</p>
                    <?php endif; ?>
                </div>
            </div>

        </div>

        <!-- RIGHT COLUMN: CUSTOMER, INVOICE & PAYMENTS -->
        <div class="col-12 col-lg-4">
            
            <!-- CARD PELANGGAN -->
            <div class="card-premium shadow-sm mb-4">
                <div class="card-header-clean">
                    <h6 class="fw-bold text-main mb-0 d-flex align-items-center gap-2">
                        <i class="bi bi-person text-primary"></i> Informasi Pelanggan
                    </h6>
                </div>
                <div class="p-3.5">
                    <div class="d-flex align-items-center gap-3 mb-3">
                        <div class="avatar-lg"><?= $customerInitial ?></div>
                        <div>
                            <h6 class="fw-bold text-main mb-0"><?= esc($order['customer_name']) ?></h6>
                            <small class="text-sub"><?= esc($order['customer_email'] ?? 'Tidak ada email') ?></small>
                        </div>
                    </div>
                    <div class="pt-2 border-top border-color d-flex flex-column gap-2">
                        <div class="d-flex justify-content-between align-items-center">
                            <span class="small text-sub"><i class="bi bi-telephone me-1"></i> No. Telepon</span>
                            <span class="small fw-semibold text-main"><?= esc($order['customer_phone'] ?? '-') ?></span>
                        </div>
                        <?php if ($waLink): ?>
                            <a href="<?= $waLink ?>" target="_blank" class="btn btn-sm btn-outline-success rounded-3 mt-1 w-100 d-inline-flex align-items-center justify-content-center gap-2 fw-semibold">
                                <i class="bi bi-whatsapp"></i> Chat Customer via WA
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- CARD INVOICE & AKSI -->
            <div class="card-premium shadow-sm mb-4">
                <div class="card-header-clean d-flex align-items-center justify-content-between">
                    <h6 class="fw-bold text-main mb-0 d-flex align-items-center gap-2">
                        <i class="bi bi-receipt-cutoff text-info"></i> Status Invoice
                    </h6>
                    <?php if (!empty($invoice)): ?>
                        <?php 
                            $invStatus = strtolower($invoice['status'] ?? 'unpaid');
                            $invBadge = $invStatus === 'paid' ? 'badge-soft-success' : 'badge-soft-danger';
                        ?>
                        <span class="badge <?= $invBadge ?> rounded-pill px-2.5 py-0.5" style="font-size: 0.7rem;">
                            <?= strtoupper($invStatus) ?>
                        </span>
                    <?php endif; ?>
                </div>
                <div class="p-3.5">
                    <?php if (!empty($invoice)): ?>
                        <div class="mb-3">
                            <div class="fw-bold text-main font-monospace"><?= esc($invoice['nomor_invoice']) ?></div>
                            <div class="small text-sub mt-1">
                                <i class="bi bi-calendar-event me-1"></i> Jatuh Tempo: 
                                <strong><?= esc($invoice['tanggal_jatuh_tempo'] ?? '-') ?></strong>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="text-center py-2 mb-3">
                            <p class="small text-sub mb-0">Invoice belum diterbitkan. Invoice akan terbuat otomatis saat transaksi dicatat.</p>
                        </div>
                    <?php endif; ?>

                    <div class="d-flex flex-column gap-2">
                        <form action="<?= base_url('admin/payments/invoice/send/' . $order['id_order']) ?>" method="post">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-primary rounded-3 w-100 d-inline-flex align-items-center justify-content-center gap-2 fw-semibold py-2">
                                <i class="bi bi-send"></i> Kirim Invoice (WA/Email)
                            </button>
                        </form>
                        <a href="<?= base_url('admin/payments/invoice/view/' . $order['id_order']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary rounded-3 w-100 d-inline-flex align-items-center justify-content-center gap-2 fw-semibold py-2">
                            <i class="bi bi-printer"></i> Tampilkan & Cetak Invoice
                        </a>
                    </div>
                </div>
            </div>

            <!-- CARD RIWAYAT PEMBAYARAN -->
            <div class="card-premium shadow-sm">
                <div class="card-header-clean d-flex align-items-center justify-content-between">
                    <h6 class="fw-bold text-main mb-0 d-flex align-items-center gap-2">
                        <i class="bi bi-clock-history text-success"></i> Riwayat Pembayaran
                    </h6>
                    <a href="<?= base_url('admin/payments/create/' . $order['id_order']) ?>" class="small text-primary text-decoration-none fw-semibold">
                        + Tambah
                    </a>
                </div>
                <div class="p-0">
                    <?php if (!empty($payments)): ?>
                        <div class="table-responsive">
                            <table class="table custom-table align-middle mb-0" style="font-size: 0.82rem;">
                                <thead>
                                    <tr>
                                        <th class="ps-3">Tanggal / Metode</th>
                                        <th class="text-end pe-3">Jumlah</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($payments as $payment): ?>
                                        <tr>
                                            <td class="ps-3">
                                                <div class="fw-semibold text-main"><?= esc($payment['tanggal_bayar']) ?></div>
                                                <small class="text-sub"><?= esc($payment['nama_metode'] ?? 'Metode Default') ?></small>
                                            </td>
                                            <td class="text-end pe-3">
                                                <div class="fw-bold text-success">
                                                    Rp <?= number_format((float)($payment['jumlah_bayar'] ?? 0), 0, ',', '.') ?>
                                                </div>
                                                <span class="badge badge-soft-success rounded-pill py-0 px-2" style="font-size: 0.65rem;">
                                                    <?= strtoupper(esc($payment['status'] ?? 'SUCCESS')) ?>
                                                </span>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php else: ?>
                        <div class="p-4 text-center text-sub">
                            <i class="bi bi-wallet2 fs-3 d-block opacity-40 mb-1"></i>
                            <span class="small">Belum ada riwayat pembayaran yang dicatat.</span>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

        </div>
    </div>

</div>

<?= $this->endSection() ?>
