<div>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">

    <div class="container-fluid py-2">
        <div class="row mt-4">
            <div class="col-12">
            @if (session('message'))
                <div class="alert alert-success alert-dismissible fade show text-white" role="alert">
                    {{ session('message') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            @if (session('error'))
                <div class="alert alert-danger alert-dismissible fade show text-white" role="alert">
                    {{ session('error') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif
                <div class="card">
                    <!-- Card header -->
                    <div class="card-header d-flex justify-content-between">
                        <h5 class="mb-0">All Jobs Posted</h5>
                        <button wire:click="openModal" class="btn btn-primary-custom py-2 px-3">Post Job</button>
                    </div>

                    <!-- Modal -->
                    <div wire:ignore.self class="modal fade" id="exampleModal" tabindex="-1" role="dialog"
                         aria-labelledby="exampleModalLabel" aria-hidden="{{ $showModal ? 'false' : 'true' }}">
                        <div class="modal-dialog modal-dialog-centered" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title font-weight-normal" id="exampleModalLabel">
                                        {{ $isEdit ? 'Edit Job' : 'Post Job' }}
                                    </h5>
                                    <button type="button" class="btn-close text-dark" data-bs-dismiss="modal"
                                            wire:click="closeModal"></button>
                                </div>
                                <div class="modal-body">
                                    <form wire:submit.prevent="save">
                                        <div class="mb-3">
                                            <label class="block text-gray-700 mb-2">Job Title</label>
                                            <input wire:model="job_title" type="text" class="form-control border p-2" placeholder="Driver">
                                            @error('job_title')
                                                <span class="text-red-500">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="mb-3">
                                            <label class="block text-gray-700 mb-2">Job Location</label>
                                            <input wire:model="job_location" type="text" class="form-control border p-2" placeholder="london">
                                            @error('job_location')
                                                <span class="text-red-500">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="mb-3">
                                            <label class="block text-gray-700 mb-2">Job Description</label>
                                            <textarea wire:model="description" class="form-control border p-2" rows="5"></textarea>
                                            @error('description')
                                                <span class="text-red-500">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="mb-3">
                                            <label class="block text-gray-700 mb-2">Department</label>
                                            <input wire:model="department" type="text" class="form-control border p-2" placeholder="IT">
                                            @error('department')
                                                <span class="text-red-500">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="mb-3">
                                            <label class="block text-gray-700 mb-2">Employment Type</label>
                                            <select wire:model="employment_type" class="form-control border p-2">
                                                <option value="">Select Type</option>
                                                <option value="full-time">Full-Time</option>
                                                <option value="part-time">Part-Time</option>
                                                <option value="contract">Contract</option>
                                                <option value="temporary">Temporary</option>
                                            </select>
                                            @error('employment_type')
                                                <span class="text-red-500">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="mb-3">
                                            <label class="block text-gray-700 mb-2">Minimum Experience</label>
                                            <input wire:model="minimum_experience" type="text" class="form-control border p-2" placeholder="2years">
                                            @error('minimum_experience')
                                                <span class="text-red-500">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="mb-3">
                                            <label class="block text-gray-700 mb-2">Salary</label>
                                            <input wire:model="salary" type="text" class="form-control border p-2" placeholder="£10000 - £20000">
                                            @error('salary')
                                                <span class="text-red-500">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="mb-3">
                                            <label class="block text-gray-700 mb-2">Status</label>
                                            <select wire:model="status" class="form-control border">
                                                <option value="active">Active</option>
                                                <option value="closed">Closed</option>
                                            </select>
                                            @error('status')
                                                <span class="text-red-500">{{ $message }}</span>
                                            @enderror
                                        </div>
                                        <div class="d-flex justify-content-end">
                                            <button type="button" class="btn bg-gradient-secondary me-2"
                                                    data-bs-dismiss="modal" wire:click="closeModal">Close</button>
                                            <button type="submit" class="btn btn-primary-custom" wire:loading.attr="disabled">Save changes</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

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

                    <div class="table-responsive">
                        <table class="table table-flush table-striped table-hover table-bordered" id="datatable-search">
                            <thead class="thead-light">
                                <tr>
                                    <th class="text-center">s/n</th>
                                    <th class="text-center">Job Title</th>
                                    <th class="text-center">Job Location</th>
                                    <th class="text-center">Job Description</th>
                                    <th class="text-center">Department</th>
                                    <th class="text-center">Employment Type</th>
                                    <th class="text-center">Minimum Experience</th>
                                    <th class="text-center">Salary</th>
                                    <th class="text-center">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($jobs as $job)
                                    <tr>
                                        <td class="text-sm font-weight-normal text-center">{{ $loop->iteration }}</td>
                                        <td class="text-sm font-weight-normal text-center">{{ $job->title }}</td>
                                        <td class="text-sm font-weight-normal text-center">{{ $job->location ?? 'N/A' }}</td>
                                        <td class="text-sm font-weight-normal text-center">{{ Str::limit($job->description, 50) }}</td>
                                        <td class="text-sm font-weight-normal text-center">{{ $job->department ?? 'N/A' }}</td>
                                        <td class="text-sm font-weight-normal text-center">{{ ucfirst($job->employment_type) ?? 'N/A' }}</td>
                                        <td class="text-sm font-weight-normal text-center">{{ $job->minimum_experience ?? 'N/A' }}</td>
                                        <td class="text-sm font-weight-normal text-center">{{ $job->salary ?? 'N/A' }}</td>
                                        <td class="text-sm font-weight-normal text-center">
                                            <button wire:click="editJob({{ $job->id }})" class="btn btn-sm btn-icon-only text-light"
                                                    data-bs-toggle="tooltip" title="Edit job">
                                                <i class="bi bi-pencil fs-6 text-primary-custom"></i>
                                            </button>
                                            <button wire:click="deleteJob({{ $job->id }})" class="btn btn-sm btn-icon-only text-light"
                                                    data-bs-toggle="tooltip" title="Delete job">
                                                <i class="bi bi-trash fs-6 text-danger"></i>
                                            </button>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/umd/simple-datatables.min.js"></script>
        <script>
            document.addEventListener('livewire:initialized', function () {
                const dataTableSearch = new simpleDatatables.DataTable("#datatable-search", {
                    searchable: true,
                    fixedHeight: true,
                    perPage: {{ $perPage }},
                    perPageSelect: [5, 10, 25, 50],
                    labels: {
                        placeholder: "Search...",
                        perPage: "{select} entries per page",
                        noRows: "No entries found",
                        info: "Showing {start} to {end} of {rows} entries"
                    }
                });

                // Update DataTable when Livewire updates the table
                Livewire.on('tableUpdated', () => {
                    dataTableSearch.destroy();
                    dataTableSearch = new simpleDatatables.DataTable("#datatable-search", {
                        searchable: true,
                        fixedHeight: true,
                        perPage: {{ $perPage }},
                        perPageSelect: [5, 10, 25, 50],
                        labels: {
                            placeholder: "Search...",
                            perPage: "{select} entries per page",
                            noRows: "No entries found",
                            info: "Showing {start} to {end} of {rows} entries"
                        }
                    });
                });

                Livewire.on('show-modal', () => {
                    const myModal = new bootstrap.Modal(document.getElementById('exampleModal'));
                    myModal.show();
                });

                Livewire.on('hide-modal', () => {
                    const myModal = bootstrap.Modal.getInstance(document.getElementById('exampleModal'));
                    if (myModal) {
                        myModal.hide();
                    }
                });
            });
        </script>
    @endpush
</div>
