Your IP : 216.73.216.86


Current Path : /home/emeraadmin/public_html/pages/taskprovider/
Upload File :
Current File : /home/emeraadmin/public_html/pages/taskprovider/viewschedules.php

<?php
include('head.php');
// Include database connection and ScheduleService class

require_once '../../Service/ScheduleService.php';
require_once '../../Classes/System.php';

$System = new System();
// Create ScheduleService instance
$scheduleService = new ScheduleService();

// Initialize filters array
$filters = [];

// Check for filter inputs and add to the filters array
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    
    if (!empty($_GET['date_from'])) {
        $filters['date_from'] = $_GET['date_from'];
    }
    if (!empty($_GET['date_to'])) {
        $filters['date_to'] = $_GET['date_to'];
    }
}

// Fetch schedules from the database based on filters
$schedules = $scheduleService->getAllSchedules($filters);

?>

<div class="main-content">
    <div class="container-fluid">
        <div class="page-header">
            <div class="row align-items-end">
                <div class="col-lg-8">
                    <div class="page-header-title">
                        <i class="ik ik-layers bg-blue"></i>
                        <div class="d-inline">
                            <h5>View Schedules</h5>
                            <span>View all schedules</span>
                        </div>
                    </div>
                </div>
                <div class="col-lg-4">
                    <nav class="breadcrumb-container" aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item">
                                <a href="../../index.html"><i class="ik ik-home"></i></a>
                            </li>
                            <li class="breadcrumb-item">
                                <a href="#">Schedules</a>
                            </li>
                            <li class="breadcrumb-item active" aria-current="page">View Schedules</li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>



        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <div class="card-header-left">
                            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter"><i class="ik ik-plus"></i> Create Schedule</button>
                        </div>
                        <div class="card-header-right">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- Filter Form -->
        <div class="row mb-3">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h3>Filter Schedules</h3>
                    </div>
                    <div class="card-body">
                        <form method="GET" action="">
                            <div class="form-row">
                                


                                <div class="form-group col-md-2">
                                    <label for="date_from">Date From</label>
                                    <input type="date" class="form-control" id="date_from" name="date_from" value="<?php echo htmlspecialchars($_GET['date_from'] ?? ''); ?>">
                                </div>
                                <div class="form-group col-md-2">
                                    <label for="date_to">Date To</label>
                                    <input type="date" class="form-control" id="date_to" name="date_to" value="<?php echo htmlspecialchars($_GET['date_to'] ?? ''); ?>">
                                </div>
                            </div>
                            <button type="submit" class="btn btn-primary">Apply Filters</button>
                            <a href="viewschedules.php" class="btn btn-secondary">Clear Filters</a>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <!-- End of Filter Form -->

        <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalCenterLabel">Add a new schedule</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <form id="scheduleForm" class="forms-sample">
                                <div class="col-md-12">
                                    <div class="form-group row">
                                        <div class="col-sm-3">
                                            <label for="schedule-name" class="col-sm-3 col-form-label">Schedule Name:</label>
                                        </div>
                                        <div class="col-sm-9">
                                            <input type="text" id="schedule-name" name="schedule-name" class="form-control" required placeholder="Enter schedule name">
                                        </div>
                                        <input type="hidden"  name="created_by" value="<?php echo $companyId; ?>">
                                    </div>
                                    
                                </div>
                            </form>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                            </div>
                        </div>
                        <div id="message" class="mt-3"></div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="submit" form="scheduleForm" class="btn btn-primary">Add Schedule</button>
                    </div>
                </div>
            </div>
        </div>
        <script>
            document.getElementById('scheduleForm').addEventListener('submit', function (event) {
                event.preventDefault();

                const formData = new FormData(this);
                fetch('submit_schedule.php', {
                    method: 'POST',
                    body: formData
                })
                    .then(response => response.text())
                    .then(data => {
                        const messageDiv = document.getElementById('message');
                        messageDiv.textContent = data;
                        messageDiv.classList.add('alert', 'alert-success');

                        Swal.fire({
                            title: 'Success!',
                            text: data,
                            icon: 'success',
                            confirmButtonText: 'Ok'
                        }).then(() => {
                            location.reload();
                        });
                        // Reset form
                        document.getElementById('scheduleForm').reset();
                    })
                    .catch(error => {
                        const messageDiv = document.getElementById('message');
                        messageDiv.textContent = 'Error: ' + error;
                        messageDiv.classList.add('alert', 'alert-danger');

                        Swal.fire({
                            title: 'Error!',
                            text: error,
                            icon: 'error',
                            confirmButtonText: 'Ok'
                        });
                    });
            });
        </script>

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <table id="advanced_table" class="table">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>Schedule Name</th>
                                

                                <th>Created By</th>
                                <th>Created At</th>
                                <th>Actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($schedules as $schedule): ?>
                                <tr>
                                    <td><?php echo 'SCH-000' . $schedule->id; ?></td>
                                    <td><?php echo $schedule->schedule_name; ?></td>
                                    

                                    <td><?php echo $schedule->created_by; ?></td>
                                    <td><?php echo date('F j, Y, g:i a', strtotime($schedule->created_at)); ?></td>
                                    <td>
                                        <a href="viewscheduletasks.php?id=<?php echo $System->encryptData($schedule->id) ?>" class="btn btn-primary">View Tasks</a>
                                        &nbsp;
                                        <!--                                        --><?php //if (!$scheduleService->hasAssignedTasks($schedule->id)): ?>
                                        <!--                                            <button class="btn btn-danger delete-schedule" data-schedule-id="--><?php //echo $System->encryptData($schedule->id); ?><!--">Delete</button>-->
                                        <!--                                        --><?php //endif; ?>
                                        <?php if (!$scheduleService->hasSubmittedSubTasks($schedule->id)): ?>
                                            <button type="button" class="btn btn-icon btn-danger delete-schedule" data-schedule-id="<?php echo $System->encryptData($schedule->id); ?>"><i class="ik ik-trash-2"></i></button>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Find all delete buttons and attach click event
        var deleteButtons = document.querySelectorAll('.delete-schedule');
        deleteButtons.forEach(function(button) {
            button.addEventListener('click', function() {
                var scheduleId = button.getAttribute('data-schedule-id');

                // Show confirmation dialog using SweetAlert
                Swal.fire({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, delete it!'
                }).then((result) => {
                    if (result.isConfirmed) {
                        // If confirmed, make AJAX request to delete_schedule.php
                        var xhr = new XMLHttpRequest();
                        xhr.open('POST', 'delete_schedule.php', true);
                        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                        xhr.onreadystatechange = function() {
                            if (xhr.readyState == 4 && xhr.status == 200) {
                                console.log(xhr.responseText); // Log the response for debugging
                                if (xhr.responseText === 'success') {
                                    Swal.fire({
                                        title: 'Deleted!',
                                        text: 'The schedule has been deleted.',
                                        icon: 'success'
                                    }).then(() => {
                                        window.location.reload(); // Reload the page after deletion
                                    });
                                } else {
                                    Swal.fire({
                                        title: 'Error!',
                                        text: 'An error occurred while deleting the schedule. Please try again.',
                                        icon: 'error'
                                    });
                                }
                            }
                        };
                        xhr.send('scheduleId=' + scheduleId); // Send schedule ID to delete_schedule.php
                    }
                });
            });
        });
    });
</script>
<?php include('footer.php'); ?>