<?php
$page_title = 'Schedule Timeline';
require_once('includes/load.php');
page_require_level(3);
include_once('layouts/header.php');

// Fetch production schedules
$query = "SELECT ps.*, p.name AS product_name 
          FROM production_schedule ps 
          LEFT JOIN products p ON ps.part_number = p.part_number 
          ORDER BY ps.delivery_date ASC";
$result = $db->query($query);
$schedules = [];

while ($schedule = $result->fetch_assoc()) {
    $schedules[] = [
        'title' => $schedule['product_name'] . " (" . $schedule['part_number'] . ")",
        'start' => $schedule['delivery_date'],
        'description' => 'No Order: ' . $schedule['no_order'] . ', Client: ' . $schedule['client'] . ', Quantity: ' . $schedule['quantity_order']
    ];
}
?>

<div class="row">
    <div class="col-md-12">
        <h2>Schedule Timeline</h2>
        <div id='calendar'></div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');

    var calendar = new FullCalendar.Calendar(calendarEl, {
        initialView: 'dayGridMonth',
        events: <?php echo json_encode($schedules); ?>,
        eventDidMount: function(info) {
            var tooltip = new Tooltip(info.el, {
                title: info.event.extendedProps.description,
                placement: 'top',
                trigger: 'hover',
                container: 'body'
            });
        }
    });

    calendar.render();
});
</script>

<?php include_once('layouts/footer.php'); ?>
