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

@extends('adminlte::page')

@section('title', __('modules')['dashboard']['restaurant']['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'];
        @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')

                    {{-- Report List --}}
                    <div class="row card-general justify-content-center">

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

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

                    </div>

                @endif

                {{-- Reportes de Restaurante --}}
                    @php
                        $show_hotel_reports = Auth::user()->report_permissions['general_reports'] ||
                                            Auth::user()->report_permissions['income_reports'];
                    @endphp

                @if ($show_hotel_reports || Auth::user()->role === 'administrador')

                    {{-- Divisor --}}
                    <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')['restaurant']['title'] }}
                                            </h3>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    {{-- Reportes Generales --}}
                    @if (Auth::user()->report_permissions['general_reports'] || Auth::user()->role === 'administrador')
                        <div class="panel panel-primary card-info">

                            {{-- Title --}}
                            <div class="panel-heading">
                                <h4 class="reportSubtitle">
                                    {{ __('reports')['restaurant']['generalReports'] }}
                                </h4>
                            </div>

                            {{-- Reportes --}}
                            <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

                    {{-- Estadísticos --}}
                        @php
                            $show_statistical_reports = Auth::user()->report_permissions['income_reports'];
                        @endphp
                    @if ($show_statistical_reports || Auth::user()->role === 'administrador')
                        <div class="card-info">
                            {{-- Titulo --}}
                            <div class="panel-heading">
                                <h4 class="reportSubtitle">
                                    {{ __('reports')['restaurant']['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')['restaurant']['statisticalReports']['incomes']['title'] }}
                                                    </td>
                                                </tr>
                                                @foreach ($statisticalReports->ingresos 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('restaurant.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

                                        </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')['restaurant']['not_allowed']['title'] }}
                            </h3>

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

                            {{-- Solution --}}
                            <p class="px-3 text-center">
                                {{ __('reports')['restaurant']['not_allowed']['solution_part_1'] }}
                                <span style="font-weight: bold; color:#0d6889">{{ __('reports')['restaurant']['not_allowed']['solution_part_2'] }}</span>
                                {{ __('neutral_words')['or'] }}
                                <span style="font-weight: bold; color:#0d6889">{{ __('reports')['restaurant']['not_allowed']['solution_part_3'] }}</span>
                                {{ __('reports')['restaurant']['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 getDataOnTime(){
            var resume = localStorage.getItem("DataOnTime");

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

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

                    if(data != resume){

                        data = JSON.parse(data);

                        var html_select = '';
                        var valSum = 0.00;

                        $('#saleDay').empty();
                        $('#saleMonth').empty();
                        $('#orderDay').empty();
                        $('#orderMonth').empty();
                        $('#products').empty();
                        $('#tables').empty();

                        valSum = parseFloat(data.saleDay);
                        valSum = valSum.toFixed(2);
                        html_select = `<span class="info-box-number">${formatCurrency(data.saleDay)}</span>`;
                        $('#saleDay').html(html_select);

                        valSum = parseFloat(data.saleMonth);
                        valSum = valSum.toFixed(2);
                        html_select = `<span class="info-box-number">${formatCurrency(data.saleMonth)}</span>`;
                        $('#saleMonth').html(html_select);

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

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

                } else {

                        var html_select = '';
                        var valSum = 0.00;

                        valSum = parseFloat(data.saleDay);
                        valSum = valSum.toFixed(2);
                        html_select = `<span class="info-box-number">${formatCurrency(data.saleDay)}</span>`;
                        $('#saleDay').html(html_select);

                        valSum = parseFloat(data.saleMonth);
                        valSum = valSum.toFixed(2);
                        html_select = `<span class="info-box-number">${formatCurrency(data.saleMonth)}</span>`;
                        $('#saleMonth').html(html_select);

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

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

            });
        }
    </script>

@endsection
