@extends('layout.main')
@section('body')
    @php
        $menu = 'Data Master';
        $pageTitle = 'Data Ekstrakurikuler';
    @endphp

    @include('layout.navbar')
    @include('layout.sidebar')

    {{-- Main Content --}}
    <div class="main-container">
        <div class="pd-ltr-20 xs-pd-20-10">
            <div class="min-height-200px">
                <div class="page-header">
                    <div class="row">
                        <div class="col-md-6 col-sm-12">
                            <div class="title">
                                <h4>{{ $menu }}</h4>
                            </div>
                            <nav aria-label="breadcrumb" role="navigation">
                                <ol class="breadcrumb">
                                    <li class="breadcrumb-item"><a href="{{ route('dashboard.index') }}">Dashboard</a></li>
                                    @if (isset($pageTitle))
                                        <li class="breadcrumb-item active" aria-current="page">{{ $pageTitle }}</li>
                                    @else
                                        <li class="breadcrumb-item active" aria-current="page"></li>
                                    @endif
                                </ol>
                            </nav>
                        </div>
                    </div>
                </div>
                <div class="card-box mb-30">
                    <div class="pd-20">
                        <h4 class="text-blue h4 mb-0">Data Ekstrakurikuler</h4>
                    </div>
                    <div class="card-header d-flex align-items-center">
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#add-extracuricullar">
                            <i class="icon dw dw-file"></i> Tambah Ekstrakurikuler</button>
                        <form class="form-inline ml-auto" action="/ekstrakurikuler" method="GET">
                            <div class="input-group">
                                <input type="search" class="form-control" name="search" placeholder="Search">
                                <div class="input-group-append">
                                    <button class="btn btn-primary" type="submit">
                                        <i class="icon dw dw-search"></i>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="pb-10">
                        <div class="table-responsive">
                            <table class="data-table table stripe hover nowrap">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Nama Ekstrakurikuler</th>
                                        <th>Kategori</th>
                                        <th>Deskripsi</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse ($extracuricullars as $index => $extracuricullar)
                                        <tr>
                                            <td>{{ $index + $extracuricullars->firstItem() }}</td>
                                            <td>{{ $extracuricullar->nama }}</td>
                                            <td>{{ $extracuricullar->kategori }}</td>
                                            <td>{{ $extracuricullar->deskripsi }}</td>
                                            <td>
                                                <a href="{{ route('ekstrakurikuler.edit', $extracuricullar->id) }}"
                                                    class="badge btn-primary" data-toggle="modal"
                                                    data-target="#edit-extracuricullar-{{ $extracuricullar->id }}" title="Edit">
                                                    <i class="icon dw dw-edit2"></i>
                                                </a>
                                                <a href="{{ route('ekstrakurikuler.destroy', $extracuricullar->id) }}"
                                                    class="badge btn-danger" data-toggle="modal"
                                                    data-target="#delete-extracuricullar" title="Delete">
                                                    <i class="icon dw dw-delete-3"></i>
                                                </a>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="5" class="text-center">Tidak ada data.</td>
                                        </tr>
                                     @endforelse
                                </tbody>
                            </table>
                            <div>
                                <div style="display: flex; justify-content: space-between; align-items: center; margin: 0 20px;">
                                    <div style="font-size: 14px;">
                                        Showing data {{ $extracuricullars->firstItem() }} to {{ $extracuricullars->lastItem() }} of {{ $extracuricullars->total() }} entries
                                    </div>
                                    <div style="font-size: 14px;">{{ $extracuricullars->links() }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="footer-wrap pd-20 mb-20 card-box">
                <strong>Sistem Pendukung Keputusan Pemilihan Ekstrakurikuler Metode SMARTER</strong>
            </div>
        </div>
    </div>

    {{-- Modal Add --}}
    <div class="modal fade" id="add-extracuricullar" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"
        style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                @if (session('error'))
                    <div class="alert alert-danger">{{ session('error') }}</div>
                @endif
                <div class="modal-header">
                    <h4 class="modal-title" id="myLargeModalLabel">
                        Tambah Ekstrakurikuler
                    </h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('ekstrakurikuler.store') }}" class="needs-validation" novalidate="" method="POST">
                        @csrf
                        <div class="card-body">
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Nama</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="nama" name="nama" required="">
                                    <div class="invalid-feedback">
                                        Tolong isi Nama Ekstrakurikuler!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('nama'))
                                <div class="alert alert-danger">{{ $errors->first('nama') }}</div>
                            @endif
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Kategori</label>
                                <div class="col-sm-10">
                                    <select class="form-control" id="kategori" name="kategori" required>
                                        <option value="" selected disabled>Pilih Kategori</option>
                                        @foreach(\App\Models\Extracuricullar::getValidCategories() as $category)
                                            <option value="{{ $category }}">{{ ucfirst($category) }}</option>
                                        @endforeach
                                    </select>
                                    <div class="invalid-feedback">
                                        Tolong pilih Kategori ekstrakurikuler!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('kategori'))
                                <div class="alert alert-danger">{{ $errors->first('kategori') }}</div>
                            @endif
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Deskripsi</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="deskripsi" name="deskripsi"
                                        required="">
                                    <div class="valid-feedback">
                                        Lengkap!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('deskripsi'))
                                <div class="alert alert-danger">{{ $errors->first('deskripsi') }}</div>
                            @endif
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                            <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    {{-- Modal Edit --}}
    @foreach ($extracuricullars as $extracuricullar)
        <div class="modal fade" id="edit-extracuricullar-{{ $extracuricullar->id }}" tabindex="-1" role="dialog"
            aria-labelledby="myLargeModalLabel" style="display: none;" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="myLargeModalLabel">
                            Edit Kriteria
                        </h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    </div>
                    <div class="modal-body">
                        <form action="{{ route('ekstrakurikuler.update', $extracuricullar->id) }}" class="needs-validation"
                            novalidate="" method="POST">
                            @csrf
                            @method('PUT')
                            <div class="card-body">
                                <div class="form-group row">
                                    <label class="col-sm-2 col-form-label">Nama</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="nama" name="nama"
                                            required="" value="{{ $extracuricullar->nama }}">
                                        <div class="invalid-feedback">
                                            Tolong isi Nama Ekstrakurikuler!
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-sm-2 col-form-label">Kategori</label>
                                    <div class="col-sm-10">
                                        <select class="form-control" id="kategori" name="kategori" required>
                                            <option value="" selected disabled>Pilih Kategori</option>
                                            @foreach(\App\Models\Extracuricullar::getValidCategories() as $category)
                                            <option value="{{ $category }}" {{ old('kategori', $extracuricullar->kategori) == $category ? 'selected' : '' }}>
                                                {{ ucfirst($category) }}
                                            </option>
                                        @endforeach
                                        </select>
                                    </div>
                                </div>
                                @if ($errors->has('kategori'))
                                    <div class="alert alert-danger">{{ $errors->first('kategori') }}</div>
                                @endif
                                <div class="form-group row">
                                    <label class="col-sm-2 col-form-label">Deskripsi</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="deskripsi" name="deskripsi"
                                            required="" value="{{ $extracuricullar->deskripsi }}">
                                        <div class="valid-feedback">
                                            Lengkap!
                                        </div>
                                    </div>
                                </div>
                                @if ($errors->has('deskripsi'))
                                    <div class="alert alert-danger">{{ $errors->first('deskripsi') }}</div>
                                @endif
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                                <button type="submit" class="btn btn-primary">Simpan</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    <!-- Modal Delete -->
    <div class="modal fade" id="delete-extracuricullar" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"
        style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myLargeModalLabel">
                        Konfirmasi Hapus Data Ekstrakurikuler
                    </h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                </div>
                <div class="modal-body">
                    <p>Apakah Anda yakin ingin menghapus data ekstrakurikuler ini?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                    <form action="{{ route('ekstrakurikuler.destroy', $extracuricullar->id) }}" method="POST">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger">Hapus</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    @endforeach
@endsection
