<?= $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, .payment-create-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;
    }

    /* Form Inputs */
    .form-control-custom, .form-select-custom {
        background-color: var(--bg-input) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-main) !important;
        border-radius: var(--radius-md) !important;
        padding: 0.65rem 0.9rem;
        transition: var(--transition);
    }

    .form-control-custom:focus, .form-select-custom:focus {
        border-color: var(--primary) !important;
        box-shadow: 0 0 0 3px var(--primary-light) !important;
    }

    .input-group-text-custom {
        background-color: var(--bg-accent);
        border: 1px solid var(--border-color);
        color: var(--text-sub);
        border-radius: var(--radius-md) 0 0 var(--radius-md) !important;
        font-weight: 600;
    }

    /* Avatar Minimalist */
    .avatar-md {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background: var(--primary-light);
        color: var(--primary);
        font-weight: 700;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    /* 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-secondary { background: rgba(100, 116, 139, 0.12); color: #64748b; border: 1px solid rgba(100, 116, 139, 0.2); }

    /* Quick Fill Buttons */
    .btn-preset {
        background-color: var(--bg-accent);
        border: 1px solid var(--border-color);
        color: var(--text-main);
        font-size: 0.78rem;
        font-weight: 600;
        border-radius: var(--radius-sm);
        padding: 0.35rem 0.65rem;
        transition: var(--transition);
    }

    .btn-preset:hover {
        background-color: var(--primary-light);
        border-color: var(--primary);
        color: var(--primary);
    }
</style>

<div class="container-fluid py-4 payment-create-workspace">

    <?php 
        $totalOrder = (float)($order['total'] ?? 0);
        $paidAmount = (float)($order['paid_amount'] ?? 0);
        $remainingAmount = (float)($order['remaining_amount'] ?? 0);
        $payStatus = strtolower($order['payment_status'] ?? 'unpaid');
        
        $statusBadgeClass = $payStatus === 'paid' ? 'badge-soft-success' : ($payStatus === 'partial' ? 'badge-soft-warning' : 'badge-soft-danger');
        $progressPercent = $totalOrder > 0 ? min(100, round(($paidAmount / $totalOrder) * 100)) : 0;
        $customerInitial = strtoupper(substr(trim($order['customer_name'] ?? 'C'), 0, 1));
    ?>

    <!-- PAGE HEADER -->
    <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/show/' . $order['id_order']) ?>" class="btn btn-sm btn-outline-secondary rounded-2 px-2 py-1" title="Kembali ke detail order">
                    <i class="bi bi-arrow-left"></i>
                </a>
                <h3 class="fw-bold mb-0 text-main">Catat Pembayaran Baru</h3>
            </div>
            <p class="text-sub small mb-0">Input penerimaan pembayaran untuk Order #<?= esc($order['order_number']) ?></p>
        </div>

        <a href="<?= base_url('admin/orders/show/' . $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-box-arrow-up-left"></i> Kembali ke Detail Order
        </a>
    </div>

    <!-- FLASH MESSAGES -->
    <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger 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-exclamation-triangle-fill fs-5"></i>
                <div><?= esc(session()->getFlashdata('error')) ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif ?>

    <?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><?= esc(session()->getFlashdata('success')) ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif ?>

    <!-- MAIN GRID -->
    <div class="row g-4">
        
        <!-- LEFT COLUMN: RINGKASAN ORDER & FINANSIAL -->
        <div class="col-12 col-lg-5">
            <div class="card-premium shadow-sm h-100">
                <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 text-primary"></i> Ringkasan Order
                    </h6>
                    <span class="badge <?= $statusBadgeClass ?> rounded-pill px-2.5 py-1">
                        <?= strtoupper($payStatus) ?>
                    </span>
                </div>
                <div class="p-3.5">
                    
                    <!-- INFO PELANGGAN -->
                    <div class="d-flex align-items-center gap-3 p-3 rounded-3 mb-4" style="background: var(--bg-accent);">
                        <div class="avatar-md"><?= $customerInitial ?></div>
                        <div class="overflow-hidden">
                            <small class="text-sub d-block">Pelanggan</small>
                            <div class="fw-bold text-main text-truncate"><?= esc($order['customer_name']) ?></div>
                        </div>
                    </div>

                    <!-- BREAKDOWN ANGKA -->
                    <div class="d-flex flex-column gap-3 mb-4">
                        <div class="d-flex justify-content-between align-items-center pb-2 border-bottom border-color">
                            <span class="text-sub small">Total Nilai Order</span>
                            <span class="fw-semibold text-main">Rp <?= number_format($totalOrder, 0, ',', '.') ?></span>
                        </div>
                        <div class="d-flex justify-content-between align-items-center pb-2 border-bottom border-color">
                            <span class="text-sub small">Telah Dibayar</span>
                            <span class="fw-semibold text-success">Rp <?= number_format($paidAmount, 0, ',', '.') ?></span>
                        </div>
                        <div class="d-flex justify-content-between align-items-center">
                            <span class="fw-bold text-main">Sisa Tagihan</span>
                            <span class="fw-bolder <?= $remainingAmount > 0 ? 'text-danger' : 'text-sub' ?> fs-5">
                                Rp <?= number_format($remainingAmount, 0, ',', '.') ?>
                            </span>
                        </div>
                    </div>

                    <!-- PROGRESS BAR PEMBAYARAN -->
                    <div class="pt-2">
                        <div class="d-flex justify-content-between align-items-center small mb-1">
                            <span class="text-sub">Progres Pembayaran</span>
                            <span class="fw-bold text-main"><?= $progressPercent ?>%</span>
                        </div>
                        <div class="progress rounded-pill" style="height: 8px; background-color: var(--bg-accent);">
                            <div class="progress-bar bg-success rounded-pill" role="progressbar" style="width: <?= $progressPercent ?>%" aria-valuenow="<?= $progressPercent ?>" aria-valuemin="0" aria-valuemax="100"></div>
                        </div>
                    </div>

                </div>
            </div>
        </div>

        <!-- RIGHT COLUMN: FORM TRANSAKSI PEMBAYARAN -->
        <div class="col-12 col-lg-7">
            <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-wallet2 text-success"></i> Form Input Transaksi
                    </h6>
                </div>
                <div class="p-3.5">
                    
                    <form action="<?= base_url('admin/payments/store') ?>" method="post">
                        <?= csrf_field() ?>
                        <input type="hidden" name="id_order" value="<?= esc($order['id_order']) ?>">

                        <!-- METODE PEMBAYARAN -->
                        <div class="mb-4">
                            <label class="form-label fw-semibold text-main small mb-1">
                                Metode Pembayaran <span class="text-danger">*</span>
                            </label>
                            <select name="id_payment_method" class="form-select form-select-custom" required>
                                <option value="">-- Pilih Metode Pembayaran --</option>
                                <?php foreach ($methods as $method): ?>
                                    <option value="<?= esc($method['id_payment_method']) ?>"
                                        <?= old('id_payment_method') == $method['id_payment_method'] ? 'selected' : '' ?>>
                                        <?= esc($method['nama_metode']) ?>
                                    </option>
                                <?php endforeach ?>
                            </select>
                        </div>

                        <!-- JUMLAH BAYAR -->
                        <div class="mb-3">
                            <div class="d-flex justify-content-between align-items-center mb-1">
                                <label class="form-label fw-semibold text-main small mb-0">
                                    Jumlah Bayar (Rp) <span class="text-danger">*</span>
                                </label>
                            </div>
                            
                            <div class="input-group">
                                <span class="input-group-text input-group-text-custom">Rp</span>
                                <input type="number" 
                                       step="0.01" 
                                       min="0.01" 
                                       id="jumlah_bayar_input"
                                       name="jumlah_bayar" 
                                       class="form-control form-control-custom fw-bold fs-5 text-main" 
                                       value="<?= old('jumlah_bayar', $remainingAmount) ?>" 
                                       placeholder="0"
                                       required>
                            </div>
                        </div>

                        <!-- QUICK NOMINAL PRESETS -->
                        <?php if ($remainingAmount > 0): ?>
                            <div class="mb-4">
                                <small class="text-sub d-block mb-1.5 fs-8 fw-semibold">PILIH CEPAT NOMINAL:</small>
                                <div class="d-flex flex-wrap gap-2">
                                    <button type="button" class="btn btn-preset" onclick="setNominal(<?= $remainingAmount ?>)">
                                        <i class="bi bi-check2-all me-1"></i> Lunasi Sisa (Rp <?= number_format($remainingAmount, 0, ',', '.') ?>)
                                    </button>
                                    <button type="button" class="btn btn-preset" onclick="setNominal(<?= round($remainingAmount * 0.5) ?>)">
                                        50% Sisa
                                    </button>
                                    <button type="button" class="btn btn-preset" onclick="setNominal(<?= round($totalOrder * 0.3) ?>)">
                                        30% Total
                                    </button>
                                </div>
                            </div>
                        <?php endif; ?>

                        <hr class="my-4" style="border-color: var(--border-color);">

                        <!-- SUBMIT BUTTON -->
                        <button type="submit" class="btn btn-primary rounded-3 w-100 py-2.5 fw-semibold d-inline-flex align-items-center justify-content-center gap-2 shadow-sm">
                            <i class="bi bi-check-circle-fill"></i> Simpan Pembayaran
                        </button>
                    </form>

                </div>
            </div>
        </div>

    </div>

</div>

<!-- JAVASCRIPT UNTUK PRESET NOMINAL -->
<script>
    function setNominal(amount) {
        const inputField = document.getElementById('jumlah_bayar_input');
        if (inputField) {
            inputField.value = amount;
            inputField.focus();
        }
    }
</script>

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