@extends('layouts.reports')

@section('title')
Hotel | {{ __('reports.hotel.statisticalReports.reservas.arrives_per_period') }}
@endsection

@section('content')

    {{-- Content --}}
    <div class="mt-4 container-fluid">

        {{-- Header --}}
        <div class="mb-4 row cardtitle oculto-impresion">

            {{-- Buttons --}}
            <div class="boton">

                {{-- Go Back --}}
                <a type="button" style=" background-color: #fff !important;" class="oculto-impresion btn "
                    href="{{ route('hotel.reports') }}">
                    <img src="{{ asset('icons/arrow_inverse.svg') }}">
                </a>
                &nbsp;

                {{-- Print --}}
                <button style=" background-color: #0D6889 !important;" class="oculto-impresion btn" onclick="imprimir()">
                    <img src="{{ asset('icons/print.svg') }}">
                </button>
                &nbsp;

                {{-- Download as CSV --}}
                <button
                    style="color: #f5f5f5 !important;"
                    onclick="print_csv()"
                    class="btn btn-success oculto-impresion"
                    title="{{ __('reports')['hotel']['common']['download'] }}"
                >
                    <img src="{{ asset('icons/csv_white.svg') }}">
                </button>
            </div>

            {{-- Title --}}
            <h5 class="mr-5 oculto-impresion col-lg-3 col-md-3">
                {{ __('reports.hotel.statisticalReports.reservas.arrives_per_period') }}
            </h5>

            {{-- Filter Form --}}
                {{-- From --}}
                <p> {{ __('reports')['hotel']['common']['from'] }} </p>&nbsp;
                <input type="date" id="txtMin" class="mr-3 col-lg-2 col-md-2"
                    value="<?php echo date("Y-m-01");?>">

                {{-- To --}}
                <p> {{ __('reports')['hotel']['common']['to'] }} </p> &nbsp;
                <input type="date" id="txtMax" class="mr-3 col-lg-2 col-md-2"
                    value="<?php echo date("Y-m-t");?>">

                {{-- Submit --}}
                <div align="right">
                    <button id="btnGo" align="center" type="button" class="mt-1 btn"
                        style=" background-color: #0D6889 !important">
                        {{ __('common_actions')['filter'] }}
                    </button>
                </div>
        </div>

        {{-- Title for Print --}}
        <h2 class="text-center col-lg-12" id="uno">
            {{ __('reports.hotel.statisticalReports.reservas.arrives_per_period') }}
        </h2>
        <h3 class="mt-3 text-center col-lg-12" id="uno"><span id="period_of_arrives"></span></h3>

        {{-- Table --}}
        <div class="mb-5 table-responsive">
            <table class="table text-center cardtable table-sm" id="example">
                <thead>
                    <tr class="cardtabletr">
                        <th class="text-center" scope="col">ID</th>
                        <th class="text-center" scope="col">{{ __('entity_fields')['reservation']['client'] }}</th>
                        <th class="text-center" scope="col">{{ __('entity_fields')['reservation']['agency'] }}</th>
                        <th class="text-center" scope="col">Check-In</th>
                        <th class="text-center" scope="col">Check-Out</th>
                        <th class="text-center" scope="col">{{ __('entity_fields')['reservation']['room'] }}</th>
                        <th class="text-center" scope="col">{{ __('entity_fields')['common']['room_type'] }}</th>
                        <th class="text-center" scope="col">{{ __('entity_fields')['reservation']['number_adults'] }}</th>
                        <th class="text-center" scope="col">{{ __('entity_fields')['reservation']['number_children'] }}</th>
                        <th class="text-center" scope="col">{{ __('entity_fields')['reservation']['number_nights'] }}</th>
                        <th class="text-center" scope="col">{{ __('entity_fields')['reservation']['regimen'] }}</th>
                    </tr>
                </thead>
            </table>
        </div>

    </div>

    <script>
        // Filter Executor
        $.fn.dataTable.ext.search.push(
            function (settings, data, dataIndex) {
                var valid = true;
                var min = moment($("#txtMin").val());
                if (!min.isValid()) {
                    min = null;
                }

                var max = moment($("#txtMax").val());
                if (!max.isValid()) {
                    max = null;
                }

                if (min === null && max === null) {
                    // no filter applied or no date columns
                    valid = true;
                } else {

                    $.each(settings.aoColumns, function (i, col) {

                        if (col.type == 'date') {
                            var cDate = moment(data[i]);

                            if (cDate.isValid()) {
                                if (max !== null && max.isBefore(cDate)) {
                                    valid = false;
                                }
                                if (min !== null && cDate.isBefore(min)) {
                                    valid = false;
                                }
                            } else {
                                valid = false;
                            }
                        }
                    });
                }
                return valid;
            }
        );

        // Table Data Cycle
        $(document).ready(function () {

            // Redraw Table for Filter
            $("#btnGo").click(function () {
                $('#example').DataTable().draw();
            });

            // Table Controller
            $('#example').DataTable({
                "order": [0, 'asc'],

                lengthMenu: [
                    [-1, 5, 10, 25, 50, 100],
                    ['All', 5, 10, 25, 50, 100]
                ],

                language: getDataTableLanguageConfig(),
                ajax: {
                    url: "{{ route('hotel.reports.arrivesPerPeriod') }}",
                },
                columns: [
                    {
                        data: 'id',
                        name: 'id'
                    },
                    {
                        data: 'client_reference',
                        name: 'client_reference'
                    },
                    {
                        data: 'agency',
                    },
                    {
                        data: 'date_entry',
                        name: 'date_entry',
                        type: 'date',
                    },
                    {
                        data: 'date_departure',
                        name: 'date_departure',
                    },
                    {
                        data: 'room',
                    },
                    {
                        data: 'room_type',
                    },
                    {
                        data: 'number_adults',
                        name: 'number_adults'
                    },
                    {
                        data: 'number_children',
                        name: 'number_children'
                    },
                    {
                        data: 'number_nights',
                        name: 'number_nights'
                    },
                    {
                        data: 'regimen',
                    },
                ]
            });
        });

        function cargarTabla() {
            $('#example').DataTable().ajax.reload(null, false);
        }

        $(document).ready(function () {
            setInterval(cargarTabla, 30000); //Cada 30 segundos (30 mil milisegundos)
        });

        // Report Interactions
        function imprimir() {
            const beforePrint = function () {
                const from = $('#txtMin').val();
                const to = $('#txtMax').val();

                $('#period_of_arrives').text(`${formatDate(from)} - ${formatDate(to)}`);
            };

            if (isIosDevice()) {
                openPrintWindow(beforePrint);
            } else {
                beforePrint();
                window.print();
            }
        }

        function print_csv() {
            var start = $("#txtMin").val();
            var end = $("#txtMax").val();

            window.location = `/hotel/csv/arrives-per-period/${start}/${end}`
        }

    </script>

@endsection
