@extends('layouts.reports')

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

@section('content')

    {{-- Styles --}}
    <style>
        table th, table td {
            vertical-align: middle !important;
        }
        td {
            word-break: break-all;

        }
    </style>

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

        {{-- Header --}}
        <section class="flex-wrap px-3 d-flex justify-content-between align-items-center cardtitle oculto-impresion">

            {{-- Action & Titles --}}
            <div class="flex-wrap py-2 justify-content-between align-items-center d-flex">
                {{-- Buttons --}}
                <div class="mr-3 d-flex">
                    {{-- Go Back --}}
                    <a type="button" href="{{ route('hotel.reports') }}"
                        class="mx-1 fixed-width oculto-impresion btn return-button"
                    >
                        <img src="{{ asset('icons/arrow_inverse.svg') }}">
                    </a>

                    {{-- Print --}}
                    <button onclick="imprimir()"
                        class="px-0 mx-1 fixed-width oculto-impresion btn harsi-button"
                    >
                        <img src="{{ asset('icons/print.svg') }}">
                    </button>

                    {{-- Download as CSV --}}
                    <button onclick="print_csv()"
                        class="px-0 mx-1 btn btn-success fixed-width oculto-impresion download-csv-button"
                        title="{{ __('reports')['hotel']['common']['download'] }}"
                    >
                        <img src="{{ asset('icons/csv_white.svg') }}">
                    </button>
                </div>

                {{-- Title --}}
                <h5 class="mb-0 oculto-impresion">
                    {{ __('reports.hotel.statisticalReports.clientes.clients_per_period') }}
                </h5>
            </div>

            {{-- Filter Form --}}
            <form id="filter_report" class="py-2 d-flex align-items-center">
                {{-- Year --}}
                <div class="mx-1 input-group">
                    <div class="input-group-prepend">
                        <span class="px-3 input-group-text harsi-color fw-bold">
                            {{ __('reports')['hotel']['common']['from'] }}
                        </span>
                    </div>

                    <input type="date" id="from" class="form-control"
                        value="{{ now()->format('Y-m-01') }}">
                </div>

                {{-- Month --}}
                <div class="mx-1 input-group">
                    <div class="input-group-prepend">
                        <span class="px-3 input-group-text harsi-color fw-bold">
                            {{ __('reports')['hotel']['common']['to'] }}
                        </span>
                    </div>

                    <input type="date" id="to" class="form-control"
                        value="{{ now()->format('Y-m-t') }}">
                </div>

                {{-- Submit --}}
                <button type="submit" class="mx-2 btn harsi-button">
                    {{ __('common_actions')['filter'] }}
                </button>
            </form>

        </section>

        {{-- Title for Print --}}
        <section class="mb-4">
            <h2 class="text-center col-lg-12" id="uno">
                {{ __('reports.hotel.statisticalReports.clientes.clients_per_period') }}
            </h2>
            <h3 class="mt-1 text-center col-lg-12 fw-bold" id="uno"><span id="period_of_clients"></span></h3>
        </section>

        {{-- Table --}}
        <div class="mb-5 table-responsive">
            <table class="table mb-4 text-center cardtable" id="example">
                <thead>
                    <tr class="cardtabletr">
                        <th scope="col" width="22.5%" class="text-center">{{ __('entity_fields')['reservation']['guest'] }}</th>
                        <th scope="col" width="12.5%" class="text-center">{{ __('entities')['hotel']['reservation'] }}</th>
                        <th scope="col" width="12%" class="text-center">{{ __('entity_fields')['reservation']['contract'] }}</th>
                        <th scope="col" width="12%" class="text-center">{{ __('entities')['hotel']['reservation_source'] }}</th>
                        <th scope="col" width="18%" class="text-center">{{ __('entity_fields')['reservation']['length_of_stay_large'] }}</th>
                        <th scope="col" width="14%" class="text-center">{{ __('entity_fields')['reservation']['room'] }}</th>
                        <th scope="col" class="text-center">PAX</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>

    </div>

    {{-- Scripts --}}
    <script>
        // Global Controls
        $(document).ready(function () {
          // Redraw Table for Filter
            $('#filter_report').on('submit', function (e) {
                e.preventDefault();
                reportTable.ajax.reload();
            });

          // Table Controller
            const reportTable =  $('#example').DataTable({
                language: getDataTableLanguageConfig(),
                "order": [0, 'asc'],
                lengthMenu: [[10, 25, 50, 100, -1],[10, 25, 50, 100, 'All']],
                processing: false,
                serverSide: true,

                ajax: {
                    url: "{{ route('hotel.reports.clientsPerPeriod') }}",
                    type: "GET",
                    data: function (d) {
                        d.from = $('#from').val();
                        d.to = $('#to').val();
                    }
                },
                columns: [
                    { data: 'guest', searchable: true },
                    { data: 'id', name: 'id', searchable: false },
                    { data: 'contract', searchable: true },
                    { data: 'source', searchable: true },
                    { data: 'stay', searchable: true },
                    { data: 'room_data', searchable: true },
                    { data: 'pax_compute', searchable: true },
                ]
            });
        });

        // Table Data Cycle
        const cargarTabla = ()=>$('#example').DataTable().ajax.reload(null, false);
        $(document).ready(() => setInterval(cargarTabla, 30000) ); //Cada 30 segundos (30 mil milisegundos)

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

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

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

        function print_csv() {
            var from = $("#from").val();
            var to = $("#to").val();

            window.location = `/hotel/csv/clients-per-period?from=${from}&to=${to}`
        }

    </script>

@endsection
