@extends('admin.layouts.app')

@section('title', 'Daftar Kelas')

@section('content')

<style>
    .page-title {
        font-size: 30px;
        font-weight: 800;
        margin-bottom: 10px;
        margin-top: -20px;
    }

    .custom-card {
        background: white;
        border-radius: 20px;
        border: 2px solid #e5e5e5;
        padding: 25px;
    }

    .btn-primary-custom {
        background: #2b8ef3;
        color: white;
        border-radius: 10px;
        padding: 8px 18px;
        border: none;
        display: inline-flex;
        align-items: center;
        gap: 8px;
        font-size: 14px;
        text-decoration: none;
    }

    .table-header {
        background: #a5e6ba;
    }

    .search-box {
        background: #a5e6ba;
        border-radius: 30px;
        padding: 6px 15px;
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .search-box input {
        border: none;
        outline: none;
        background: transparent;
        width: 150px;
    }

    .action-icon {
        width: 20px;
        cursor: pointer;
        margin: 0 5px;
    }

    .per-page-select {
        border-radius: 10px;
        padding: 5px;
        border: 1px solid #ccc;
    }

    .modal {
    left: auto !important;
    right: 0;
    width: calc(100% - 250px);
}

.modal-backdrop {
    left: auto !important;
    right: 0;
    width: calc(100% - 250px);
}

.modal-dialog {
    margin-right: auto;
    margin-left: auto;
}

.modal-content {
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.modal-header-pastel {
    background: #FFD97D !important;
    color: black !important;
    border-bottom: none;
    padding: 15px 20px;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
}


.btn-save-pastel {
    background: #2b8ef3;
    color: white;
    border-radius: 10px;
    padding: 8px 18px;
    border: none;
}

</style>

<h3 class="page-title">DAFTAR KELAS</h3>

<div class="custom-card">

    <div class="d-flex justify-content-between align-items-center mb-3">

        <button class="btn-primary-custom" data-bs-toggle="modal" data-bs-target="#modalTambah">
            <img src="{{ asset('images/icon/main/add.png') }}" width="18">
            Tambah Data
        </button>

        <form method="GET">
            <div class="search-box">
                <input type="text" name="search" placeholder="Cari" value="{{ request('search') }}">
                <button style="border:none;background:none">
                    <img src="{{ asset('images/icon/main/search.png') }}" width="18">
                </button>
            </div>
        </form>

    </div>

    <form method="GET" class="mb-2">
        <span>Tampilkan</span>

        <select name="perPage" onchange="this.form.submit()" class="per-page-select">
            <option value="10" {{ request('perPage') == 10 ? 'selected' : '' }}>10</option>
            <option value="25" {{ request('perPage') == 25 ? 'selected' : '' }}>25</option>
            <option value="50" {{ request('perPage') == 50 ? 'selected' : '' }}>50</option>
        </select>

        <span>data</span>

        <input type="hidden" name="search" value="{{ request('search') }}">
    </form>

    <table class="table text-center align-middle">
        <thead class="table-header">
            <tr>
                <th>No</th>
                <th>ID Kelas</th>
                <th>Nama Kelas</th>
                <th>Tingkat</th>
                <th>Aksi</th>
            </tr>
        </thead>

        <tbody>
            @forelse($kelass as $index => $kelas)
            <tr>
                <td>{{ $kelass->firstItem() + $index }}</td>
                <td>{{ $kelas->id_kelas }}</td>
                <td>{{ $kelas->nama_kelas }}</td>
                <td>{{ $kelas->tingkat }}</td>

                <td>
                    <button onclick="openEditModal('{{ $kelas->id_kelas }}', '{{ $kelas->nama_kelas }}', '{{ $kelas->tingkat }}')"
                            style="border:none;background:none">
                        <img src="{{ asset('images/icon/main/edit.png') }}" class="action-icon">
                    </button>

                    <form action="{{ route('admin.kelas.destroy', $kelas->id_kelas) }}" 
                          method="POST" class="d-inline"
                          onsubmit="return confirm('Yakin ingin menghapus data?')">
                        @csrf
                        @method('DELETE')
                        <button type="submit" style="border:none;background:none">
                            <img src="{{ asset('images/icon/main/del.png') }}" class="action-icon">
                        </button>
                    </form>
                </td>
            </tr>

            @empty
            <tr>
                <td colspan="5">Belum ada data kelas</td>
            </tr>
            @endforelse
        </tbody>
    </table>

    <div class="d-flex justify-content-end">
        {{ $kelass->links() }}
    </div>

</div>

{{-- MODAL TAMBAH --}}
<div class="modal fade" id="modalTambah">
  <div class="modal-dialog modal-lg modal-dialog-centered">
    <div class="modal-content">

      <div class="modal-header modal-header-pastel">
    <h5 class="modal-title w-100">Tambah Data Kelas</h5>
    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>


      <form action="{{ route('admin.kelas.store') }}" method="POST">
        @csrf

        <div class="modal-body">

          <div class="mb-3">
            <label>Nama Kelas</label>
            <input type="text" id="editNama" name="nama_kelas" class="form-control" required>

@error('nama_kelas')
    <small class="text-danger">{{ $message }}</small>
@enderror

          </div>

          <div class="mb-3">
            <label>Tingkat</label>
            <select name="tingkat" class="form-control">
                <option>X</option>
                <option>XI</option>
                <option>XII</option>
            </select>
          </div>

        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button class="btn btn-success">Simpan Data</button>
        </div>

      </form>

    </div>
  </div>
</div>

{{-- MODAL EDIT --}}
<div class="modal fade" id="modalEdit">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">

      <div class="modal-header modal-header-pastel">
    <h5 class="modal-title w-100">Edit Data Kelas</h5>
    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>


      <form id="formEdit" method="POST">
        @csrf
        @method('PUT')

        <div class="modal-body">

          <div class="mb-3">
            <label>ID Kelas</label>
            <input type="text" id="editId" class="form-control" disabled>
          </div>

          <div class="mb-3">
            <label>Nama Kelas</label>
            <input type="text" name="nama_kelas" class="form-control" required>

@error('nama_kelas')
    <small class="text-danger">{{ $message }}</small>
@enderror
          </div>

          <div class="mb-3">
            <label>Tingkat</label>
            <select name="tingkat" id="editTingkat" class="form-control">
                <option>X</option>
                <option>XI</option>
                <option>XII</option>
            </select>
          </div>

        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button class="btn btn-success">Update</button>
        </div>

      </form>

    </div>
  </div>
</div>

<script>
function openEditModal(id, nama, tingkat) {
    document.getElementById('editId').value = id;
    document.getElementById('editNama').value = nama;
    document.getElementById('editTingkat').value = tingkat;

    document.getElementById('formEdit').action = "{{ url('admin/kelas') }}/" + id;

    new bootstrap.Modal(document.getElementById('modalEdit')).show();
}
</script>

@if ($errors->any())
<script>
    document.addEventListener("DOMContentLoaded", function() {

        @if(session('error_from') == 'edit')
            var modal = new bootstrap.Modal(document.getElementById('modalEdit'));
        @else
            var modal = new bootstrap.Modal(document.getElementById('modalTambah'));
        @endif

        modal.show();
    });
</script>
@endif


@endsection
