MIF_E31230356/resources/views/admin/challenge/index.blade.php

970 lines
35 KiB
PHP

@extends('admin.layouts.app')
@section('title', 'Daftar Challenge')
@section('content')
<style>
.page-title {
font-size: 30px;
font-weight: 800;
margin-bottom: 10px;
margin-top: -20px;
}
.custom-card {
background: white;
border-radius: 20px;
border: 2px solid #e5e5e5;
padding: 25px;
}
.btn-primary-custom {
background: #2b8ef3;
color: white;
border-radius: 10px;
padding: 8px 18px;
border: none;
display: inline-flex;
align-items: center;
gap: 8px;
font-size: 14px;
text-decoration: none;
cursor: pointer;
font-family: 'Poppins', sans-serif;
}
.table-header { background: #a5e6ba; }
.search-box {
background: #a5e6ba;
border-radius: 30px;
padding: 6px 15px;
display: flex;
align-items: center;
gap: 8px;
}
.search-box input {
border: none;
outline: none;
background: transparent;
width: 160px;
}
.action-icon { width: 20px; cursor: pointer; margin: 0 4px; }
.deadline-badge {
font-size: 11px;
font-weight: 700;
padding: 3px 8px;
border-radius: 99px;
display: inline-block;
}
.deadline-aktif { background: #dcfce7; color: #16a34a; }
.deadline-lewat { background: #fee2e2; color: #ef4444; }
.kelas-chip {
display: inline-block;
background: #e6f0ff;
color: #1d4ed8;
font-size: 11px;
font-weight: 600;
padding: 2px 8px;
border-radius: 99px;
margin: 1px;
}
.soal-count {
background: #f0fdf4;
color: #16a34a;
font-size: 12px;
font-weight: 700;
padding: 3px 10px;
border-radius: 99px;
}
.alert-success-custom {
background: #dcfce7;
color: #166534;
border-radius: 10px;
padding: 12px 16px;
margin-bottom: 16px;
font-weight: 500;
font-size: 14px;
}
/* ── MODAL SHARED ─────────────────────────────────── */
.modal-content {
border-radius: 16px;
border: none;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
/* Header kuning (Tambah) */
.modal-header-yellow {
background: #f9c946;
color: #1e293b;
border-bottom: none;
border-radius: 16px 16px 0 0;
padding: 18px 24px;
}
.modal-header-yellow .modal-title { font-weight: 800; font-size: 18px; }
.modal-header-yellow .btn-close { filter: none; }
/* Header ungu (Edit — tetap seperti semula) */
.modal-header-challenge {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
border-bottom: none;
border-radius: 16px 16px 0 0;
}
.modal-header-challenge .btn-close { filter: brightness(0) invert(1); }
.modal-body label { font-weight: 600; font-size: 13px; }
/* ── STEP INDICATOR ───────────────────────────────── */
.step-indicator {
display: flex;
align-items: center;
gap: 0;
margin-bottom: 20px;
background: #f8fafc;
border-radius: 12px;
padding: 12px 16px;
border: 1px solid #e2e8f0;
}
.step-dot {
width: 30px;
height: 30px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 13px;
font-weight: 800;
flex-shrink: 0;
transition: all 0.3s;
}
.step-dot.active { background: #f9c946; color: #1e293b; box-shadow: 0 0 0 3px rgba(249,201,70,.3); }
.step-dot.done { background: #16a34a; color: white; }
.step-dot.inactive { background: #e2e8f0; color: #94a3b8; }
.step-label {
font-size: 12px;
font-weight: 700;
margin-left: 8px;
transition: color 0.3s;
}
.step-label.active { color: #1e293b; }
.step-label.inactive { color: #94a3b8; }
.step-line {
flex: 1;
height: 2px;
background: #e2e8f0;
margin: 0 12px;
border-radius: 2px;
overflow: hidden;
position: relative;
}
.step-line-fill {
position: absolute;
inset: 0;
background: #16a34a;
transform: scaleX(0);
transform-origin: left;
transition: transform 0.4s ease;
}
.step-line-fill.filled { transform: scaleX(1); }
/* ── STEP PANELS ──────────────────────────────────── */
.step-panel { display: none; }
.step-panel.active { display: block; }
/* ── SOAL CARD ────────────────────────────────────── */
.soal-card {
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 14px;
padding: 16px 18px;
margin-bottom: 12px;
position: relative;
}
.soal-card .soal-number {
position: absolute;
top: -10px;
left: 16px;
background: #667eea;
color: white;
font-size: 11px;
font-weight: 700;
padding: 2px 10px;
border-radius: 99px;
}
.soal-card .btn-hapus-soal {
position: absolute;
top: 10px;
right: 12px;
background: #fee2e2;
color: #ef4444;
border: none;
border-radius: 8px;
width: 28px;
height: 28px;
font-size: 14px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}
.soal-card .btn-hapus-soal:hover { background: #fca5a5; }
.opsi-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 8px;
}
.opsi-item { display: flex; flex-direction: column; gap: 4px; }
.opsi-item label { font-size: 12px; color: #64748b; font-weight: 600; }
.opsi-item input { border-radius: 8px; border: 1px solid #cbd5e1; padding: 6px 10px; font-size: 13px; }
.jawaban-row {
display: flex;
gap: 12px;
align-items: center;
margin-top: 10px;
background: #fff;
border-radius: 8px;
padding: 8px 12px;
border: 1px solid #e2e8f0;
}
.jawaban-row label { font-size: 12px; color: #64748b; font-weight: 600; min-width: 90px; }
.btn-tambah-soal {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
border: none;
border-radius: 10px;
padding: 9px 18px;
font-size: 13px;
font-weight: 600;
cursor: pointer;
width: 100%;
margin-top: 4px;
font-family: 'Poppins', sans-serif;
}
.kelas-checkbox-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.kelas-check-item {
display: flex;
align-items: center;
gap: 8px;
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 8px 10px;
cursor: pointer;
transition: all 0.2s;
}
.kelas-check-item:hover { background: #e6f0ff; border-color: #2b8ef3; }
.kelas-check-item input[type="checkbox"] { cursor: pointer; }
.modal-dialog-fixed .modal-content {
max-height: 90vh;
overflow: hidden;
display: flex;
flex-direction: column;
}
/* form harus ikut flex agar modal-body bisa grow */
.modal-dialog-fixed form {
display: contents;
}
.modal-dialog-fixed .modal-header,
.modal-dialog-fixed .modal-footer {
flex-shrink: 0;
}
.modal-dialog-fixed .modal-body {
flex: 1 1 auto;
overflow: hidden;
display: flex;
flex-direction: column;
min-height: 0;
padding-bottom: 0;
}
/* step-indicator tidak ikut scroll */
.modal-dialog-fixed .step-indicator {
flex-shrink: 0;
margin-bottom: 16px;
}
.modal-dialog-fixed .step-panel {
display: none;
}
.modal-dialog-fixed .step-panel.active {
display: flex;
flex-direction: column;
flex: 1 1 auto;
min-height: 0;
}
/* Step 1: scroll jika form terlalu panjang */
#tambah-step-1,
#edit-step-1 {
overflow-y: auto;
padding-right: 4px;
}
/* Step 2: soal-scroll-area yang mengelola scroll */
#tambah-step-2,
#edit-step-2 {
overflow: hidden;
}
.soal-scroll-area {
flex: 1 1 auto;
min-height: 0;
overflow-y: auto;
padding-right: 6px;
scroll-behavior: smooth;
}
.soal-scroll-area::-webkit-scrollbar { width: 6px; }
.soal-scroll-area::-webkit-scrollbar-track { background: #f1f5f9; border-radius: 99px; }
.soal-scroll-area::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 99px; }
.soal-scroll-area::-webkit-scrollbar-thumb:hover { background: #94a3b8; }
.section-divider {
font-size: 13px;
font-weight: 700;
color: #667eea;
border-bottom: 2px solid #e6f0ff;
padding-bottom: 6px;
margin: 16px 0 12px;
}
/* ── FOOTER BUTTONS ───────────────────────────────── */
.btn-green {
background: #16a34a;
color: white;
border: none;
border-radius: 10px;
padding: 9px 22px;
font-size: 14px;
font-weight: 700;
cursor: pointer;
font-family: 'Poppins', sans-serif;
display: inline-flex;
align-items: center;
gap: 6px;
transition: background 0.2s;
}
.btn-green:hover { background: #15803d; }
</style>
<h3 class="page-title">DAFTAR CHALLENGE</h3>
@if(session('success'))
<div class="alert-success-custom"> {{ session('success') }}</div>
@endif
<div class="custom-card">
<div class="d-flex justify-content-between align-items-center mb-3">
<button class="btn-primary-custom" onclick="openTambahModal()">
<img src="{{ asset('images/icon/main/add.png') }}" width="18" alt="Tambah"> Tambah Challenge
</button>
<form method="GET">
<div class="search-box">
<input type="text" name="search" placeholder="Cari challenge..."
value="{{ request('search') }}">
<button style="border:none;background:none" type="submit">
<img src="{{ asset('images/icon/main/search.png') }}" width="18" alt="Cari">
</button>
</div>
</form>
</div>
<table class="table text-center align-middle">
<thead class="table-header">
<tr>
<th>No</th>
<th>Judul Challenge</th>
<th>Kelas</th>
<th>Soal</th>
<th>Total EXP</th>
<th>Tenggat</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
@forelse($challenges as $i => $ch)
@php $isLewat = \Carbon\Carbon::parse($ch->tenggat_waktu)->isPast(); @endphp
<tr>
<td>{{ $challenges->firstItem() + $i }}</td>
<td style="text-align:left">
<div style="font-weight:700;color:#1e293b">{{ $ch->judul_challenge }}</div>
@if($ch->deskripsi)
<div style="font-size:12px;color:#94a3b8">{{ Str::limit($ch->deskripsi, 50) }}</div>
@endif
</td>
<td>
@foreach($ch->kelas as $k)
<span class="kelas-chip">{{ $k->tingkat }} {{ $k->nama_kelas }}</span>
@endforeach
</td>
<td><span class="soal-count">{{ $ch->soal_count }} soal</span></td>
<td style="font-weight:700;color:#667eea">{{ $ch->exp }} EXP</td>
<td>
<span class="deadline-badge {{ $isLewat ? 'deadline-lewat' : 'deadline-aktif' }}">
{{ $isLewat ? '⏰ Lewat' : '✅ Aktif' }}
</span>
<div style="font-size:11px;color:#64748b;margin-top:3px">
{{ \Carbon\Carbon::parse($ch->tenggat_waktu)->format('d M Y, H:i') }}
</div>
</td>
<td>
<button onclick="openEditModal({{ $ch->id_challenge }})"
style="border:none;background:none">
<img src="{{ asset('images/icon/main/edit.png') }}" class="action-icon" alt="Edit">
</button>
<a href="{{ route('admin.challenge.show', $ch->id_challenge) }}"
style="border:none;background:none;display:inline">
<img src="{{ asset('images/icon/main/search.png') }}" class="action-icon" alt="Detail">
</a>
<form action="{{ route('admin.challenge.destroy', $ch->id_challenge) }}"
method="POST" class="d-inline"
onsubmit="return confirm('Yakin hapus challenge ini?')">
@csrf @method('DELETE')
<button type="submit" style="border:none;background:none">
<img src="{{ asset('images/icon/main/del.png') }}" class="action-icon" alt="Hapus">
</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="7" class="text-muted py-4">Belum ada challenge.</td>
</tr>
@endforelse
</tbody>
</table>
<div class="d-flex justify-content-end">{{ $challenges->links() }}</div>
</div>
{{-- ============================================================ --}}
{{-- MODAL TAMBAH (2-step) --}}
{{-- ============================================================ --}}
<div class="modal fade" id="modalTambah" tabindex="-1">
<div class="modal-dialog modal-xl modal-dialog-centered modal-dialog-fixed">
<div class="modal-content">
{{-- Header kuning --}}
<div class="modal-header modal-header-yellow">
<h5 class="modal-title">🏆 Tambah Challenge Baru</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form action="{{ route('admin.challenge.store') }}" method="POST" id="formTambah">
@csrf
<div class="modal-body">
{{-- Step Indicator --}}
<div class="step-indicator" style="flex-shrink:0">
<div class="step-dot active" id="tambah-dot-1">1</div>
<span class="step-label active" id="tambah-label-1">Informasi Challenge</span>
<div class="step-line">
<div class="step-line-fill" id="tambah-line-fill"></div>
</div>
<div class="step-dot inactive" id="tambah-dot-2">2</div>
<span class="step-label inactive" id="tambah-label-2">Daftar Soal</span>
</div>
{{-- ─── STEP 1: Info Challenge ─── --}}
<div class="step-panel active" id="tambah-step-1">
<div class="section-divider">📋 Informasi Challenge</div>
<div class="row g-3 mb-2">
<div class="col-8">
<label>Judul Challenge <span class="text-danger">*</span></label>
<input type="text" name="judul_challenge" id="tambah-judul" class="form-control"
placeholder="Contoh: Challenge Matematika Minggu Ini">
</div>
<div class="col-4">
<label>Total EXP <span class="text-danger">*</span></label>
<input type="number" name="exp" id="tambah-exp" class="form-control" value="100" min="0">
</div>
</div>
<div class="mb-3">
<label>Deskripsi</label>
<textarea name="deskripsi" class="form-control" rows="2"
placeholder="Deskripsi singkat challenge (opsional)"></textarea>
</div>
<div class="row g-3 mb-3">
<div class="col-6">
<label>Tenggat Waktu <span class="text-danger">*</span></label>
<input type="datetime-local" name="tenggat_waktu" id="tambah-tenggat" class="form-control"
min="{{ now()->format('Y-m-d\TH:i') }}">
</div>
<div class="col-6">
<label>Badge Reward</label>
<select name="id_badge" class="form-control">
<option value="">-- Tanpa Badge --</option>
@foreach($badges as $badge)
<option value="{{ $badge->id_badge }}">{{ $badge->nama_badge }}</option>
@endforeach
</select>
</div>
</div>
<div class="section-divider">🏫 Target Kelas</div>
<div class="kelas-checkbox-grid mb-1">
@foreach($kelas as $k)
<label class="kelas-check-item">
<input type="checkbox" name="id_kelas[]" value="{{ $k->id_kelas }}">
<span style="font-size:13px;font-weight:600">{{ $k->tingkat }} {{ $k->nama_kelas }}</span>
</label>
@endforeach
</div>
</div>{{-- /step-1 --}}
{{-- ─── STEP 2: Soal ─── --}}
<div class="step-panel" id="tambah-step-2">
<div class="section-divider" style="flex-shrink:0">📝 Daftar Soal</div>
<div id="soalContainer" class="soal-scroll-area"></div>
<div style="flex-shrink:0;padding-top:8px">
<button type="button" class="btn-tambah-soal" onclick="tambahSoal('soalContainer')">
+ Tambah Soal
</button>
</div>
</div>{{-- /step-2 --}}
</div>{{-- /modal-body --}}
<div class="modal-footer" style="gap:8px">
{{-- Batal (step 1) / Kembali (step 2) --}}
<button type="button" class="btn btn-secondary" id="tambah-btn-back"
data-bs-dismiss="modal">Batal</button>
{{-- Next (step 1) --}}
<button type="button" class="btn-green" id="tambah-btn-next"
onclick="tambahGoStep2()">
Berikutnya
</button>
{{-- Submit (step 2, hidden awalnya) --}}
<button type="submit" class="btn-green" id="tambah-btn-submit" style="display:none">
💾 Simpan Challenge
</button>
</div>
</form>
</div>
</div>
</div>
{{-- ============================================================ --}}
{{-- MODAL EDIT --}}
{{-- ============================================================ --}}
<div class="modal fade" id="modalEdit" tabindex="-1">
<div class="modal-dialog modal-xl modal-dialog-centered modal-dialog-fixed">
<div class="modal-content">
<div class="modal-header modal-header-yellow">
<h5 class="modal-title">✏️ Edit Challenge</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form id="formEdit" method="POST">
@csrf @method('PUT')
<div class="modal-body">
{{-- Step Indicator --}}
<div class="step-indicator" style="flex-shrink:0">
<div class="step-dot active" id="edit-dot-1">1</div>
<span class="step-label active" id="edit-label-1">Informasi Challenge</span>
<div class="step-line">
<div class="step-line-fill" id="edit-line-fill"></div>
</div>
<div class="step-dot inactive" id="edit-dot-2">2</div>
<span class="step-label inactive" id="edit-label-2">Daftar Soal</span>
</div>
{{-- ─── STEP 1: Info Challenge ─── --}}
<div class="step-panel active" id="edit-step-1">
<div class="section-divider">📋 Informasi Challenge</div>
<div class="row g-3 mb-2">
<div class="col-8">
<label>Judul Challenge <span class="text-danger">*</span></label>
<input type="text" name="judul_challenge" id="editJudul" class="form-control" required>
</div>
<div class="col-4">
<label>Total EXP <span class="text-danger">*</span></label>
<input type="number" name="exp" id="editExp" class="form-control" min="0" required>
</div>
</div>
<div class="mb-3">
<label>Deskripsi</label>
<textarea name="deskripsi" id="editDeskripsi" class="form-control" rows="2"></textarea>
</div>
<div class="row g-3 mb-3">
<div class="col-6">
<label>Tenggat Waktu <span class="text-danger">*</span></label>
<input type="datetime-local" name="tenggat_waktu" id="editTenggat" class="form-control" required>
</div>
<div class="col-6">
<label>Badge Reward</label>
<select name="id_badge" id="editBadge" class="form-control">
<option value="">-- Tanpa Badge --</option>
@foreach($badges as $badge)
<option value="{{ $badge->id_badge }}">{{ $badge->nama_badge }}</option>
@endforeach
</select>
</div>
</div>
<div class="section-divider">🏫 Target Kelas</div>
<div class="kelas-checkbox-grid mb-1" id="editKelasContainer">
@foreach($kelas as $k)
<label class="kelas-check-item">
<input type="checkbox" name="id_kelas[]"
value="{{ $k->id_kelas }}" class="edit-kelas-check">
<span style="font-size:13px;font-weight:600">{{ $k->tingkat }} {{ $k->nama_kelas }}</span>
</label>
@endforeach
</div>
</div>{{-- /edit-step-1 --}}
{{-- ─── STEP 2: Soal ─── --}}
<div class="step-panel" id="edit-step-2">
<div class="section-divider" style="flex-shrink:0">📝 Daftar Soal</div>
<div id="editSoalContainer" class="soal-scroll-area"></div>
<div style="flex-shrink:0;padding-top:8px">
<button type="button" class="btn-tambah-soal" onclick="tambahSoal('editSoalContainer')">
+ Tambah Soal
</button>
</div>
</div>{{-- /edit-step-2 --}}
</div>{{-- /modal-body --}}
<div class="modal-footer" style="gap:8px">
<button type="button" class="btn btn-secondary" id="edit-btn-back"
data-bs-dismiss="modal">Batal</button>
<button type="button" class="btn-green" id="edit-btn-next"
onclick="editGoStep2()">
Berikutnya
</button>
<button type="submit" class="btn-green" id="edit-btn-submit" style="display:none">
💾 Update Challenge
</button>
</div>
</form>
</div>
</div>
</div>
<script>
let soalCountTambah = 0;
let soalCountEdit = 0;
/* ══════════════════════════════════════════════════
STEP NAVIGATION — MODAL TAMBAH
══════════════════════════════════════════════════ */
function tambahGoStep2() {
// Validasi step 1
const judul = document.getElementById('tambah-judul').value.trim();
const tenggat = document.getElementById('tambah-tenggat').value;
if (!judul) { alert('Judul challenge wajib diisi.'); return; }
if (!tenggat) { alert('Tenggat waktu wajib diisi.'); return; }
// Transisi
document.getElementById('tambah-step-1').classList.remove('active');
document.getElementById('tambah-step-2').classList.add('active');
// Indikator
document.getElementById('tambah-dot-1').classList.replace('active', 'done');
document.getElementById('tambah-dot-1').textContent = '✓';
document.getElementById('tambah-label-1').classList.replace('active', 'inactive');
document.getElementById('tambah-dot-2').classList.replace('inactive', 'active');
document.getElementById('tambah-label-2').classList.replace('inactive', 'active');
document.getElementById('tambah-line-fill').classList.add('filled');
// Footer buttons
document.getElementById('tambah-btn-next').style.display = 'none';
document.getElementById('tambah-btn-submit').style.display = '';
const backBtn = document.getElementById('tambah-btn-back');
backBtn.removeAttribute('data-bs-dismiss');
backBtn.textContent = '← Kembali';
backBtn.onclick = tambahGoStep1;
}
function tambahGoStep1() {
document.getElementById('tambah-step-2').classList.remove('active');
document.getElementById('tambah-step-1').classList.add('active');
document.getElementById('tambah-dot-1').classList.replace('done', 'active');
document.getElementById('tambah-dot-1').textContent = '1';
document.getElementById('tambah-label-1').classList.replace('inactive', 'active');
document.getElementById('tambah-dot-2').classList.replace('active', 'inactive');
document.getElementById('tambah-label-2').classList.replace('active', 'inactive');
document.getElementById('tambah-line-fill').classList.remove('filled');
document.getElementById('tambah-btn-next').style.display = '';
document.getElementById('tambah-btn-submit').style.display = 'none';
const backBtn = document.getElementById('tambah-btn-back');
backBtn.setAttribute('data-bs-dismiss', 'modal');
backBtn.textContent = 'Batal';
backBtn.onclick = null;
}
/* ══════════════════════════════════════════════════
STEP NAVIGATION — MODAL EDIT
══════════════════════════════════════════════════ */
function editGoStep2() {
const judul = document.getElementById('editJudul').value.trim();
const tenggat = document.getElementById('editTenggat').value;
if (!judul) { alert('Judul challenge wajib diisi.'); return; }
if (!tenggat) { alert('Tenggat waktu wajib diisi.'); return; }
document.getElementById('edit-step-1').classList.remove('active');
document.getElementById('edit-step-2').classList.add('active');
document.getElementById('edit-dot-1').classList.replace('active', 'done');
document.getElementById('edit-dot-1').textContent = '✓';
document.getElementById('edit-label-1').classList.replace('active', 'inactive');
document.getElementById('edit-dot-2').classList.replace('inactive', 'active');
document.getElementById('edit-label-2').classList.replace('inactive', 'active');
document.getElementById('edit-line-fill').classList.add('filled');
document.getElementById('edit-btn-next').style.display = 'none';
document.getElementById('edit-btn-submit').style.display = '';
const backBtn = document.getElementById('edit-btn-back');
backBtn.removeAttribute('data-bs-dismiss');
backBtn.textContent = '← Kembali';
backBtn.onclick = editGoStep1;
}
function editGoStep1() {
document.getElementById('edit-step-2').classList.remove('active');
document.getElementById('edit-step-1').classList.add('active');
document.getElementById('edit-dot-1').classList.replace('done', 'active');
document.getElementById('edit-dot-1').textContent = '1';
document.getElementById('edit-label-1').classList.replace('inactive', 'active');
document.getElementById('edit-dot-2').classList.replace('active', 'inactive');
document.getElementById('edit-label-2').classList.replace('active', 'inactive');
document.getElementById('edit-line-fill').classList.remove('filled');
document.getElementById('edit-btn-next').style.display = '';
document.getElementById('edit-btn-submit').style.display = 'none';
const backBtn = document.getElementById('edit-btn-back');
backBtn.setAttribute('data-bs-dismiss', 'modal');
backBtn.textContent = 'Batal';
backBtn.onclick = null;
}
/* ══════════════════════════════════════════════════
EXP HELPER — hitung & sebar exp_per_soal otomatis
══════════════════════════════════════════════════ */
function getExpInput(containerId) {
// Tambah → pakai #tambah-exp | Edit → pakai #editExp
return containerId === 'editSoalContainer'
? document.getElementById('editExp')
: document.getElementById('tambah-exp');
}
function recalcExp(containerId) {
const expInput = getExpInput(containerId);
const totalExp = parseInt(expInput ? expInput.value : 0) || 0;
const cards = document.querySelectorAll(`#${containerId} .soal-card`);
const jumlah = cards.length;
if (jumlah === 0) return;
// Bagi rata, sisa EXP diberikan ke soal terakhir
const base = Math.floor(totalExp / jumlah);
const remainder = totalExp - base * jumlah;
cards.forEach((card, i) => {
const hiddenInput = card.querySelector('input[name="exp_per_soal[]"]');
const displaySpan = card.querySelector('.exp-per-soal-display');
const val = base + (i === jumlah - 1 ? remainder : 0);
if (hiddenInput) hiddenInput.value = val;
if (displaySpan) displaySpan.textContent = val + ' EXP';
});
}
/* ══════════════════════════════════════════════════
SOAL HELPERS
══════════════════════════════════════════════════ */
function tambahSoal(containerId, data = null) {
const isEdit = containerId === 'editSoalContainer';
const count = isEdit ? ++soalCountEdit : ++soalCountTambah;
const container = document.getElementById(containerId);
const card = document.createElement('div');
card.className = 'soal-card';
card.innerHTML = `
<span class="soal-number">Soal ${count}</span>
<button type="button" class="btn-hapus-soal" onclick="hapusSoal(this, '${containerId}')">✕</button>
<div class="mb-2" style="margin-top:10px">
<label>Pertanyaan <span class="text-danger">*</span></label>
<textarea name="pertanyaan[]" class="form-control" rows="2"
placeholder="Tuliskan pertanyaan..." required>${data ? data.pertanyaan : ''}</textarea>
</div>
<div class="opsi-grid">
<div class="opsi-item">
<label>Opsi A</label>
<input type="text" name="opsi_a[]" class="form-control"
placeholder="Opsi A" required value="${data ? data.opsi_a : ''}">
</div>
<div class="opsi-item">
<label>Opsi B</label>
<input type="text" name="opsi_b[]" class="form-control"
placeholder="Opsi B" required value="${data ? data.opsi_b : ''}">
</div>
<div class="opsi-item">
<label>Opsi C</label>
<input type="text" name="opsi_c[]" class="form-control"
placeholder="Opsi C" required value="${data ? data.opsi_c : ''}">
</div>
<div class="opsi-item">
<label>Opsi D</label>
<input type="text" name="opsi_d[]" class="form-control"
placeholder="Opsi D" required value="${data ? data.opsi_d : ''}">
</div>
</div>
<div class="jawaban-row">
<label>Jawaban Benar</label>
<select name="jawaban_benar[]" class="form-select" style="width:100px" required>
${['A','B','C','D'].map(o =>
`<option value="${o}" ${data && data.jawaban_benar === o ? 'selected' : ''}>${o}</option>`
).join('')}
</select>
{{-- Hidden — nilainya diisi recalcExp() --}}
<input type="hidden" name="exp_per_soal[]" value="0">
<span style="margin-left:auto;background:#f0fdf4;color:#16a34a;font-size:12px;font-weight:700;
padding:4px 12px;border-radius:99px;white-space:nowrap">
⚡ <span class="exp-per-soal-display">0 EXP</span>
</span>
</div>
`;
container.appendChild(card);
renumberSoal(containerId);
recalcExp(containerId); // update semua kartu setelah soal baru ditambah
// Scroll ke soal baru
const scrollArea = container.closest('.soal-scroll-area') || container;
scrollArea.scrollTop = scrollArea.scrollHeight;
}
function hapusSoal(btn, containerId) {
const container = document.getElementById(containerId);
if (container.querySelectorAll('.soal-card').length <= 1) {
alert('Minimal harus ada 1 soal.');
return;
}
btn.closest('.soal-card').remove();
renumberSoal(containerId);
recalcExp(containerId); // update ulang setelah soal dihapus
}
function renumberSoal(containerId) {
document.querySelectorAll(`#${containerId} .soal-card`).forEach((card, i) => {
const num = card.querySelector('.soal-number');
if (num) num.textContent = `Soal ${i + 1}`;
});
}
/* ══════════════════════════════════════════════════
OPEN MODAL TAMBAH
══════════════════════════════════════════════════ */
function openTambahModal() {
// Reset state
soalCountTambah = 0;
document.getElementById('soalContainer').innerHTML = '';
document.getElementById('formTambah').reset();
// Paksa step 1
tambahGoStep1();
// Default 1 soal
tambahSoal('soalContainer');
// Recalc EXP otomatis setiap kali angka EXP di step-1 berubah
const expElTambah = document.getElementById('tambah-exp');
expElTambah.oninput = () => recalcExp('soalContainer');
new bootstrap.Modal(document.getElementById('modalTambah')).show();
}
/* ══════════════════════════════════════════════════
OPEN MODAL EDIT
══════════════════════════════════════════════════ */
async function openEditModal(idChallenge) {
soalCountEdit = 0;
document.getElementById('editSoalContainer').innerHTML = '';
// Reset ke step 1
editGoStep1();
const res = await fetch(`/admin/challenge/${idChallenge}/edit-data`);
const data = await res.json();
document.getElementById('formEdit').action = `/admin/challenge/${idChallenge}`;
document.getElementById('editJudul').value = data.judul_challenge;
document.getElementById('editExp').value = data.exp;
document.getElementById('editDeskripsi').value = data.deskripsi ?? '';
document.getElementById('editBadge').value = data.id_badge ?? '';
const dt = new Date(data.tenggat_waktu);
const pad = n => String(n).padStart(2, '0');
document.getElementById('editTenggat').value =
`${dt.getFullYear()}-${pad(dt.getMonth()+1)}-${pad(dt.getDate())}T${pad(dt.getHours())}:${pad(dt.getMinutes())}`;
document.querySelectorAll('.edit-kelas-check').forEach(cb => {
cb.checked = data.kelas.includes(parseInt(cb.value));
});
data.soal.forEach(s => tambahSoal('editSoalContainer', s));
// Recalc EXP otomatis setiap kali angka EXP di modal edit berubah
const expElEdit = document.getElementById('editExp');
expElEdit.oninput = () => recalcExp('editSoalContainer');
// Recalc sekali saat modal dibuka
recalcExp('editSoalContainer');
new bootstrap.Modal(document.getElementById('modalEdit')).show();
}
</script>
@endsection