<?php

namespace MotaRaido\Menu\Profile;

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('/profile/#user', function($user) use ($query) {
    $view = new View();
    $view->assign('title', 'Profile');
    $view->assign('user', $user);
    $view->assign('image', IMG . $user);
    $view->assign('edit', IMG . 'pen.png');
    $view->include('header', APPDIR . '/Menu/HeaderWithMenu.php');
    
    $usrData = $query->select('user', ['username' => $user]);
    $ratingData = $query->select('history', ['usernameDriver' => $user]);
    $preferredLoc = $query->select('preferredloc', ['username' => $user]);

    $view->assign('username', $usrData[0][0]);
    $view->assign('name', $usrData[0][2]);
    $view->assign('phone', $usrData[0][3]);
    if($usrData[0][4]){
        $view->assign('isdriver', 'Driver');
    }
    $view->assign('email', $usrData[0][5]);
    if ($ratingData)
    {
        $count = 0;
        $amount = 0;
        foreach($ratingData as $rating) {
            $amount += $rating[2];
            $count++;
        }
    
    	$view->assign('rating', $amount / $count);
    	$view->assign('votes', $count);
    }
    if (count($preferredLoc) != 0)
    {
        $view->assign('preferredloc', $preferredLoc);
    }
    else
    {
        $view->assign('preferredloc', []);
    }
    $view->render(__DIR__ . '/Profile.view.php');
});