<?= $this->extend('layout/template'); ?>

<?= $this->section('content'); ?>
<section class="bg-[#BA290D] pt-28 pb-7 relative overflow-hidden">
    <div class="absolute inset-0 z-0">
        <img src="<?= base_url('assets/img/gb_hero/karir.png') ?>" alt="Karir Background"
            class="w-full h-full object-cover">
        <div class="absolute inset-0 bg-black/50"></div>
        <div class="absolute inset-0 flex items-center justify-end select-none pointer-events-none overflow-hidden">
            <h1 class="text-[10rem] sm:text-[15rem] md:text-[20rem] lg:text-[25rem] font-black leading-none text-white opacity-[0.1] uppercase tracking-tighter translate-x-10 md:translate-x-20">
                CAREER
            </h1>
        </div>
    </div>
    <div class="container mx-auto px-4 md:px-12 relative z-10">
        <div class="w-full md:w-1/2 text-white">
            <h2 class="text-6xl sm:text-7xl md:text-[8rem] lg:text-[7rem] font-black uppercase leading-[0.8] tracking-tighter italic">
                <span class="block text-[#BA290D] drop-shadow-md transform hover:translate-x-2 transition-transform duration-300">JOIN</span>
                <span class="block text-[#FBBD42] drop-shadow-md ml-4 md:ml-8">US?</span>
            </h2>
        </div>
    </div>
</section>

<section class="bg-gray-50 py-16 min-h-[50vh]">
    <div class="container mx-auto px-4 max-w-6xl">
        <div class="text-center mb-12">
            <h2 class="text-3xl font-black text-red-900 uppercase tracking-wide mb-3">Bergabung Bersama Kami</h2>
            <p class="text-red-700">Jadilah bagian dari keluarga besar kami dan ciptakan pengalaman kuliner terbaik.</p>
        </div>

        <?php if (session()->getFlashdata('success_apply')): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-8 text-center shadow-sm">
                <strong class="font-bold">Berhasil!</strong> <?= session()->getFlashdata('success_apply') ?>
            </div>
        <?php endif; ?>
        <?php if (session()->getFlashdata('error_apply')): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-8 text-center shadow-sm">
                <strong class="font-bold">Gagal!</strong> <?= session()->getFlashdata('error_apply') ?>
            </div>
        <?php endif; ?>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
            <?php if (empty($karirList)): ?>
                <div class="col-span-2 text-center bg-white p-10 rounded-2xl border border-red-100 shadow-sm">
                    <div class="flex flex-col items-center justify-center">
                        <svg class="w-12 h-12 text-red-200 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4" /></svg>
                        <p class="text-red-700 font-bold">Saat ini belum ada lowongan pekerjaan yang tersedia.</p>
                    </div>
                </div>
            <?php else: ?>
                <?php foreach ($karirList as $k): ?>
                    <div class="bg-white rounded-2xl overflow-hidden border border-red-100 hover:border-red-400 transition-all duration-300 shadow-sm hover:shadow-xl flex flex-col relative group">
                        
                        <?php if(!empty($k['image'])): ?>
                            <div class="h-52 w-full relative overflow-hidden border-b border-gray-100">
                                <img src="<?= base_url('uploads/karir/' . esc($k['image'])) ?>" alt="<?= esc($k['title']) ?>" class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-700">
                                <div class="absolute inset-0 bg-gradient-to-t from-black/40 to-transparent"></div>
                                <span class="absolute top-4 right-4 bg-white/90 backdrop-blur-md text-red-800 text-[10px] font-bold px-3 py-1.5 rounded-full uppercase tracking-wider shadow-sm border border-white/50">
                                    <?= esc($k['type']) ?>
                                </span>
                            </div>
                            <div class="p-6 md:p-8 flex-1 flex flex-col relative">
                                <h3 class="text-2xl md:text-3xl font-black text-red-900 mb-2 group-hover:text-[#BA290D] transition-colors leading-tight uppercase">
                        <?php else: ?>
                            <div class="p-6 md:p-8 flex-1 flex flex-col relative">
                                <span class="absolute top-6 right-6 bg-red-100 text-red-800 text-[10px] font-bold px-3 py-1.5 rounded-full uppercase tracking-wider border border-red-200">
                                    <?= esc($k['type']) ?>
                                </span>
                                <h3 class="text-2xl md:text-3xl font-black text-red-900 mb-2 mt-6 md:mt-2 group-hover:text-[#BA290D] transition-colors leading-tight uppercase">
                        <?php endif; ?>
                        
                                <?= esc($k['title']) ?>
                            </h3>
                            
                            <p class="text-red-500 text-xs font-bold uppercase tracking-wider mb-4 bg-red-50 inline-block px-2.5 py-1.5 rounded-lg w-max border border-red-100">
                                ⏳ Tenggat: <?= esc($k['deadline']) ?>
                            </p>

                            <p class="text-gray-600 text-sm mb-6 leading-relaxed flex-1">
                                <?= esc($k['short_desc']) ?>
                            </p>

                            <div class="flex items-center justify-between pt-5 border-t border-gray-100 mt-auto">
                                <span class="text-gray-500 text-sm font-semibold flex items-center gap-1.5">
                                    <svg class="w-4 h-4 text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"></path>
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                    </svg>
                                    <?= esc($k['location']) ?>
                                </span>
                                <?php $dataJson = htmlspecialchars(json_encode($k), ENT_QUOTES, 'UTF-8'); ?>
                                <button type="button" data-job='<?= $dataJson ?>' onclick="openDetailModal(this)"
                                    class="bg-red-700 text-white px-5 py-2.5 rounded-lg font-bold text-xs uppercase tracking-wider hover:bg-red-800 hover:shadow-lg transition-all transform group-hover:-translate-y-1">
                                    Lihat Detail
                                </button>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
</section>

<div id="detailModal" class="hidden fixed inset-0 z-[150] flex items-center justify-center bg-black/70 backdrop-blur-sm p-4">
    <div class="bg-white w-full max-w-3xl rounded-3xl relative max-h-[90vh] overflow-hidden flex flex-col shadow-2xl">

        <div id="detailHeader" class="relative shrink-0 bg-[#BA290D] bg-cover bg-center bg-no-repeat transition-all duration-300">
            <div id="detailOverlay" class="absolute inset-0 bg-black/60 hidden"></div>
            
            <div class="relative z-10 p-6 md:p-8">
                <button onclick="closeDetailModal()" class="absolute top-4 right-5 text-white/60 hover:text-white text-4xl font-light leading-none">&times;</button>
                <span id="detailType" class="bg-white/20 text-white text-[10px] font-bold px-3 py-1 rounded-full uppercase tracking-wider border border-white/30 mb-3 inline-block shadow-sm">Full-Time</span>
                <h2 id="detailTitle" class="text-3xl md:text-4xl font-black text-white uppercase tracking-tight mb-2 drop-shadow-md">Judul Pekerjaan</h2>
                <p class="text-red-50 text-sm font-medium flex items-center gap-2 drop-shadow-md">
                    📍 <span id="detailLocation">Lokasi</span> <span class="mx-1 opacity-50">|</span> ⏳ <span id="detailDeadline">Deadline</span>
                </p>
            </div>
        </div>

        <div class="p-6 md:p-8 overflow-y-auto text-gray-700 text-sm leading-relaxed space-y-6">
            <div>
                <h4 class="text-lg font-bold text-red-900 mb-3 flex items-center gap-2 border-b border-gray-100 pb-2">
                    <svg class="w-5 h-5 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path></svg>
                    Tugas & Tanggung Jawab
                </h4>
                <ul id="detailJobdesc" class="list-disc pl-5 space-y-2 marker:text-red-400"></ul>
            </div>

            <div>
                <h4 class="text-lg font-bold text-red-900 mb-3 flex items-center gap-2 border-b border-gray-100 pb-2">
                    <svg class="w-5 h-5 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                    Kualifikasi
                </h4>
                <ul id="detailKualifikasi" class="list-disc pl-5 space-y-2 marker:text-red-400"></ul>
            </div>
        </div>

        <div class="bg-gray-50 p-6 border-t border-gray-200 shrink-0 flex justify-end gap-3">
            <button onclick="closeDetailModal()" class="px-6 py-3 rounded-xl font-bold text-gray-500 hover:bg-gray-200 transition">Tutup</button>
            <button onclick="openApplyModal()" class="bg-[#FABB15] text-[#BA290D] px-8 py-3 rounded-xl font-black uppercase tracking-widest hover:bg-yellow-400 hover:-translate-y-1 transition-transform shadow-lg">
                Apply Now!
            </button>
        </div>
    </div>
</div>

<div id="applyModal" class="hidden fixed inset-0 z-[200] flex items-center justify-center bg-black/80 backdrop-blur-sm p-4">
    <div class="bg-white w-full max-w-xl rounded-3xl relative overflow-hidden shadow-2xl">

        <div class="bg-gray-50 p-5 flex justify-between items-center border-b">
            <h3 class="text-xl font-black text-gray-800">Kirim Lamaran</h3>
            <button onclick="closeApplyModal()" class="text-gray-400 hover:text-red-500 text-3xl font-light leading-none">&times;</button>
        </div>

        <form action="<?= base_url('karir/apply') ?>" method="POST" enctype="multipart/form-data" class="p-6 md:p-8 space-y-4">
            
            <input type="hidden" name="lowongan_id" id="applyJobId">
            <input type="hidden" name="job_title" id="applyJobTitle">

            <div class="bg-red-50 border border-red-100 p-4 rounded-xl mb-4 text-sm text-red-800 shadow-inner">
                Anda melamar untuk posisi: <strong id="applyJobTitleDisplay" class="uppercase block text-lg mt-1">Posisi</strong>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-bold text-gray-600 mb-1 uppercase">Nama Lengkap</label>
                    <input type="text" name="nama_pelamar" required class="w-full bg-white border border-gray-200 rounded-xl px-4 py-3 focus:ring-2 focus:ring-red-500 focus:border-red-500 transition shadow-sm">
                </div>
                <div>
                    <label class="block text-xs font-bold text-gray-600 mb-1 uppercase">No. WhatsApp / HP</label>
                    <input type="number" name="telp_pelamar" required class="w-full bg-white border border-gray-200 rounded-xl px-4 py-3 focus:ring-2 focus:ring-red-500 focus:border-red-500 transition shadow-sm">
                </div>
            </div>

            <div>
                <label class="block text-xs font-bold text-gray-600 mb-1 uppercase">Alamat Email Aktif</label>
                <input type="email" name="email_pelamar" required class="w-full bg-white border border-gray-200 rounded-xl px-4 py-3 focus:ring-2 focus:ring-red-500 focus:border-red-500 transition shadow-sm">
            </div>

            <div class="border-t border-dashed border-gray-200 pt-4 mt-4">
                <label class="block text-xs font-bold text-gray-600 mb-1 uppercase">Upload Berkas <span class="text-red-500">*</span></label>
                <p class="text-xs text-gray-400 mb-2">Jadikan 1 File (Surat Lamaran, CV, dll). Format PDF Max: 5MB.</p>
                <input type="file" name="berkas" accept=".pdf,.doc,.docx,.zip,.rar" required
                    class="w-full bg-gray-50 border border-gray-200 rounded-xl px-4 py-2 text-sm file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-bold file:bg-red-100 file:text-red-700 hover:file:bg-red-200 cursor-pointer transition">
            </div>

            <button type="submit" class="w-full bg-[#BA290D] text-white font-black uppercase tracking-widest py-4 mt-6 rounded-xl shadow-lg hover:bg-red-800 hover:-translate-y-0.5 transition-all">
                Kirim Lamaran Sekarang
            </button>
        </form>
    </div>
</div>

<script>
    let currentJobId = "";
    let currentJobTitle = "";

    function renderList(text, targetElementId) {
        const ul = document.getElementById(targetElementId);
        ul.innerHTML = '';
        if (!text) return;
        const items = text.split('\n');
        items.forEach(item => {
            const cleanItem = item.replace(/^-/, '').trim();
            if (cleanItem) {
                const li = document.createElement('li');
                li.innerText = cleanItem;
                ul.appendChild(li);
            }
        });
    }

    function openDetailModal(btnElement) {
        const dataStr = btnElement.getAttribute('data-job');
        const data = JSON.parse(dataStr);

        currentJobId = data.id;
        currentJobTitle = data.title;

        // Isi Teks
        document.getElementById('detailTitle').innerText = data.title;
        document.getElementById('detailType').innerText = data.type;
        document.getElementById('detailLocation').innerText = data.location;
        document.getElementById('detailDeadline').innerText = data.deadline;
        renderList(data.jobdesc, 'detailJobdesc');
        renderList(data.kualifikasi, 'detailKualifikasi');

        // Logika Gambar Header Modal
        const header = document.getElementById('detailHeader');
        const overlay = document.getElementById('detailOverlay');
        
        if (data.image) {
            header.style.backgroundImage = `url('<?= base_url('uploads/karir/') ?>${data.image}')`;
            overlay.classList.remove('hidden');
        } else {
            header.style.backgroundImage = 'none';
            overlay.classList.add('hidden');
        }

        // Buka Modal
        document.getElementById('detailModal').classList.remove('hidden');
        document.body.classList.add('overflow-hidden');
    }

    function closeDetailModal() {
        document.getElementById('detailModal').classList.add('hidden');
        document.body.classList.remove('overflow-hidden');
    }

    function openApplyModal() {
        document.getElementById('detailModal').classList.add('hidden');

        // Passing Data ke form Apply
        document.getElementById('applyJobId').value = currentJobId;
        document.getElementById('applyJobTitle').value = currentJobTitle;
        document.getElementById('applyJobTitleDisplay').innerText = currentJobTitle;

        document.getElementById('applyModal').classList.remove('hidden');
    }

    function closeApplyModal() {
        document.getElementById('applyModal').classList.add('hidden');
        document.body.classList.remove('overflow-hidden');
    }
</script>
<?= $this->endSection(); ?>