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


<style>
    /* 1. Card & Modal Background */
    [data-theme="dark"] .card,
    [data-theme="dark"] .modal-content {
        background-color: var(--bg-card);
        border: 1px solid var(--border-color);
        color: var(--text-main);
    }

    [data-theme="dark"] .card-header,
    [data-theme="dark"] .modal-header,
    [data-theme="dark"] .modal-footer {
        border-color: var(--border-color);
    }

    /* 2. Form Input & Select */
    [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"] .form-control:focus,
    [data-theme="dark"] .form-select:focus {
        background-color: var(--hover-bg);
        color: var(--text-main);
        border-color: var(--primary-color);
        box-shadow: 0 0 0 0.25rem rgba(67, 97, 238, 0.25);
    }
    
    [data-theme="dark"] .form-control::placeholder {
        color: var(--text-muted);
        opacity: 0.5;
    }

    /* 3. Typography & Labels */
    [data-theme="dark"] h2, 
    [data-theme="dark"] h5, 
    [data-theme="dark"] strong,
    [data-theme="dark"] .form-label {
        color: var(--text-main);
    }

    [data-theme="dark"] .text-muted {
        color: #cbd5e1 !important;
    }

    /* 4. Tabel Dark Mode */
    [data-theme="dark"] .table {
        color: var(--text-main);
        border-color: var(--border-color);
    }
    
    [data-theme="dark"] .table-light th,
    [data-theme="dark"] .table-secondary th,
    [data-theme="dark"] .table-secondary {
        background-color: var(--hover-bg);
        color: var(--text-main);
        border-color: var(--border-color) !important;
    }

    [data-theme="dark"] .table-hover tbody tr:hover {
        background-color: rgba(255, 255, 255, 0.05);
        color: var(--text-main);
    }

    /* 5. Tombol Close Modal */
    [data-theme="dark"] .btn-close {
        filter: invert(1) grayscale(100%) brightness(200%);
    }

    /* 6. Styling Accordion */
    .accordion-atp .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;
    }
    .accordion-atp .accordion-button {
        font-size: 1.05rem;
        padding: 1rem 1.25rem;
    }
    .accordion-atp .accordion-button:not(.collapsed) {
        background-color: #f8f9fa;
        color: #0d6efd;
        box-shadow: inset 0 -1px 0 rgba(0,0,0,.125);
    }
    
    /* Dark Mode Accordion */
    [data-theme="dark"] .accordion-atp .accordion-item { background-color: var(--bg-card); border-color: var(--border-color); }
    [data-theme="dark"] .accordion-atp .accordion-button { background-color: var(--bg-card); color: var(--text-main); }
    [data-theme="dark"] .accordion-atp .accordion-button:not(.collapsed) { background-color: var(--hover-bg); color: #6ea8fe; border-bottom: 1px solid var(--border-color); }
    [data-theme="dark"] .accordion-atp .accordion-button::after { filter: invert(1); }
</style>

<div class="container py-4">
    
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold">Alur Tujuan Pembelajaran (ATP)</h2>
            <p class="text-muted mb-0">Menurunkan Capaian Pembelajaran (CP) menjadi Tujuan Pembelajaran (TP)</p>
        </div>
        <div>
            <button type="button" class="btn btn-secondary me-2 shadow-sm" data-bs-toggle="modal" data-bs-target="#modalCetakFilter">
                <i class="bi bi-printer"></i> Cetak
            </button>
            <button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#modalATP">
                <i class="bi bi-plus-circle"></i> Tambah ATP
            </button>
        </div>
    </div>

    
    <?php if(session('success')): ?>
    <div class="alert alert-success alert-dismissible fade show shadow-sm border-0 border-start border-success border-4">
        <i class="bi bi-check-circle-fill me-2"></i> <?php echo e(session('success')); ?>

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

    <?php if(session('error')): ?>
    <div class="alert alert-danger alert-dismissible fade show shadow-sm border-0 border-start border-danger border-4">
        <i class="bi bi-exclamation-triangle-fill me-2"></i> <?php echo e(session('error')); ?>

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

    
    <?php
        $groupedAtp = $atp->groupBy(function($item) {
            return $item->subject->name ?? 'Tanpa Mata Pelajaran';
        })->map(function($group) {
            // Urutkan berdasarkan Fase (E, F, dst) lalu Group by Fase
            return $group->sortBy('phase')->groupBy('phase'); 
        });
    ?>

    
    <div class="accordion accordion-atp" id="accordionAtpMapel">
        <?php $__empty_1 = true; $__currentLoopData = $groupedAtp; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $subjectName => $phaseGroups): $__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-diagram-3-fill me-2 text-primary"></i> 
                        <?php echo e($subjectName); ?>

                        
                        <?php $totalInMapel = $phaseGroups->flatten()->count(); ?>
                        <span class="badge bg-primary ms-3 rounded-pill"><?php echo e($totalInMapel); ?> Data ATP</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 p-0">
                        <div class="table-responsive">
                            <table class="table table-hover align-middle mb-0">
                                <thead class="table-light">
                                    <tr>
                                        <th class="py-3 text-center" width="8%">Fase</th>
                                        <th class="py-3" width="35%">Capaian Pembelajaran (CP)</th>
                                        <th class="py-3" width="37%">Tujuan Pembelajaran (TP)</th>
                                        <th class="py-3 text-center" width="8%">Alokasi</th>
                                        <th class="py-3 text-center" width="12%">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    
                                    <?php $__currentLoopData = $phaseGroups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $phase => $items): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php
                                            $items = $items->values(); // Reset index
                                            $itemCount = $items->count();
                                            $totalJp = 0;
                                            
                                            // Menghitung Rowspan untuk CP
                                            $rowspansCp = [];
                                            $currentCp = null;
                                            $startIndex = 0;
                                            
                                            foreach ($items as $index => $item) {
                                                // Ekstrak angka JP untuk ditotalkan
                                                $totalJp += (int) filter_var($item->alokasi_waktu, FILTER_SANITIZE_NUMBER_INT);
                                                
                                                if ($item->learning_outcome === $currentCp) {
                                                    $rowspansCp[$startIndex]++;
                                                    $rowspansCp[$index] = 0; 
                                                } else {
                                                    $currentCp = $item->learning_outcome;
                                                    $startIndex = $index;
                                                    $rowspansCp[$startIndex] = 1;
                                                }
                                            }
                                        ?>

                                        
                                        <?php $__currentLoopData = $items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <tr>
                                            
                                            <?php if($index === 0): ?>
                                                <td rowspan="<?php echo e($itemCount); ?>" class="text-center align-middle border-end">
                                                    <span class="fw-bold fs-5 text-primary"><?php echo e($phase); ?></span>
                                                </td>
                                            <?php endif; ?>

                                            
                                            <?php if($rowspansCp[$index] > 0): ?>
                                                <td rowspan="<?php echo e($rowspansCp[$index]); ?>" class="align-middle border-end border-start" style="background-color: rgba(0,0,0,0.015);">
                                                    <div style="font-size: 0.9rem; white-space: pre-line;">
                                                        <?php echo e($item->learning_outcome ?? '-'); ?>

                                                    </div>
                                                </td>
                                            <?php endif; ?>

                                            
                                            <td class="align-middle">
                                                <div style="font-size: 0.9rem; white-space: pre-line;">
                                                    <?php echo e($item->objective_description ?? '-'); ?>

                                                </div>
                                            </td>

                                            
                                            <td class="text-center align-middle">
                                                <span class="badge bg-info text-dark"><?php echo e($item->alokasi_waktu ?? 0); ?> JP</span>
                                            </td>

                                            
                                            <td class="text-center align-middle">
                                                <div class="d-flex justify-content-center gap-1">
                                                    <button type="button" class="btn btn-sm btn-warning text-white btn-edit"
                                                        title="Edit Data"
                                                        data-id="<?php echo e($item->id); ?>"
                                                        data-fase="<?php echo e($item->phase); ?>"
                                                        data-subject="<?php echo e($item->subject_id); ?>"
                                                        data-cp="<?php echo e($item->learning_outcome); ?>"
                                                        data-tp="<?php echo e($item->objective_description); ?>"
                                                        data-alokasi="<?php echo e($item->alokasi_waktu); ?>"
                                                        data-bs-toggle="modal" 
                                                        data-bs-target="#modalEditATP">
                                                        <i class="bi bi-pencil-square"></i>
                                                    </button>
                                                    <a href="<?php echo e(route('atp.printSingle', $item->id)); ?>" target="_blank" class="btn btn-sm btn-success text-white" title="Cetak ATP ini">
                                                        <i class="bi bi-printer-fill"></i>
                                                    </a>
                                                    <form action="<?php echo e(route('atp.destroy', $item->id)); ?>" method="POST" onsubmit="return confirm('Yakin ingin menghapus ATP ini?')">
                                                        <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                                                        <button class="btn btn-sm btn-danger" title="Hapus Data">
                                                            <i class="bi bi-trash"></i>
                                                        </button>
                                                    </form>
                                                </div>
                                            </td>
                                        </tr>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                                        
                                        <tr class="table-secondary" style="border-bottom: 3px solid var(--border-color, #dee2e6);">
                                            <td colspan="3" class="text-end fw-bold pe-3">
                                                Total Alokasi Waktu Fase <?php echo e($phase); ?>:
                                            </td>
                                            <td class="text-center fw-bold text-primary fs-6"><?php echo e($totalJp); ?> JP</td>
                                            <td></td> </tr>

                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </tbody>
                            </table>
                        </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 text-muted">
                    <div class="mb-2"><i class="bi bi-diagram-3" style="font-size: 3rem;"></i></div>
                    <h5 class="fw-bold">Belum ada data ATP yang dibuat.</h5>
                    <p class="mb-0">Klik tombol "Tambah ATP" untuk memulai menurunkan Capaian Pembelajaran.</p>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>


<div class="modal fade" id="modalATP" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title">Tambah Alur Tujuan Pembelajaran</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form action="<?php echo e(route('atp.store')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <div class="modal-body p-4">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">Fase</label>
                            <select name="fase" id="input_fase" class="form-select" required>
                                <option value="E">Fase E (Kelas X)</option>
                                <option value="F">Fase F (Kelas XI/XII)</option>
                            </select>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">Mata Pelajaran</label>
                            <select name="subject_id" id="input_subject" class="form-select" required>
                                <option value="">-- 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); ?>"><?php echo e($s->name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold">Capaian Pembelajaran (CP)</label>
                        <textarea name="cp" id="input_cp" class="form-control" rows="3" placeholder="Salin CP dari kurikulum..." required></textarea>
                    </div>

                    
                    <div class="p-3 mb-4 bg-primary bg-opacity-10 border border-primary rounded">
                        <div class="d-flex justify-content-between align-items-center mb-2">
                            <label class="form-label fw-bold text-primary mb-0"><i class="bi bi-robot me-1"></i> Generate TP dengan Asisten AI</label>
                        </div>
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <input type="number" id="input_total_jp" class="form-control" placeholder="Masukkan total JP untuk CP ini (Contoh: 36)">
                                <small class="text-muted">AI akan membagi total JP ini ke dalam beberapa TP.</small>
                            </div>
                            <div class="col-md-4">
                                <button type="button" id="btnGenerateAI" class="btn btn-primary w-100">
                                    <i class="bi bi-magic"></i> Generate
                                </button>
                            </div>
                        </div>
                    </div>

                    
                    <div id="tp_container">
                        <label class="form-label fw-bold">Tujuan Pembelajaran (TP) & Alokasi Waktu</label>
                        <div class="row mb-2 tp-row align-items-start">
                            <div class="col-md-9 mb-2">
                                <textarea name="tp[]" class="form-control" rows="2" placeholder="Deskripsikan tujuan pembelajaran..." required></textarea>
                            </div>
                            <div class="col-md-2 mb-2">
                                <input type="number" name="alokasi_waktu[]" class="form-control" placeholder="JP" required>
                            </div>
                            <div class="col-md-1 mb-2 text-center">
                                <button type="button" class="btn btn-outline-danger btn-sm btn-remove-tp w-100 h-100" disabled>
                                    <i class="bi bi-trash"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                    
                    <button type="button" id="btnAddTpManual" class="btn btn-sm btn-outline-secondary mt-2">
                        <i class="bi bi-plus-circle"></i> Tambah Baris Manual
                    </button>
                </div>
                
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary px-4"><i class="bi bi-save me-1"></i> Simpan Semua</button>
                </div>
            </form>
        </div>
    </div>
</div>


<div class="modal fade" id="modalEditATP" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-warning text-white">
                <h5 class="modal-title text-white"><i class="bi bi-pencil-square me-2"></i> Edit ATP</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            
            <form id="formEditATP" action="" method="POST">
                <?php echo csrf_field(); ?>
                <?php echo method_field('PUT'); ?> 
                
                <div class="modal-body p-4">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">Fase</label>
                            <select name="fase" id="edit_fase" class="form-select" required>
                                <option value="E">Fase E (Kelas X)</option>
                                <option value="F">Fase F (Kelas XI/XII)</option>
                            </select>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">Mata Pelajaran</label>
                            <select name="subject_id" id="edit_subject_id" class="form-select" required>
                                <option value="">-- 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); ?>"><?php echo e($s->name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold">Capaian Pembelajaran (CP)</label>
                        <textarea name="cp" id="edit_cp" class="form-control" rows="3" required></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-bold">Tujuan Pembelajaran (TP)</label>
                        <textarea name="tp" id="edit_tp" class="form-control" rows="3" required></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-bold">Alokasi Waktu (JP)</label>
                        <input type="number" name="alokasi_waktu" id="edit_alokasi" class="form-control" required>
                    </div>
                </div>
                
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-warning text-white px-4"><i class="bi bi-check-circle me-1"></i> Update Perubahan</button>
                </div>
            </form>
        </div>
    </div>
</div>


<div class="modal fade" id="modalCetakFilter" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-secondary text-white">
                <h5 class="modal-title"><i class="bi bi-printer-fill me-2"></i> Konfigurasi Cetak ATP</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form action="<?php echo e(route('atp.print')); ?>" method="GET" target="_blank">
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-bold">Mata Pelajaran</label>
                        <select name="subject_id" class="form-select" required>
                            <option value="">-- Pilih Mata Pelajaran --</option>
                            <?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($s->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <small class="text-muted mt-2 d-block">
                            Sistem akan mencetak semua dokumen ATP khusus untuk mata pelajaran yang dipilih.
                        </small>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-light border" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary px-4">
                        <i class="bi bi-printer me-1"></i> Mulai Cetak
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>


<script>
    document.addEventListener('DOMContentLoaded', function () {
        // ==========================================
        // 1. FUNGSI UNTUK MODAL EDIT ATP
        // ==========================================
        const editButtons = document.querySelectorAll('.btn-edit');

        editButtons.forEach(function (button) {
            button.addEventListener('click', function () {
                const id = this.getAttribute('data-id');
                const fase = this.getAttribute('data-fase');
                const subject = this.getAttribute('data-subject');
                const cp = this.getAttribute('data-cp');
                const tp = this.getAttribute('data-tp');
                const alokasi = this.getAttribute('data-alokasi');

                document.getElementById('edit_fase').value = fase;
                document.getElementById('edit_subject_id').value = subject;
                document.getElementById('edit_cp').value = cp;
                document.getElementById('edit_tp').value = tp;
                document.getElementById('edit_alokasi').value = alokasi;

                const form = document.getElementById('formEditATP');
                form.action = "<?php echo e(url('atp')); ?>/" + id; 
            });
        });

        // ==========================================
        // 2. FUNGSI UNTUK ASISTEN AI & DINAMIS ROW
        // ==========================================
        const btnGenerateAI = document.getElementById('btnGenerateAI');
        const tpContainer = document.getElementById('tp_container');
        const btnAddTpManual = document.getElementById('btnAddTpManual');

        // Fungsi Tambah Baris Manual
        btnAddTpManual.addEventListener('click', function() {
            addTpRow('', '');
        });

        // Fungsi Menghapus Baris
        tpContainer.addEventListener('click', function(e) {
            // Cari elemen tombol atau icon di dalam tombol
            const btn = e.target.closest('.btn-remove-tp');
            if(btn) {
                const rows = document.querySelectorAll('.tp-row');
                if(rows.length > 1) {
                    btn.closest('.tp-row').remove();
                }
            }
        });

        // Fungsi Template Baris Baru
        function addTpRow(tpText = '', jpVal = '') {
            const row = document.createElement('div');
            row.className = 'row mb-2 tp-row align-items-start';
            row.innerHTML = `
                <div class="col-md-9 mb-2">
                    <textarea name="tp[]" class="form-control" rows="2" required>${tpText}</textarea>
                </div>
                <div class="col-md-2 mb-2">
                    <input type="number" name="alokasi_waktu[]" class="form-control" value="${jpVal}" required>
                </div>
                <div class="col-md-1 mb-2 text-center">
                    <button type="button" class="btn btn-outline-danger btn-sm btn-remove-tp w-100 h-100">
                        <i class="bi bi-trash"></i>
                    </button>
                </div>
            `;
            tpContainer.appendChild(row);
            
            // Aktifkan semua tombol hapus jika baris > 1
            document.querySelectorAll('.btn-remove-tp').forEach(btn => btn.disabled = false);
        }

        // Proses Generate AI
        btnGenerateAI.addEventListener('click', async function() {
            const cp = document.getElementById('input_cp').value;
            const totalJp = document.getElementById('input_total_jp').value;
            const subject = document.getElementById('input_subject').options[document.getElementById('input_subject').selectedIndex].text;
            const fase = document.getElementById('input_fase').value;

            if(!cp || !totalJp) {
                alert('Mohon isi Capaian Pembelajaran (CP) dan Total JP terlebih dahulu!');
                return;
            }

            // Ubah state tombol menjadi loading
            const originalText = btnGenerateAI.innerHTML;
            btnGenerateAI.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Loading...';
            btnGenerateAI.disabled = true;

            try {
                const response = await fetch('<?php echo e(route("api.ai.generate-tp")); ?>', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRF-TOKEN': '<?php echo e(csrf_token()); ?>'
                    },
                    body: JSON.stringify({ cp, totalJp, subject, fase })
                });

                const data = await response.json();

                if(data.status === 'success') {
                    // Kosongkan container dan isi dengan hasil AI
                    tpContainer.innerHTML = '<label class="form-label fw-bold">Tujuan Pembelajaran (TP) & Alokasi Waktu</label>';
                    data.results.forEach(item => {
                        addTpRow(item.tujuan_pembelajaran, item.alokasi_jp);
                    });
                } else {
                    alert('Gagal Generate: ' + data.message);
                }
            } catch (error) {
                console.error('Error:', error);
                alert('Terjadi kesalahan koneksi ke server.');
            } finally {
                btnGenerateAI.innerHTML = originalText;
                btnGenerateAI.disabled = false;
            }
        });
    });
</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/atp/index.blade.php ENDPATH**/ ?>