@extends('backend.layouts.app')
@section('title')
    {{ $title }}
@endsection
@section('content')
    <div class="main-content">
        <div class="page-title">
            <div class="container-fluid">
                <div class="row">
                    <div class="col">
                        <div class="title-content">
                            <h2 class="title">{{ $title }}</h2>
                            @can('space-type-create')
                                <a href="" class="title-btn" type="button" data-bs-toggle="modal"
                                   data-bs-target="#addSpaceTypeModal"><i data-lucide="plus-circle"></i>{{ __('Add New') }}
                                </a>
                            @endcan
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="container-fluid">
            <div class="row">
                <div class="col-xl-12">
                    <div class="site-table table-responsive">
                        <form action="{{ request()->url() }}" method="get">
                            <div class="table-filter">
                                <div class="filter">
                                    <div class="search">
                                        <input type="text" id="search" name="query" value="{{ request('query') }}"
                                               placeholder="Search" />
                                    </div>

                                    <select name="status" id="status" class="form-select form-select-sm">
                                        <option value="" selected>{{ __('Filter By Status') }}</option>
                                        <option value="1" {{ request('status') == '1' ? 'selected' : '' }}>{{ __('Active') }}</option>
                                        <option value="0" {{ request('status') == '0' ? 'selected' : '' }}>{{ __('Disabled') }}</option>
                                    </select>
                                    <button type="submit" class="apply-btn"><i data-lucide="search"></i>{{ __('Search') }}</button>
                                </div>
                            </div>
                        </form>

                        <table class="table">
                            <thead>
                            <tr>
                                <th>{{ __('Name') }}</th>
                                <th>{{ __('Status') }}</th>
                                <th>{{ __('Action') }}</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($space_types as $space_type)
                                <tr>
                                    <td>
                                        {{ $space_type->name }}
                                    </td>
                                    <td>
                                        @include('backend.space_type.include.__status', ['status' => $space_type->status])
                                    </td>
                                    <td>
                                        @include('backend.space_type.include.__action', ['space_type' => $space_type])
                                    </td>
                                </tr>
                            @empty
                                <td colspan="8" class="text-center">{{ __('No Data Found!') }}</td>
                            @endforelse
                            </tbody>
                        </table>
                        {{ $space_types->links('backend.include.__pagination') }}
                    </div>
                </div>
            </div>
        </div>
    </div>

    @include('backend.space_type.include.__add_new_space_type')
    @include('backend.space_type.include.__edit_space_type')

@endsection

@section('script')
    <script>
        (function ($) {
            "use strict";

            // Handle Delete Button Click
            $('body').on('click', '#deleteModal', function () {
                var id = $(this).data('id');
                var name = $(this).data('name');

                $('#data-name').html(name);
                var url = '{{ route("admin.space-type.destroy", ":id") }}';
                url = url.replace(':id', id);
                $('#deleteForm').attr('action', url);
                $('#delete').modal('toggle');
            });


            // Handle Edit Button Click
            $('body').on('click', '#edit', function (event) {
                event.preventDefault();

                var id = $(this).data('id');
                var url = '{{ route("admin.space-type.edit", ":id") }}';
                url = url.replace(':id', id);

                $.ajax({
                    url: url,
                    type: 'GET',
                    success: function (response) {
                        $('#edit-Space-Type-body').html(response); // Inject the form into the modal body
                        $('#editModal').modal('show'); // Show the modal

                    },
                    error: function () {
                        alert('Failed to load data. Please try again.');
                    }
                });
            });

        })(jQuery);

    </script>
@endsection
