{{-- resources/views/super-admin/trial-approvals/index.blade.php --}}
@extends('layouts.super-admin')

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4 p-3 rounded" style="background: #ffffff; border: 1px solid #e5e7eb;">
        <div>
            <h2 class="mb-1 text-success">Trial Approval Requests</h2>
            <small class="text-muted">Review overdue trials and activate paid subscriptions quickly.</small>
        </div>
        <a href="{{ route('super.admin.dashboard') }}" class="btn btn-outline-primary rounded-pill px-3">
            <i class="fas fa-arrow-left me-1"></i>Back to Dashboard
        </a>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius: 14px; overflow: hidden;">
        <div class="card-body p-0">
            <table class="table table-hover align-middle mb-0">
                <thead>
                    <tr class="table-success">
                        <th>Hotel</th>
                        <th>Package</th>
                        <th>Price</th>
                        <th>Trial Ended</th>
                        <th>Days Overdue</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($pendingRequests as $request)
                    <tr>
                        <td class="fw-semibold">
                            <strong>{{ $request->hotel_name }}</strong><br>
                            <small>{{ $request->hotel_email }}</small>
                        </td>
                        <td>{{ ucfirst($request->package) }}</td>
                        <td>UGX {{ number_format($request->monthly_price) }}/month</td>
                        <td>{{ $request->trial_ended_at->format('d M Y') }}</td>
                        <td>
                            @php
                                $daysOverdue = now()->diffInDays($request->trial_ended_at);
                            @endphp
                            <span class="badge bg-warning text-dark">{{ $daysOverdue }} days</span>
                        </td>
                        <td>
                            <button class="btn btn-sm btn-success rounded-pill px-3 me-1" onclick="approveRequest({{ $request->id }})">
                                <i class="fas fa-check"></i> Approve & Activate
                            </button>
                            <button class="btn btn-sm btn-danger rounded-pill px-3" onclick="rejectRequest({{ $request->id }})">
                                <i class="fas fa-times"></i> Reject & Suspend
                            </button>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
function approveRequest(id) {
    Swal.fire({
        title: 'Approve Trial Hotel?',
        text: 'This will create a payment and activate the hotel',
        icon: 'question',
        input: 'textarea',
        inputPlaceholder: 'Optional notes...',
        showCancelButton: true,
        confirmButtonColor: '#28a745',
        confirmButtonText: 'Yes, approve'
    }).then((result) => {
        if (result.isConfirmed) {
            $.post('{{ route("super.admin.trial-approvals.process") }}', {
                _token: '{{ csrf_token() }}',
                request_id: id,
                action: 'approve',
                notes: result.value
            }).done(function(response) {
                Swal.fire('Approved!', response.message, 'success');
                location.reload();
            }).fail(function(xhr) {
                Swal.fire('Error!', xhr.responseJSON.message, 'error');
            });
        }
    });
}

function rejectRequest(id) {
    Swal.fire({
        title: 'Reject Trial Hotel?',
        text: 'This will suspend the hotel immediately',
        icon: 'warning',
        input: 'textarea',
        inputPlaceholder: 'Reason for rejection...',
        inputValidator: (value) => {
            if (!value) {
                return 'You need to provide a reason'
            }
        },
        showCancelButton: true,
        confirmButtonColor: '#dc3545',
        confirmButtonText: 'Yes, reject'
    }).then((result) => {
        if (result.isConfirmed) {
            $.post('{{ route("super.admin.trial-approvals.process") }}', {
                _token: '{{ csrf_token() }}',
                request_id: id,
                action: 'reject',
                notes: result.value
            }).done(function(response) {
                Swal.fire('Rejected!', response.message, 'info');
                location.reload();
            }).fail(function(xhr) {
                Swal.fire('Error!', xhr.responseJSON.message, 'error');
            });
        }
    });
}
</script>
@endsection