@extends('layouts.app')
@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Supplier Invoice Details', __('Supplier Invoice Details')))

@section('content')
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Supplier Invoice', __('Supplier Invoice')) }}:
                            {{ $invoice->invoice_number }}</h3>
                        <div class="card-tools">
                            <a href="{{ route('supplier-invoices.print', $invoice) }}" class="btn btn-sm btn-info print-hidden" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Print Invoice', __('Print Invoice')) }}" data-toggle="tooltip" data-placement="bottom" target="_blank">
                                <i class="fas fa-print"></i>
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Print', __('Print')) }}
                            </a>
                            
                            @can('update', $invoice)
                            <a href="{{ route('supplier-invoices.edit', $invoice) }}" class="btn btn-sm btn-warning print-hidden" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit Invoice Details', __('Edit Invoice Details')) }}" data-toggle="tooltip" data-placement="bottom">
                                <i class="fas fa-edit"></i>
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('Edit')) }}
                            </a>
                            @endcan
                            
                            @can('create', App\Models\SupplierPayment::class)
                            <a href="{{ route('supplier-payments.create', ['invoice_id' => $invoice->id]) }}" class="btn btn-sm btn-success print-hidden" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Add New Payment to Invoice', __('Add New Payment to Invoice')) }}" data-toggle="tooltip" data-placement="bottom">
                                <i class="fas fa-money-bill"></i>
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Add Payment', __('Add Payment')) }}
                            </a>
                            @endcan
                            
                            <a href="{{ route('supplier-invoices.index') }}" class="btn btn-sm btn-secondary print-hidden" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Return to Invoice List', __('Return to Invoice List')) }}" data-toggle="tooltip" data-placement="bottom">
                                <i class="fas fa-arrow-left"></i>
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Return', __('Return')) }}
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        {{-- Company Information with Logo --}}
                        <div class="row mb-4 pb-4 border-bottom">
                            @if ($invoice->company && $invoice->company->logo)
                                <div class="col-md-1">
                                    <img src="{{ asset('storage/' . $invoice->company->logo) }}"
                                        alt="{{ $invoice->company->name }}" class="img-fluid rounded"
                                        style="max-width: 100px; height: auto;">
                                </div>
                            @endif
                            <div class="col-md-5">
                                <h5 class="fw-bold">{{ $invoice->company->name ?? config('app.name', 'عقودي') }}</h5>
                                <small class="text-muted d-block">
                                    <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Tax Number', __('Tax Number')) }}:</strong>
                                    {{ $invoice->company->tax_number ?? '-' }}
                                </small>
                                <small class="text-muted d-block">
                                    <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Commercial Register', __('Commercial Register')) }}:</strong>
                                    {{ $invoice->company->commercial_record ?? '-' }}
                                </small>
                                <small class="text-muted d-block">
                                    <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Address', __('Address')) }}:</strong>
                                    {{ $invoice->company->address ?? '-' }}
                                </small>
                                <small class="text-muted d-block">
                                    <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Phone', __('Phone')) }}:</strong>
                                    {{ $invoice->company->phone ?? '-' }}
                                </small>
                                <small class="text-muted d-block">
                                    <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Email', __('Email')) }}:</strong>
                                    {{ $invoice->company->email ?? '-' }}
                                </small>
                            </div>
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-3">
                                <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Invoice Number', __('Invoice Number')) }}:</strong>
                                <p>{{ $invoice->invoice_number }}</p>
                            </div>
                            <div class="col-md-3">
                                <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Supplier Name', __('Supplier Name')) }}:</strong>
                                <p>{{ $invoice->supplier->name ?? '-' }}</p>
                            </div>
                            <div class="col-md-3">
                                <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Invoice Date', __('Invoice Date')) }}:</strong>
                                <p>{{ $invoice->invoice_date->format('Y-m-d') }}</p>
                            </div>
                            <div class="col-md-3">
                                <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Due Date', __('Due Date')) }}:</strong>
                                <p>{{ $invoice->due_date->format('Y-m-d') }}</p>
                            </div>
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-3">
                                <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status')) }}:</strong>
                                <p>
                                    @switch($invoice->status)
                                        @case('draft')
                                            <span class="badge badge-secondary">{{ \App\Helpers\TranslationHelper::transFromDatabase('Draft', __('Draft')) }}</span>
                                        @break

                                        @case('approved')
                                            <span class="badge badge-info">{{ \App\Helpers\TranslationHelper::transFromDatabase('Approved', __('Approved')) }}</span>
                                        @break

                                        @case('paid')
                                            <span class="badge badge-success">{{ \App\Helpers\TranslationHelper::transFromDatabase('Paid', __('Paid')) }}</span>
                                        @break

                                        @case('partially_paid')
                                            <span class="badge badge-warning">{{ \App\Helpers\TranslationHelper::transFromDatabase('Partially Paid', __('Partially Paid')) }}</span>
                                        @break

                                        @case('overdue')
                                            <span class="badge badge-danger">{{ \App\Helpers\TranslationHelper::transFromDatabase('Overdue', __('Overdue')) }}</span>
                                        @break

                                        @case('cancelled')
                                            <span class="badge badge-dark">{{ \App\Helpers\TranslationHelper::transFromDatabase('Cancelled', __('Cancelled')) }}</span>
                                        @break
                                    @endswitch
                                </p>
                            </div>
                            @if($invoice->warehouse)
                                <div class="col-md-3">
                                    <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Warehouse', __('Warehouse')) }}:</strong>
                                    <p>{{ $invoice->warehouse->name }}</p>
                                </div>
                            @endif
                        </div>

                        <div class="form-group mt-4">
                            <h5>{{ \App\Helpers\TranslationHelper::transFromDatabase('Invoice Items', __('Invoice Items')) }}</h5>
                            <div class="table-responsive">
                                <table class="table table-bordered table-sm">
                                    <thead class="bg-light">
                                        <tr>
                                            <th>#</th>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Item Name', __('Item Name')) }}</th>
                                            <th style="width: 100px;">{{ \App\Helpers\TranslationHelper::transFromDatabase('Item Unit', __('Item Unit')) }}</th>
                                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Description', __('Description')) }}</th>
                                            <th style="width: 80px;">{{ \App\Helpers\TranslationHelper::transFromDatabase('Quantity', __('Quantity')) }}</th>
                                            <th style="width: 100px;">{{ \App\Helpers\TranslationHelper::transFromDatabase('Unit Price', __('Unit Price')) }}</th>
                                            <th style="width: 80px;">{{ \App\Helpers\TranslationHelper::transFromDatabase('Discount %', __('Discount %')) }}</th>
                                            <th style="width: 80px;">{{ \App\Helpers\TranslationHelper::transFromDatabase('Tax %', __('Tax %')) }}</th>
                                            <th style="width: 100px;">{{ \App\Helpers\TranslationHelper::transFromDatabase('Line Total', __('Line Total')) }}</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @forelse($invoice->items as $index => $item)
                                            <tr>
                                                <td>{{ $index + 1 }}</td>
                                                <td>{{ $item->item?->name ?? '-' }}</td>
                                                <td>{{ $item->itemUnit && $item->itemUnit->unit ? $item->itemUnit->unit->name : '-' }}</td>
                                                <td>{{ $item->description }}</td>
                                                <td class="text-right">{{ number_format($item->quantity, 2) }}</td>
                                                <td class="text-right">{{ number_format($item->unit_price, 2) }}</td>
                                                <td class="text-right">{{ number_format($item->discount_percentage, 2) }}%</td>
                                                <td class="text-right">{{ number_format($item->tax_rate + ($item->tax_rate_2 ?? 0), 2) }}%</td>
                                                <td class="text-right font-weight-bold text-primary">{{ number_format($item->line_total, 2) }}</td>
                                            </tr>
                                        @empty
                                            <tr>
                                                <td colspan="9" class="text-center text-muted">
                                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('No Items In This Invoice', __('No Items In This Invoice')) }}
                                                </td>
                                            </tr>
                                        @endforelse
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-8"></div>
                            <div class="col-md-4">
                                <div class="card bg-light">
                                    <div class="card-body">
                                        <div class="d-flex justify-content-between mb-2">
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Subtotal', __('Subtotal')) }}:</span>
                                            <strong>{{ number_format($invoice->subtotal ?? 0, 2) }}</strong>
                                        </div>
                                        <div class="d-flex justify-content-between mb-2">
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Discount', __('Total Discount')) }}:</span>
                                            <strong>{{ number_format($invoice->total_discount ?? 0, 2) }}</strong>
                                        </div>
                                        <div class="d-flex justify-content-between mb-2">
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Tax', __('Tax')) }}:</span>
                                            <strong>{{ number_format($invoice->tax_amount ?? 0, 2) }}</strong>
                                        </div>
                                        <hr>
                                        <div class="d-flex justify-content-between">
                                            <span class="font-weight-bold">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Amount', __('Total Amount')) }}:</span>
                                            <strong class="text-primary h5">{{ number_format($invoice->total_amount, 2) }}</strong>
                                        </div>
                                        <hr>
                                        <div class="d-flex justify-content-between">
                                            <span class="font-weight-bold">{{ \App\Helpers\TranslationHelper::transFromDatabase('Remaining Amount', __('Remaining Amount')) }}:</span>
                                            <strong class="text-danger h6">{{ number_format($invoice->remaining_amount ?? 0, 2) }}</strong>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        @if ($invoice->notes)
                            <div class="row mb-4">
                                <div class="col-md-12">
                                    <h6>{{ \App\Helpers\TranslationHelper::transFromDatabase('Notes', __('Notes')) }}:</h6>
                                    <p>{{ $invoice->notes }}</p>
                                </div>
                            </div>
                        @endif

                        @if ($invoice->attachment)
                            <div class="row mb-4">
                                <div class="col-md-12">
                                    <h6>{{ \App\Helpers\TranslationHelper::transFromDatabase('Attachments', __('Attachments')) }}:</h6>
                                    <a href="{{ Storage::url($invoice->attachment) }}" class="btn btn-sm btn-info" target="_blank">
                                        <i class="fas fa-download"></i>
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Download File', __('Download File')) }}
                                    </a>
                                </div>
                            </div>
                        @endif

                        @if($invoice->contract)
                            <div class="row">
                                <div class="col-md-12">
                                    <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Related Contract', __('Related Contract')) }}:</strong>
                                    <p>
                                        <a href="{{ route('contracts.show', $invoice->contract->id) }}">
                                            {{ $invoice->contract->contract_number }} - {{ $invoice->contract->title }}
                                        </a>
                                    </p>
                                </div>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
