<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-02-28
 * Time: 12:09
 */


session_start();

require_once getcwd() . "/../functions/db.class.php";
require_once getcwd() . "/../functions/form.data.php";
require_once getcwd() . "/../functions/robot.php";

if (isset($_POST['userUsername'])) {

//moving profile to images folder
    $userPicture = move_files($_FILES, "userPicture", "../images/");

//adding user profile picture in the array list
    $_POST['userPicture'] = $userPicture;

//fetching the Roles and Privileges informations
    $roleName = $_POST['userRole'];
    $privilegeStatus = $_POST['userPrivilege'];
    switch ($_POST['userRole']) {
        case 'User':
            $roleLocation = "users/";
            break;
        case 'Admin':
            $roleLocation = "admin/";
            break;
        default:
            $roleLocation = "error/";
    }

//unsetting the unnecessary info in the user array
    unset($_POST['userRole']);
    unset($_POST['userPrivilege']);

//preparing the sql statement for the insert command
    $stmtUser = SQLStatement("udut_users", $_POST);

//saving the user's details in the database
    $query->execute($stmtUser);

//fetching the last inserted user details
    $userDetails = $query->getOne("select * from udut_users order by userId DESC limit 1");

//compiling roles details
    $_ROLES = array("userId" => $userDetails['userId'], "roleName" => $roleName, "roleLocation" => $roleLocation);

//prepare the role information
    $stmtRole = SQLStatement("udut_users_roles", $_ROLES);

//save the roles
    $query->execute($stmtRole);

//fetching the roles details
    $roleDetails = $query->getOne("select * from udut_users_roles order by roleId DESC limit 1");

//compiling the privileges
    $_PRIVILEGES = array("roleId" => $roleDetails['roleId']);

//preparing the privilege statement
    $stmtPrivilege = SQLStatement("udut_users_privileges", $_PRIVILEGES);

//saving privileges
    $query->execute($stmtPrivilege);


    header("location: ../about-user/" . $userDetails['userId']);

} else {
    header("location: ../add-patient");
}


//echo $stmtUser;
//
//print("<pre>");
//print_r($_POST);