{% extends 'base.html' %}
{% block title %}Tahun Akademik{% endblock %}
{% block page_title %}Tahun Akademik{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal"><i class="fas fa-plus me-2"></i>Tambah Tahun</button>
    <form method="POST" id="massDeleteForm">{% csrf_token %}<button type="submit" name="delete_massal" class="btn btn-danger mass-delete-btn" id="massDeleteBtn"><i class="fas fa-trash me-2"></i>Hapus Terpilih</button></form>
</div>
<div class="card border-0 shadow-sm"><div class="card-body"><div class="table-responsive"><table class="table table-hover align-middle mb-0"><thead class="bg-light"><tr><th width="40"><input type="checkbox" id="checkAll" class="form-check-input"></th><th>Tahun Akademik</th><th>Status</th><th width="120" class="text-center">Aksi</th></tr></thead><tbody>
{% for t in tahun_all %}
<tr>
    <td><input type="checkbox" name="selected_ids" value="{{ t.id }}" form="massDeleteForm" class="form-check-input item-checkbox"></td>
    <td>{{ t.nama }}</td>
    <td>{% if t.is_active %}<span class="badge bg-success">Aktif</span>{% else %}<span class="badge bg-secondary">Non-Aktif</span>{% endif %}</td>
    <td class="text-center">
        <button class="btn btn-sm btn-info text-white edit-btn" data-pk="{{ t.id }}" data-nama="{{ t.nama }}" data-active="{{ t.is_active|yesno:'on,off' }}" data-bs-toggle="modal" data-bs-target="#editModal"><i class="fas fa-edit"></i></button>
        <a href="{% url 'tahun_delete' t.id %}" class="btn btn-sm btn-outline-danger" onclick="return confirm('Hapus?')"><i class="fas fa-trash"></i></a>
    </td>
</tr>
{% endfor %}
</tbody></table></div></div></div>
<div class="modal fade" id="addModal" tabindex="-1"><div class="modal-dialog"><form method="POST" class="modal-content">{% csrf_token %}<div class="modal-header"><h5 class="modal-title">Tambah Tahun Akademik</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><div class="mb-3"><label class="form-label">Nama Tahun</label><input type="text" name="nama" class="form-control" placeholder="2025/2026" required></div><div class="form-check form-switch"><input class="form-check-input" type="checkbox" name="is_active" id="add_active"><label class="form-check-label" for="add_active">Set Aktif</label></div></div><div class="modal-footer"><button type="submit" class="btn btn-primary">Simpan</button></div></form></div></div>
<div class="modal fade" id="editModal" tabindex="-1"><div class="modal-dialog"><form method="POST" class="modal-content">{% csrf_token %}<input type="hidden" name="pk" id="edit_pk"><div class="modal-header"><h5 class="modal-title">Edit Tahun Akademik</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><div class="mb-3"><label class="form-label">Nama Tahun</label><input type="text" name="nama" id="edit_nama" class="form-control" required></div><div class="form-check form-switch"><input class="form-check-input" type="checkbox" name="is_active" id="edit_active"><label class="form-check-label" for="edit_active">Set Aktif</label></div></div><div class="modal-footer"><button type="submit" class="btn btn-primary">Update</button></div></form></div></div>
{% endblock %}
{% block scripts %}
<script>$(document).ready(function(){$('.edit-btn').on('click',function(){$('#edit_pk').val($(this).data('pk'));$('#edit_nama').val($(this).data('nama'));$('#edit_active').prop('checked',$(this).data('active')==='on');});});</script>
{% endblock %}