@extends('layouts.default')

@push('title', get_phrase('Purchase History'))
@push('meta')@endpush
@push('css')@endpush

@section('content')
<section class="wishlist-content">
    <div class="profile-banner-area"></div>
    <div class="container profile-banner-area-container">
        <div class="row">

            @include('frontend.default.student.left_sidebar')

            <div class="col-lg-9">
                <h4 class="g-title mb-4">{{ get_phrase('Payment History') }}</h4>

                <div class="mb-4 d-flex gap-2 flex-wrap">
                    <button class="btn btn-outline-secondary filter-btn" data-status="all">
                        {{ get_phrase('All') }}
                    </button>
                    <button class="btn btn-outline-warning filter-btn" data-status="unpaid">
                        {{ get_phrase('Unpaid') }}
                    </button>
                    <button class="btn btn-outline-success filter-btn" data-status="paid">
                        {{ get_phrase('Paid') }}
                    </button>
                    <button class="btn btn-outline-danger filter-btn" data-status="failed">
                        {{ get_phrase('Failed') }}
                    </button>
                </div>

                <div class="my-panel purchase-history-panel">
                    @if ($payments->count() > 0)
                        <div class="table-responsive">
                            <table class="table eTable">
                                <thead>
                                    <tr>
                                        <th>{{ get_phrase('Item') }}</th>
                                        <th>{{ get_phrase('Date') }}</th>
                                        <th>{{ get_phrase('Payment Method') }}</th>
                                        <th>{{ get_phrase('Price') }}</th>
                                        <th>{{ get_phrase('Status') }}</th>
                                        <th>{{ get_phrase('Action') }}</th>
                                    </tr>
                                </thead>
                                <tbody id="paymentTable">
                                    @foreach ($payments as $payment)
                                        <tr data-status="{{ $payment->payment_status }}">
                                            <td>{{ $payment->title }}</td>

                                            <td>
                                                {{ date('Y-m-d', strtotime($payment->created_at)) }}
                                            </td>

                                            <td>
                                                {{ ucfirst($payment->payment_type) }}
                                            </td>

                                            <td>
                                                {{ currency($payment->amount) }}
                                            </td>

                                            <td>
                                                @if ($payment->payment_status === 'paid')
                                                    <span class="badge bg-success">PAID</span>
                                                @elseif ($payment->payment_status === 'unpaid')
                                                    <span class="badge bg-warning">UNPAID</span>
                                                @else
                                                    <span class="badge bg-danger">FAILED</span>
                                                @endif
                                            </td>

                                            <td>
                                                @if ($payment->payment_status === 'paid')
                                                    <a href="{{ route('student.invoice', $payment->invoice_id) }}"
                                                    class="btn btn-sm btn-primary d-flex align-items-center justify-content-center"
                                                    style="width: 42px; height: 42px;"
                                                    data-bs-toggle="tooltip"
                                                    title="{{ get_phrase('Print Invoice') }}">
                                                        <i class="fi fi-rr-print fs-4"></i>
                                                    </a>
                                                @elseif ($payment->payment_status === 'unpaid')
                                                    <a href="{{ route('payment.va.show', $payment->id) }}"
                                                    class="btn btn-sm btn-warning d-flex align-items-center justify-content-center"
                                                    style="width: 42px; height: 42px;"
                                                    data-bs-toggle="tooltip"
                                                    title="{{ get_phrase('Pay') }}">
                                                        <i class="fi fi-rr-credit-card fs-4"></i>
                                                    </a>
                                                @else
                                                    <span class="text-muted">-</span>
                                                @endif
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    @else
                        <div class="row bg-white radius-10 mx-2">
                            <div class="col-md-12">
                                @include('frontend.default.empty')
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</section>
@endsection

@push('js')
<script>
    document.querySelectorAll('.filter-btn').forEach(btn => {
        btn.addEventListener('click', function () {
            const status = this.dataset.status;

            document.querySelectorAll('#paymentTable tr').forEach(row => {
                if (status === 'all' || row.dataset.status === status) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            });
        });
    });
</script>
@endpush
