<?php
class UsersController{
    private $model;

    function __construct(){
        $this->model = new UsersModel();
    }

    /**
     * include users template
     */
    public function usersTemplate(){
        global $user;
        require_once('templates/UsersTemplate.php');
    }

    /**
     * display and edit users
     * @return array of users
     */
    public function showEditUsersTemplate() {
        require_once('templates/ShowEditUsersTemplate.php');
    }

    /**
     * include create new user template
     */
    public function createUserTemplate(){
        require_once('templates/CreateUserTemplate.php');
    }

    /**
     * include link customers template
     */
    public function linkCustomersTemplate(){
        require_once('templates/LinkCustomersTemplate.php');
    }

    /**
     * returns json array with all the users from the webshop
     * @return array with users info
     */
    public function getUsers(){
        echo json_encode($this->model->getUsers());
    }

    /**
     * get all the user types available in the webshop
     * @return array of user types
     */
    public function getUserTypes() {
        echo json_encode($this->model->getUserTypes());
    }

    /**
     * get all the commercial leads from the webshop
     * @return array with all available commercial leads
     */
    public function getCommercialLeads() {
        echo json_encode($this->model->getCommercialLeads());
    }

    /**
     * add new user in DB
     * @return confirmation message
     */
    public function saveUserInDB() {
        $info = isset($_REQUEST['info']) ? $_REQUEST['info'] : '';
        $commercialLeads = isset($_REQUEST['cl']) ? $_REQUEST['cl'] : '';
        echo json_encode($this->model->saveUserInDB($info, $commercialLeads));
    }

    /**
     * get the list of customers and commercial leads
     * @return Array  list of customers and comemrcial leads
     */
    public function getCustomersAndCl() {
        echo json_encode($this->model->getCustomersAndCl());
    }

    public function updateLinkedCustomers(){
        $idCommercialLead = isset($_REQUEST['idCommercialLead']) ? $_REQUEST['idCommercialLead'] : 0;
        $customers = isset($_REQUEST['customers']) ? $_REQUEST['customers'] : '[]';
        echo json_encode($this->model->updateLinkedCustomers($idCommercialLead, $customers));
    }

    /**
     * get the list of all the companies available
     * @return Array all the companies available in the system
     */
    public function getCompanies() {
        echo json_encode($this->model->getCompanies());
    }

    /**
     * open users page
     */
    public function showPage(){
        global $user;
        require_once('UsersPage.php');
    }
}
