<?php $__env->startSection('content'); ?>


<style>
    /* Dark Mode Support */
    [data-theme="dark"] .modal-content, [data-theme="dark"] .card {
        background-color: var(--bg-card); border: 1px solid var(--border-color); color: var(--text-main);
    }
    [data-theme="dark"] .form-control, [data-theme="dark"] .form-select {
        background-color: var(--hover-bg); border-color: var(--border-color); color: var(--text-main);
    }
    [data-theme="dark"] .text-muted { color: #cbd5e1 !important; }
    [data-theme="dark"] h2, [data-theme="dark"] h5, [data-theme="dark"] label { color: #f1f5f9; }
    [data-theme="dark"] .btn-close { filter: invert(1) grayscale(100%) brightness(200%); }
    
    /* Agar Textarea lebih rapat secara visual */
    textarea.form-control { line-height: 1.3; }

    /* --- Styling Accordion & Dark Mode Support --- */
    .accordion-lkpd .accordion-item {
        border: 1px solid var(--border-color, #dee2e6);
        border-radius: 8px !important;
        margin-bottom: 15px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.02);
        overflow: hidden;
        background-color: transparent;
    }
    .accordion-lkpd .accordion-button {
        font-size: 1.05rem;
        padding: 1rem 1.25rem;
    }
    .accordion-lkpd .accordion-button:not(.collapsed) {
        background-color: #f8f9fa;
        color: #0d6efd;
        box-shadow: inset 0 -1px 0 rgba(0,0,0,.125);
    }
    .accordion-lkpd .accordion-body {
        background-color: #f8f9fa; /* Latar belakang abu-abu terang agar kartu menonjol */
        padding: 1.5rem;
    }
    
    /* Dark Mode Accordion */
    [data-theme="dark"] .accordion-lkpd .accordion-item { background-color: var(--bg-card); border-color: var(--border-color); }
    [data-theme="dark"] .accordion-lkpd .accordion-button { background-color: var(--bg-card); color: var(--text-main); }
    [data-theme="dark"] .accordion-lkpd .accordion-button:not(.collapsed) { background-color: var(--hover-bg); color: #6ea8fe; border-bottom: 1px solid var(--border-color); }
    [data-theme="dark"] .accordion-lkpd .accordion-button::after { filter: invert(1); }
    [data-theme="dark"] .accordion-lkpd .accordion-body { background-color: #1e293b; }
</style>

<div class="container py-4">
    
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold">Lembar Kerja (LKPD)</h2>
            <p class="text-muted mb-0">Bank tugas siswa, panduan praktikum, dan lembar diskusi.</p>
        </div>
        <div>
            <button class="btn btn-secondary me-2 shadow-sm" data-bs-toggle="modal" data-bs-target="#modalCetakLKPD">
                <i class="bi bi-printer"></i> Cetak LKPD
            </button>
            <button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#modalCreateLKPD">
                <i class="bi bi-plus-circle"></i> Buat LKPD Baru
            </button>
        </div>
    </div>

    <?php if(session('success')): ?>
    <div class="alert alert-success alert-dismissible fade show">
        <?php echo e(session('success')); ?>

        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    
    <?php
        $groupedLkpds = $lkpds->groupBy(function($item) {
            return $item->subject->name ?? 'Tanpa Mata Pelajaran';
        })->map(function($group) {
            // Mengurutkan LKPD terbaru di atas
            return $group->sortByDesc('created_at')->values();
        });
    ?>

    <div class="accordion accordion-lkpd" id="accordionLKPDMapel">
        <?php $__empty_1 = true; $__currentLoopData = $groupedLkpds; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $subjectName => $subjectLkpds): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
            <div class="accordion-item border-0 shadow-sm">
                <h2 class="accordion-header" id="heading-<?php echo e($loop->index); ?>">
                    
                    <button class="accordion-button fw-bold collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-<?php echo e($loop->index); ?>" aria-expanded="false" aria-controls="collapse-<?php echo e($loop->index); ?>">
                        <i class="bi bi-journal-richtext me-2 text-primary"></i> 
                        <?php echo e($subjectName); ?>

                        <span class="badge bg-primary ms-3 rounded-pill"><?php echo e($subjectLkpds->count()); ?> LKPD</span>
                    </button>
                </h2>
                
                <div id="collapse-<?php echo e($loop->index); ?>" class="accordion-collapse collapse" aria-labelledby="heading-<?php echo e($loop->index); ?>">
                    <div class="accordion-body">
                        
                        <div class="row">
                            <?php $__currentLoopData = $subjectLkpds; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <div class="col-md-6 mb-4">
                                <div class="card h-100 shadow-sm border-0">
                                    <div class="card-header d-flex justify-content-between align-items-center bg-transparent border-bottom-0 pt-3 pb-0">
                                        <span class="badge bg-primary"><?php echo e($item->subject->name); ?></span>
                                        <small class="text-muted"><i class="bi bi-calendar3"></i> <?php echo e($item->created_at->format('d M Y')); ?></small>
                                    </div>
                                    <div class="card-body">
                                        <h5 class="card-title fw-bold mb-3"><?php echo e($item->title); ?></h5>
                                        <div class="mb-3">
                                            <span class="badge bg-success text-white text-wrap me-1" style="max-width: 100%;"><i class="bi bi-tag"></i> <?php echo e($item->type); ?></span>
                                            <span class="badge bg-secondary"><i class="bi bi-people"></i> <?php echo e(ucfirst($item->mode)); ?></span>
                                        </div>
                                        
                                        <p class="card-text text-muted small p-3 bg-light rounded" style="border-left: 3px solid #198754;">
                                            <strong>Langkah Kerja (Preview):</strong><br>
                                            <?php echo e(Str::limit($item->activity_steps, 120)); ?>

                                        </p>
                                    </div>
                                    <div class="card-footer bg-transparent border-top-0 pb-3 pt-0">
                                        <div class="d-flex justify-content-end gap-2">
                                            <button class="btn btn-sm btn-outline-info" data-bs-toggle="modal" data-bs-target="#detailModalLKPD<?php echo e($item->id); ?>" title="Lihat Detail">
                                                <i class="bi bi-eye"></i> Detail
                                            </button>
                                            <button class="btn btn-sm btn-warning text-white" data-bs-toggle="modal" data-bs-target="#editModalLKPD<?php echo e($item->id); ?>" title="Edit">
                                                <i class="bi bi-pencil"></i> Edit
                                            </button>
                                            <form action="<?php echo e(route('lkpd.destroy', $item->id)); ?>" method="POST" onsubmit="return confirm('Hapus LKPD ini?')">
                                                <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                                                <button class="btn btn-sm btn-outline-danger" title="Hapus"><i class="bi bi-trash"></i> Hapus</button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
            <div class="card shadow-sm border-0">
                <div class="card-body text-center py-5">
                    <div class="text-muted opacity-50 mb-3">
                        <i class="bi bi-journal-text" style="font-size: 4rem;"></i>
                    </div>
                    <h5 class="text-muted fw-bold">Belum Ada LKPD</h5>
                    <p class="text-muted">Mulai buat Lembar Kerja Peserta Didik (LKPD) Anda sekarang.</p>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>


<div class="modal fade" id="modalCetakLKPD" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-secondary text-white">
                <h5 class="modal-title"><i class="bi bi-printer"></i> Cetak Lembar Kerja</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form action="<?php echo e(route('lkpd.print_bulk')); ?>" method="POST" target="_blank">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <div class="table-responsive" style="max-height: 400px; overflow-y: auto;">
                        <table class="table table-sm table-bordered table-hover mb-0">
                            <thead class="table-light sticky-top">
                                <tr>
                                    <th width="40" class="text-center"><input type="checkbox" id="checkAllLKPD"></th>
                                    <th>Judul LKPD</th>
                                    <th>Mapel</th>
                                    <th>Jenis</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $lkpds; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $m): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td class="text-center"><input type="checkbox" name="lkpd_ids[]" value="<?php echo e($m->id); ?>" class="check-item-lkpd"></td>
                                    <td><?php echo e($m->title); ?></td>
                                    <td><?php echo e($m->subject->name); ?></td>
                                    <td><?php echo e($m->type); ?></td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-light" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-dark">Cetak Terpilih</button>
                </div>
            </form>
        </div>
    </div>
</div>


<div class="modal fade" id="modalCreateLKPD" tabindex="-1">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title"><i class="bi bi-robot"></i> Buat LKPD Baru</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            
            <form action="<?php echo e(route('lkpd.store')); ?>" method="POST" id="formCreateLKPD">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">Mata Pelajaran <span class="text-danger">*</span></label>
                            <select name="subject_id" id="ai_lkpd_subject" class="form-select" required>
                                <option value="" selected disabled>-- Pilih Mapel --</option>
                                <?php $__currentLoopData = $subjects; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> 
                                    <option value="<?php echo e($s->id); ?>" data-name="<?php echo e($s->name); ?>"><?php echo e($s->name); ?></option> 
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">Judul LKPD <span class="text-danger">*</span></label>
                            <input type="text" name="title" id="ai_lkpd_title" class="form-control" placeholder="Cth: Praktikum Hukum Ohm" required>
                        </div>
                    </div>

                    <div class="row bg-light p-3 rounded border mx-1 mb-4">
                        <div class="col-md-6 mb-2">
                            <label class="form-label fw-bold">Jenis Kegiatan <span class="text-danger">*</span></label>
                            <select name="type" id="ai_lkpd_type" class="form-select" required>
                                <option value="" selected disabled>-- Pilih Jenis Kegiatan --</option>                                
                                <optgroup label="Praktik & Eksperimen">
                                    <option value="Eksperimen / Praktikum">Eksperimen / Praktikum</option>
                                    <option value="Demonstrasi">Demonstrasi Guru</option>
                                    <option value="Simulasi / Role Play">Simulasi / Role Play</option>
                                </optgroup>
                                <optgroup label="Pemecahan Masalah">
                                    <option value="Problem Based Learning (PBL)">Problem Based Learning (PBL)</option>
                                    <option value="Project Based Learning (PjBL)">Project Based Learning (PjBL)</option>
                                    <option value="Studi Kasus">Studi Kasus / Troubleshooting</option>
                                </optgroup>
                                <optgroup label="Literasi & Diskusi">
                                    <option value="Diskusi Kelompok / FGD">Diskusi Kelompok / FGD</option>
                                    <option value="Observasi Lapangan">Observasi Lapangan</option>
                                    <option value="Studi Literatur">Studi Literatur / Resensi</option>
                                </optgroup>
                            </select>
                        </div>
                        <div class="col-md-6 mb-2">
                            <label class="form-label fw-bold">Mode Pengerjaan <span class="text-danger">*</span></label>
                            <select name="mode" id="ai_lkpd_mode" class="form-select" required>
                                <option value="" selected disabled>-- Pilih Mode --</option>
                                <option value="kelompok">Kelompok</option>
                                <option value="individu">Individu</option>
                            </select>
                        </div>
                    </div>

                    
                    <div class="card bg-white border-primary mb-4 shadow-sm">
                        <div class="card-body py-3">
                            <h6 class="text-primary fw-bold border-bottom pb-2 mb-3"><i class="bi bi-cpu"></i> Generator AI (Bantu Buat Langkah Kerja)</h6>
                            <div class="row align-items-center">
                                <div class="col-md-8 mb-2">
                                    <input type="text" id="ai_lkpd_topic" class="form-control form-control-sm" placeholder="Topik Utama (Cth: Instalasi Listrik)">
                                </div>
                                <div class="col-md-4 mb-2">
                                    <button type="button" id="btnGenerateLKPDAI" class="btn btn-primary w-100 fw-bold">
                                        <i class="bi bi-magic"></i> Generate Konten
                                    </button>
                                </div>
                                <div class="col-12 mt-2">
                                    <textarea id="ai_lkpd_material" class="form-control form-control-sm" rows="3" placeholder="Ketik detail langkah atau spesifikasi yang diinginkan (Cth: Merakit rangkaian seri paralel, fokus pada prosedur keselamatan...)"></textarea>
                                </div>
                            </div>
                            <div id="loadingIndicatorLKPD" class="text-center mt-3" style="display:none;">
                                <div class="spinner-border text-primary" role="status"></div>
                                <div class="text-muted mt-2 fw-bold">AI sedang menyusun langkah kerja. Harap tunggu...</div>
                            </div>
                        </div>
                    </div>
                    
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold">Tujuan Pembelajaran</label>
                        <textarea name="objectives" id="lkpd_tujuan" class="form-control" rows="5" placeholder="Hasil generate..."></textarea>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">Alat & Bahan</label>
                            <textarea name="tools_materials" id="lkpd_alat" class="form-control" rows="6" placeholder="Hasil generate..."></textarea>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">Langkah Kerja</label>
                            <textarea name="activity_steps" id="lkpd_langkah" class="form-control" rows="6" placeholder="Hasil generate..."></textarea>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-bold">Pertanyaan Diskusi</label>
                        <textarea name="discussion_questions" id="lkpd_diskusi" class="form-control" rows="5" placeholder="Hasil generate..."></textarea>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-success px-4 fw-bold">Simpan LKPD</button>
                </div>
            </form>
        </div>
    </div>
</div>


<?php $__currentLoopData = $lkpds; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    
    <div class="modal fade" id="detailModalLKPD<?php echo e($item->id); ?>" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header bg-info text-white">
                    <h5 class="modal-title"><i class="bi bi-file-text"></i> <?php echo e($item->title); ?></h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="bg-light p-3 rounded mb-3 border">
                        <h6 class="fw-bold text-primary border-bottom pb-2"><i class="bi bi-bullseye"></i> Tujuan Pembelajaran:</h6>
                        <p class="text-dark mt-2 mb-0" style="white-space: pre-line;"><?php echo e($item->objectives ?? '-'); ?></p>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="bg-light p-3 rounded h-100 border">
                                <h6 class="fw-bold text-warning border-bottom pb-2"><i class="bi bi-tools"></i> Alat & Bahan:</h6>
                                <p class="text-dark mt-2 mb-0" style="white-space: pre-line;"><?php echo e($item->tools_materials ?? '-'); ?></p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="bg-light p-3 rounded h-100 border">
                                <h6 class="fw-bold text-success border-bottom pb-2"><i class="bi bi-list-ol"></i> Langkah Kerja / Prosedur:</h6>
                                <p class="text-dark mt-2 mb-0" style="white-space: pre-line;"><?php echo e($item->activity_steps); ?></p>
                            </div>
                        </div>
                    </div>

                    <div class="bg-light p-3 rounded mt-3 border">
                        <h6 class="fw-bold text-info border-bottom pb-2"><i class="bi bi-chat-dots"></i> Pertanyaan Diskusi:</h6>
                        <p class="text-dark mt-2 mb-0" style="white-space: pre-line;"><?php echo e($item->discussion_questions ?? '-'); ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    
    <div class="modal fade" id="editModalLKPD<?php echo e($item->id); ?>" tabindex="-1">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header bg-warning text-dark">
                    <h5 class="modal-title"><i class="bi bi-pencil-square"></i> Edit LKPD</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form action="<?php echo e(route('lkpd.update', $item->id)); ?>" method="POST">
                    <?php echo csrf_field(); ?> <?php echo method_field('PUT'); ?>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Mata Pelajaran</label>
                                <select name="subject_id" class="form-select" required>
                                    <?php $__currentLoopData = $subjects; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> 
                                        <option value="<?php echo e($s->id); ?>" <?php echo e($item->subject_id == $s->id ? 'selected' : ''); ?>><?php echo e($s->name); ?></option> 
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Judul LKPD</label>
                                <input type="text" name="title" class="form-control" value="<?php echo e($item->title); ?>" required>
                            </div>
                        </div>

                        <div class="row bg-light p-3 rounded border mx-1 mb-3">
                            <div class="col-md-6 mb-2">
                                <label class="form-label fw-bold">Jenis Kegiatan</label>
                                <select name="type" class="form-select" required>
                                    <optgroup label="Praktik & Eksperimen">
                                        <option value="Eksperimen / Praktikum" <?php echo e($item->type == 'Eksperimen / Praktikum' ? 'selected' : ''); ?>>Eksperimen / Praktikum</option>
                                        <option value="Demonstrasi" <?php echo e($item->type == 'Demonstrasi' ? 'selected' : ''); ?>>Demonstrasi Guru</option>
                                        <option value="Simulasi / Role Play" <?php echo e($item->type == 'Simulasi / Role Play' ? 'selected' : ''); ?>>Simulasi / Role Play</option>
                                    </optgroup>
                                    <optgroup label="Pemecahan Masalah">
                                        <option value="Problem Based Learning (PBL)" <?php echo e($item->type == 'Problem Based Learning (PBL)' ? 'selected' : ''); ?>>Problem Based Learning (PBL)</option>
                                        <option value="Project Based Learning (PjBL)" <?php echo e($item->type == 'Project Based Learning (PjBL)' ? 'selected' : ''); ?>>Project Based Learning (PjBL)</option>
                                        <option value="Studi Kasus" <?php echo e($item->type == 'Studi Kasus' ? 'selected' : ''); ?>>Studi Kasus</option>
                                    </optgroup>
                                    <optgroup label="Literasi & Diskusi">
                                        <option value="Diskusi Kelompok / FGD" <?php echo e($item->type == 'Diskusi Kelompok / FGD' ? 'selected' : ''); ?>>Diskusi Kelompok / FGD</option>
                                        <option value="Observasi Lapangan" <?php echo e($item->type == 'Observasi Lapangan' ? 'selected' : ''); ?>>Observasi Lapangan</option>
                                        <option value="Studi Literatur" <?php echo e($item->type == 'Studi Literatur' ? 'selected' : ''); ?>>Studi Literatur</option>
                                    </optgroup>
                                </select>
                            </div>
                            <div class="col-md-6 mb-2">
                                <label class="form-label fw-bold">Mode Pengerjaan</label>
                                <select name="mode" class="form-select" required>
                                    <option value="kelompok" <?php echo e($item->mode == 'kelompok' ? 'selected' : ''); ?>>Kelompok</option>
                                    <option value="individu" <?php echo e($item->mode == 'individu' ? 'selected' : ''); ?>>Individu</option>
                                </select>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Tujuan Pembelajaran</label>
                            <textarea name="objectives" class="form-control" rows="4"><?php echo e($item->objectives); ?></textarea>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Alat & Bahan</label>
                                <textarea name="tools_materials" class="form-control" rows="5"><?php echo e($item->tools_materials); ?></textarea>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Langkah Kerja</label>
                                <textarea name="activity_steps" class="form-control" rows="5" required><?php echo e($item->activity_steps); ?></textarea>
                            </div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold">Pertanyaan Diskusi</label>
                            <textarea name="discussion_questions" class="form-control" rows="4"><?php echo e($item->discussion_questions); ?></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-warning px-4 fw-bold">Simpan Perubahan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>


<script>
document.addEventListener('DOMContentLoaded', function() {
    
    // --- RESET FORM SAAT BUKA MODAL ---
    const modalCreateLKPD = document.getElementById('modalCreateLKPD');
    if (modalCreateLKPD) {
        modalCreateLKPD.addEventListener('show.bs.modal', function () {
            document.getElementById('formCreateLKPD').reset();
            ['lkpd_tujuan','lkpd_alat','lkpd_langkah','lkpd_diskusi'].forEach(id => document.getElementById(id).value = '');
            
            const btnAI = document.getElementById('btnGenerateLKPDAI');
            btnAI.disabled = false;
            btnAI.innerHTML = '<i class="bi bi-magic"></i> Generate Konten';
            document.getElementById('loadingIndicatorLKPD').style.display = 'none';
        });
    }

    // --- HELPER FORMAT LIST ---
    function formatAIOutput(data) {
        if (!data) return "";
        if (Array.isArray(data)) return data.map(item => "- " + item).join('\n');
        return data;
    }

    // --- LOGIC TOMBOL GENERATE AI ---
    const btnAI = document.getElementById('btnGenerateLKPDAI');
    if(btnAI) {
        btnAI.addEventListener('click', function() {
            const subjectSelect = document.getElementById('ai_lkpd_subject');
            if(subjectSelect.selectedIndex === 0 || !subjectSelect.value) { 
                alert("Pilih Mata Pelajaran terlebih dahulu."); 
                return; 
            }

            const mapelName = subjectSelect.options[subjectSelect.selectedIndex].getAttribute('data-name');
            
            // Cek Input Wajib
            const topikVal = document.getElementById('ai_lkpd_topic').value;
            const jenisVal = document.getElementById('ai_lkpd_type').value;
            const modeVal = document.getElementById('ai_lkpd_mode').value;

            if(!topikVal) { alert("Mohon isi Topik Utama agar AI dapat menyusun langkah kerja."); return; }
            if(!jenisVal) { alert("Mohon pilih Jenis Kegiatan terlebih dahulu."); return; }
            if(!modeVal) { alert("Mohon pilih Mode Pengerjaan terlebih dahulu."); return; }

            const payload = {
                mapel: mapelName || 'Umum',
                judul: document.getElementById('ai_lkpd_title').value,
                topik: topikVal,
                materi: document.getElementById('ai_lkpd_material').value,
                jenis: jenisVal,
                mode: modeVal
            };

            // Loading State
            document.getElementById('loadingIndicatorLKPD').style.display = 'block';
            btnAI.disabled = true;
            btnAI.innerHTML = '<i class="spinner-border spinner-border-sm"></i> Menyusun...';

            // Fetch dengan Asset URL
            fetch("<?php echo e(asset('ai-lkpd.php')); ?>", { 
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(payload)
            })
            .then(response => {
                if (!response.ok) throw new Error("Server Error: " + response.status);
                return response.json();
            })
            .then(data => {
                if(data.error) {
                    alert("Error AI: " + data.error);
                } else if(data.tujuan || data.langkah) {
                    document.getElementById('lkpd_tujuan').value = formatAIOutput(data.tujuan);
                    document.getElementById('lkpd_alat').value = formatAIOutput(data.alat);
                    document.getElementById('lkpd_langkah').value = formatAIOutput(data.langkah);
                    document.getElementById('lkpd_diskusi').value = formatAIOutput(data.diskusi);
                } else {
                    alert("Gagal memproses. Cek console.");
                    console.log(data);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('Gagal menghubungi server AI. ' + error.message);
            })
            .finally(() => {
                document.getElementById('loadingIndicatorLKPD').style.display = 'none';
                btnAI.disabled = false;
                btnAI.innerHTML = '<i class="bi bi-magic"></i> Generate Konten';
            });
        });
    }
    
    // Checkbox Select All
    const checkAll = document.getElementById('checkAllLKPD');
    if(checkAll){
        checkAll.addEventListener('change', function(){
            document.querySelectorAll('.check-item-lkpd').forEach(c => c.checked = this.checked);
        });
    }
});
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/vol15_7/infinityfree.com/if0_40829333/htdocs/resources/views/pages/perangkat/lkpd/index.blade.php ENDPATH**/ ?>