@extends('frontEnd.layouts.master')
@section('title','My Refunds')

@push('css')
@include('frontEnd.layouts.customer.partials.cp-styles')
@endpush

@section('content')
<div class="cp-wrap">

    <div class="cp-nav">
        <a href="{{ route('home') }}"><i class="fa-solid fa-house" style="font-size:11px;"></i> Home</a>
        <span>/</span>
        <a href="{{ route('customer.account') }}">My Account</a>
        <span>/</span>
        <strong>Refunds</strong>
    </div>

    <div class="cp-tabs">
        <a href="{{ route('customer.account') }}" class="cp-tab"><i class="fa-solid fa-gauge-high"></i> Dashboard</a>
        <a href="{{ route('customer.orders') }}" class="cp-tab"><i class="fa-solid fa-box-open"></i> My Orders</a>
        <a href="{{ route('customer.order_track') }}" class="cp-tab"><i class="fa-solid fa-truck-fast"></i> Track Order</a>
        <a href="{{ route('customer.refunds') }}" class="cp-tab active"><i class="fa-solid fa-rotate-left"></i> Refunds</a>
        <a href="{{ route('customer.profile_edit') }}" class="cp-tab"><i class="fa-solid fa-user-gear"></i> Settings</a>
    </div>

    <div class="cp-card">
        <div class="cp-card-head">
            <h3>🔄 Refund Requests</h3>
            @if($refunds->count())
                <span style="font-size:13px;color:#888;">{{ $refunds->total() }} requests</span>
            @endif
        </div>

        @if($refunds->count())
        <div style="overflow-x:auto;">
            <table class="cp-table">
                <thead>
                    <tr>
                        <th>Refund ID</th>
                        <th>Order</th>
                        <th>Amount</th>
                        <th>Status</th>
                        <th>Date</th>
                        <th style="text-align:right;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($refunds as $refund)
                    @php
                        $s = $refund->status;
                        if ($s=='pending')   { $bc='badge-processing'; $bt='Pending'; }
                        elseif($s=='approved'){ $bc='badge-shipped';   $bt='Approved'; }
                        elseif($s=='rejected'){ $bc='badge-cancelled'; $bt='Rejected'; }
                        else                  { $bc='badge-delivered'; $bt='Processed'; }
                    @endphp
                    <tr>
                        <td><strong style="color:#E31019;">#{{ $refund->refund_id }}</strong></td>
                        <td>
                            <a href="{{ route('customer.invoice', ['id'=>$refund->order->id]) }}"
                               style="color:#2563eb;font-weight:700;text-decoration:none;">
                                #{{ $refund->order->invoice_id ?? $refund->order->id }}
                            </a>
                        </td>
                        <td><strong>৳{{ number_format($refund->amount + $refund->shipping_charge, 0) }}</strong></td>
                        <td><span class="{{ $bc }}">{{ $bt }}</span></td>
                        <td style="color:#888;">{{ $refund->created_at->format('d M Y') }}</td>
                        <td style="text-align:right;">
                            <div style="display:flex;gap:6px;justify-content:flex-end;">
                                <a href="{{ route('customer.refunds.show', $refund->id) }}" class="cp-btn cp-btn-blue">
                                    <i class="fa-solid fa-eye"></i>
                                </a>
                                @if($refund->status == 'pending')
                                <form action="{{ route('customer.refunds.cancel', $refund->id) }}" method="POST"
                                      onsubmit="return confirm('Cancel this refund request?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="cp-btn cp-btn-red">
                                        <i class="fa-solid fa-xmark"></i>
                                    </button>
                                </form>
                                @endif
                            </div>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @if($refunds->hasPages())
        <div class="cp-pagination">
            <a href="{{ $refunds->previousPageUrl() ?? '#' }}" class="cp-page-btn {{ $refunds->onFirstPage() ? 'disabled' : '' }}">
                <i class="fa-solid fa-chevron-left"></i>
            </a>
            @for($p = max(1,$refunds->currentPage()-2); $p <= min($refunds->lastPage(),$refunds->currentPage()+2); $p++)
                <a href="{{ $refunds->url($p) }}" class="cp-page-btn {{ $p == $refunds->currentPage() ? 'active' : '' }}">{{ $p }}</a>
            @endfor
            <a href="{{ $refunds->nextPageUrl() ?? '#' }}" class="cp-page-btn {{ !$refunds->hasMorePages() ? 'disabled' : '' }}">
                <i class="fa-solid fa-chevron-right"></i>
            </a>
        </div>
        @endif
        @else
        <div class="cp-empty">
            <i class="fa-solid fa-file-invoice-dollar"></i>
            <h5>No refund requests</h5>
            <p>You haven't submitted any refund requests yet.</p>
            <a href="{{ route('customer.orders') }}" class="cp-btn cp-btn-primary" style="margin-top:14px;">View Orders</a>
        </div>
        @endif
    </div>

</div>
@endsection
