<link href="{{ asset('css/pages/shared/informes.css') }}" rel="stylesheet">

@extends('adminlte::page')

@section('title', __('modules')['dashboard']['hotel']['reports'])

@section ('content')

    {{-- Styles --}}
    <style>

        .cardReport{
            border-radius: 20px 20px 20px 20px;
            box-shadow: 7px 7px 12px 1px rgba(20, 20, 20, 0.3);
            background-color: #ffffffd7 !important;
            padding: 0% 0% 0% 0%;
            margin-right: 3px !important;
            margin-left: 3px !important;
            margin-bottom: 3px !important;
        }

        .info-box-text{
            font-size: 0.95rem !important;
        }

        .reportTitle{
            color: #0d6889 !important;
            text-transform: uppercase !important;
            font-weight: 700 !important;
            font-size: 1.65rem;

            margin-left: 0px !important;
            margin-top: 0px !important;
            margin-bottom: 0px !important;
            padding-top: 0px !important;
        }

        .reportSubtitle{
            margin-left: 5px !important;
            margin-top: 20px !important;
            margin-bottom: 10px !important;
            padding-top: 20px !important;
            color: #0d6889 !important;
            text-transform: uppercase !important;
            font-weight: 700 !important;
            font-size: 1.50rem;
        }

        .summarieSubtitle{
            margin-left: 5px !important;
            margin-bottom: 1.2rem !important;
            margin-top: 0.2rem !important;
            color: #0d6889 !important;
            text-transform: uppercase !important;
            font-weight: 700 !important;
            font-size: 1.50rem;
        }

    </style>

    {{-- Body --}}
        {{-- Casos de Uso --}}
        @php
            $is_admin = Auth::user()->role === 'administrador';
            $is_enable = Auth::user()->report_permissions['has_permission'];
            $is_any_filled = Auth::user()->report_permissions['statistical_summaries'] ||
                            Auth::user()->report_permissions['general_reports'] ||
                            Auth::user()->report_permissions['income_reports'] ||
                            Auth::user()->report_permissions['booking_reports'] ||
                            Auth::user()->report_permissions['client_reports'];
        @endphp

    <section class="pr-3">
        @if ( $is_admin || ($is_enable && $is_any_filled) )
            <div class="mt-2">
                {{-- Resumen Estadistico --}}
                @if (Auth::user()->report_permissions['statistical_summaries'] || Auth::user()->role === 'administrador')
                    <div class="row card-general justify-content-center">

                        <div class="row justify-content-center col-12">
                            <div class="col-4">
                                <h3 class="summarieSubtitle tex-center">
                                    {{ __('reports')['hotel']['statisticalSummaries'] }}
                                </h3>
                            </div>
                        </div>

                        @foreach ($statisticalSummaries as $summarie)
                            <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
                                <div class="info-box" title="{{ $summarie['title'] }}">
                                    <span class="info-box-icon bg-aqua"><i class="{{ $summarie['icon'] }}" style="padding: 22px;"></i></span>
                                    <div class="info-box-content">
                                        <span class="info-box-text">{{ $summarie['title'] }}</span>
                                        <div id="{{ $summarie['html_id'] }}"></div>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>
                @endif

                {{-- Reportes de Hoteleria --}}

                    @php
                        $show_hotel_reports = Auth::user()->report_permissions['general_reports'] ||
                                                Auth::user()->report_permissions['income_reports'] ||
                                                Auth::user()->report_permissions['booking_reports'] ||
                                                Auth::user()->report_permissions['client_reports'];
                    @endphp
                    @if ($show_hotel_reports || Auth::user()->role === 'administrador')

                        {{-- Reportes Generales --}}
                        <div>
                            {{-- Titulo --}}
                            <div class="row justify-content-center align-items-center g-2">
                                <div class="col-11">
                                    <div class="text-center card cardReport">
                                        <div class="card-body">
                                            <div class="row justify-content-center">
                                                <div class="col-6">
                                                    <h3 class="reportTitle tetx-center">
                                                        {{ __('reports')['hotel']['title'] }}
                                                    </h3>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            @if (Auth::user()->report_permissions['general_reports'] || Auth::user()->role === 'administrador')
                                <div class="panel panel-primary card-info">
                                    <div class="panel-heading">
                                        <h4 class="reportSubtitle">
                                            {{ __('reports')['hotel']['generalReports'] }}
                                        </h4>
                                    </div>

                                    <div class="panel-body">
                                        <div class="row justify-content-center">
                                            @foreach ($generalReports as $report)
                                                <div class="hidden-sm hidden-xs col-lg-4 col-md-6 col-sm-12 col-xs-12">
                                                    <a target="_blank" href="{{ route($report['route']) }}" class="btn-default btn-lg btn-block"
                                                        style=" background-color: white; margin-top:10px;">
                                                        <i class="letra-type" style="padding-right: 5px"> <img class="info-opcion"
                                                                src="{{ asset('icons/paper_blue.svg') }}" alt="">&nbsp; {{ $report['title'] }}</i>
                                                    </a>
                                                </div>

                                                <div class="hidden-lg hidden-md col-lg-4 col-md-6 col-sm-12 col-xs-12">
                                                    <a target="_blank" href="{{ route($report['route']) }}" class="btn-default btn-lg btn-block"
                                                        style="margin-top:10px; background-color: white;">
                                                        <i class="letra-type" style="padding-right: 5px"> <img class="info-opcion"
                                                                src="{{ asset('icons/paper_blue.svg') }}" alt="">&nbsp; {{ $report['title'] }}</i>
                                                    </a>
                                                </div>
                                            @endforeach
                                        </div>
                                    </div>
                                    <br>
                                </div>
                            @endif
                        </div>

                        {{-- Reportes Estadisticos --}}
                        @php
                            $show_statistical_reports = Auth::user()->report_permissions['income_reports'] ||
                                                    Auth::user()->report_permissions['booking_reports'] ||
                                                    Auth::user()->report_permissions['client_reports'];
                        @endphp

                        @if ($show_statistical_reports || Auth::user()->role === 'administrador')
                            <div class="card-info">
                                {{-- Titulo --}}
                                <div class="panel-heading">
                                    <h4 class="reportSubtitle">
                                        {{ __('reports')['hotel']['statisticalReports']['title'] }}
                                    </h4>
                                </div>

                                {{-- Reservas, Ingresos y Clientes --}}
                                <div class="panel-body">
                                    <div class="mt-3 row">

                                        <table class="table table-striped table-bordered">
                                            <tbody>
                                                {{-- Ingresos --}}

                                                @if (Auth::user()->report_permissions['income_reports'] || Auth::user()->role === 'administrador')
                                                    <tr>
                                                        <td colspan="2" style="font-weight: bold">
                                                            {{ __('reports')['hotel']['statisticalReports']['incomes']['title'] }}
                                                        </td>
                                                    </tr>
                                                    @foreach ($statisticalReports->ingresos as $report)
                                                        <tr>
                                                            <td class="pl-4 col-lg-9 col-md-9 col-sm-9 col-xs-9 ng-binding">
                                                                {{ $report['title'] }}
                                                            </td>
                                                            <td class="text-center col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                                                @isset( $report['route'])
                                                                    <a class="pt-1 pb-1 btn with-icon" style="color:#FFF;" title="Abrir" href="{{ route('hotel.reports.'.$report['route']) }}">
                                                                        <i class="fa fa-link"></i>
                                                                    </a>
                                                                @endisset
                                                                @isset( $report['action'])
                                                                    <button class="pt-1 pb-1 btn with-icon" style="color:#FFF;" title="Abrir" onclick="{{ $report['action'] }}">
                                                                        <i class="fa fa-link"></i>
                                                                    </button>
                                                                @endisset
                                                            </td>
                                                        </tr>
                                                    @endforeach
                                                @endif

                                                {{-- Reservaciones --}}
                                                @if (Auth::user()->report_permissions['booking_reports'] || Auth::user()->role === 'administrador')
                                                    <tr>
                                                        <td colspan="2" style="font-weight: bold">
                                                            {{ __('reports')['hotel']['statisticalReports']['reservations']['title'] }}
                                                        </td>
                                                    </tr>

                                                    @foreach ($statisticalReports->reservas as $report)

                                                        @php
                                                            $show_report = isset($report['conditional'])
                                                                ? \App\Helpers\Validations\ReportConditionHelper::evaluate($report['conditional'])
                                                                : true;
                                                        @endphp

                                                        @if ($show_report)
                                                            <tr>
                                                                <td class="pl-4 col-lg-9 col-md-9 col-sm-9 col-xs-9 ng-binding">
                                                                    {{ $report['title'] }}
                                                                </td>
                                                                <td class="text-center col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                                                    @isset( $report['route'])
                                                                        <a class="pt-1 pb-1 btn with-icon" style="color:#FFF;" title="Abrir" href="{{ route('hotel.reports.'.$report['route']) }}">
                                                                            <i class="fa fa-link"></i>
                                                                        </a>
                                                                    @endisset
                                                                    @isset( $report['action'])
                                                                        <button class="pt-1 pb-1 btn with-icon" style="color:#FFF;" title="Abrir" onclick="{{ $report['action'] }}">
                                                                            <i class="fa fa-link"></i>
                                                                        </button>
                                                                    @endisset
                                                                </td>
                                                            </tr>
                                                        @endif
                                                    @endforeach
                                                @endif

                                                {{-- Clientes --}}
                                                @if (Auth::user()->report_permissions['client_reports'] || Auth::user()->role === 'administrador')
                                                    <tr>
                                                        <td colspan="2" style="font-weight: bold">
                                                            {{ __('reports')['hotel']['statisticalReports']['clients']['title'] }}
                                                        </td>
                                                    </tr>

                                                    @foreach ($statisticalReports->clientes as $report)
                                                        <tr>
                                                            <td class="pl-4 col-lg-9 col-md-9 col-sm-9 col-xs-9 ng-binding">
                                                                {{ $report['title'] }}
                                                            </td>
                                                            <td class="text-center col-lg-3 col-md-3 col-sm-3 col-xs-3">
                                                                @isset( $report['route'])
                                                                    <a class="pt-1 pb-1 btn with-icon" style="color:#FFF;" title="Abrir" href="{{ route('hotel.reports.'.$report['route']) }}">
                                                                        <i class="fa fa-link"></i>
                                                                    </a>
                                                                @endisset
                                                                @isset( $report['action'])
                                                                    <button class="pt-1 pb-1 btn with-icon" style="color:#FFF;" title="Abrir" onclick="{{ $report['action'] }}">
                                                                        <i class="fa fa-link"></i>
                                                                    </button>
                                                                @endisset
                                                            </td>
                                                        </tr>
                                                    @endforeach
                                                @endif
                                            </tbody>
                                        </table>

                                    </div>
                                </div>
                            </div>
                        @endif
                    @endif
            </div>
        @else
            <div class="mt-5 row justify-content-center align-items-center g-2">
                <div class="col-6 card-general">

                    <div class="text-center">
                        @if (Auth::user()->profile_photo === null)
                            <img class="img-fluid;" style="max-width: 10rem; height: auto; margin:auto" src="{{ asset('images/only_harsi_bigger.png') }}" alt="">
                        @else
                            <img class="img-fluid;" style="max-width: 10rem; height: auto; margin:auto" src="{{ Auth::user()->profile_photo }}" alt="">
                        @endif
                        <div class="card-body">
                            {{-- Title --}}
                            <h3 class="text-center">
                                {{ __('reports')['hotel']['not_allowed']['title'] }}
                            </h3>

                            {{-- Description --}}
                            <p class="card-text">
                                {{ __('reports')['hotel']['not_allowed']['description_part_1'] }}
                                <b>{{ __('reports')['hotel']['not_allowed']['description_part_2'] }}</b><br>
                                {{ __('reports')['hotel']['not_allowed']['description_part_3'] }}
                            </p>

                            {{-- Solution --}}
                            <p class="px-3 text-center">
                                {{ __('reports')['hotel']['not_allowed']['solution_part_1'] }}
                                <span style="font-weight: bold; color:#0d6889">{{ __('reports')['hotel']['not_allowed']['solution_part_2'] }}</span>
                                {{ __('neutral_words')['or'] }}
                                <span style="font-weight: bold; color:#0d6889">{{ __('reports')['hotel']['not_allowed']['solution_part_3'] }}</span>
                                {{ __('reports')['hotel']['not_allowed']['solution_part_4'] }}
                            </p>
                        </div>
                    </div>

                </div>
            </div>
        @endif
    </section>

    {{-- JS Control --}}
    <script type="text/javascript">
        "use strict";
    </script>

    {{-- Información de los Reportes --}}
    <script>
        // On api token generated
        window.apiTokenReady.then(token => {
            // Initial Call
            getDataOnTime();

            // Cycles
            setInterval(getDataOnTime, 120000); //Cada 120 segundos (120 mil milisegundos)
        });

        // On window unload
        window.onbeforeunload = function () {
            localStorage.removeItem("DataOnTime");
        };

        function getBookingHistory(){

            Swal.fire({
                title: "{{ __('reports')['hotel']['consult_history']['title'] }}",
                html: '<input id="swal-folio" class="swal2-input" type="number" min="1" step="0.0" oninput="this.value = Math.abs(this.value)" value="1">',
                showCancelButton: true,
                confirmButtonColor: '#0d6889',
                cancelButtonColor: '#d33',
                cancelButtonText: "{{ __('common_actions')['cancel'] }}",
                confirmButtonText: "{{ __('common_actions')['accept'] }}",
                focusConfirm: false,
                preConfirm: () => {
                    const folio = Swal.getPopup().querySelector('#swal-folio').value
                    if (!folio) {
                        Swal.showValidationMessage("{{ __('reports')['hotel']['consult_history']['instructions'] }}")
                    }
                    return {
                        folio: folio
                    }
                }
            }).then((result) => {
                folio = result.value.folio;

                if(folio <= 0){
                    Swal.fire(
                        "{{ __('reports')['hotel']['consult_history']['0_error_title'] }}",
                        "{{ __('reports')['hotel']['consult_history']['0_error_description'] }}",
                        'error'
                    );
                    return;
                }

                var settings = {
                    "url": `/reservations/${folio}/find-history-data`,
                    "method": "GET",
                };

                $.ajax(settings)
                .done(() => {
                    window.location.href = `/hotel/reports/booking-update-history/${folio}`;
                }).fail(() => {
                    Swal.fire(
                        "{{ __('reports')['hotel']['consult_history']['error_title'] }}",
                        "{{ __('reports')['hotel']['consult_history']['error_description'] }}",
                        'error'
                    );
                    return;
                });
            });
        }

        function getDataOnTime() {
            let hotelResume = localStorage.getItem("DataOnTime");

            apiGet("{{ route('api.hotel.summary') }}", function (data) {

                if(hotelResume != ''){
                    data = JSON.stringify(data);

                    if(data != hotelResume){

                        data = JSON.parse(data);

                        var html_select = '';
                        var valSum = 0.00;

                        $('#roomType').empty();
                        $('#room').empty();
                        $('#agencies').empty();
                        $('#checkins').empty();
                        $('#reservations').empty();
                        $('#lodgingEarningsM').empty();
                        $('#extraEarningsM').empty();
                        $('#consumptionEarningsM').empty();
                        $('#totalEarningsM').empty();
                        $('#occupiedRooms').empty();
                        $('#clients').empty();
                        $('#userTotal').empty();

                        html_select = `<span class="info-box-number">${data.roomType}</span>`;
                        $('#roomType').html(html_select);
                        html_select = `<span class="info-box-number">${data.room}</span>`;
                        $('#room').html(html_select);
                        html_select = `<span class="info-box-number">${data.agencies}</span>`;
                        $('#agencies').html(html_select);
                        html_select = `<span class="info-box-number">${data.checkouts}</span>`;
                        $('#checkins').html(html_select);
                        html_select = `<span class="info-box-number">${data.reservations}</span>`;
                        $('#reservations').html(html_select);
                        html_select = `<span class="info-box-number">${formatCurrency(data.lodgingEarnings)}</span>`;
                        $('#lodgingEarningsM').html(html_select);
                        html_select = `<span class="info-box-number">${formatCurrency(data.extraEarningsM)}</span>`;
                        $('#extraEarningsM').html(html_select);
                        html_select = `<span class="info-box-number">${formatCurrency(data.consumptionEarningsM)}</span>`;
                        $('#consumptionEarningsM').html(html_select);

                        html_select = `<span class="info-box-number">${formatCurrency(data.total)}</span>`;
                        $('#totalEarningsM').html(html_select);

                        html_select = `<span class="info-box-number">${data.occupiedRooms}</span>`;
                        $('#occupiedRooms').html(html_select);

                        html_select = `<span class="info-box-number">${data.clients}</span>`;
                        $('#clients').html(html_select);

                        html_select = `<span class="info-box-number">${data.userTotal}</span>`;
                        $('#userTotal').html(html_select);

                        data = JSON.stringify(data);
                        localStorage.setItem("DataOnTime",data);
                    }

                } else {

                        var html_select = '';
                        var valSum = 0.00;

                        $('#roomType').empty();
                        $('#room').empty();
                        $('#agencies').empty();
                        $('#checkins').empty();
                        $('#reservations').empty();
                        $('#lodgingEarningsM').empty();
                        $('#extraEarningsM').empty();
                        $('#consumptionEarningsM').empty();
                        $('#totalEarningsM').empty();
                        $('#occupiedRooms').empty();
                        $('#clients').empty();
                        $('#userTotal').empty();

                        html_select = `<span class="info-box-number">${data.roomType}</span>`;
                        $('#roomType').html(html_select);
                        html_select = `<span class="info-box-number">${data.room}</span>`;
                        $('#room').html(html_select);
                        html_select = `<span class="info-box-number">${data.agencies}</span>`;
                        $('#agencies').html(html_select);
                        html_select = `<span class="info-box-number">${data.checkouts}</span>`;
                        $('#checkins').html(html_select);
                        html_select = `<span class="info-box-number">${data.reservations}</span>`;
                        $('#reservations').html(html_select);
                        html_select = `<span class="info-box-number">${formatCurrency(data.lodgingEarnings)}</span>`;
                        $('#lodgingEarningsM').html(html_select);
                        html_select = `<span class="info-box-number">${formatCurrency(data.extraEarningsM)}</span>`;
                        $('#extraEarningsM').html(html_select);
                        html_select = `<span class="info-box-number">${formatCurrency(data.consumptionEarningsM)}</span>`;
                        $('#consumptionEarningsM').html(html_select);


                        html_select = `<span class="info-box-number">${formatCurrency(data.total)}</span>`;
                        $('#totalEarningsM').html(html_select);

                        html_select = `<span class="info-box-number">${data.occupiedRooms}</span>`;
                        $('#occupiedRooms').html(html_select);

                        html_select = `<span class="info-box-number">${data.clients}</span>`;
                        $('#clients').html(html_select);

                        html_select = `<span class="info-box-number">${data.userTotal}</span>`;
                        $('#userTotal').html(html_select);

                        data = JSON.stringify(data);
                        localStorage.setItem("DataOnTime",data);
                }

            });
        }
    </script>

@endsection

