{% extends 'base.html' %}

{% block title %}Daftar Ruangan{% endblock %}
{% block page_title %}Daftar Ruangan{% 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 Ruangan
        </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 'ruangan_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>Nama Ruangan</th>
                        <th>Kapasitas</th>
                        <th>Program Studi</th>
                        <th width="120" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    {% for ruangan in ruangan_all %}
                    <tr>
                        <td>
                            <input type="checkbox" name="selected_ids" value="{{ ruangan.id }}" form="massDeleteForm" class="form-check-input item-checkbox">
                        </td>
                        <td>{{ ruangan.nama }}</td>
                        <td>{{ ruangan.kapasitas }} Kursi</td>
                        <td>{{ ruangan.prodi.nama|default:"-" }}</td>
                        <td class="text-center">
                            <button class="btn btn-sm btn-info text-white me-1 edit-btn" 
                                    data-pk="{{ ruangan.id }}" 
                                    data-nama="{{ ruangan.nama }}"
                                    data-kapasitas="{{ ruangan.kapasitas }}"
                                    data-prodi="{{ ruangan.prodi.id|default:'' }}"
                                    data-bs-toggle="modal" data-bs-target="#editModal">
                                <i class="fas fa-edit"></i>
                            </button>
                            <a href="{% url 'ruangan_delete' ruangan.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="5" class="text-center py-4 text-muted">Belum ada data ruangan.</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 Ruangan</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 Ruangan</label>
                    <input type="text" name="nama" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Kapasitas</label>
                    <input type="number" name="kapasitas" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Program Studi</label>
                    <select name="prodi" class="form-select select2-modal">
                        <option value="">Umum (Semua 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 Ruangan</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 Ruangan</label>
                    <input type="text" name="nama" id="edit_nama" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Kapasitas</label>
                    <input type="number" name="kapasitas" id="edit_kapasitas" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Program Studi</label>
                    <select name="prodi" id="edit_prodi" class="form-select select2-modal">
                        <option value="">Umum (Semua 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 'ruangan_import' %}" enctype="multipart/form-data" class="modal-content">
            {% csrf_token %}
            <div class="modal-header">
                <h5 class="modal-title">Import Ruangan</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_nama').val($(this).data('nama'));
            $('#edit_kapasitas').val($(this).data('kapasitas'));
            $('#edit_prodi').val($(this).data('prodi')).trigger('change');
        });
    });
</script>
{% endblock %}
