@extends('admin.layouts.app')

@section('title', $title)

@section('content')
    @include('admin.layouts.notifications')

    <div class="col-md-12">
        <!-- Page Heading & Search Area -->
        <div class="row mb-3">
            <div class="col-md-9">
                <h4 class="c-grey-900">{{ $title }} <a class="btn btn-primary" href="{{ route('ch-admin.webhooks.create') }}"><i class="ti-plus"></i></a></h4>
            </div>
            <div class="col-md-3">
                <form method="GET" action="{{ route('ch-admin.webhooks.index') }}">
                    <div class="input-group">
                        <input type="text" name="search" class="form-control" placeholder="Search…" aria-label="Search" value="{{ request('search') }}">
                        <button class="btn btn-info"><i class="ti ti-search"></i></button>
                    </div>
                    <!-- Hidden Inputs to Retain Filters -->
                    <input type="hidden" name="events" value="{{ request('events') }}">
                </form>
            </div>
        </div>

        <!-- Main Content -->
        <div class="row">
            <div class="bgc-white bd bdrs-3 p-20 mB-20">
                <!-- Filter Section -->
                <div class="row mb-3">
                    <form method="GET" action="{{ route('ch-admin.webhooks.index') }}" class="d-flex align-items-center gap-3 w-100">
                        <!-- Events -->
                        <div class="flex-shrink-0">
                            @php
                                 $selectedEvents = old('events', request('events', ''));
                            @endphp

                            <select id="events" name="events" class="form-control @error('events') is-invalid @enderror">
                                <option value="">Select Event</option>
                                <option value="order.created" {{ $selectedEvents == 'order.created' ? 'selected' : '' }}>Order Created</option>
                                <option value="order.status.change" {{ $selectedEvents == 'order.status.change' ? 'selected' : '' }}>Order Status Change</option>
                                <option value="subscription.created" {{ $selectedEvents == 'subscription.created' ? 'selected' : '' }}>Subscription Created</option>
                                <option value="subscription.status.change" {{ $selectedEvents == 'subscription.status.change' ? 'selected' : '' }}>Subscription Status Change</option>
                            </select>

                            @error('events')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>


                        <!-- Action Buttons -->
                        <div class="flex-shrink-0">
                            <button type="submit" class="btn btn-info">Apply Filters</button>
                            @if(request()->hasAny(['search', 'event']))
                                <a href="{{ route('ch-admin.webhooks.index') }}" class="btn btn-secondary">Reset</a>
                            @endif
                            <button class="btn btn-danger" id="delete-bulk-btn" type="button"><i class="ti-trash"></i>Delete</button>
                        </div>

                        <!-- Hidden Inputs to Retain Filters -->
                        <input type="hidden" name="search" value="{{ request('search') }}">
                    </form>
                </div>

                <!-- Webhooks Table -->
                <div class="row">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th><input type="checkbox" id="select-all" class="form-check-input"></th>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Events</th>
                            <th>Url</th>
                            <th>Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($webhooks as $webhook)
                            <tr>
                                <td><input type="checkbox" class="form-check-input select-single" value="{{ $webhook->id }}"></td>
                                <th>{{ $webhook->id }}</th>
                                <td>{{ $webhook->name }}</td>
                                <td>
                                    @php
                                        $selectedEvents = old('events', $webhook->events);

                                        if (is_string($selectedEvents)) {
                                            $selectedEvents = json_decode($selectedEvents, true);
                                        }

                                        $selectedEvents = is_array($selectedEvents) ? $selectedEvents : [];
                                    @endphp

                                    @foreach($selectedEvents as $event)
                                        {{ $event }}
                                        @if(!$loop->last)
                                            ,
                                        @endif
                                    @endforeach
                                </td>
                                <td>{{ \Illuminate\Support\Str::limit($webhook->url, 50, '...') }}</td>
                                <td class="d-flex flex-row">
                                    <a href="{{ route('ch-admin.webhooks.edit', [$webhook->id]) }}" class="btn btn-sm btn-primary me-1"><i class="fa fa-pencil"></i></a>
                                    <form method="post" action="{{ route('ch-admin.webhooks.destroy', $webhook->id) }}">
                                        @csrf
                                        @method('DELETE')
                                        <button class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this webhook?')">
                                            <i class="fa fa-trash-o"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td class="text-center" colspan="10">No Webhooks found.</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>

                    @if($webhooks->hasPages())
                        <div class="mt-4">
                            {{ $webhooks->links() }}
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script src="{{url('assets/custom/js/axios.min.js')}}"></script>
    <script src="{{url('assets/backend/js/bulkDelete.js')}}"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            initBulkActions('{{ route("ch-admin.webhooks.bulk.destroy") }}');
        });
    </script>
@endpush
