<link href="{{ asset('css/components/tables/entity-tables.css') }}" rel="stylesheet">

@extends('adminlte::page')
@section('plugins.Datatables',true)

@section('title', __('modules')['dashboard']['restaurant']['orders'])

@section('content')

    {{-- Body --}}
    <div class="col-xs-12">
        {{-- Title --}}
        <div class="mb-3 row cardtitle-nota">
            <div class="col-12">
                <h3 align="center">{{ __('entity_forms')['orders']['page']  }}</h3>
            </div>
        </div>

        {{-- Table Structure --}}
        <div class="table-responsive">
            <table id="orders_table" class="table text-center cardtable">
                <thead>
                    <tr class="cardtabletr">
                        <th scope="col">Folio</th>
                        <th scope="col">{{ __('entity_fields')['common']['date']  }}</th>
                        <th scope="col">{{ __('entity_fields')['order']['table']  }}</th>
                        <th scope="col">{{ __('entity_fields')['order']['cashier']  }}</th>
                        <th scope="col">{{ __('entity_fields')['common']['payment_method']  }}</th>
                        <th scope="col">{{ __('entity_fields')['order']['total']  }}</th>
                        <th scope="col">{{ __('entity_fields')['common']['created_at']  }}</th>
                        <th scope="col" width="15%">{{ __('entity_fields')['common']['actions']  }}</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>

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

    {{-- DataTable Controller & Cycle --}}
    <script>
      // Initialization
        $(document).ready(function() {
          // Table Construction
            $('#orders_table').DataTable({
                language: getDataTableLanguageConfig(),
                processing: false,
                serverSide: true,
                ajax: {
                    url: "{{ route('orders.index') }}",
                },
                columns: [
                    {
                        data: 'id',
                        name: 'id'
                    },
                    {
                        data: 'order_date',
                        name: 'order_date'
                    },
                    {
                        data: 'order_table',
                        name: 'order_table'
                    },
                    {
                        data: 'order_cashier',
                        name: 'order_cashier'
                    },
                    {
                        data: 'order_payment_method',
                        name: 'order_payment_method'
                    },
                    {
                        data: 'order_total',
                        name: 'order_total',
                        searchable: false,
                        orderable: false
                    },
                    {
                        data: 'created_at',
                        name: 'created_at',
                        searchable: false,
                        orderable: false
                    },
                    {
                        data: 'action',
                        name: 'action',
                        searchable: false,
                        orderable: false
                    }
                ]
            });


          // CRUD Actions
            // Delete
            $(document).on('click', '.delete', function () {
                var id = $(this).attr('id');
                Swal.fire({
                    type: 'warning',
                    title: "{{ __('common_responses_and_messages')['confirm_deletion'] }}",
                    confirmButtonText: "{{ __('common_actions')['approve'] }} {{ __('common_actions')['confirm_deletion'] }}",
                    confirmButtonColor: `#0d6889`,
                    showCancelButton: true,
                    cancelButtonText: `{{ __('common_actions')['cancel'] }}`,
                    cancelButtonColor: `#c82333`
                }).then((result) => {
                    /* Read more about isConfirmed, isDenied below */
                    if (result.value) {
                        $.ajax({
                            url: `/orders/${id}/destroy`,
                            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                            type: 'DELETE',
                            success: function (data) {
                                setTimeout(function () {
                                    Swal.fire(
                                        "{{ __('common_responses_and_messages')['deleted'] }}",
                                        "{{ __('common_responses_and_messages')['deleted_successfully'] }}",
                                        'success'
                                    );
                                    $('#orders_table').DataTable().ajax.reload(null, false);
                                }, 200);
                            }
                        });
                    }
                });
            });
        });

      // Table Control
        function cargarTabla(){
            $('#orders_table').DataTable().ajax.reload(null, false);
        }

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

      // Support Functions
        function invoiceService(service_type, service_id) {
            window.open(`/invoicing/invoices/create/${service_type}/${service_id}`, '_blank');
            window.location.href = `/orders/${service_id}`;
        }

    </script>

@endsection


