Your IP : 216.73.216.86


Current Path : /home/emeraadmin/public_html/pages/emeraadmin/
Upload File :
Current File : /home/emeraadmin/public_html/pages/emeraadmin/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 (isset($_GET['completed'])) {
        $filters['completed'] = $_GET['completed'];
    }
    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="completed">Completed</label>
                                    <select class="form-control" id="completed" name="completed">
                                        <option value="">Select</option>
                                        <option value="1" <?php echo (isset($_GET['completed']) && $_GET['completed'] === '1') ? 'selected' : ''; ?>>Yes</option>
                                        <option value="0" <?php echo (isset($_GET['completed']) && $_GET['completed'] === '0') ? 'selected' : ''; ?>>No</option>
                                    </select>
                                </div>
                                <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">
                        <form id="scheduleForm" class="forms-sample">
                            <div class="form-group">
                                <label for="schedule-name">Schedule Name:</label>
                                <input type="text" id="schedule-name" name="schedule-name" class="form-control" required placeholder="Enter schedule name">
                            </div>
                            <div id="message" class="mt-3"></div>
                        </form>
                    </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();
                        });
                        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>Completed</th>
                                <th>Created By</th>
                                <th>Created At</th>
                                <th>Actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php if (!empty($schedules)): ?>
                                <?php foreach ($schedules as $schedule): ?>
                                    <tr>
                                        <td><?php echo 'SCH-000' . $schedule->id; ?></td>
                                        <td><?php echo htmlspecialchars($schedule->schedule_name); ?></td>
                                        <td><?php echo ($schedule->completed ? '<label class="badge badge-success">Completed</label>' : '<label class="badge badge-danger">Not Completed</label>'); ?></td>
                                        <td><?php echo htmlspecialchars($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;
                                            <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>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="6">No schedules found.</td>
                                </tr>
                            <?php endif; ?>
                            </tbody>

                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        var deleteButtons = document.querySelectorAll('.delete-schedule');

        deleteButtons.forEach(function(button) {
            button.addEventListener('click', function() {
                var scheduleId = button.getAttribute('data-schedule-id');

                Swal.fire({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonText: 'Yes, delete it!'
                }).then((result) => {
                    if (result.isConfirmed) {
                        // Create a fake hidden password input to trick autofill
                        let fakePasswordInput = document.createElement('input');
                        fakePasswordInput.setAttribute('type', 'password');
                        fakePasswordInput.setAttribute('style', 'display:none;');
                        document.body.appendChild(fakePasswordInput);

                        // Prompt for password
                        Swal.fire({
                            title: 'Enter your password',
                            input: 'password',
                            showCancelButton: true,
                            confirmButtonText: 'Submit',
                            inputPlaceholder: 'Password',
                            inputAttributes: {
                                'autocomplete': 'new-password',  // Set to "new-password" to trick autofill
                                'autocapitalize': 'off',
                                'autocorrect': 'off',
                                'autofocus': 'true'
                            }
                        }).then((passwordResult) => {
                            if (passwordResult.isConfirmed) {
                                var password = passwordResult.value;
                                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) {
                                        if (xhr.responseText === 'success') {
                                            Swal.fire({
                                                title: 'Deleted!',
                                                text: 'The schedule has been deleted.',
                                                icon: 'success'
                                            }).then(() => {
                                                location.reload(); // Refresh the page
                                            });
                                        } else {
                                            Swal.fire({
                                                title: 'Error!',
                                                text: xhr.responseText,
                                                icon: 'error'
                                            });
                                        }
                                    }
                                };
                                xhr.send('scheduleId=' + scheduleId + '&password=' + encodeURIComponent(password));
                            }
                        });
                    }
                });
            });
        });
    });


</script>


<?php include('footer.php'); ?>