<x-app-layout>
    <x-slot name="header">
        <div class="d-flex align-items-center justify-content-between gap-3">
            <h2 class="h4 mb-0">Delete Log</h2>
            <a href="{{ route('admin.customers.index') }}" class="btn btn-outline-primary"><i class="fas fa-arrow-left"></i> Customer List</a>
        </div>
    </x-slot>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <form method="GET" class="row g-2 align-items-center mb-3">
                <div class="col-12 col-md-6 col-lg-4">
                    <div class="input-group">
                        <span class="input-group-text bg-white"><i class="fas fa-search text-muted"></i></span>
                        <input type="search" name="q" value="{{ $search }}" class="form-control" placeholder="Code, name, phone or username">
                        <button class="btn btn-primary" type="submit">Search</button>
                    </div>
                </div>
                @if($search !== '')
                    <div class="col-auto"><a href="{{ route('admin.customers.deleted') }}" class="btn btn-outline-secondary">Clear</a></div>
                @endif
            </form>

            <div class="alert alert-warning d-flex align-items-start gap-2 py-2 px-3">
                <i class="fas fa-info-circle mt-1"></i>
                <span>Restore returns the customer and queues the complete MikroTik PPPoE setup. Permanent delete cannot be undone.</span>
            </div>

            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead><tr><th>Customer ID</th><th>Customer Name</th><th>Phone</th><th>PPPoE Username</th><th>Package</th><th>MikroTik</th><th>Deleted At</th><th class="text-end">Action</th></tr></thead>
                    <tbody>
                        @forelse($customers as $customer)
                            <tr id="deleted-customer-{{ $customer->id }}">
                                <td><span class="btn btn-sm btn-outline-secondary fw-semibold">{{ $customer->customer_code }}</span></td>
                                <td class="fw-semibold">{{ $customer->name }}</td>
                                <td>{{ $customer->contact_phone ?: '—' }}</td>
                                <td><code>{{ $customer->ip_address ?: '—' }}</code></td>
                                <td>{{ $customer->package?->name ?: '—' }}</td>
                                <td>{{ $customer->mikrotik?->name ?: 'Software only' }}</td>
                                <td class="text-nowrap">{{ $customer->deleted_at?->format('d M Y, h:i A') }}</td>
                                <td class="text-end">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-sm btn-outline-success restore-customer" data-url="{{ route('admin.customers.restore', $customer->id) }}" data-name="{{ $customer->name }}">Restore</button>
                                        <button type="button" class="btn btn-sm btn-outline-danger force-delete-customer" data-url="{{ route('admin.customers.force-delete', $customer->id) }}" data-name="{{ $customer->name }}">Permanent Delete</button>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="8" class="text-center text-muted py-5"><i class="fas fa-trash-restore fa-2x mb-2 d-block"></i>No deleted customer found.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="mt-3">{{ $customers->links() }}</div>
        </div>
    </div>

    @push('scripts')
        <script>
            document.addEventListener('DOMContentLoaded', function () {
                $(document).on('click', '.restore-customer', function () {
                    const button = $(this);
                    Swal.fire({title: 'Restore customer?', text: button.data('name') + ' will return to the customer list and MikroTik setup will be queued.', icon: 'question', showCancelButton: true, confirmButtonText: 'Yes, restore', confirmButtonColor: '#198754'}).then(function (result) {
                        if (!result.isConfirmed) return;
                        button.prop('disabled', true);
                        axios.patch(button.data('url')).then(function (response) {
                            button.closest('tr').remove();
                            showToast(response.data.message, 'success');
                        }).catch(function (error) {
                            button.prop('disabled', false);
                            showToast(error.response?.data?.message || 'Customer could not be restored.', 'error');
                        });
                    });
                });

                $(document).on('click', '.force-delete-customer', function () {
                    const button = $(this);
                    Swal.fire({title: 'Permanently delete?', html: '<strong>' + $('<div>').text(button.data('name')).html() + '</strong> and all related billing data will be deleted. This cannot be undone.', icon: 'error', showCancelButton: true, confirmButtonText: 'Delete permanently', confirmButtonColor: '#dc3545'}).then(function (result) {
                        if (!result.isConfirmed) return;
                        button.prop('disabled', true);
                        axios.delete(button.data('url')).then(function (response) {
                            button.closest('tr').remove();
                            showToast(response.data.message, 'success');
                        }).catch(function (error) {
                            button.prop('disabled', false);
                            showToast(error.response?.data?.message || 'Permanent deletion failed.', 'error');
                        });
                    });
                });
            });
        </script>
    @endpush
</x-app-layout>
