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

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

<script src="https://cdn.tailwindcss.com"></script>
<script>
  tailwind.config = {
    darkMode: 'class',
    theme: {
      extend: {
        colors: {
          slate: { 950: '#0b1329' }
        }
      }
    }
  }
</script>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
    body {
        font-family: 'Inter', sans-serif !important;
        background-color: #f8fafc;
        transition: background-color 0.3s ease, color 0.3s ease;
    }

    /* Premium Detail Container Visuals */
    .detail-card-premium {
        background: #ffffff;
        border: 1px solid rgba(226, 232, 240, 0.8);
        border-radius: 1.25rem;
        transition: all 0.3s ease;
    }

    /* Soft Glow Badges */
    .badge-paid-glow {
        background-color: rgba(16, 185, 129, 0.1);
        color: #10b981;
        border: 1px solid rgba(16, 185, 129, 0.2);
    }
    .badge-pending-glow {
        background-color: rgba(245, 158, 11, 0.1);
        color: #f59e0b;
        border: 1px solid rgba(245, 158, 11, 0.2);
    }

    /* Custom scrollbars */
    ::-webkit-scrollbar { width: 6px; height: 6px; }
    ::-webkit-scrollbar-track { background: #f1f5f9; }
    ::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }

    /* Dark Mode Theme Integrations */
    html.dark body {
        background-color: #0b1329 !important;
        color: #f8fafc !important;
    }
    html.dark .detail-card-premium {
        background-color: #1e293b !important;
        border-color: #334155 !important;
        color: #f1f5f9 !important;
    }
    html.dark .text-slate-900, html.dark h1, html.dark h2, html.dark h3, html.dark h4, html.dark h5, html.dark h6 {
        color: #f8fafc !important;
    }
    html.dark .text-slate-500, html.dark .text-muted, html.dark .text-slate-400 {
        color: #94a3b8 !important;
    }
    html.dark .border-slate-100 {
        border-color: #334155 !important;
    }
    html.dark .bg-slate-50 {
        background-color: #111a2e !important;
    }
    html.dark .badge-paid-glow {
        background-color: rgba(16, 185, 129, 0.15);
        color: #34d399;
        border-color: rgba(16, 185, 129, 0.3);
    }
    html.dark .badge-pending-glow {
        background-color: rgba(245, 158, 11, 0.15);
        color: #fbbf24;
        border-color: rgba(245, 158, 11, 0.3);
    }

    /* ==========================================
       SUPER ADVANCED PRINT OVERRIDES (Slip Gaji)
       ========================================== */
    @media print {
        /* Hide all interface elements */
        body, html, .container-fluid {
            background-color: #ffffff !important;
            color: #000000 !important;
            font-size: 11pt !important;
        }
        .no-print {
            display: none !important;
        }
        .print-layout {
            border: none !important;
            box-shadow: none !important;
            padding: 0 !important;
            margin: 0 !important;
            width: 100% !important;
        }
        /* Style specifically formatted slip print card */
        .print-slip-sheet {
            display: block !important;
            background: #ffffff !important;
            color: #000000 !important;
            padding: 20px !important;
            border: 2px solid #000000 !important;
            border-radius: 0.5rem !important;
            max-width: 100% !important;
            margin-top: 20px !important;
        }
        .print-border-dashed {
            border-top: 1px dashed #000000 !important;
            border-bottom: 1px dashed #000000 !important;
        }
    }
</style>

<div class="container-fluid py-4 px-3 sm:px-4 lg:px-6">

    <!-- Header & Action Deck -->
    <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 mb-6 pb-6 border-b border-slate-200 dark:border-slate-700 no-print">
        <div>
            <div class="flex items-center gap-2 mb-2">
                <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-semibold bg-indigo-50 text-indigo-700 dark:bg-indigo-950/30 dark:text-indigo-400 border border-indigo-200 dark:border-indigo-800/50 shadow-sm">
                    <span class="w-2 h-2 rounded-full bg-indigo-600 animate-pulse"></span>
                    Arsip Digital Slip Gaji
                </span>
            </div>
            <h1 class="text-2xl sm:text-3xl font-extrabold text-slate-900 tracking-tight flex items-center gap-2">
                <i class="fas fa-file-invoice-dollar text-indigo-600 dark:text-indigo-400"></i>
                Detail Slip Gaji Karyawan
            </h1>
            <p class="text-sm text-slate-500 mt-1">Lakukan peninjauan rekaman rincian penggajian bulanan, unduh kuitansi resmi, atau verifikasi upah tunai.</p>
        </div>

        <!-- Utility Action Buttons -->
        <div class="flex flex-wrap items-center gap-2 w-full md:w-auto justify-end">
            <!-- Theme Changer Button -->
            <button type="button" onclick="toggleDarkMode()" class="bg-white dark:bg-slate-800 hover:bg-slate-50 dark:hover:bg-slate-700 text-slate-700 dark:text-slate-200 border border-slate-200 dark:border-slate-600 rounded-xl p-2.5 shadow-sm transition-all flex items-center justify-center" title="Ubah Tema Gelap/Terang">
                <i id="themeIcon" class="fas fa-moon text-lg"></i>
            </button>
            <!-- Go Back Link -->
            <a href="<?= base_url('owner/payroll') ?>" class="bg-white dark:bg-slate-800 hover:bg-slate-50 dark:hover:bg-slate-700 text-slate-700 dark:text-slate-200 border border-slate-200 dark:border-slate-600 font-semibold text-sm rounded-xl px-4 py-2.5 shadow-sm transition-all flex items-center gap-2 decoration-none no-underline">
                <i class="fas fa-arrow-left text-slate-400"></i>
                <span>Kembali</span>
            </a>
            <!-- Premium Print Button Trigger -->
            <button onclick="window.print()" class="bg-emerald-600 hover:bg-emerald-700 text-white font-semibold text-sm rounded-xl px-4 py-2.5 shadow-md hover:shadow-lg transition-all flex items-center gap-2 border-0">
                <i class="fas fa-print"></i>
                <span>Cetak Slip Gaji</span>
            </button>
            <!-- Direct Edit Button -->
            <a href="<?= base_url('owner/payroll/edit/'.($payroll['id_payroll'] ?? '')) ?>" class="bg-indigo-600 hover:bg-indigo-700 text-white font-semibold text-sm rounded-xl px-4 py-2.5 shadow-md hover:shadow-lg transition-all flex items-center gap-2 decoration-none no-underline">
                <i class="fas fa-pen"></i>
                <span>Ubah Angka Gaji</span>
            </a>
        </div>
    </div>

    <?php 
        $gapok = floatval($payroll['gaji_pokok'] ?? 0);
        $bonus = floatval($payroll['bonus'] ?? 0);
        $potongan = floatval($payroll['potongan'] ?? 0);
        $totalGaji = floatval($payroll['total_gaji'] ?? ($gapok + $bonus - $potongan));
        $statusRaw = strtolower($payroll['status'] ?? 'pending');
        
        $employeeName = $payroll['nama_lengkap'] ?? 'Pegawai Tanpa Nama';
        $fallbackInitial = strtoupper(substr($employeeName, 0, 2));
    ?>

    <!-- Main View Grid (Hidden during specific high-contrast print sessions if desired, but here we styled it hibrida) -->
    <div class="row g-4 print-layout">
        
        <!-- Left Panel: Profile and Status Recap Card -->
        <div class="col-12 col-lg-4 no-print">
            <div class="detail-card-premium p-4 sm:p-5 shadow-xs text-center">
                <h3 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-4 pb-2 border-b border-slate-100 dark:border-slate-800">Penerima Slip Upah</h3>
                
                <!-- Graphic Employee Circle Avatar -->
                <div class="w-20 h-20 bg-indigo-50 dark:bg-indigo-950/30 text-indigo-600 dark:text-indigo-400 rounded-full flex items-center justify-center text-2xl font-extrabold mx-auto mb-3 shadow-inner border border-indigo-100 dark:border-indigo-800/30">
                    <?= $fallbackInitial ?>
                </div>

                <h4 class="text-base font-extrabold text-slate-900 mb-1"><?= esc($employeeName) ?></h4>
                <p class="text-xs text-slate-400 font-medium mb-3">ID Karyawan: #<?= esc($payroll['id_user'] ?? 'N/A') ?></p>

                <!-- Status Badge -->
                <div class="mb-4">
                    <?php if($statusRaw === 'paid'): ?>
                        <span class="inline-flex items-center gap-1.5 px-4 py-1.5 rounded-full text-xs font-bold badge-paid-glow">
                            <span class="w-2 h-2 rounded-full bg-emerald-500"></span>
                            Status: Lunas (Terbayar)
                        </span>
                    <?php else: ?>
                        <span class="inline-flex items-center gap-1.5 px-4 py-1.5 rounded-full text-xs font-bold badge-pending-glow">
                            <span class="w-2 h-2 rounded-full bg-amber-500 animate-pulse"></span>
                            Status: Pending (Belum Dibayar)
                        </span>
                    <?php endif; ?>
                </div>

                <!-- Instant Quick Payment Button Box if Pending -->
                <?php if($statusRaw !== 'paid'): ?>
                    <div class="mt-4 p-4 bg-amber-50/50 dark:bg-amber-950/10 rounded-2xl border border-amber-200/50 dark:border-amber-800/20">
                        <p class="text-xs text-slate-500 dark:text-slate-400 leading-normal mb-3">Slip gaji ini bernilai total bersih siap transfer. Lakukan pelunasan instan sekarang jika dana sudah diserahkan:</p>
                        <button onclick="triggerQuickPay('<?= base_url('owner/payroll/pay/'.$payroll['id_payroll']) ?>', '<?= esc(addslashes($employeeName)) ?>', '<?= esc($payroll['periode'] ?? '') ?>', <?= $totalGaji ?>)" 
                                class="w-full bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs rounded-xl py-2.5 shadow-sm hover:shadow transition-all border-0 flex items-center justify-center gap-1.5">
                            <i class="fas fa-circle-dollar-to-slot"></i>
                            Tandai Lunas Sekarang
                        </button>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Right Panel: Full Financial Audit Breakdown -->
        <div class="col-12 col-lg-8 print-layout">
            
            <!-- Standard Desktop Screen View Card -->
            <div class="detail-card-premium p-4 sm:p-5 shadow-xs bg-white no-print">
                <h3 class="text-base font-bold text-slate-900 mb-4 pb-2 border-b border-slate-100 flex items-center gap-2">
                    <i class="fas fa-calculator text-indigo-500"></i>
                    Audit Komponen Slip Gaji (<?= esc($payroll['periode'] ?? '-') ?>)
                </h3>

                <div class="space-y-4">
                    
                    <!-- Earnings Section (Gaji Pokok & Bonus) -->
                    <div>
                        <h4 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2.5 flex items-center gap-1.5">
                            <i class="fas fa-circle-plus text-emerald-500"></i> Komponen Penerimaan / Pendapatan
                        </h4>
                        <div class="p-3 bg-slate-50 dark:bg-slate-900/50 rounded-2xl border border-slate-100 dark:border-slate-800/80 space-y-2 text-xs">
                            <div class="flex justify-between items-center py-1">
                                <span class="text-slate-500 font-medium">Gaji Pokok Bulanan</span>
                                <span class="font-bold text-slate-800 dark:text-slate-200">Rp <?= number_format($gapok, 0, ',', '.') ?></span>
                            </div>
                            <div class="flex justify-between items-center py-1 border-t border-slate-150/50 dark:border-slate-800/50">
                                <span class="text-slate-500 font-medium">Bonus / Lemburan Kinerja CNC</span>
                                <span class="font-bold text-emerald-600 dark:text-emerald-400">+Rp <?= number_format($bonus, 0, ',', '.') ?></span>
                            </div>
                            <div class="flex justify-between items-center pt-2 border-t border-dashed border-slate-200 dark:border-slate-700 font-bold">
                                <span class="text-slate-700 dark:text-slate-300">Total Kotor (Kolektif)</span>
                                <span class="text-slate-800 dark:text-slate-150">Rp <?= number_format($gapok + $bonus, 0, ',', '.') ?></span>
                            </div>
                        </div>
                    </div>

                    <!-- Deductions Section -->
                    <div>
                        <h4 class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2.5 flex items-center gap-1.5">
                            <i class="fas fa-circle-minus text-rose-500"></i> Komponen Potongan Internal
                        </h4>
                        <div class="p-3 bg-slate-50 dark:bg-slate-900/50 rounded-2xl border border-slate-100 dark:border-slate-800/80 text-xs">
                            <div class="flex justify-between items-center py-1">
                                <span class="text-slate-500 font-medium">Potongan Kasir / Sanksi Kedisiplinan / Kasbon</span>
                                <span class="font-bold text-rose-500">-Rp <?= number_format($potongan, 0, ',', '.') ?></span>
                            </div>
                        </div>
                    </div>

                    <!-- Final Take Home Pay Box -->
                    <div class="p-4 bg-indigo-50/40 dark:bg-indigo-950/20 rounded-2xl border border-indigo-100/50 dark:border-indigo-900/30 flex flex-col sm:flex-row justify-between items-start sm:items-center gap-3">
                        <div>
                            <span class="text-[10px] font-bold text-indigo-500 uppercase tracking-wider block mb-0.5">Total Gaji Bersih (Take Home Pay)</span>
                            <span id="calculatedTotalWords" class="text-xs text-slate-400 font-medium italic block">Format Terbilang: <strong class="text-indigo-600 dark:text-indigo-400" id="indonesiaWordsText">Rp 0</strong></span>
                        </div>
                        <div class="text-right">
                            <span class="text-2xl font-extrabold text-indigo-600 dark:text-indigo-400 block">Rp <?= number_format($totalGaji, 0, ',', '.') ?></span>
                        </div>
                    </div>

                </div>
            </div>

            <!-- ==========================================
               ESTETIKA TAMPILAN RESMI PRINT SLIP SHEET
               ========================================== -->
            <!-- This container only formats nicely and displays on print paper or standard screen copy -->
            <div class="print-slip-sheet hidden bg-white text-black p-6 border-2 border-black rounded-lg">
                <div class="text-center mb-4">
                    <h2 class="text-lg font-bold uppercase tracking-wide m-0">Slip Gaji Resmi Karyawan</h2>
                    <h3 class="text-sm font-semibold tracking-wide m-0">Bengkel Bubut & Fabrikasi CNC</h3>
                    <p class="text-[10px] text-gray-500 m-0">Telepon Internal Administrasi Keuangan Owner</p>
                </div>

                <div class="border-t border-b border-black py-2 my-3 grid grid-cols-2 gap-2 text-xs">
                    <div>
                        <p class="m-0"><strong>Nama Pegawai:</strong> <?= esc($employeeName) ?></p>
                        <p class="m-0"><strong>ID Pegawai:</strong> #<?= esc($payroll['id_user'] ?? 'N/A') ?></p>
                    </div>
                    <div class="text-right">
                        <p class="m-0"><strong>Periode Pembayaran:</strong> <?= esc($payroll['periode'] ?? '-') ?></p>
                        <p class="m-0"><strong>Status Slip:</strong> <?= ($statusRaw === 'paid') ? 'LUNAS (Terbayar)' : 'PENDING' ?></p>
                    </div>
                </div>

                <h4 class="text-xs font-bold uppercase tracking-wider mb-2">Rincian Perhitungan Upah:</h4>
                <div class="space-y-1.5 text-xs">
                    <div class="flex justify-between">
                        <span>Gaji Pokok Bulanan</span>
                        <span>Rp <?= number_format($gapok, 0, ',', '.') ?></span>
                    </div>
                    <div class="flex justify-between text-emerald-700">
                        <span>Bonus / Lembur CNC (+)</span>
                        <span>+Rp <?= number_format($bonus, 0, ',', '.') ?></span>
                    </div>
                    <div class="flex justify-between text-rose-700">
                        <span>Potongan Internal / Kasbon (-)</span>
                        <span>-Rp <?= number_format($potongan, 0, ',', '.') ?></span>
                    </div>
                    <div class="border-t border-dashed border-black pt-2 flex justify-between font-bold text-sm">
                        <span>TOTAL UPAH BERSIH (TAKE HOME PAY)</span>
                        <span>Rp <?= number_format($totalGaji, 0, ',', '.') ?></span>
                    </div>
                </div>

                <!-- Receipt Signature Deck at the bottom -->
                <div class="grid grid-cols-2 gap-4 mt-8 pt-6 border-t border-gray-200 text-xs text-center">
                    <div>
                        <p class="mb-12">Penerima Upah,</p>
                        <p class="font-bold underline m-0"><?= esc($employeeName) ?></p>
                    </div>
                    <div>
                        <p class="mb-12">Pihak Keuangan / Owner,</p>
                        <p class="font-bold underline m-0">Administrasi Bengkel</p>
                    </div>
                </div>
            </div>

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

<!-- PREMIUM QUICK PAYMENT CONFIRMATION DIALOG MODAL -->
<div id="quickPayModal" class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-950/60 backdrop-blur-xs hidden opacity-0 transition-all duration-300 no-print">
    <div class="bg-white dark:bg-slate-800 rounded-2xl max-w-md w-full overflow-hidden shadow-2xl transform scale-95 transition-transform duration-300 border border-slate-200 dark:border-slate-700">
        <div class="h-1.5 bg-emerald-500 w-full"></div>
        <div class="p-6">
            <div class="w-12 h-12 rounded-full bg-emerald-50 dark:bg-emerald-950/30 text-emerald-600 dark:text-emerald-400 flex items-center justify-center text-xl mb-4">
                <i class="fas fa-circle-dollar-to-slot"></i>
            </div>
            <h3 class="text-lg font-bold text-slate-900 dark:text-slate-100 mb-2">Konfirmasi Pembayaran Gaji</h3>
            <p class="text-xs text-slate-500 leading-relaxed mb-4">
                Tindakan ini akan memverifikasi penyerahan upah dan menandai status slip gaji pegawai berikut menjadi <strong class="text-emerald-600 dark:text-emerald-400">LUNAS (Paid)</strong>:
            </p>
            
            <!-- Details card -->
            <div class="p-3 bg-slate-50 dark:bg-slate-900/50 rounded-xl border border-slate-150 dark:border-slate-800 space-y-1.5 text-xs">
                <div class="flex justify-between">
                    <span class="text-slate-400">Nama Pegawai:</span>
                    <span id="modalEmployeeName" class="font-bold text-slate-800 dark:text-slate-200"></span>
                </div>
                <div class="flex justify-between">
                    <span class="text-slate-400">Periode Bulan:</span>
                    <span id="modalPeriodName" class="font-semibold text-slate-850 dark:text-slate-300"></span>
                </div>
                <div class="flex justify-between border-t border-dashed border-slate-200 dark:border-slate-800 pt-1.5 mt-1">
                    <span class="text-slate-400 font-medium">Realisasi Gaji Bersih:</span>
                    <strong id="modalGajiTotal" class="text-emerald-600 dark:text-emerald-400 font-extrabold"></strong>
                </div>
            </div>
        </div>
        <div class="p-4 bg-slate-50 dark:bg-slate-900/40 border-t border-slate-100 dark:border-slate-700 flex justify-end gap-2">
            <button onclick="dismissQuickPay()" class="px-4 py-2 text-xs font-semibold text-slate-500 dark:text-slate-300 bg-transparent hover:bg-slate-100 dark:hover:bg-slate-700/50 rounded-xl transition-all border-0">
                Batalkan
            </button>
            <a id="payAnchorConfirm" href="#" class="px-4 py-2 text-xs font-semibold text-white bg-emerald-600 hover:bg-emerald-700 rounded-xl shadow-sm transition-all text-center decoration-none no-underline flex items-center">
                Ya, Konfirmasi Bayar
            </a>
        </div>
    </div>
</div>

<script>
document.addEventListener("DOMContentLoaded", function() {

    // ==========================================
    // AUTO CONVERT VALUE TO INDONESIAN RUPIAH WORDS
    // ==========================================
    const totalAmount = <?= $totalGaji ?>;
    const wordDisplay = document.getElementById('indonesiaWordsText');

    if (wordDisplay) {
        wordDisplay.innerText = new Intl.NumberFormat('id-ID', {
            style: 'currency',
            currency: 'IDR',
            maximumFractionDigits: 0
        }).format(totalAmount);
    }

    // ==========================================
    // INSTANT QUICK PAY MODAL DIALOGS
    // ==========================================
    const quickModal = document.getElementById('quickPayModal');
    const modalEmployeeName = document.getElementById('modalEmployeeName');
    const modalPeriodName = document.getElementById('modalPeriodName');
    const modalGajiTotal = document.getElementById('modalGajiTotal');
    const payAnchor = document.getElementById('payAnchorConfirm');

    window.triggerQuickPay = function(payUrl, employeeName, period, amount) {
        modalEmployeeName.innerText = employeeName;
        modalPeriodName.innerText = period;
        
        modalGajiTotal.innerText = new Intl.NumberFormat('id-ID', {
            style: 'currency',
            currency: 'IDR',
            maximumFractionDigits: 0
        }).format(amount);

        payAnchor.setAttribute('href', payUrl);

        // Show smooth fade animation
        quickModal.classList.remove('hidden');
        setTimeout(() => {
            quickModal.classList.remove('opacity-0');
            quickModal.querySelector('.scale-95').classList.remove('scale-95');
        }, 30);
    };

    window.dismissQuickPay = function() {
        quickModal.classList.add('opacity-0');
        quickModal.querySelector('.transform').classList.add('scale-95');
        setTimeout(() => {
            quickModal.classList.add('hidden');
        }, 300);
    };

    if (quickModal) {
        quickModal.addEventListener('click', function(e) {
            if (e.target === quickModal) {
                dismissQuickPay();
            }
        });
    }

    // ==========================================
    // SYSTEM DARK MODE CONTROLLER
    // ==========================================
    window.toggleDarkMode = function() {
        const html = document.documentElement;
        const icon = document.getElementById('themeIcon');
        
        if (html.classList.contains('dark')) {
            html.classList.remove('dark');
            localStorage.setItem('theme', 'light');
            icon.className = "fas fa-moon text-lg";
        } else {
            html.classList.add('dark');
            localStorage.setItem('theme', 'dark');
            icon.className = "fas fa-sun text-lg text-amber-400";
        }
    };

    // Sync theme options on page load
    if (localStorage.getItem('theme') === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
        document.documentElement.classList.add('dark');
        const icon = document.getElementById('themeIcon');
        if(icon) {
            icon.className = "fas fa-sun text-lg text-amber-400";
        }
    }
});
</script>

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