{% extends 'base.html' %}

{% block title %}Daftar Hadir Perkuliahan{% endblock %}
{% block page_title %}Daftar Hadir Perkuliahan{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">
            <i class="fas fa-plus me-2"></i>Input Kehadiran
        </button>
    </div>
    <form method="POST" id="massDeleteForm" onsubmit="return confirm('Hapus terpilih?')">
        {% 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>Tanggal</th>
                        <th>Pertemuan</th>
                        <th>Jadwal / Matkul</th>
                        <th>Dosen</th>
                        <th>Keterangan</th>
                        <th width="120" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    {% for hadir in hadir_all %}
                    <tr>
                        <td>
                            <input type="checkbox" name="selected_ids" value="{{ hadir.id }}" form="massDeleteForm" class="form-check-input item-checkbox">
                        </td>
                        <td>{{ hadir.tanggal|date:"d/m/Y" }}</td>
                        <td>Ke-{{ hadir.pertemuan_ke }}</td>
                        <td>
                            <div class="fw-bold">{{ hadir.jadwal.dosen_pengampu.matkul.nama }}</div>
                            <small class="text-muted">{{ hadir.jadwal.hari.nama }}, {{ hadir.jadwal.jam.range_waktu }}</small>
                        </td>
                        <td>{{ hadir.jadwal.dosen_pengampu.dosen.nama }}</td>
                        <td>{{ hadir.keterangan|default:"-" }}</td>
                        <td class="text-center">
                            <button class="btn btn-sm btn-info text-white me-1 edit-btn" 
                                    data-pk="{{ hadir.id }}" 
                                    data-jadwal="{{ hadir.jadwal.id }}"
                                    data-tanggal="{{ hadir.tanggal|date:'Y-m-d' }}"
                                    data-pertemuan="{{ hadir.pertemuan_ke }}"
                                    data-ket="{{ hadir.keterangan|default:'' }}"
                                    data-bs-toggle="modal" data-bs-target="#editModal">
                                <i class="fas fa-edit"></i>
                            </button>
                            <a href="{% url 'daftar_hadir_delete' hadir.id %}" class="btn btn-sm btn-outline-danger" onclick="return confirm('Hapus data ini?')">
                                <i class="fas fa-trash"></i>
                            </a>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="7" class="text-center py-4 text-muted">Belum ada data kehadiran.</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Add Modal -->
<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">Input Kehadiran</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">Pilih Jadwal</label>
                    <select name="jadwal" class="form-select select2-modal" required>
                        <option value="">Cari Jadwal...</option>
                        {% for j in jadwal_all %}
                        <option value="{{ j.id }}">
                            {{ j.dosen_pengampu.matkul.nama }} - {{ j.kelas.nama }} ({{ j.hari.nama }} {{ j.jam.range_waktu }})
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Tanggal</label>
                        <input type="date" name="tanggal" class="form-control" value="{% now 'Y-m-d' %}" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Pertemuan Ke-</label>
                        <input type="number" name="pertemuan_ke" class="form-control" value="1" required>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">Keterangan</label>
                    <textarea name="keterangan" class="form-control" rows="2"></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-primary">Simpan</button>
            </div>
        </form>
    </div>
</div>

<!-- Edit Modal -->
<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 Kehadiran</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">Jadwal</label>
                    <select name="jadwal" id="edit_jadwal" class="form-select select2-modal" required>
                        {% for j in jadwal_all %}
                        <option value="{{ j.id }}">{{ j.dosen_pengampu.matkul.nama }} - {{ j.kelas.nama }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Tanggal</label>
                        <input type="date" name="tanggal" id="edit_tanggal" class="form-control" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Pertemuan Ke-</label>
                        <input type="number" name="pertemuan_ke" id="edit_pertemuan" class="form-control" required>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">Keterangan</label>
                    <textarea name="keterangan" id="edit_ket" class="form-control" rows="2"></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-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_jadwal').val($(this).data('jadwal')).trigger('change');
            $('#edit_tanggal').val($(this).data('tanggal'));
            $('#edit_pertemuan').val($(this).data('pertemuan'));
            $('#edit_ket').val($(this).data('ket'));
        });
    });
</script>
{% endblock %}
