<?php

namespace MotaRaido\Menu\History;

use \MotaRaido\Core\View;
use \MotaRaido\Core\Route;

if (!defined('MOTAFW')) 
{
    echo 'This file can only be called via the main index.php file, and not directly';
    exit();
}

Route::add('/history/#user', function($user) use ($query) {
    $view = new View();
	$userData = $query->select('history', ['usernameUser' => $user, 'ishide' => false]);
    $driverData = $query->select('history', ['usernameDriver' => $user, 'ishide' => false]);
    
    $view->assign('user', $user);
    $view->assign('title', 'History');
    $view->assign('image', IMG);
    $view->assign('historyUser', $userData);
    $view->assign('historyDriver', $driverData);
    $view->assign('script', JS . "history.js?time=" . date("H:i:s"));

    $view->render(__DIR__ . '/History.view.php');
});

Route::add('/history/#user/update/#driver/#date', function($user, $driver, $date) use ($query) {
    $query->transaction(function() use ($query, $user, $driver, $date) {
        $query->update('history', ['ishide' => 1], ['usernameUser' => $user, 'usernameDriver' => $driver, 'dateOrder' => $date]);
    });
    header('Location: /history/' . $user);
});