<div>
    <div class="card">
        <div class="card-header">
            <h4 class="card-title">Payments</h4>
        </div>

        <div class="card-body">
            @if(session()->has('error'))<div class="alert alert-danger">{{ session('error') }}</div>@endif
            <div class="row align-items-center g-1">
                <div class="col-lg-3 col-md-6">
                    <button class="btn btn-success btn-sm-block"
                            wire:click="onAddModal()"
                            data-bs-toggle="modal"
                            data-bs-target="#payment-modal">Add New Payment
                    </button>
                </div>
                <div class="col-lg-3 col-md-6">
                    <select wire:model.defer="bulkAction" class="form-select">
                        <option value="">Bulk actions</option><option value="activate">Activate</option><option value="deactivate">Deactivate</option><option value="delete">Delete</option>
                    </select>
                </div>
                <div class="col-lg-3 col-md-6"><button type="button" class="btn btn-outline-light w-100" wire:click="applyBulkAction">Apply to selected</button></div>
                <div class="col-lg-3 col-md-6"><span class="loot-muted">{{ count($selectedPayments) }} selected</span></div>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                <tr class="text-truncate">
                    <th class="admin-check-cell"><input class="form-check-input" type="checkbox" wire:model="selectPage" aria-label="Select all payment methods"></th>
                    <th>Logo</th>
                    <th>Name</th>
                    <th>Currency</th>
                    <th>Status</th>
                    <th>Created At</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tbody>
                @foreach($payments as $payment)
                    <tr class="text-truncate">
                        <td class="admin-check-cell"><input class="form-check-input" type="checkbox" wire:model="selectedPayments" value="{{ $payment->id }}" aria-label="Select {{ $payment->name }}"></td>
                        <td>
                            <div class="d-flex align-items-center">
                                @if($payment->photo_path)
                                    <img class="payment-admin-logo" src="{{ Storage::url($payment->photo_path) }}"
                                         alt="{{ $payment->name }}">
                                @else
                                    <span class="payment-admin-logo fallback">{{ Str::substr($payment->name, 0, 1) }}</span>
                                @endif
                            </div>
                        </td>
                        <td>{{ $payment->name }}</td>
                        <td><strong>{{ $payment->currency }}</strong></td>
                        <td>
                            <button type="button" wire:click="toggleStatus({{ $payment->id }})"
                                    class="loot-switch-button {{ $payment->is_active ? 'active' : '' }}">
                                <span></span>{{ $payment->is_active ? 'Active' : 'Inactive' }}
                            </button>
                        </td>
                        <td>{{ \Carbon\Carbon::parse($payment->created_at)->diffForHumans() }}</td>
                        <td>
                            <button class="btn btn-sm btn-block btn-primary"
                                    wire:click="view({{ $payment->id }})"
                                    data-bs-toggle="modal"
                                    data-bs-target="#payment-modal">
                                <i data-feather="edit-2"></i>
                            </button>
                            <button class="btn btn-sm btn-block btn-danger" wire:click="delete({{ $payment->id }})">
                                <i data-feather="trash"></i>
                            </button>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
        <div class="form-modal-ex">
            <div wire:ignore.self class="modal fade text-start" id="payment-modal" tabindex="-1" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">{{ $is_edit ? 'Update Payment' : 'Add Payment' }}</h4>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>

                        <form wire:submit.prevent="submit">
                            <div class="modal-body">
                                @if(session()->has('success'))
                                    <div class="alert alert-success">
                                        <div class="alert-body">
                                            <p>{{ session('success') }}</p>
                                        </div>
                                    </div>
                                @endif

                                <div class="mb-1">
                                    <input @class(['form-control', 'is-invalid' => $errors->has('name')])
                                           type="text" wire:model="name" placeholder="Name" required/>
                                    @error('name')<span class="text-danger">{{ $message }}</span>@enderror
                                </div>

                                <div class="mb-1">
                                    <input @class(['form-control', 'is-invalid' => $errors->has('currency')])
                                           type="text" wire:model="currency" placeholder="Currency" required/>
                                    @error('currency')<span class="text-danger">{{ $message }}</span>@enderror
                                </div>

                                <div class="mb-1">
                                    <label class="form-label">Logo</label>
                                    <input @class(['form-control', 'is-invalid' => $errors->has('photo')])
                                           type="file" wire:model="photo" id="customFile1" accept="image/*"/>
                                    @error('photo')<span class="text-danger">{{ $message }}</span>@enderror
                                    @if($is_edit && isset($current_payment) && $current_payment->photo_path)
                                        <img class="payment-admin-logo mt-1" src="{{ Storage::url($current_payment->photo_path) }}" alt="{{ $name }}">
                                    @endif
                                </div>
                            </div>
                            <div class="modal-footer">
                                <div class="col-12 text-centerpt-50">
                                    <button type="submit" class="btn btn-success btn-block">Save</button>
                                    @if($is_edit)
                                        <button wire:click="changeStatus" type="button"
                                                class="btn btn-warning btn-block">
                                            Change Status
                                        </button>
                                    @endif
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
    <script>
        document.addEventListener("DOMContentLoaded", () => {
            Livewire.hook('element.updated', (el, component) => {
                if (window.BasketLootIcons) {
                    window.BasketLootIcons.refresh(el);
                }
            })
        });
    </script>
@endpush
