@extends('layout')

@section('title', __('store.clientarea_nav_invoices'))

@push('styles')
    @vite('resources/css/store/tables.css')
@endpush

@section('content')
<div class="container py-5">
    <div class="row">
        <div class="col-md-12">
            @php
                $isSwitched = session('active_client_id') && session('active_client_id') != auth('client')->id();
            @endphp
            <x-clientarea.page-header
                icon="fas fa-file-invoice"
                :title="__('store.clientarea_nav_invoices')"
                :subtitle="$invoices->total() . ' ' . __('store.clientarea_total_invoices')"
            />

            @if($invoices->count() > 0)
                <div class="card">
                    <div class="card-body">
                        <table id="invoicesTable" class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>{{ __('store.invoice_id_column') }}</th>
                                    <th>{{ __('store.invoice_number') }}</th>
                                    <th class="d-none d-md-table-cell">{{ __('store.creation_date') }}</th>
                                    <th class="d-none d-lg-table-cell">{{ __('store.due_date') }}</th>
                                    <th class="d-none d-lg-table-cell">{{ __('store.payment_date') }}</th>
                                    <th>{{ __('store.payment_amount') }}</th>
                                    <th>{{ __('store.common_status') }}</th>
                                    <th class="text-end">{{ __('store.common_actions') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($invoices as $invoice)
                                    @php
                                        $isPaid = $invoice->status === 'paid';
                                        $isOverdue = in_array($invoice->status, ['unpaid', 'overdue']) && $invoice->due_date && $invoice->due_date->isPast();
                                    @endphp
                                    <tr>
                                        <td class="fw-semibold text-muted">#{{ $invoice->id }}</td>
                                        <td>
                                            <strong>{{ $invoice->invoice_number }}</strong>
                                        </td>
                                        <td class="d-none d-md-table-cell" data-order="{{ $invoice->created_at->timestamp }}">
                                            {{ $invoice->created_at->format($dateFormat) }}
                                        </td>
                                        <td class="d-none d-lg-table-cell" data-order="{{ $invoice->due_date ? $invoice->due_date->timestamp : 0 }}">
                                            @if($isPaid)
                                                <span class="text-muted">&mdash;</span>
                                            @elseif($invoice->due_date)
                                                <span class="@if($isOverdue) text-danger @else text-warning @endif">
                                                    {{ $invoice->due_date->format($dateFormat) }}
                                                </span>
                                            @else
                                                <span class="text-muted">&mdash;</span>
                                            @endif
                                        </td>
                                        <td class="d-none d-lg-table-cell" data-order="{{ $invoice->paid_date ? \Carbon\Carbon::parse($invoice->paid_date)->timestamp : 0 }}">
                                            @if($isPaid && $invoice->paid_date)
                                                <span class="text-success">{{ \Carbon\Carbon::parse($invoice->paid_date)->format($dateFormat) }}</span>
                                            @else
                                                <span class="text-muted">&mdash;</span>
                                            @endif
                                        </td>
                                        <td data-order="{{ $invoice->total_amount }}">
                                            <strong>{{ $invoice->currency ? $invoice->currency->format($invoice->total_amount) : number_format($invoice->total_amount, 2) . ' TRY' }}</strong>
                                        </td>
                                        <td>
                                            <span class="badge bg-{{ $invoice->status === 'paid' ? 'success' : ($invoice->status === 'pending' ? 'info' : ($invoice->status === 'overdue' ? 'danger' : 'warning')) }}">
                                                {{ __(match($invoice->status) {
                                                    'paid' => 'store.payment_status_paid',
                                                    'pending' => 'store.payment_status_pending',
                                                    'unpaid' => 'store.payment_status_unpaid',
                                                    'overdue' => 'store.payment_status_overdue',
                                                    'cancelled' => 'store.payment_status_cancelled',
                                                    default => 'store.payment_status_unknown'
                                                }) }}
                                            </span>
                                        </td>
                                        <td class="text-end">
                                            @if($canManageInvoices)
                                                <div class="btn-group btn-group-sm" role="group">
                                                    <a href="{{ route('store.auth.invoices.details', $invoice->id) }}" class="btn btn-outline-info" title="{{ __('store.view_details') }}">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    @if(in_array($invoice->status, ['unpaid', 'pending', 'overdue']))
                                                        <a href="{{ route('store.payment.show', $invoice->id) }}" class="btn btn-primary" title="{{ __('store.pay_now') }}">
                                                            <i class="fas fa-credit-card"></i>
                                                        </a>
                                                    @endif
                                                    @if($invoice->status === 'paid' && ($enablePdfInvoices ?? true))
                                                        <a href="{{ route('store.auth.invoices.download', $invoice->id) }}" target="_blank" class="btn btn-outline-success" title="{{ __('store.download_invoice') }}">
                                                            <i class="fas fa-download"></i>
                                                        </a>
                                                    @endif
                                                    @if($invoice->legal_invoice_pdf_path)
                                                        <a href="{{ route('store.auth.invoices.legal-pdf', $invoice->id) }}" target="_blank" class="btn btn-outline-info" title="{{ __('store.download_legal_invoice') }}">
                                                            <i class="fas fa-file-invoice"></i>
                                                        </a>
                                                    @endif
                                                </div>
                                            @else
                                                <span class="text-muted"><i class="fas fa-lock me-1"></i>{{ __('store.no_permission') }}</span>
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            @else
                <div class="card">
                    <div class="card-body text-center py-5">
                        <i class="fas fa-file-invoice fa-3x text-muted mb-3"></i>
                        <h5>{{ __('store.clientarea_no_invoices') }}</h5>
                        <p class="text-muted">{{ __('store.clientarea_no_invoices_description') }}</p>
                        <a href="{{ route('store.products.index') }}" class="btn btn-primary">
                            {{ __('store.clientarea_nav_order_new') }}
                        </a>
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
// DataTables translations
window.dataTablesTranslations = {
    "sEmptyTable": "{{ __('store.datatable_empty_table') }}",
    "sInfo": "{{ __('store.datatable_info') }}",
    "sInfoEmpty": "{{ __('store.datatable_info_empty') }}",
    "sInfoFiltered": "{{ __('store.datatable_info_filtered') }}",
    "sLengthMenu": "{{ __('store.datatable_length_menu') }}",
    "sLoadingRecords": "{{ __('store.datatable_loading') }}",
    "sProcessing": "{{ __('store.datatable_processing') }}",
    "sSearch": "{{ __('store.datatable_search') }}",
    "sZeroRecords": "{{ __('store.datatable_zero_records') }}",
    "oPaginate": {
        "sFirst": "{{ __('store.datatable_first') }}",
        "sLast": "{{ __('store.datatable_last') }}",
        "sNext": "{{ __('store.datatable_next') }}",
        "sPrevious": "{{ __('store.datatable_previous') }}"
    },
    "all": "{{ __('store.datatable_all') }}"
};

window.csrfToken = '{{ csrf_token() }}';
</script>

@vite('resources/js/store/clientarea-invoices.js')
@endpush
