@extends('layouts.layout-2')

@section('content')
    <h4 class="font-weight-bold py-3 mb-1">
        {{ __('Anfragen') }}
    </h4>

    <div class="card">
        <div class="card-header">
            <div class="">
                <div class="form-row align-items-center">
                    <div class="col-md-4 mb-3">
                        <label class="form-label" for="travel_option_search">Filter Anreise</label>
                        <select class="custom-select" name="travel_option_search" id="travel_option_search">
                            <option value="">Filter aus</option>
                            <option value="before_2">Anreise vor 2 Monaten</option>
                            <option value="brefore_1">Anreise vor 1 Monat</option>
                            <option value="on_site">vor Ort</option>
                            <option value="after_1">Anreise in 1 Monat</option>
                            <option value="after_1">Anreise in 2 Monat</option>
                        </select>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Filter Anreise Datum</label>
                        <div class="row">
                            <div class="input-group col-6 pr-0">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">ab</span>
                                </div>
                                <input class="form-control datepicker-base" name="arrival_start_date" type="text" value="">
                            </div>

                            <div class="input-group col-6 pl-1">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">bis</span>
                                </div>
                                <input class="form-control datepicker-base" name="arrival_end_date" type="text" value="">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Filter Abreise Datum</label>
                        <div class="row">
                            <div class="input-group col-6 pr-0">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">ab</span>
                                </div>
                                <input class="form-control datepicker-base" name="departure_start_date" type="text" value="">
                            </div>

                            <div class="input-group col-6 pl-1">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">bis</span>
                                </div>
                                <input class="form-control datepicker-base" name="departure_end_date" type="text" value="">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-row align-items-center">
                    <div class="col-md-3 mb-3">
                        <label class="form-label" for="travel_option_country_id">Filter Reiseland</label>
                        <select class="custom-select" name="travel_option_country_id" id="travel_option_country_id">
                            <option value="">Filter aus</option>
                            @foreach($travel_countries as $id=>$name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3 mb-3">
                        <label class="form-label" for="travel_option_agenda_id">Filter Programm</label>
                        <select class="custom-select" name="travel_option_agenda_id" id="travel_option_agenda_id">
                            <option value="">Filter aus</option>
                        </select>
                    </div>
                    <div class="col-md-3 mb-3">
                        <label class="form-label" for="travel_option_company_id">Filter Reiseveranstalter</label>
                        <select class="custom-select" name="travel_option_company_id" id="travel_option_company_id">
                            <option value="">Filter aus</option>
                            @foreach($filter_travel_company as $id=>$name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3 mb-3">
                        <label class="form-label" for="travel_option_lead_status_id">Filter Status</label>
                        <select class="custom-select" name="travel_option_lead_status_id" id="travel_option_lead_status_id">
                            <option value="">Filter aus</option>
                            @foreach($filter_lead_status as $id=>$name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="form-row align-items-center">
                    <div class="col-md-3 mb-3">
                        <label class="form-label" for="travel_option_paying_out">Filter K-Wunsch</label>
                        <select class="custom-select" name="travel_option_paying_out" id="travel_option_paying_out">
                            <option value="">Filter aus</option>
                            @foreach($filter_paying_out as $id=>$name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3 mb-3">
                        <label class="form-label" for="travel_option_paying_out_status">Filter KW-Status</label>
                        <select class="custom-select" name="travel_option_paying_out_status" id="travel_option_paying_out_status">
                            <option value="">Filter aus</option>
                            @foreach($filter_paying_out_status as $id=>$name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2 mb-3">
                        <label class="form-label" for="travel_option_refund">Filter Refund</label>
                        <select class="custom-select" name="travel_option_refund" id="travel_option_refund">
                            <option value="">Filter aus</option>
                            @foreach($filter_refund as $id=>$name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2 mb-3">
                        <label class="form-label" for="travel_option_xx_tkt">Filter XX TKT</label>
                        <select class="custom-select" name="travel_option_xx_tkt" id="travel_option_xx_tkt">
                            <option value="">Filter aus</option>
                            @foreach($filter_xx_tkt as $id=>$name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2 mb-3">
                        <label class="form-label" for="travel_option_airline_id">Filter Airline</label>
                        <select class="custom-select" name="travel_option_airline_id" id="travel_option_airline_id">
                            <option value="">Filter aus</option>
                            @foreach($filter_airlines as $id=>$name)
                                <option value="{{$id}}">{{$name}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="form-row align-items-center">
                    <div class="col-5 col-sm-2 mb-2">
                        <label class="form-label">AnfrageID</label>
                        <input class="form-control full_search" name="full_lead_id_search" placeholder="suche" type="text" value="">
                    </div>
                    <div class="col-6 col-sm-3 mb-2">
                        <label class="form-label">Vorname</label>
                        <input class="form-control full_search" name="full_firstname_search" placeholder="suche"  type="text" value="">
                    </div>
                    <div class="col-6 col-sm-3 mb-2">
                        <label class="form-label">Nachname</label>
                        <input class="form-control full_search" name="full_lastname_search" placeholder="suche"  type="text" value="">
                    </div>

                    <div class="col-5 col-sm-2 mb-2">
                        <label class="form-label">BuchungsID</label>
                        <input class="form-control full_search" name="full_booking_id_search" placeholder="suche"  type="text" value="">
                    </div>
                    <div class="col-2 col-sm-2 mb-2 mt-4">
                        <a href="{{ route('requests') }}" class="btn icon-btn btn-sm btn-outline-dark float-right">
                            <span class="fa fa-sync"></span>
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <div class="card-datatable table-responsive py-2">
            <input type="hidden" name="sort_travel_country_id" value="">
            <input type="hidden" name="sort_travelagenda_id" value="">
            <input type="hidden" name="sort_travel_documents" value="">
            <input type="hidden" name="sort_sf_guard_user_id" value="">
            <input type="hidden" name="sort_lead_status_id" value="">

            <table id="datatables-requests" class="table table-striped table-bordered">
                <thead>
                <tr>
                    <th style="max-width: 10px;">&nbsp;</th>
                    <th>{{__('AnfrageID')}}</th>
                    <th>{{__('Vorname')}}</th>
                    <th>{{__('Nachname')}}</th>
                    <th style="max-width: 10px;">&nbsp;</th>
                    <th>{{__('BuchungsID')}}</th>
                    <th>{{__('Reiseland')}}</th>
                    <th>{{__('Programm')}}</th>
                    <th>{{__('Reiseveranstalter')}}</th>
                   {{--<th>{{__('Kategorie')}}</th>--}}
                    <th>{{__('Anreise')}}</th>
                    <th>{{__('Abreise')}}</th>
                    <th>{{__('U.')}}</th>
                    <th>{{__('L.')}}</th>
                    <th>{{__('Sachbearbeiter')}}</th>
                    <th>{{__('Status')}}</th>
                    <th>{{__('E-Mail')}}</th>
                    <th>{{__('K-Wunsch')}}</th>
                    <th>{{__('KW-Status')}}</th>
                    <th>{{__('Al')}}</th>
                    <th>{{__('Refund')}}</th>
                    <th>{{__('H')}}</th>
                    <th>{{__('XX TKT')}}</th>
                </tr>
                </thead>
            </table>
        </div>
        <div class="card-footer d-flex flex-wrap justify-content-between align-items-center px-0 pt-0 pb-3">
            <div class="px-4 pt-3">
            </div>
            <div class="px-4 pt-3">
                <button type="button" class="btn btn-info" data-toggle="modal"
                        data-target="#modals-load-next"
                        data-id="new"
                        data-model="customerMail"
                        data-action="new-customer-mail"
                        data-url="{{route('customer_mail_send_mail')}}"
                        data-redirect="back"
                        data-route="{{ route('requests_modal_load') }}"><i class="ion ion-md-mail-open"></i>&nbsp; E-Mail-Nachricht an Auswahl (max. 50)</button>
            </div>
        </div>
    </div>
    <script>
        $( document ).ready(function() {

            $('#travel_option_agenda_id').prop('disabled', true);

            var table = $('#datatables-requests').DataTable({
                "processing": true,
                "serverSide": true,
                "searching": false,
               ajax: {
                   url: '{!! route( 'data_table_requests' ) !!}',
                   data: function(d) {
                       d.arrival_start_date = $('input[name=arrival_start_date]').val();
                       d.arrival_end_date = $('input[name=arrival_end_date]').val();
                       d.departure_start_date = $('input[name=departure_start_date]').val();
                       d.departure_end_date = $('input[name=departure_end_date]').val();
                       d.sort_travel_country_id = $('input[name=sort_travel_country_id]').val();
                       d.sort_travelagenda_id = $('input[name=sort_travelagenda_id]').val();
                       d.sort_travel_documents = $('input[name=sort_travel_documents]').val();
                       d.sort_sf_guard_user_id = $('input[name=sort_sf_guard_user_id]').val();
                       d.sort_lead_status_id = $('input[name=sort_lead_status_id]').val();
                       d.full_firstname_search = $('input[name=full_firstname_search]').val();
                       d.full_lastname_search = $('input[name=full_lastname_search]').val();
                       d.full_lead_id_search = $('input[name=full_lead_id_search]').val();
                       d.full_booking_id_search = $('input[name=full_booking_id_search]').val();
                       d.travel_option_search = $('select[name=travel_option_search]').val();
                       d.travel_option_country_id = $('select[name=travel_option_country_id]').val();
                       d.travel_option_agenda_id = $('select[name=travel_option_agenda_id]').val();
                       d.travel_option_company_id = $('select[name=travel_option_company_id]').val();
                       d.travel_option_lead_status_id = $('select[name=travel_option_lead_status_id]').val();
                       d.travel_option_airline_id = $('select[name=travel_option_airline_id]').val();
                       d.travel_option_paying_out = $('select[name=travel_option_paying_out]').val();
                       d.travel_option_paying_out_status = $('select[name=travel_option_paying_out_status]').val();
                       d.travel_option_refund = $('select[name=travel_option_refund]').val();
                       d.travel_option_xx_tkt = $('select[name=travel_option_xx_tkt]').val();
                   }
               },
                "columns": [
                    { data: 'action_lead_edit', orderable: false, searchable: false},
                    { data: 'lead_id', name: 'lead_id' },
                    { data: 'customer.firstname', name: 'customer.firstname' },
                    { data: 'customer.name', name: 'customer.name' },
                    { data: 'action_booking_edit', orderable: false, searchable: false},
                    { data: 'id', name: 'id' },
                    { data: 'travel_country_id', name: 'travel_country_id', orderable: false },
                    { data: 'travelagenda_id', name: 'travelagenda_id', orderable: false },
                    { data: 'travel_company_id', name: 'travel_company_id', orderable: true },
                    { data: 'start_date', name: 'start_date' },
                    { data: 'end_date', name: 'end_date' },
                    { data: 'travel_documents', name: 'travel_documents', orderable: false },
                    { data: 'booking_services', name: 'booking_services', orderable: false },
                    { data: 'sf_guard_user_id', name: 'sf_guard_user_id', orderable: false },
                    { data: 'lead.status_id', name: 'lead.status_id',  orderable: false },
                    { data: 'last_customer_email', name: 'last_customer_email',  orderable: true },
                    { data: 'paying_out', name: 'paying_out' },
                    { data: 'paying_out_status', name: 'paying_out_status' },
                    { data: 'airline_id', name: 'airline_id' },
                    { data: 'refund', name: 'refund' },
                    { data: 'hold', name: 'hold' },
                    { data: 'xx_tkt', name: 'xx_tkt' },
                ],
                "bLengthChange": false,
                "iDisplayLength": 50,
                "orderSequence": ["desc", "asc"],
                "order": [[ 5, "desc" ]],
                "language": {
                    "url": "/js/German.json"
                },
                initComplete: function () {
                    /*this.api().columns(10).every( function () {
                        var column = this;
                        var title = $(column.header()).html();
                        var select = $('<select class="selectpicker"><option value="">'+title+'</option></select>')
                            .appendTo( $(column.header()).empty() )
                            .on( 'change', function () {
                                $('input[name=sort_travel_documents]').val($(this).val());
                                table.order( [ 3, 'desc' ] ).draw();
                            } );
                        column.data().unique().sort().each( function ( d, j ) {
                            if(d !== ""){
                                var text = $(d).data('order') ? "Ja" : "Nein";
                                if(text !== "" && text !== "-"){
                                    select.append( '<option value="'+$(d).data('order')+'">'+ text+'</option>' );
                                }
                            }
                        } );
                    } );*/
                    this.api().columns(13).every( function () {
                        var column = this;
                        var title = $(column.header()).html();
                        var select = $('<select class="selectpicker"><option value="">'+title+'</option></select>')
                            .appendTo( $(column.header()).empty() )
                            .on( 'change', function () {
                                $('input[name=sort_sf_guard_user_id]').val($(this).val());
                                table.order( [ 5, 'desc' ] ).draw();
                            } );
                        column.data().unique().sort().each( function ( d, j ) {
                            if(d !== ""){
                                var text = $(d+' span').text();
                                if(text !== "" && text !== "-"){
                                    select.append( '<option value="'+$(d).data('order')+'">'+ $(d+' span').text()+'</option>' );
                                }
                            }
                        } );
                    } );
                   /* this.api().columns(12).every( function () {
                        var column = this;
                        var title = $(column.header()).html();
                        var select = $('<select class="selectpicker"><option value="">'+title+'</option></select>')
                            .appendTo( $(column.header()).empty() )
                            .on( 'change', function () {
                                $('input[name=sort_sf_guard_user_id]').val($(this).val());
                                table.order( [ 3, 'desc' ] ).draw();
                            } );
                        column.data().unique().sort().each( function ( d, j ) {
                            if(d !== ""){
                                var text = $(d+' span').text();
                                if(text !== "" && text !== "-"){
                                    select.append( '<option value="'+$(d).data('order')+'">'+ $(d+' span').text()+'</option>' );
                                }
                            }
                        } );
                    } );
                    */
                    $('.selectpicker').selectpicker();
                },
                drawCallback: function () {
                    //console.log($('#travel_option_agenda_id').prop('disabled'));
                    //only when is disabled, refesh list is load by other filter.

                    $('#datatables-requests [rel="tooltip"]').tooltip({trigger: "hover"});
                    $('#datatables-requests [data-toggle="popover"]').popover({trigger: "hover", content: get_popover_content,  html: true,});

                 /*   $('#datatables-requests [data-toggle="popover"]').hover(function() {
                        var e = $(this);
                        e.off('hover');
                        console.log(e.data('booking_id'))
                        e.popover({content: "test"}).popover('show');
                        /*$.get(e.data('load_id'),function(d) {
                            e.popover({content: d}).popover('show');
                        });
                    });*/


                   if($('#travel_option_agenda_id').prop('disabled')) {
                       var data = table.ajax.params();
                       //console.log(data);
                       var url = '{!! route( 'data_ajax_requests' ) !!}';
                       contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
                       $.ajax({
                           url: url,
                           data: data,
                           type: "POST",
                           dataType: "json",
                           cache: false,
                           contentType: contentType,
                           encode: true,
                           headers: {
                               'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                           },
                           success: function (data) {
                               // console.log(data);
                               var select = $('#travel_option_agenda_id');
                               select.empty().append('<option value="">Filter aus</option>');
                              // console.log(select);
                               $.each(data, function (i, item) {
                                   select.append('<option value="' + i + '">' + item + '</option>');
                               });
                               $('#travel_option_agenda_id').prop('disabled', false);
                           },
                           error: function (xhr, status, errorThrown) {
                               console.log(xhr);
                               console.log(xhr.responseText);
                               console.log(errorThrown);
                               console.log("Sorry, there was a problem!");
                           }
                       });
                   }
                }
            });


            function get_popover_content() {



                if ($(this).data('booking_id')) {
                    var data = {};
                    data['action'] = 'get_popover_booking_services';
                    data['booking_id'] = $(this).data('booking_id');
                    $(this).addClass("loading");
                    var icontent = $.ajax({
                        url: '{!! route( 'data_ajax_requests' ) !!}',
                        data: data,
                        type: "POST",
                        dataType: "html",
                        cache: false,
                        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                        encode: true,
                        async: false,
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        success: function() {
                            // just get the response
                        },
                        error: function() {
                            // nothing
                        }
                    }).responseText;
                    $(this).removeClass("loading");
                    return icontent;
                }
                return "Keine Buchungs-ID";
            }

            function clearTravelOptionAgenda(){
                $('#travel_option_agenda_id').empty().prop('disabled', true);
            }
            $('#travel_option_search').on('change', function(){
                if($(this).val() !== ""){
                    $('.datepicker-base').removeClass('active');
                    $('.datepicker-base').addClass('deactive');
                    $(this).removeClass('deactive');
                    $(this).addClass('active');
                    $('.datepicker-base').val(null);
                }else{
                    $('.datepicker-base').removeClass('deactive');
                    $(this).removeClass('active');
                }
                clearTravelOptionAgenda();
                table.draw();
            });
            $('#travel_option_country_id').on('change', function(){
                clearTravelOptionAgenda();
                table.draw();
            });
            $('#travel_option_agenda_id').on('change', function(){
                table.draw();
            });
            $('#travel_option_company_id').on('change', function(){
                table.draw();
            });
            $('#travel_option_lead_status_id').on('change', function(){
                table.draw();
            });
            $('#travel_option_airline_id').on('change', function(){
                table.draw();
            });

            $('#travel_option_paying_out').on('change', function(){
                table.draw();
            });
            $('#travel_option_paying_out_status').on('change', function(){
                table.draw();
            });
            $('#travel_option_refund').on('change', function(){
                table.draw();
            });
            $('#travel_option_xx_tkt').on('change', function(){
                table.draw();
            });

            $('.datepicker-base').on('change', function(){
                if($(this).val() != ""){
                    $('#travel_option_search').removeClass('active');
                    $('#travel_option_search').addClass('deactive');
                    $('.datepicker-base').removeClass('deactive');
                    $('.datepicker-base').addClass('active');
                    $('#travel_option_search').val(null);
                }else{
                    $('.datepicker-base').removeClass('active');
                    $('#travel_option_search').removeClass('deactive');
                }
                clearTravelOptionAgenda();
                table.order( [ 5, 'desc' ] ).draw();
            });
            $('.full_search').on('keyup', function(){
                clearTravelOptionAgenda();
                table.order( [ 5, 'desc' ] ).draw();
            });

            function loadModalInner(self, data){

                var url = data.route,
                    contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
                $.ajax({
                    url: url,
                    data: data,
                    type: "POST",
                    dataType: "json",
                    cache: false,
                    contentType: contentType,
                    encode: true,
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function(data) {
                        console.log(data);

                        $(data.response.target).find('.modal-dialog').html(data.html);
                        $('.selectpicker').selectpicker('refresh');
                        //datepicker_birthday_init();
                    },
                    error: function(xhr, status, errorThrown) {
                        console.log(xhr);
                        console.log(xhr.responseText);
                        console.log(errorThrown);
                        console.log("Sorry, there was a problem!");
                    }
                });
                return false;
            }

            $('#modals-load-next').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget);
                if (!button.data('id')) {
                    return;
                }
                var data = table.ajax.params();
                $.each(button.data(), function(index, value){
                    data[index] = value;
                });
                console.log(data['url']);
                loadModalInner(this, data);

            });
        });
    </script>
    <div class="modal fade" id="modals-load-next">
        <div class="modal-dialog modal-lg">

        </div>
    </div>
@endsection