{% extends 'base.html' %}

{% block title %}Daftar Waktu / Jam{% endblock %}
{% block page_title %}Daftar Waktu / Jam{% 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 Jam
        </button>
        <button class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#generateModal">
            <i class="fas fa-magic me-2"></i>Generate Otomatis
        </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>Range Waktu</th>
                        <th width="120" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    {% for jam in jam_all %}
                    <tr>
                        <td>
                            <input type="checkbox" name="selected_ids" value="{{ jam.id }}" form="massDeleteForm" class="form-check-input item-checkbox">
                        </td>
                        <td>{{ jam.range_waktu }}</td>
                        <td class="text-center">
                            <button class="btn btn-sm btn-info text-white me-1 edit-btn" 
                                    data-pk="{{ jam.id }}" 
                                    data-range="{{ jam.range_waktu }}"
                                    data-bs-toggle="modal" data-bs-target="#editModal">
                                <i class="fas fa-edit"></i>
                            </button>
                            <a href="{% url 'jam_delete' jam.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="3" class="text-center py-4 text-muted">Belum ada data jam.</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 Jam Manual</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">Range Waktu</label>
                    <input type="text" name="range_waktu" class="form-control" placeholder="Contoh: 07:00 - 08:40" 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-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 Jam</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">Range Waktu</label>
                    <input type="text" name="range_waktu" id="edit_range" class="form-control" 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-primary">Update</button>
            </div>
        </form>
    </div>
</div>

<!-- Generate Modal -->
<div class="modal fade" id="generateModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" action="{% url 'jam_generate' %}" class="modal-content">
            {% csrf_token %}
            <div class="modal-header">
                <h5 class="modal-title">Generate Jam Otomatis</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Jam Mulai</label>
                        <input type="time" name="jam_mulai" class="form-control" value="07:00" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Jam Selesai</label>
                        <input type="time" name="jam_selesai" class="form-control" value="17:00" required>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Menit Per SKS</label>
                        <input type="number" name="durasi_per_sks" class="form-control" value="50" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">SKS Per Slot</label>
                        <input type="number" name="jumlah_sks" class="form-control" value="2" required>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">Waktu Jeda (menit)</label>
                    <input type="number" name="jeda" class="form-control" value="0" placeholder="Contoh: 15" required>
                </div>
                <div class="alert alert-info py-2 small mb-0">
                    <i class="fas fa-info-circle me-1"></i> Slot akan dibuat berurutan dengan jeda antar slot.
                </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-warning">Generate Now</button>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    $(document).ready(function() {
        $('.edit-btn').on('click', function() {
            $('#edit_pk').val($(this).data('pk'));
            $('#edit_range').val($(this).data('range'));
        });
    });
</script>
{% endblock %}
