<?php

use Mtc\Shop\Assessment\Form;

$path = '../../../';
require_once $path . 'core/includes/header.inc.php';

$page_meta['title'] = 'Manage Assessment Forms';

$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? null) === 'delete') {
    if (empty($_SESSION['csrf_token']) || empty($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], (string) $_POST['csrf_token'])) {
        $errors[] = 'Invalid CSRF token.';
    } elseif (!is_numeric($_POST['delete_id'] ?? null) || !($form = (new Form())->find($_POST['delete_id']))) {
        $errors[] = 'Assessment form not found.';
    } elseif ($form->answers()->count() > 0) {
        $errors[] = 'The assessment form cannot deleted as it has answers attached to it';
    } else {
        // Delete the questions
        $form->questions()->delete();
        // Delete the form
        $form->delete();
        header('location: manage.forms.php');
        exit;
    }
}

if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

$forms = Form::query()->orderBy('name')->get();

require_once $path . 'core/admin/templates/adminheader.html.php';

?>
<ul class="breadcrumbs el">
    <li><a href="/admin/">Home</a> /</li>

    <li class="end">Assessment Forms</li>
</ul>

<h1>Assessment Forms</h1>
<?php

include __DIR__ . '/tabnav.php';

if (count($errors)>0) { showerrors($errors); }

if (!$forms->isEmpty()) {
    ?>
        <table class="manage-items">
            <tr>
                <th>Name</th>
                <th>Number of questions</th>
                <th class="center">Copy</th>
                <th class="center">Edit</th>
                <th class="center">Delete</th>
            </tr>
            <tbody class="tabular">
            <?php
            foreach ($forms as $form) {
                ?>
                <tr>
                    <td><?= $form->name; ?></td>
                    <td><?= $form->questions->count(); ?></td>
                    <td class="center"><a href="/shop/admin/assessment/copy.form.php?id=<?= $form->id; ?>" title="Make a copy of this form"><i class="fa fa-copy"></i></a></td>
                    <td class="center"><a href="manage.form.php?id=<?=$form->id; ?>" class="edit" title="Edit this form"><i class="fa fa-pencil"></i></a></td>
                    <td class="center">
                        <form method="post" action="manage.forms.php" style="display:inline;" onsubmit="return confirm('Are you sure you want to delete the Assessment Form?');">
                            <input type="hidden" name="action" value="delete">
                            <input type="hidden" name="delete_id" value="<?= $form->id; ?>">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']); ?>">
                            <button type="submit" class="alert picto error" title="Delete this assessment form"><i class="fa fa-times-circle"></i></button>
                        </form>
                    </td>
                </tr>
                <?php
            }
            ?>
            </tbody>
        </table>
    <?php
} else {
    ?>
    <p>No Assessment Forms have been created</p>
    <?php
}

require_once $path . 'core/admin/templates/adminfooter.html.php';
