
@extends('admin.layouts.app')

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

@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, .filter-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 SISWA</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>

        <span class="ms-3">Filter Kelas</span>

        <select name="filter_kelas" onchange="this.form.submit()" class="filter-select">
            <option value="">Semua Kelas</option>
            @foreach($kelass as $kelas)
                <option value="{{ $kelas->id_kelas }}" 
                    {{ request('filter_kelas') == $kelas->id_kelas ? 'selected' : '' }}>
                    {{ $kelas->tingkat }} - {{ $kelas->nama_kelas }}
                </option>
            @endforeach
        </select>

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

    {{-- Alert Success --}}
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <table class="table text-center align-middle">
        <thead class="table-header">
            <tr>
                <th>No</th>
                <th>NISN</th>
                <th>Nama</th>
                <th>Tempat Lahir</th>
                <th>Tanggal Lahir</th>
                <th>Kelas</th>
                <th>Aksi</th>
            </tr>
        </thead>

        <tbody>
            @forelse($siswas as $index => $siswa)
            <tr>
                <td>{{ $siswas->firstItem() + $index }}</td>
                <td>{{ $siswa->nisn }}</td>
                <td>{{ $siswa->nama }}</td>
                <td>{{ $siswa->tempat_lahir }}</td>
                <td>{{ \Carbon\Carbon::parse($siswa->tanggal_lahir)->format('d M Y') }}</td>
                <td>{{ $siswa->kelas->tingkat }} - {{ $siswa->kelas->nama_kelas }}</td>

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

                    <form action="{{ route('admin.siswa.destroy', $siswa->id_siswa) }}" 
                          method="POST" class="d-inline"
                          onsubmit="return confirm('Yakin ingin menghapus data siswa {{ $siswa->nama }}?')">
                        @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="7">Belum ada data siswa</td>
            </tr>
            @endforelse
        </tbody>
    </table>

    <div class="d-flex justify-content-end">
        {{ $siswas->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 Siswa</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

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

        <div class="modal-body">

          <div class="mb-3">
            <label>NISN <span class="text-danger">*</span></label>
            <input type="text" name="nisn" class="form-control @error('nisn') is-invalid @enderror" 
                   value="{{ old('nisn') }}" required>
            @error('nisn')
                <small class="text-danger">{{ $message }}</small>
            @enderror
          </div>

          <div class="mb-3">
            <label>Nama Lengkap <span class="text-danger">*</span></label>
            <input type="text" name="nama" class="form-control @error('nama') is-invalid @enderror" 
                   value="{{ old('nama') }}" required>
            @error('nama')
                <small class="text-danger">{{ $message }}</small>
            @enderror
          </div>

          <div class="row">
            <div class="col-md-6 mb-3">
              <label>Tempat Lahir <span class="text-danger">*</span></label>
              <input type="text" name="tempat_lahir" class="form-control @error('tempat_lahir') is-invalid @enderror" 
                     value="{{ old('tempat_lahir') }}" required>
              @error('tempat_lahir')
                  <small class="text-danger">{{ $message }}</small>
              @enderror
            </div>

            <div class="col-md-6 mb-3">
              <label>Tanggal Lahir <span class="text-danger">*</span></label>
              <input type="date" name="tanggal_lahir" class="form-control @error('tanggal_lahir') is-invalid @enderror" 
                     value="{{ old('tanggal_lahir') }}" required>
              @error('tanggal_lahir')
                  <small class="text-danger">{{ $message }}</small>
              @enderror
            </div>
          </div>

          <div class="mb-3">
            <label>Kelas <span class="text-danger">*</span></label>
            <select name="id_kelas" class="form-control @error('id_kelas') is-invalid @enderror" required>
                <option value="">Pilih Kelas</option>
                @foreach($kelass as $kelas)
                    <option value="{{ $kelas->id_kelas }}" {{ old('id_kelas') == $kelas->id_kelas ? 'selected' : '' }}>
                        {{ $kelas->tingkat }} - {{ $kelas->nama_kelas }}
                    </option>
                @endforeach
            </select>
            @error('id_kelas')
                <small class="text-danger">{{ $message }}</small>
            @enderror
          </div>

          <div class="mb-3">
            <label>Password <span class="text-danger">*</span></label>
            <input type="password" name="password" class="form-control @error('password') is-invalid @enderror" required>
            @error('password')
                <small class="text-danger">{{ $message }}</small>
            @enderror
            <small class="text-muted">Minimal 6 karakter</small>
          </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-lg modal-dialog-centered">
    <div class="modal-content">

      <div class="modal-header modal-header-pastel">
        <h5 class="modal-title w-100">Edit Data Siswa</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>NISN</label>
            <input type="text" id="editNisn" class="form-control bg-light" disabled>
            <small class="text-muted">NISN tidak dapat diubah</small>
          </div>

          <div class="mb-3">
            <label>Nama Lengkap <span class="text-danger">*</span></label>
            <input type="text" id="editNama" name="nama" class="form-control" required>
            @error('nama')
                <small class="text-danger">{{ $message }}</small>
            @enderror
          </div>

          <div class="row">
            <div class="col-md-6 mb-3">
              <label>Tempat Lahir <span class="text-danger">*</span></label>
              <input type="text" id="editTempatLahir" name="tempat_lahir" class="form-control" required>
              @error('tempat_lahir')
                  <small class="text-danger">{{ $message }}</small>
              @enderror
            </div>

            <div class="col-md-6 mb-3">
              <label>Tanggal Lahir <span class="text-danger">*</span></label>
              <input type="date" id="editTanggalLahir" name="tanggal_lahir" class="form-control" required>
              @error('tanggal_lahir')
                  <small class="text-danger">{{ $message }}</small>
              @enderror
            </div>
          </div>

          <div class="mb-3">
            <label>Kelas <span class="text-danger">*</span></label>
            <select id="editKelas" name="id_kelas" class="form-control" required>
                <option value="">Pilih Kelas</option>
                @foreach($kelass as $kelas)
                    <option value="{{ $kelas->id_kelas }}">
                        {{ $kelas->tingkat }} - {{ $kelas->nama_kelas }}
                    </option>
                @endforeach
            </select>
            @error('id_kelas')
                <small class="text-danger">{{ $message }}</small>
            @enderror
          </div>

          <div class="mb-3">
            <label>Password Baru</label>
            <input type="password" name="password" class="form-control" placeholder="Kosongkan jika tidak ingin mengubah">
            @error('password')
                <small class="text-danger">{{ $message }}</small>
            @enderror
            <small class="text-muted">Isi hanya jika ingin mengubah password</small>
          </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(nisn, nama, tempatLahir, tanggalLahir, idKelas) {
    document.getElementById('editNisn').value = nisn;
    document.getElementById('editNama').value = nama;
    document.getElementById('editTempatLahir').value = tempatLahir;
    document.getElementById('editTanggalLahir').value = tanggalLahir;
    document.getElementById('editKelas').value = idKelas;

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

    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