@extends('layout.app')

@section('title', 'Mengajar')

@section('content')
    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex justify-content-between">
            <h6 class="m-0 font-weight-bold text-primary">Daftar Guru Mengajar</h6>
            <!-- Button to trigger the modal -->
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addMapelModal">Tambah
                Data</button>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead class="text-center">
                        <tr>
                            <th>#</th>
                            <th>Kelas</th>
                            <th>Guru</th>
                            <th>Mata Pelajaran</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody class="text-center">
                        @foreach ($mengajar as $m)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>{{ $m->kelas->nama_kelas }}</td>
                                <td>{{ $m->guru->nama_guru }}</td>
                                <td>{{ $m->mata_pelajaran->nama_mapel }}</td>
                                <td>
                                    <!-- Edit Button -->
                                    <button class="btn btn-warning btn-sm" data-toggle="modal"
                                        data-target="#editMapelModal{{ $m->id_mengajar }}">Edit</button>
                                    <!-- Delete Button -->
                                    <form action="{{ route('mengajar.destroy', $m->id_mengajar) }}" method="POST"
                                        style="display:inline;">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                                    </form>
                                </td>
                            </tr>
                            <!-- Edit Modal -->
                            <div class="modal fade" id="editMapelModal{{ $m->id_mengajar }}" tabindex="-1" role="dialog">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <form action="{{ route('mengajar.update', $m->id_mengajar) }}" method="POST"
                                            enctype="multipart/form-data">
                                            @csrf
                                            @method('PUT')
                                            <div class="modal-header">
                                                <h5 class="modal-title">Edit Materi</h5>
                                                <button type="button" class="close" data-dismiss="modal">
                                                    <span>&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="form-group">
                                                    <label for="kelas">Kelas</label>
                                                    <select class="form-control" name="kelas" required>
                                                        <option value="{{ $m->id_kelas }}" selected>
                                                            {{ $m->kelas->nama_kelas }}
                                                        </option>
                                                        <option value="" disabled>Pilih Kelas
                                                        </option>
                                                        @foreach ($kelas as $k)
                                                            <option value="{{ $k->id_kelas }}">{{ $k->nama_kelas }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label for="guru">Guru</label>
                                                    <select class="form-control" name="guru" required>
                                                        <option value="{{ $m->guru->nip }}" selected>
                                                            {{ $m->guru->nama_guru }}
                                                        </option>
                                                        <option value="" disabled>Pilih Guru
                                                        </option>
                                                        @foreach ($guru as $k)
                                                            <option value="{{ $k->nip }}">{{ $k->nama_guru }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                                <div class="form-group">
                                                    <label for="mata_pelajaran">Mata Pelajaran</label>
                                                    <select class="form-control" name="mata_pelajaran" required>
                                                        <option value="{{ $m->id_mapel }}" selected>
                                                            {{ $m->mata_pelajaran->nama_mapel }}
                                                        </option>
                                                        <option value="" disabled>Pilih Mata Pelajaran
                                                        </option>
                                                        @foreach ($mapel as $k)
                                                            <option value="{{ $k->id_mapel }}">{{ $k->nama_mapel }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary"
                                                    data-dismiss="modal">Close</button>
                                                <button type="submit" class="btn btn-primary">Save changes</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Add Modal -->
    <div class="modal fade" id="addMapelModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form action="{{ route('mengajar.store') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title">Tambah Materi</h5>
                        <button type="button" class="close" data-dismiss="modal">
                            <span>&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="kelas">Kelas</label>
                            <select class="form-control" name="kelas" required>
                                <option value="" selected disabled>Pilih Kelas</option>
                                @foreach ($kelas as $k)
                                    <option value="{{ $k->id_kelas }}">{{ $k->nama_kelas }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="guru">Guru</label>
                            <select class="form-control" name="guru" required>
                                <option value="" selected disabled>Pilih Guru</option>
                                @foreach ($guru as $k)
                                    <option value="{{ $k->nip }}">{{ $k->nama_guru }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="mata_pelajaran">Mata Pelajaran</label>
                            <select class="form-control" name="mata_pelajaran" required>
                                <option value="" selected disabled>Pilih Mata Pelajaran</option>
                                @foreach ($mapel as $k)
                                    <option value="{{ $k->id_mapel }}">{{ $k->nama_mapel }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save changes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    @if (session('success'))
        <script>
            Swal.fire({
                icon: 'success',
                title: 'Berhasil',
                text: '{{ session('success') }}'
            });
        </script>
    @endif
    @if ($errors->any())
        <script>
            Swal.fire({
                icon: 'warning',
                title: 'Oopss...',
                text: '{{ $errors->first() }}'
            });
        </script>
    @endif

@endsection

@push('css')
    <link href="{{ asset('vendor/datatables/dataTables.bootstrap4.min.css') }}" rel="stylesheet">
@endpush

@push('js')
    <script src="{{ asset('vendor/datatables/jquery.dataTables.min.js') }}"></script>
    <script src="{{ asset('vendor/datatables/dataTables.bootstrap4.min.js') }}"></script>

    <script>
        $(document).ready(function() {
            $('#dataTable').DataTable();
        });
    </script>
@endpush
