<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style>
        body {
            font-family: Helvetica, Arial, sans-serif;
            font-size:14px;
        }
        footer {
            position: fixed;
            bottom: -20px;
            left: 0px;
            right: 0px;
            height: 50px;
        }
        .logo {
            max-width: 200px;
            max-height: 100px;
            object-fit: contain;
        }
    </style>
</head>
<body>
    @php
        $azienda = App\Models\Azienda::first();
        $logoService = app(App\Services\LogoUploadServices::class);
        $logoUrl = $azienda ? $logoService->getLogoUrl($azienda) : null;

        $display_date = $receipt->created_at;
        if ($receipt->receipt_date) {
            $display_date = $receipt->receipt_date;
        }
    @endphp

    {{-- Display logo from database/Wasabi instead of env --}}
    @if($logoUrl)
        <img src="{{ $logoUrl }}" class="logo" alt="Logo">
    @elseif($azienda && $azienda->logo)
        {{-- Fallback if URL generation fails --}}
        <div style="width: 200px; height: 100px; border: 1px dashed #ccc; display: flex; align-items: center; justify-content: center;">
            <small>Logo non disponibile</small>
        </div>
    @endif

    <br><br><br>
    <div align="right"><b>RICEVUTA DI PAGAMENTO N. {{$receipt->number . "/" . $receipt->year}} del {{date("d/m/Y", strtotime($display_date))}}</b></div><br><br>

    @if($receipt->status == 99)
        <div align="right"><b style="color:red">ANNULLATA</b></div><br><br>
    @endif

    <br>

    @if($azienda)
        @if($azienda->ragione_sociale)
            {{$azienda->ragione_sociale}}
        @elseif($azienda->nome_associazione)
            {{$azienda->nome_associazione}}
        @endif
        <br><br>

        <b>Indirizzo</b>:
        @php
            $addressParts = array_filter([
                $azienda->sede_legale_indirizzo,
                $azienda->sede_legale_cap,
                $azienda->sede_legale_comune,
                $azienda->sede_legale_provincia ? '(' . $azienda->sede_legale_provincia . ')' : null
            ]);
        @endphp
        {{ implode(' ', $addressParts) }}
        <br><br>

        @if($azienda->codice_fiscale)
            <b>C.F.</b>: {{$azienda->codice_fiscale}}<br><br>
        @endif

        @if($azienda->partita_iva)
            <b>P.IVA</b>: {{$azienda->partita_iva}}<br><br>
        @endif

        @if($azienda->email)
            <b>Email</b>: {{$azienda->email}}<br><br>
        @endif

        @if($azienda->pec)
            <b>PEC</b>: {{$azienda->pec}}<br><br>
        @endif
    @else
        <div style="color: red; font-weight: bold;">
            ATTENZIONE: Configurare i dati aziendali nel sistema
        </div><br><br>
    @endif

    <hr><br>

    <b>Intestata a</b><br><br>
    @if(!$receipt->member->isAdult())
        <b>Genitore</b>: {{$receipt->parent}}<br><br>
    @endif
    @if($receipt->member->last_name != '')
        <b>Cognome</b>: {{$receipt->member->last_name}}<br><br>
    @endif
    @if($receipt->member->first_name != '')
        <b>Nome</b>: {{$receipt->member->first_name}}<br><br>
    @endif
    @if(@$receipt->member->birth_city->name != '' || @$receipt->member->birth_province->name != '' || @$receipt->member->birth_date != '')
        <b>Nato/a a</b>:
            @if(@$receipt->member->birth_city->name != '')
                {{@$receipt->member->birth_city->name}}
            @endif
            @if(@$receipt->member->birth_province->name != '')
                ({{@$receipt->member->birth_province->name}})
            @endif
            @if(@$receipt->member->birth_date != '')
                il {{$receipt->member->birth_date ? date("d/m/Y", strtotime($receipt->member->birth_date)) : ""}}
            @endif
        <br><br>
    @endif
    @if($receipt->member->address != '' || @$receipt->member->city->name != ''  || @$receipt->member->province->name != '')
        <b>Residente in</b>:
            @if($receipt->member->address != '')
                {{$receipt->member->address}}
            @endif
            @if(@$receipt->member->city->name != '')
                {{@$receipt->member->city->name}}
            @endif
            @if(@$receipt->member->province->name != '')
                ({{@$receipt->member->province->name}})
            @endif
            <br><br>
    @endif
    @if($receipt->member->fiscal_code != '')
        <b>Codice Fiscale</b>: {{$receipt->member->fiscal_code}}<br><br>
    @endif
    <hr><br>

    @php
    $total = 0;
    $totalSconto = 0;
    $totalPrediscount = 0;
    $hasDiscount = false;
    @endphp

    @foreach($receipt->rows as $row)
        <b>Causale</b>: {{@$row->causal->getTree()}}<br><br>
        @if ($row->course)
            <b>Corso</b>: {{$row->course->getDetailsName()}}<br><br>
        @endif
        @if ($row->subscription)
            <b>Abbonamento</b>: {{$row->subscription->name}}<br><br>
        @endif
        @if ($row->note != '')
            <b>Dettaglio causale</b>: {{$row->note}}<br><br>
        @endif

        @if($row->sconto > 0)
            @php $hasDiscount = true; @endphp
            <b>Importo</b>: {{formatPrice($row->prediscount_amount)}}<br><br>
            <b>Sconto</b>: {{formatPrice($row->sconto)}}<br><br>
        @endif

        <b>{{$row->sconto > 0 ? 'Importo finale' : 'Importo'}}</b>: {{formatPrice($row->amount)}}<br><br>
        <hr><br>
        @php
            $totalSconto += $row->sconto;
            $totalPrediscount += $row->prediscount_amount;
            $total += $row->amount;
        @endphp
    @endforeach

    <br><br>

    @if($hasDiscount)
        <b>Totale</b>: {{formatPrice($totalPrediscount)}}<br><br>
        <b>Sconto totale</b>: {{formatPrice($totalSconto)}}<br><br>
    @endif

    <b>Totale</b>: {{formatPrice($total)}}<br><br>

    <footer>
        <small>
            @if($azienda)
                @php
                    $locationParts = array_filter([
                        $azienda->sede_legale_comune,
                        $azienda->sede_legale_provincia ? '(' . $azienda->sede_legale_provincia . ')' : null
                    ]);
                    $location = implode(' ', $locationParts);
                @endphp
                @if ($location)
                    {{ $location }} li {{date("d/m/Y", strtotime($display_date))}}
                @else
                    {{date("d/m/Y", strtotime($display_date))}}
                @endif
            @else
                {{date("d/m/Y", strtotime($display_date))}}
            @endif
        </small>
    </footer>

</body>
</html>
