$(document).ready(function () {

});
    var todays_date = new Date();
    var selected_date = null;

    calender_load(todays_date)



    function calender_load(load_date) {
        $('#calendar').fullCalendar('destroy');
        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaDay,listWeek'
            },
            defaultDate: load_date,
            selectable: true,
            selectHelper: true,
            editable: true,
            navLinks: false, // can click day/week names to navigate views
            eventLimit: true, // allow "more" link when too many events
            events: {
                url: 'get_events_minutes',
                error: function () {
                    $('#script-warning').show();
                }
            },
            loading: function (bool) {
                $('#loading').toggle(bool);
            },
            eventRender: function (event, el) {
                // render the timezone offset below the event title
                if (event.start.hasZone()) {
                    el.find('.fc-title').after(
                        $('<div class="tzo"/>').text(event.start.format('Z'))
                    );
                }
            },
            dayClick: function (date) {
                selected_date = date;
                var returnData=get_day_meetings(date)

                if(returnData){
                     $('#calander_container').hide(500);
                     $('.event_container').show(500);
                }


            },
            eventMouseover: function (event, jsEvent, view) {
                var start = event.start._i;
                var end = '';
                if (event.end) {
                    end = event.end._i;
                }
                $(this).popover({
                    html: true,
                    animation: true,
                    delay: 300,
                    title: event.title,
                    placement: 'bottom',
                    trigger: 'manual',
                    content: '<div class=""> Start Date :' + start + '</div><div class=""> End Date : ' + end + '</div>',
                    container: '#calendar'
                }).popover('show');
            },

            eventMouseout: function (event, jsEvent, view) {
                $(this).popover('hide');
            }
        });
    }


    function get_day_meetings(meeting_date) {
        var date_data = meeting_date.format('YYYY-MM-DD')
        $.ajax({
            type: "GET",
            url: 'get_events_by_date',
            data: {
                meeting_type: 'scheduled',
                meeting_date: date_data
            },
            dataType: "json",
            success: function (data) {
                $('#meeting_card').empty();
                if (data.msgType == true) {

                    $.each(data.data, function (key, value) {
                        
                        if(value.status==1){
                           var header_color='success-color'; 
                        }else{
                            var header_color='warning-color';
                        }
                        var start = moment(value.start_date).format('DD-MM-YYYY');
                        var start_time = moment(value.start_time, 'HH:mm:ss').format('hh:mm A')

                        $('#meeting_card').append('<div class="col-md-4 card_row event_container">' +
                            '<div class="card">' +
                            '<div class="card-header '+header_color+' white-text" style="font-variant: petite-caps;">' +
                            'Meeting Details' +
                            '</div>' +
                            '<div class="card-body">' +
                            '<h4 class="card-title">'+value.name+'</h4>' +
                            '<a class="card-meta"> '+value.title+'</a>' +
                            '<p class="card-text">'+value.venue+'</p>' +
                            '<hr>' +
                            '<p class="card-meta float-left"><span> <i class="fa fa-calendar"></i> '+start+'</span></p>' +
                            '<p class="card-meta float-right"> <i class="fa fa-clock-o"></i> '+start_time+'</p>' +
                            '</div>' +
                            '<a href="#" class="meeting_view_btn" data-meeting_id="'+value.id+'">' +
                            '<div class="card-footer text-muted text-right">' +
                            '<p class="mb-0">Read Details <i class="fa fa-long-arrow-right" aria-hidden="true"></i></p>' +
                            '</div>' +
                            '</a>' +
                            '</div>' +
                            '</div>');

                    });
                   load_data()
                } else {
                       $('#meeting_card').append('<div class="col-md-12 text-center">' +
                                '<span style="font-size:18px"> No Meeting Found On '+meeting_date.format('DD-MM-YYYY')+'</span>'+
                            '</div>');
                    /*toastr.error(data.msg)*/
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {

                callAjaxErrorFunction(jqXHR, textStatus, errorThrown);
            },
            complete: function (data) {

            }
        });

        return true;

    }


    $('#back_to_calender').on('click', function (e) {
        e.preventDefault()
        $('#calander_container').show(500);
        $('.event_container').hide(500);
    })
    

    function load_data(){
        $('.meeting_view_btn').on('click',function(e){
            e.preventDefault()
            var meeting_id = $(this).data('meeting_id')
            
            if(meeting_id)
                get_meeting_data(meeting_id,0)
            
            
            $('#meeting_info_modal').modal('show');
            
        })
    }
    
   function load_data_mm(){
       $('.btn_mm_add').on('click',function(e){
           e.preventDefault()
           var agenda_id=$(this).data('agenda_id');
           var meeting_id=$(this).data('meeting_id');
           $('#meeting_id_data').val(agenda_id)
           $('#agenda_id_data').val(meeting_id)     
           $('#resolution_date').val('')
           $('#c_person').val('')  
           $('#description').val('')
           $('#resolution_date').val('')
           $('#mm_id').val('')
           
           $('.resolution_content').hide()
           $('#resolution_want').prop( "checked", false );
           
           $('#description').siblings('label.error').empty();           
           $('#c_person').siblings('label.error').empty();
           $('#mm_data_add').modal('show');
       })
       
       
       
       $('.mm_edit').on('click',function(e){
           e.preventDefault()
            var agenda_id=$(this).data('agenda_id');
            var meeting_id=$(this).data('meeting_id')
            var mm_id=$(this).data('mm_id')
             $.ajax({
                    type: "GET",
                    url: 'get_meeting_minute',
                    data: {
                        mm_id: mm_id
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.msgType == true) {
                                   $('#description').val(data.data.description)
                                   $('#meeting_id_data').val(agenda_id)
                                   $('#agenda_id_data').val(meeting_id)
                                   $('#mm_id').val(mm_id)
                                   if(data.data.resolution_date){
                                      $('#resolution_date').val( moment(data.data.resolution_date).format('DD-MM-YYYY'));
                                   }else{
                                      $('#resolution_date').val('');  
                                   }
                                   if(data.data.c_person){
                                      $('#c_person').val(data.data.c_person);
                                   }else{
                                      $('#c_person').val(''); 
                                   }
                                  if((data.data.resolution_date)||(data.data.c_person)){
                                    $('#resolution_want').prop( "checked", true );
                                    $('.resolution_content').show()
                                  }else{
                                    $('.resolution_content').hide()
                                    $('#resolution_want').prop( "checked", false );
                                  }
                                 $('#mm_data_add').modal('show');

                        }

                    },
                    error: function (jqXHR, textStatus, errorThrown) {

                        callAjaxErrorFunction(jqXHR, textStatus, errorThrown);
                    },
                    complete: function (data) {
                        $('.page-loader-wrapper').fadeOut();
                    }
                });

           
       })
   } 

/*   function get_meeting_br_id(){
       
   }*/
    
    function get_meeting_data(meeting_id,agenda_id){
            $.ajax({
                    type: "GET",
                    url: 'get_meetings_data_minutes',
                    data: {
                        meeting_id: meeting_id
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.msgType == true) {
                            if (data.data.meeting_data) {
                                    selected_meeting=meeting_id;
                                    bind_view_modal_data(data.data.meeting_data, data.data.agendas, data.data.participator,agenda_id)
                                 
                                   
                            }
                        }

                    },
                    error: function (jqXHR, textStatus, errorThrown) {

                        callAjaxErrorFunction(jqXHR, textStatus, errorThrown);
                    },
                    complete: function (data) {
                        $('.page-loader-wrapper').fadeOut();
                    }
                });

    }
    
    

    
    
    
    $('#resolution_date').bootstrapMaterialDatePicker({
        weekStart: 0,
        format: 'DD-MM-YYYY',
        clearButton: true,
        time: false
    }); 



    
   $('.mm_input').on('input',function(e){
       $(this).siblings('label.error').empty();
   })
    


    $('#mm_form_submit').submit('click',function(e){
        e.preventDefault()
        var description=$('#description').val();
        var agenda_id=$('#meeting_id_data').val();
        var meeting_id=$('#agenda_id_data').val();
        var resolution_date=$('#resolution_date').val();
        var mm_id=$('#mm_id').val(); 
   
        var c_person=$('#c_person').val();
        var error=0;
        
        if(!description){
            $('#description').siblings('label.error').empty();
            $('#description').siblings('label.error').append('Please Enter Description.');
            error=1;
        }
        
        if($('#resolution_want').prop('checked')==true){
            if(!c_person.trim()){
              $('#c_person').siblings('label.error').empty();
              $('#c_person').siblings('label.error').append('Please Enter Details OF Concerned Person.');
              error=1;              
            }

        }
            
        if((agenda_id) && (meeting_id) && (error==0)){
             $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: "POST",
                url: 'meeting_minutes_save',
                data: {
                    'agenda_id': agenda_id,
                    'meeting_id': meeting_id,
                    'description': description,
                    'resolution_date': resolution_date,
                    'mm_id':mm_id,
                    'c_person':c_person,
                },
                dataType: "json",
                success: function (data) {
                    if (data.msgType == true) {
                        get_meeting_data(meeting_id,agenda_id)
                        $('#mm_data_add').modal('hide');
                        toastr.success(data.msg)
                    } else {
                        toastr.error(data.msg)
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {

                    callAjaxErrorFunction(jqXHR, textStatus, errorThrown);
                },
                complete: function (data) {

                }
            });        
        }

    })

   $('.resolution_content').hide(500)
   $('.slider').on('click',function(e){
      if($('#resolution_want').prop('checked')==false){
         $('.resolution_content').show(500)
      }else{
          $('.resolution_content').hide(500)
          $('#resolution_date').val('')
          $('#c_person').val('')
      }
   })
    
    
