@extends('layouts.admin')

@section('page-title')
    {{__('Event')}}
@endsection
@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{route('dashboard')}}">{{__('Dashboard')}}</a></li>
    <li class="breadcrumb-item">{{__('Event')}}</li>
@endsection
@php
    $setting = \App\Models\Utility::settings();
@endphp

@section('action-btn')
    <div class="float-end">
        @can('create event')
            <a href="#" data-size="lg" data-url="{{ route('event.create') }}" data-ajax-popup="true" data-bs-toggle="tooltip" title="{{__('Create')}}" data-title="{{__('Create New Event')}}" class="btn btn-sm btn-primary">
                <i class="ti ti-plus"></i>
            </a>
        @endcan
    </div>
@endsection

@section('content')
<div class="row">
    <div class="col-lg-7 mb-4">
        <div class="card h-100 mb-0">
            <div class="card-header">
                <div class="row">
                    <div class="col-lg-6">
                        <h5>{{ __('Calendar') }}</h5>
                    </div>
                    <div class="col-lg-6">
                        @if (isset($setting['google_calendar_enable']) && $setting['google_calendar_enable'] == 'on')
                            <select class="form-control" name="calender_type" id="calender_type" onchange="get_data()">
                                <option value="goggle_calender">{{__('Google Calender')}}</option>
                                <option value="local_calender" selected="true">{{__('Local Calender')}}</option>
                            </select>
                        @endif
                        <input type="hidden" id="path_admin" value="{{url('/')}}">
                    </div>
                </div>
            </div>
            <div class="card-body">
                <div id='calendar' class='calendar'></div>
            </div>
        </div>
    </div>
    <div class="col-lg-5 mb-4">
        <div class="card h-100 mb-0">
            <div class="card-header">
                <h5>{{__('Upcoming Events')}}</h5>
            </div>
            <div class="card-body">
                <div class="event-wrp">
                    @if(!$events->isEmpty())
                        @foreach ($current_month_event as $event)
                            <div class="event-item d-flex align-items-center {{ $event->color }}">
                                <div class="event-content flex-1">
                                    <h6 class="mb-2">
                                        <a href="#" data-size="lg" data-url="{{ route('event.edit',$event->id) }}" data-ajax-popup="true" data-title="{{__('Edit Event')}}" class="dashboard-link">{{$event->title}}</a>
                                    </h6>
                                    <div class="date-wrp d-flex flex-wrap align-items-center">
                                        <div class="date text-sm">
                                            <span class="f-w-600">{{__('Start Date : ')}}</span>{{  \Auth::user()->dateFormat($event->start_date)}}
                                        </div>
                                        <div class="date text-sm">
                                            <span class="f-w-600">{{__('End Date : ')}}</span>{{  \Auth::user()->dateFormat($event->end_date) }}
                                        </div>
                                    </div>
                                </div>
                                <div class="action-btns d-flex flex-column gap-2">
                                    <a href="#" data-url="{{ route('event.edit',$event->id) }}" data-title="{{__('Edit Event')}}" data-ajax-popup="true" class="btn btn-sm bg-white shadow-sm p-1 d-flex" data-bs-toggle="tooltip" title="{{__('Edit')}}" data-original-title="{{__('Edit')}}">
                                        <i class="ti ti-pencil text-info"></i>
                                    </a>
                                    {!! Form::open(['method' => 'DELETE', 'route' => ['event.destroy', $event->id],'id'=>'delete-form-'.$event->id]) !!}
                                        <a href="#" class="btn btn-sm bs-pass-para bg-white shadow-sm d-flex p-1" data-bs-toggle="tooltip" title="{{__('Delete')}}" data-original-title="{{__('Delete')}}" data-confirm="{{__('Are You Sure?').'|'.__('This action can not be undone. Do you want to continue?')}}" data-confirm-yes="document.getElementById('delete-form-{{$event->id}}').submit();">
                                            <i class="ti ti-trash text-danger"></i>
                                        </a>
                                    {!! Form::close() !!}
                                </div>
                            </div>
                        @endforeach
                    @else
                        <div class="text-center">
                            <h6>{{__('There is no event in this month')}}</h6>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

@push('script-page')
    <script src="{{ asset('assets/js/plugins/main.min.js') }}"></script>

    <script type="text/javascript">

        $(document).ready(function()
        {
            get_data();
        });
        function get_data()
        {
            var calender_type=$('#calender_type :selected').val();
            $('#calendar').removeClass('local_calender');
            $('#calendar').removeClass('goggle_calender');
            if(calender_type==undefined){
                $('#calendar').addClass('local_calender');
            }
            $('#calendar').addClass(calender_type);
            $.ajax({
                url: $("#path_admin").val()+"/event/get_event_data" ,
                method:"POST",
                data: {"_token": "{{ csrf_token() }}",'calender_type':calender_type},
                success: function(data) {
                    (function() {
                        var etitle;
                        var etype;
                        var etypeclass;
                        var calendar = new FullCalendar.Calendar(document.getElementById('calendar'), {
                            headerToolbar: {
                                left: 'prev,next today',
                                center: 'title',
                                right: 'dayGridMonth,timeGridWeek,timeGridDay'
                            },
                            buttonText: {
                                timeGridDay: "{{ __('Day') }}",
                                timeGridWeek: "{{ __('Week') }}",
                                dayGridMonth: "{{ __('Month') }}"
                            },
                            themeSystem: 'bootstrap',
                            slotDuration: '00:10:00',
                            navLinks: true,
                            droppable: true,
                            selectable: true,
                            selectMirror: true,
                            editable: true,
                            dayMaxEvents: true,
                            handleWindowResize: true,
                            events: data,
                        });
                        calendar.render();
                    })();
                }
            });
        }
    </script>

    <script>
        $(document).ready(function() {
            var b_id = $('#branch_id').val();
            getDepartment(b_id);
        });
        $(document).on('change', 'select[name=branch_id]', function() {
            var branch_id = $(this).val();
            getDepartment(branch_id);
        });

        function getDepartment(bid) {

            $.ajax({
                url: '{{ route('event.getdepartment') }}',
                type: 'POST',
                data: {
                    "branch_id": bid,
                    "_token": "{{ csrf_token() }}",
                },
                success: function(data) {

                    $('.department_id').empty();
                    var emp_selct = ` <select class="form-control  department_id" name="department_id[]" id="choices-multiple"
                                            placeholder="Select Department" multiple >
                                            </select>`;
                    $('.department_div').html(emp_selct);

                    $('.department_id').append('<option value="0"> {{ __('All') }} </option>');
                    $.each(data, function(key, value) {
                        $('.department_id').append('<option value="' + key + '">' + value +
                            '</option>');
                    });
                    new Choices('#choices-multiple', {
                        removeItemButton: true,
                    });
                }
            });
        }

        $(document).on('change', '.department_id', function() {
            var department_id = $(this).val();
            getEmployee(department_id);
        });

        function getEmployee(did) {
            $.ajax({
                url: '{{ route('event.getemployee') }}',
                type: 'POST',
                data: {
                    "department_id": did,
                    "_token": "{{ csrf_token() }}",
                },
                success: function(data) {

                    $('.employee_id').empty();
                    var emp_selct = ` <select class="form-control  employee_id" name="employee_id[]" id="choices-multiple1"
                                            placeholder="Select Employee" multiple >
                                            </select>`;
                    $('.employee_div').html(emp_selct);

                    $('.employee_id').append('<option value="0"> {{ __('All') }} </option>');
                    $.each(data, function(key, value) {
                        $('.employee_id').append('<option value="' + key + '">' + value +
                            '</option>');
                    });
                    new Choices('#choices-multiple1', {
                        removeItemButton: true,
                    });
                }
            });
        }
    </script>
@endpush
