@extends('layouts.layout-2')

@section('content')

    <style>
        body {
         
            
        }
        .btn-xs {
            line-height: 1.3em;
        }
        .table tbody + tbody {
            border-top: 1px solid #9c9c9c;
        }
        .table th, .table td {
            border-top: none;
        }
        .table tr.border-none td, .table tr.border-none th  {
            border-top: none;
        }
        .table .thead-dark th {
            color: #4E5155;
            background-color: rgba(24, 28, 33, 0.1);
            border-color: rgba(63, 69, 74, 0.1);
        }

        .input-group-text {
            padding: 0.438rem 0.475rem;
        }


    </style>

    <div class="float-right mt-3">
        <a href="{{route('leads')}}" class="btn btn-sm btn-default">{{ __('zur Übersicht') }}</a>
        <a href="{{ make_old_url('/index.php/leads/'.$lead->id.'/edit') }}" class="btn btn-sm  btn-default float-right">{{ __('zurück ins CRM v2') }}</a>
    </div>

    <h4 class="font-weight-bold py-3 mb-1">
        Anfrage verwalten
    </h4>

    <ul class="nav nav-sm nav-tabs nav-justified tabs-alt mb-3" id="top-nav-quick-jump">
        <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)" data-collapse="#collapseLeadCustomer">
                Kunde
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)" data-collapse="#collapseLeadDetail">
                Anfrage
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)" data-collapse="#collapseLeadStatus">
                Status
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)" data-collapse="#collapseLeadBooking">
                Buchung
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)" data-collapse="#collapseLeadMails">
                E-Mails
                <span class="badge badge-outline-primary">{{ $lead->lead_mails->count() }}</span>
            </a>
        </li>
       <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)" data-collapse="#collapseLeadFiles">
                PDF Dateien
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)" data-collapse="#collapseLeadNotice">
                Notizen
            </a>
        </li>
    </ul>

    {!! Form::open(['url' => route('lead_detail', [$id]), 'class' => 'form-horizontal', 'id'=>'lead-form-validation']) !!}
    <input type="hidden" name="id" id="id" value="{{$id}}">
    <!-- Kunde -->
    @include('lead._detail_customer')
    <!-- Anfrage -->
    @include('lead._detail_lead')
    <!-- Status -->
    @include('lead._detail_status')
    {!! Form::close() !!}    

    @if($id !== "new" && $lead)
        @include('lead._detail_booking')
        @include('lead._detail_mails')
        @include('lead._detail_files')
        @include('lead._detail_notice')
    @endif


    <div class="float-right mt-3">
        <a href="{{route('leads')}}" class="btn btn-sm btn-default">{{ __('zur Übersicht') }}</a>
        <a href="{{ make_old_url('/index.php/leads/'.$lead->id.'/edit') }}" class="btn btn-sm btn-default float-right">{{ __('zurück ins CRM v2') }}</a>
    </div>

    <script>
        $(document).ready(function() {

            var collapseHashValue = null;
            $('[rel="tooltip"]').tooltip({trigger: "hover"});

            $('#top-nav-quick-jump .nav-link').on('click', function (e) {
                e.preventDefault();
                $('#top-nav-quick-jump .nav-link').removeClass('active');
                $(this).addClass('active');
                var collapse_id = $(this).data('collapse');
                //console.log(collapse_id);
                $(collapse_id).collapse('show');
                // animate
                $('html, body').animate({
                    scrollTop: $(collapse_id).parent('.card').offset().top
                }, 300, function(){
                    // when done, add hash to url
                    // (default click behaviour)
                    window.location.hash = collapse_id;
                });

            });

            $(".collapse").on('shown.bs.collapse', function (){
                if(collapseHashValue){
                    $('a[data-collapse="#'+collapseHashValue+'"]').click();
                    collapseHashValue = null;
                }
                CookiesAddJSONValue('lead_collapse', $(this).attr('id'));
                window.location.hash = "#"+$(this).attr('id');

            });

            $(".collapse").on('hidden.bs.collapse', function (){
                CookiesRemoveJSONValue('lead_collapse', $(this).attr('id'));
            });

            function init_site(){
                if(window.location.hash){
                    value = $(window.location.hash).attr('id');
                    if(value){
                        collapseHashValue = value;
                        CookiesAddJSONValue('lead_collapse', value);
                    }
                }
                var lead_collapses = Cookies.get('lead_collapse');
                if (lead_collapses != null)
                {
                    lead_collapses =  JSON.parse(lead_collapses);
                    for (var lead_collapse in lead_collapses){
                        $("#"+lead_collapses[lead_collapse]).collapse("show");
                    }
                }
            }
            init_site();

    
             var oTable = $('#datatables-lead-mails').DataTable({
                "processing": true,
                "serverSide": true,
                "searching": false,
                ajax: {
                    url: '{!! route( 'lead_mail_data_table' ) !!}',
                    data: function(d) {
                        d.model = 'LeadMail';
                        d.lead_id = $('#lead_mails_table input[name=lead_id]').val();
                        d.lead_mail_dir = $('#lead_mails_table input[name=lead_mail_dir]').val();
                        d.lead_mail_subdir = $('#lead_mails_table input[name=lead_mail_subdir]').val();
                        d.data_table_search = $('#dataTableInputSearchField').val();
                    }
                },
                "columns": [
                    { data: 'important', width: '15px', searchable: false },
                    { data: 'subject', name: 'subject', width: '', orderable: false, },
                    { data: 'date', name: 'date', width: '' },
                    { data: 'action',  width: '100px', orderable: false, searchable: false},
                ],
                "bLengthChange": false,
                "iDisplayLength": 25,
                "orderSequence": ["desc", "asc"],
                "order": [[ 2, "desc" ]],
                "language": {
                    "url": "/js/German.json"
                },
                "drawCallback": function( settings ) {
                    $('#datatables-lead-mails [rel="tooltip"]').tooltip({trigger: "hover"});
                    $('#datatables-lead-mails .lead-mail-ajax-action').on('click', function (event) {
                        ajax_object_action(event, $(this), callback_lead_mails_data_table);
                    });
                }
            });
            function callback_lead_mails_data_table(data) {
                if(data.status === 'success'){
                    oTable.draw();
                }
            }

            $('.messages-wrapper .sidebox-nav-item').on('click', function (event) {
                event.preventDefault();
                if($(this).hasClass('active')){
                    return;
                }
                $('#lead_mails_table input[name=lead_mail_dir]').val($(this).data('dir'));
                $('#lead_mails_table input[name=lead_mail_subdir]').val($(this).data('subdir'));
                if($(this).data('dir') <= 10){
                    $('.btn-compare-lead-mails').data('lead_mail_dir', $(this).data('dir'));
                    $('.btn-compare-lead-mails').data('lead_mail_subdir', $(this).data('subdir'));
                    $('.btn-compare-lead-mails').prop('disabled', false);
                }else{
                    $('.btn-compare-lead-mails').prop('disabled', true);
                }
                oTable.draw();
                $('.messages-wrapper .sidebox-nav-item').removeClass('active');
                $(this).addClass('active');
                $('#message-sidebox-title-icon').removeClass().addClass('fa ' + $(this).data('icon'));
                $('#message-sidebox-title-name').html($(this).data('name'));
                $('.messages-wrapper .sidebox-nav-item').find('.badge-primary').removeClass('badge-primary').addClass('badge-outline-primary');
                $(this).find('.badge').removeClass('badge-outline-primary').addClass('badge-primary');
            });

            $('#dataTableInputSearchField').keyup(function(){
                oTable.search($(this).val()).draw();
            });

        });
    </script>
@endsection