  <?php require_once('../auth/auth.php');?>

<br>
<section class="content">
  <div class="container-fluid">
    <div class="row">
    	     <div class="col-md-2 "> </div>

     <div class="col-md-8 response">
       <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
         <div class="card-header" style="padding: 5px 8px;"><h3 class="card-title">Event Management</h3> <div class="card-tools"> <button type="button" class="btn btn-tool" data-card-widget="remove"> <i class="fas fa-times"></i>  </button> </div>
       </div>

    <div id='calendar'></div>
</div>
</div>
    	     <div class="col-md-2 "> </div>

</div>
</div>
</section>


<script>
$(document).ready(function () {
    var calendar = $('#calendar').fullCalendar({
        editable: true,
          header:{
     left:'prev,next today',
     center:'title',
     right:'month,agendaWeek,agendaDay'
    },
        events: "../event/event_autoload.php",
        displayEventTime: true,
        eventRender: function (event, element, view) {
            if (event.allDay === 'true') {
                event.allDay = true;
            } else {
                event.allDay = false;
            }
        },
        selectable: true,
        selectHelper: true,
        select: function (start, end, allDay) {
            var title = prompt('Event Title:');

            if (title) {
                var start = $.fullCalendar.formatDate(start, "Y-MM-DD <?php echo $time ?>");
                var end = $.fullCalendar.formatDate(end, "Y-MM-DD <?php echo $time ?> ");
                $.ajax({
                    url: '../event/new_event.php',
                    data: 'title=' + title + '&start=' + start + '&end=' + end,
                    type: "POST",
                    success: function (data) {
                        toastr.success(Showing, '<?php echo "Added Successfully" ?>');

                    }
                });
                calendar.fullCalendar('renderEvent',
                        {
                            title: title,
                            start: start,
                            end: end,
                            allDay: allDay
                        },
                true
                        );
            }
            calendar.fullCalendar('unselect');
        },
        
        editable: true,
        eventDrop: function (event, delta) {
                    var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD <?php echo $time ?>");
                    var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD <?php echo $time ?>");
                    $.ajax({
                        url: '../event/event_db.php',
                        data: 'title=' + event.title + '&start=' + start + '&end=' + end + '&updateid=' + event.id,
                        type: "POST",
                        success: function (response) {

                        	    if(parseInt(response) > 0) {
                            toastr.success(Showing, '<?php echo "Updated Successfully" ?>');
                        }
                        }
                    });
                },
        eventClick: function (event) {
            var deleteMsg = confirm("Do you really want to delete?");
            if (deleteMsg) {
                $.ajax({
                    type: "POST",
                    url: "../event/event_db.php",
                    data: "&delid=" + event.id,
                    success: function (response) {
                        if(parseInt(response) > 0) {
                            $('#calendar').fullCalendar('removeEvents', event.id);
                            toastr.success(Showing, '<?php echo "Deleted Successfully" ?>');

                        }
                    }
                });
            }
        }

    });
});
</script>