<?php
/*
 * Copyright 2019 Netsyms Technologies.
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

redirectIfNotLoggedIn();
$user = new User($_SESSION['uid']);
if (!$user->hasPermission("MACHINEMANAGER_VIEW")) {
    header("Location: ./app.php?msg=no_permission");
    die();
}

$writeaccess = $user->hasPermission("MACHINEMANAGER_EDIT");

$jobs = $database->select("jobs", ["[>]machines" => ["machineid"]], ["jobid", "jobs.machineid", "jobname", "clientid", "startdate", "deadline", "finishdate"]);

$clients = Clients::getAll();
?>

<div class="btn-group">
    <?php if ($writeaccess) { ?>
        <a href="app.php?page=editjob" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("Add Job"); ?></a>
    <?php } ?>
</div>
<table id="jobstable" class="table table-bordered table-hover table-sm">
    <thead>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><?php $Strings->get('Actions'); ?></th>
            <th data-priority="3"><i class="fas fa-tasks hidden-sm"></i> <?php $Strings->get('Job Title'); ?></th>
            <th data-priority="3"><i class="fas fa-desktop hidden-sm"></i> <?php $Strings->get('Machine'); ?></th>
            <th data-priority="2"><i class="fas fa-user hidden-sm"></i> <?php $Strings->get('Client'); ?></th>
            <th data-priority="4"><i class="fas fa-calendar-plus hidden-sm"></i> <?php $Strings->get('Started'); ?></th>
            <th data-priority="2"><i class="fas fa-calendar hidden-sm"></i> <?php $Strings->get('Deadline'); ?></th>
            <th data-priority="1"><i class="fas fa-calendar-check hidden-sm"></i> <?php $Strings->get('Finished'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($jobs as $j) {
            ?>
            <tr>
                <td></td>
                <td>
                    <?php
                    if ($writeaccess) {
                        ?>
                        <a class="btn btn-primary btn-sm" href="app.php?page=editjob&arg=<?php echo $j['jobid']; ?>"><i class="fas fa-edit"></i> <?php $Strings->get("Edit"); ?></a>
                        <?php
                    }
                    ?>
                </td>
                <td><a href="./app.php?page=viewjob&id=<?php echo $j['jobid']; ?>"><?php echo htmlspecialchars($j['jobname']); ?></a></td>
                <td>
                    <?php if (!empty($j["machineid"])) { ?>
                        <a href="./app.php?page=viewmachine&id=<?php echo $j['machineid']; ?>"><?php echo htmlspecialchars($j['machineid']); ?></a>
                    <?php } ?>
                </td>
                <td><?php
                    foreach ($clients as $c) {
                        if ($c->getID() == $j['clientid']) {
                            echo $c->getName();
                            break;
                        }
                    }
                    ?></td>
                <td><?php echo $j['startdate'] ?? date($SETTINGS["datetime_format"], strtotime($j['startdate'])); ?></td>
                <td><?php echo $j['deadline'] ?? date($SETTINGS["datetime_format"], strtotime($j['deadline'])); ?></td>
                <td><?php echo $j['finishdate'] ?? date($SETTINGS["datetime_format"], strtotime($j['finishdate'])); ?></td>
            </tr>
            <?php
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <th data-priority="0"></th>
            <th data-priority="1"><?php $Strings->get('Actions'); ?></th>
            <th data-priority="3"><i class="fas fa-tasks hidden-sm"></i> <?php $Strings->get('Job Title'); ?></th>
            <th data-priority="3"><i class="fas fa-desktop hidden-sm"></i> <?php $Strings->get('Machine'); ?></th>
            <th data-priority="2"><i class="fas fa-user hidden-sm"></i> <?php $Strings->get('Client'); ?></th>
            <th data-priority="4"><i class="fas fa-calendar-plus hidden-sm"></i> <?php $Strings->get('Started'); ?></th>
            <th data-priority="2"><i class="fas fa-calendar hidden-sm"></i> <?php $Strings->get('Deadline'); ?></th>
            <th data-priority="1"><i class="fas fa-calendar-check hidden-sm"></i> <?php $Strings->get('Finished'); ?></th>
        </tr>
    </tfoot>
</table>