{% extends 'base.html' %}

{% block title %}Daftar Mata Kuliah{% endblock %}
{% block page_title %}Daftar Mata Kuliah{% 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>Tambah Matkul
        </button>
        <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#importModal">
            <i class="fas fa-file-import me-2"></i>Import
        </button>
        <a href="{% url 'matkul_template' %}" class="btn btn-outline-secondary">
            <i class="fas fa-download me-2"></i>Template
        </a>
    </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>Kode</th>
                        <th>Nama Matkul</th>
                        <th>SKS</th>
                        <th>Semester</th>
                        <th>Prodi</th>
                        <th width="120" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    {% for matkul in matkul_all %}
                    <tr>
                        <td>
                            <input type="checkbox" name="selected_ids" value="{{ matkul.id }}" form="massDeleteForm" class="form-check-input item-checkbox">
                        </td>
                        <td>{{ matkul.kode }}</td>
                        <td>{{ matkul.nama }}</td>
                        <td>{{ matkul.sks }}</td>
                        <td>{{ matkul.semester }}</td>
                        <td>{{ matkul.prodi.nama }}</td>
                        <td class="text-center">
                            <button class="btn btn-sm btn-info text-white me-1 edit-btn" 
                                    data-pk="{{ matkul.id }}" 
                                    data-kode="{{ matkul.kode }}" 
                                    data-nama="{{ matkul.nama }}"
                                    data-sks="{{ matkul.sks }}"
                                    data-semester="{{ matkul.semester }}"
                                    data-prodi="{{ matkul.prodi.id }}"
                                    data-bs-toggle="modal" data-bs-target="#editModal">
                                <i class="fas fa-edit"></i>
                            </button>
                            <a href="{% url 'matkul_delete' matkul.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 mata kuliah.</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">Tambah Mata Kuliah</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">Kode Matkul</label>
                    <input type="text" name="kode" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Nama Matkul</label>
                    <input type="text" name="nama" class="form-control" required>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">SKS</label>
                        <input type="number" name="sks" class="form-control" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Semester</label>
                        <input type="number" name="semester" class="form-control" required>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">Program Studi</label>
                    <select name="prodi" class="form-select select2-modal" required>
                        <option value="">Pilih Prodi...</option>
                        {% for prodi in prodi_all %}
                        <option value="{{ prodi.id }}">{{ prodi.nama }}</option>
                        {% endfor %}
                    </select>
                </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 Mata Kuliah</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">Kode Matkul</label>
                    <input type="text" name="kode" id="edit_kode" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Nama Matkul</label>
                    <input type="text" name="nama" id="edit_nama" class="form-control" required>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">SKS</label>
                        <input type="number" name="sks" id="edit_sks" class="form-control" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Semester</label>
                        <input type="number" name="semester" id="edit_semester" class="form-control" required>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">Program Studi</label>
                    <select name="prodi" id="edit_prodi" class="form-select select2-modal" required>
                        <option value="">Pilih Prodi...</option>
                        {% for prodi in prodi_all %}
                        <option value="{{ prodi.id }}">{{ prodi.nama }}</option>
                        {% endfor %}
                    </select>
                </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>

<!-- Import Modal -->
<div class="modal fade" id="importModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" action="{% url 'matkul_import' %}" enctype="multipart/form-data" class="modal-content">
            {% csrf_token %}
            <div class="modal-header">
                <h5 class="modal-title">Import Mata Kuliah</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">File CSV</label>
                    <input type="file" name="csv_file" class="form-control" accept=".csv" required>
                </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">Import</button>
            </div>
        </form>
    </div>
</div>

{% endblock %}

{% block scripts %}
<script>
    $(document).ready(function() {
        $('.edit-btn').on('click', function() {
            $('#edit_pk').val($(this).data('pk'));
            $('#edit_kode').val($(this).data('kode'));
            $('#edit_nama').val($(this).data('nama'));
            $('#edit_sks').val($(this).data('sks'));
            $('#edit_semester').val($(this).data('semester'));
            $('#edit_prodi').val($(this).data('prodi')).trigger('change');
        });
    });
</script>
{% endblock %}