@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="payments-container">
                @if(sizeof($payments) !== 0)
                    <h3 class="mb-3">My Payments</h3>
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Payment ID</th>
                            <th scope="col">Price (USD)</th>
                            @if(Auth::user()->type === \App\Enum\UserType::ORGANIZER->value)
                                <th scope="col">Invoice Number</th>
                                <th scope="col">Status</th>
                            @endif
                            <th scope="col">Paid At</th>
                            <th scope="col">Offer</th>
                            @if(Auth::user()->type === \App\Enum\UserType::ORGANIZER->value)
                                <th scope="col">Download Invoice</th>
                            @endif
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($payments as $payment)
                            @php
                                if (Auth::user()->type === \App\Enum\UserType::ORGANIZER->value) {
                                    $invoice = Auth::user()->findInvoice($payment->invoice_id);
                                }
                            @endphp
                            <tr>
                                <td>{{ $loop->index+1 }}</td>
                                <td>{{ $payment->name }}</td>
                                <td>@currency($payment->stripe_price)</td>
                                @if(Auth::user()->type === \App\Enum\UserType::ORGANIZER->value)
                                    <td>{{ $invoice->number }}</td>
                                    <td><span class="badge bg-success">{{ ucfirst($invoice->status) }}</span></td>
                                @endif
                                <td>{{ $payment->created_at->isoFormat('DD-MMM-Y HH:mm') }}</td>
                                <td>
                                    <a href="/offers/{{ $payment->offer->slug }}">
                                        <button type="button" class="btn btn-primary">View Offer</button>
                                    </a>
                                </td>
                                @if(Auth::user()->type === \App\Enum\UserType::ORGANIZER->value)
                                    <td>
                                        <a href="/invoices/{{ $invoice->id }}">
                                            <button type="button" class="btn btn-primary">Download</button>
                                        </a>
                                    </td>
                                @endif
                            </tr>
                        @endforeach()
                        </tbody>
                    </table>
                @else
                    <h3 class="mb-3">You have no payments at the moment</h3>
                @endif
            </div>
        </div>
    </div>
@endsection
