<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="icon" href="{{ asset('favicon.ico') }}">
    <title>Harsi Hoteleria | {{ $PDFName }}</title>
</head>
<body>

    {{-- Styles --}}
    <style>
        /* Tags */
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 13px;
        }

        section {
            margin-bottom: 1rem;
            width: 101%;
        }

        img {
            vertical-align: middle;
            border-style: none
        }

        h4 {
            font-size: 1.5rem;
            margin-bottom: .5rem;
            font-weight: 500;
            line-height: 1.2;
            margin-top: 0;
        }

        table {
            display: inline-table;
            margin-right: 10px;
        }

        footer {
            position: fixed;
            bottom: 0;
            display: block;
            width:100%;
            text-align: end;
        }

        /* Classes */
          /* Input Simulations */
            .form-box {
                display: inline-flex;
                overflow: hidden;
                white-space: nowrap;

                border: 1px solid #ccc;
                border-radius: 3px;

                font-size: 11px;
                padding: .1rem 0rem;
            }

            .form-label {
                padding: .8rem;

                font-weight: bold;
                color: #1a5c7e;

                border-right: 1px solid #ccc;
                background-color: #dedec0;
            }

            .form-value {
                padding: .8rem;

                background-color: #ffffff;
                color: #000;
            }

            .form-box-minus {
                display: block;
                margin-bottom: .4rem;
            }

            .form-label-minus {
                display: block;
                box-sizing: border-box;

                padding: 2.5px 10px;
                border-radius: 4px 2px;
                border-top: .8px solid #aaaaaaaa;
                background-color: #e6e6cf;

                font-size: 10px;
                color: #2d7a88;
                font-weight: bold;
            }

            .label-minus-offset {
                background-color: #e6e6cfa8;
                color: #5b6061dc;
            }

            .label-minus-secondary {
                background-color: #e6e6cfa8;
                color: #414546dc;
            }

            .label-minus-danger {
                background-color: #d4d4bda8;
                color: #CE3242dc;
            }

            .form-value-minus {
                display: block;
                box-sizing: border-box;

                padding: 3.5px 10px;
                border-radius: 4px;
                border-bottom: .8px solid #aaa;
                background-color: #fff;
                color: #000000e3;

                margin-top: 1.5px;
                font-size: 9px;
            }

            .form-reason-minus {
                display: block;
                box-sizing: border-box;

                padding: 3.5px 10px;
                border-radius: 4px;
                border-bottom: .8px solid #aaa;
                background-color: #fff;
                color: #000000e3;

                margin-top: 1.5px;
                font-size: 8px;
            }

          /* Title */
            .title {
                font-size: 14px;
                font-weight: bold;
                color: #1a5c7e;

                text-transform: uppercase;

                margin-bottom: 7px;
            }

            .secondary-title {
                font-size: 11px;
                font-weight: bold;
                margin-bottom: 6px;
            }

            .middle-title {
                font-size: 12.5px;
                font-weight: bold;
                color: #384047;

                margin-bottom: 6px;
            }

          /* Tables */
            .fixed-table {
                table-layout: fixed;
            }

            .fixed-table td{
                padding: 0px 10px;

                border-left: 1px solid #ccc;
            }

            .fixed-table td:first-child {
                border-left: none;
            }
    </style>

    {{-- Header --}}
    <header style="margin-bottom: 4rem">
        <div style="box-sizing: border-box; width: 100%; float: left;">

            {{-- Logo --}}
            <div style="float: left; width: 25%;">
                <img src="data:image/png;base64,<?php echo base64_encode(file_get_contents(base_path($img))); ?>" width="230" height="80"></td>
            </div>

            {{-- Title --}}
            <div style="float: right; width: 75%; padding-top: 1rem">
                <div style="border-bottom: 1.5px solid #187fd3; margin-left: 20%">
                    <h4 style="text-align: center;"> {{ $title }}</h4>
                </div>
            </div>

        </div>
    </header>

    <br>

    {{-- Content --}}
    <main>

        @php
            use Carbon\Carbon;

            $cash_translations = [
                'Efectivo',
                'Cash',
                'Espèces',
                'Contanti',
            ];

            $cash_name = "Efectivo";

            foreach ($cash_translations as $translation) {
                if (isset($audit->summary_lodging_revenues[$translation])) {
                    $foundKey = $translation;
                    break;
                }
            }
        @endphp

        {{-- Basic Data --}}
        <section>

            {{-- Folio & Timestamp --}}
            <table cellspacing="0" cellpadding="6">
                <tr>
                    <td>
                        <div class="form-box">
                            <span class="form-label">Folio</span>
                            <span class="form-value">{{ $audit->id }}</span>
                        </div>
                    </td>
                    <td>
                        <div class="form-box">
                            <span class="form-label">{{ __('pdf_reports')['counting_audition']['cash_recount_timestamp'] }}</span>
                            <span class="form-value">{{ Carbon::parse($audit->audit_date . ' ' . $audit->audit_time)->format('Y-m-d, H:i') }}</span>
                        </div>
                    </td>
                    <td>
                        <div class="form-box">
                            <span class="form-label">{{ __('pdf_reports')['counting_audition']['audit_timestamp'] }}</span>
                            <span class="form-value">{{ Carbon::parse($audit->updated_at)->format('Y-m-d, H:i') }}</span>
                        </div>
                    </td>
                </tr>
            </table>

            {{-- Shift - Receptionist - Auditor --}}
            <table cellspacing="0" cellpadding="6">
                <tr>
                    <td>
                        <div class="form-box">
                            <span class="form-label">{{ __('pdf_reports')['counting_audition']['shift'] }}</span>
                            <span class="form-value">{{ $audit->shift->name }}</span>
                        </div>
                    </td>
                    <td>
                        <div class="form-box">
                            <span class="form-label">{{ __('pdf_reports')['counting_audition']['receptionist'] }}</span>
                            <span class="form-value">{{ $audit->receptionist->name }} {{ $audit->receptionist->last_name }}</span>
                        </div>
                    </td>
                    <td>
                        <div class="form-box">
                            <span class="form-label">{{ __('pdf_reports')['counting_audition']['auditor'] }}</span>
                            <span class="form-value">{{ $audit->auditor }}</span>
                        </div>
                    </td>
                </tr>
            </table>

            {{-- Status & Reason --}}
                @php
                    $has_been_approved = ($audit_status === __('neutral_words')['approved']);
                    $has_been_rejected = ($audit_status === __('neutral_words')['rejected']);
                @endphp
            <table cellspacing="0" cellpadding="6" width="100%" @if($has_been_rejected) style="margin-top: 0.1rem" @endif>
                <tr>
                    <td width="25%">
                        <div class="form-box">
                            <span class="form-label">{{ __('pdf_reports')['counting_audition']['status'] }}</span>
                            <span class="form-value" style="
                                @if ($has_been_approved)
                                    color: #1C8B36;
                                @elseif ($has_been_rejected)
                                    color: #CE3242;
                                @endif
                            ">{{ $audit_status }}</span>
                        </div>
                    </td>
                    @if ($has_been_rejected)
                        <td style="font-size: 11px; text-align: start;" width="75%">
                            <b>{{ __('pdf_reports')['counting_audition']['rejected_reason'] }}:</b>
                            {{ $audit->rejection_reason }}
                        </td>
                    @endif
                </tr>
            </table>

        </section>

        {{-- Income --}}
        <section>

            {{-- Title --}}
            <div class="title">{{ __('pdf_reports')['counting_audition']['section_1'] }}</div>

            {{-- Income per Module --}}
                @php
                    //* Revenues Data
                        $revenue_sets = [
                            'lodging_revenues' => $audit->summary_lodging_revenues ?? [],
                            'consumption_revenues' => $audit->summary_consumption_revenues ?? [],
                            'extra_revenues' => $audit->summary_extra_revenues ?? [],
                        ];
                            if($divide_restaurant_modules)
                                $revenue_sets['mini_shop_revenues'] = $audit->summary_mini_shop_revenues ?? [];

                            if($include_restaurant)
                                $revenue_sets['restaurant_revenues'] = $audit->summary_restaurant_revenues ?? [];

                        // Pasar la sección de Efectivo al Principio
                        foreach ($revenue_sets as $key => $revenues) {
                            if (isset($revenues[$cash_name])) {
                                $cash = [$cash_name => $revenues[$cash_name]];
                                unset($revenues[$cash_name]);
                                $revenue_sets[$key] = $cash + $revenues;
                            } else {
                                $revenue_sets[$key] = $revenues;
                            }
                        }

                        // Extraer variables individuales
                        $sections = [
                            'lodging' => [
                                'title' => __('entity_forms')['audit_shift']['income_section']['lodging'],
                                'revenues' => $revenue_sets['lodging_revenues'],
                            ],
                            'consumption' => [
                                'title' => __('entity_forms')['audit_shift']['income_section']['consumption'],
                                'revenues' => $revenue_sets['consumption_revenues'],
                            ],
                            'extra' => [
                                'title' => __('entity_forms')['audit_shift']['income_section']['extras'],
                                'revenues' => $revenue_sets['extra_revenues'],
                            ],
                        ];
                            if($divide_restaurant_modules) {
                                $sections['mini_shop'] = [
                                    'title' => __('entity_forms')['audit_shift']['income_section']['mini_shop'],
                                    'revenues' => $revenue_sets['mini_shop_revenues'],
                                ];
                            }

                            if($include_restaurant) {
                                $sections['restaurant'] = [
                                    'title' => __('entity_forms')['audit_shift']['income_section']['restaurant'],
                                    'revenues' => $revenue_sets['restaurant_revenues'],
                                ];
                            }

                    //* Visual Control Data
                        $index = 0;
                        $open_tag = $divide_restaurant_modules ? "" : "<tr>";
                        $close_tag = $divide_restaurant_modules ? "" : "</tr>";
                @endphp

            <table cellspacing="0" cellpadding="6" class="fixed-table" align="center" width="100%" >
                {!! $open_tag !!}
                    @foreach ($sections as $key => $section)
                        @php
                            $theoretical = $audit->{'theoretical_' . $key . '_income'};
                            $real = $audit->{'real_' . $key . '_income'};
                        @endphp

                        @if (($index % 3 === 0) && $divide_restaurant_modules) <tr> @endif

                        <td width="@if($divide_restaurant_modules) 33% @else 25% @endif" valign="top">
                            <div class="secondary-title">{{ $section['title'] }}</div>

                            {{-- Estimated Income --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['theoretical_income'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency($theoretical) }}
                                </span>
                            </div>

                            {{-- Revenue by Payment Method --}}
                            @foreach ($section['revenues'] as $method => $data)
                                <div class="form-box-minus">
                                    <span class="form-label-minus label-minus-offset">{{ $method }}</span>
                                    <span class="form-value-minus">
                                        {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency(number_format($data['revenue'] ?? 0, 2)) }}
                                    </span>
                                </div>
                            @endforeach

                            {{-- Conformed Income --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['real_income'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency($real) }}
                                </span>
                            </div>
                        </td>

                        @php $index++; @endphp

                        @if (($index % 3 === 0 || $loop->last) && $divide_restaurant_modules) </tr> @endif
                    @endforeach
                {!! $close_tag !!}
            </table>

        </section>

        {{-- Cash Transactions & Cash Recounts --}}
        @foreach ($transaction_modules as $module)
            <section>
                {{-- Title --}}
                <div class="title">
                    {{ __('pdf_reports')['counting_audition']['section_2'] }}
                    <span class="middle-title">({{ __('modules')['dashboard'][$module]['title'] ?? __('modules')['dashboard']['restaurant'][$module] ?? ucfirst($module) }})</span>
                </div>

                {{-- Transactions per Payment Method --}}
                <table cellspacing="0" cellpadding="6" class="fixed-table" align="center" width="100%" >
                        @php
                            $index = 0;
                            $data = $audit->income_confirmation_summary[$module];
                            $income_confirmations = $data['incomes_per_method'] ?? [];

                            // Pasar la sección de Efectivo al Principio
                            if (isset($income_confirmations[$cash_name])) {
                                $cash_conformation = [$cash_name => $income_confirmations[$cash_name]];
                                unset($income_confirmations[$cash_name]);

                                $income_confirmations = $cash_conformation + $income_confirmations;
                            }
                        @endphp

                    @foreach ($income_confirmations as $title => $confirmation)
                        @if ($index % 3 === 0) <tr> @endif

                        <td width="33%" valign="top">
                            {{-- Method Name --}}
                            <div class="secondary-title">{{ $title }}</div>

                            @if ($title === $cash_name)
                                {{-- Initial Cash --}}
                                <div class="form-box-minus">
                                    <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['initial_cash'] }}</span>
                                    <span class="form-value-minus">
                                        {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency($data['initial_cash']) }}
                                    </span>
                                </div>

                                {{-- Brute Income --}}
                                <div class="form-box-minus">
                                    <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['brute_income'] }}</span>
                                    <span class="form-value-minus">
                                        {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency($data['brute_income']) }}
                                    </span>
                                </div>

                                {{-- Cash Withdrawn --}}
                                <div class="form-box-minus">
                                    <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['cash_withdrawn'] }}</span>
                                    <span class="form-value-minus">
                                        {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency($data['cash_withdrawn']) }}
                                    </span>
                                </div>

                            @else
                                {{-- Total Vouchers --}}
                                <div class="form-box-minus">
                                    <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['total_vouchers'] }}</span>
                                    <span class="form-value-minus">{{ $confirmation['total_vouchers'] }}</span>
                                </div>
                            @endif


                            {{-- Expected Balance --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus label-minus-offset">{{ __('pdf_reports')['counting_audition']['expected_balance'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency(number_format($confirmation['expected_balance'] ?? 0, 2)) }}
                                </span>
                            </div>

                            {{-- Cash Count --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['cash_count'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency(number_format($confirmation['cash_count'] ?? 0, 2)) }}
                                </span>
                            </div>

                            {{-- Balance Shortfall --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus label-minus-secondary">{{ __('pdf_reports')['counting_audition']['balance_shortfall'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency(number_format($confirmation['balance_shortfall'] ?? 0, 2)) }}
                                </span>
                            </div>

                            @isset($confirmation['balance_shortfall_reason'])
                                {{-- Shortfall Reason --}}
                                <div class="form-box-minus">
                                    <span class="form-label-minus label-minus-danger">{{ __('pdf_reports')['counting_audition']['balance_shortfall_reason'] }}</span>
                                    <span class="form-reason-minus">{{ $confirmation['balance_shortfall_reason'] }}</span>
                                </div>
                            @endisset

                        </td>

                        @php $index++; @endphp

                        @if ($index % 3 === 0 || $loop->last) </tr> @endif
                    @endforeach

                </table>
            </section>
        @endforeach

        @if ($include_petty_cash)
            <section>
                {{-- Title --}}
                <div class="title">
                    {{ __('pdf_reports')['counting_audition']['section_2'] }}
                    <span class="middle-title">({{ __('entity_forms')['audit_shift']['petty_cash_section']['title'] }})</span>
                </div>

                {{-- Petty Cash Transactions --}}
                    @php
                        $petty_data = $audit->petty_cash_summary;
                    @endphp
                <table cellspacing="0" cellpadding="6" align="center" width="100%" >
                    <tr>
                        <td width="33%" valign="top">
                            {{-- Initial Cash --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['initial_cash'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency($petty_data['initial_cash']) }}
                                </span>
                            </div>

                            {{-- Cash Withdrawn --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['cash_withdrawn'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency($petty_data['cash_withdrawn']) }}
                                </span>
                            </div>

                            @isset($petty_data['cash_withdrawn_reason'])
                                {{-- Shortfall Reason --}}
                                <div class="form-box-minus" style="margin-bottom: 1.5rem">
                                    <span class="form-label-minus label-minus-danger">{{ __('pdf_reports')['counting_audition']['balance_shortfall_reason'] }}</span>
                                    <span class="form-reason-minus">{{ $petty_data['cash_withdrawn_reason'] }}</span>
                                </div>
                            @endisset

                            {{-- Expected Balance --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus label-minus-offset">{{ __('pdf_reports')['counting_audition']['expected_balance'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency(number_format($petty_data['expected_balance'] ?? 0, 2)) }}
                                </span>
                            </div>

                            {{-- Cash Count --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus">{{ __('pdf_reports')['counting_audition']['cash_count'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency(number_format($petty_data['cash_count'] ?? 0, 2)) }}
                                </span>
                            </div>

                            {{-- Balance Shortfall --}}
                            <div class="form-box-minus">
                                <span class="form-label-minus label-minus-secondary">{{ __('pdf_reports')['counting_audition']['balance_shortfall'] }}</span>
                                <span class="form-value-minus">
                                    {{ \App\Helpers\Formats\FormatCurrencyHelper::formatCurrency(number_format($petty_data['balance_shortfall'] ?? 0, 2)) }}
                                </span>
                            </div>

                            @isset($petty_data['balance_shortfall_reason'])
                                {{-- Shortfall Reason --}}
                                <div class="form-box-minus">
                                    <span class="form-label-minus label-minus-danger">{{ __('pdf_reports')['counting_audition']['balance_shortfall_reason'] }}</span>
                                    <span class="form-reason-minus">{{ $petty_data['balance_shortfall_reason'] }}</span>
                                </div>
                            @endisset

                        </td>
                        <td width="33%" valign="top"></td>
                        <td width="33%" valign="top"></td>
                    </tr>

                </table>
            </section>
        @endif

    </main>

</body>
</html>
