<?php
/**
 * ShopController controlls the actions for docuemnts
 */
class DocumentsController{
    private $model;

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

    /**
     * get documents for documents view
     * @return json list of documents
     */
    public function getDocuments(){
        $idDocument = isset($_REQUEST['idDocument']) ? $_REQUEST['idDocument'] : 0;
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        echo json_encode($this->model->getDocuments($idDocument, $idPackage));
    }

    /**
     * get documents and apckages for linking
     * @return json a list for documetns and one for packages
     */
    public function getDocumentsAndPackages(){
        echo json_encode($this->model->getDocumentsAndPackages());
    }

    /**
     * update linking between a package and docuemtns
     * @return json update message
     */
    public function updatePackageDocuments(){
        $idPackage = isset($_REQUEST['idPackage']) ? $_REQUEST['idPackage'] : 0;
        $documents = isset($_REQUEST['documents']) ? $_REQUEST['documents'] : '[]';
        echo json_encode($this->model->updatePackageDocuments($idPackage, $documents));
    }

    /**
     * get documents types
     * @return json a list for document types
     */
    public function getDocumentTypes(){
        echo json_encode($this->model->getDocumentTypes());
    }

    public function uploadNewDocument(){
        $idDocumentType = isset($_REQUEST['idDocumentType']) ? $_REQUEST['idDocumentType'] : 0;
        $documentName = isset($_REQUEST['documentName']) ? $_REQUEST['documentName'] : '';
        $file = isset($_FILES['file']) ? $_FILES['file'] : [];
        echo json_encode($this->model->uploadNewDocument($idDocumentType, $documentName, $file));
    }

    public function addNewDocumnetType(){
        $documentNewType = isset($_REQUEST['documentNewType']) ? $_REQUEST['documentNewType'] : '';
        echo json_encode($this->model->addNewDocumnetType($documentNewType));
    }

    public function removeDocument(){
        $idDocument = isset($_REQUEST['idDocument']) ? $_REQUEST['idDocument'] : 0;
        echo json_encode($this->model->removeDocument($idDocument));
    }

    /**
     * include decouments template
     */
    public function documentsTemplate(){
        global $user;
        require_once('templates/DocumentsTemplate.php');
    }

    /**
     * include decouments view template
     */
    public function documentsViewTemplate(){
        require_once('templates/DocumentsViewTemplate.php');
    }

    /**
     * include decouments view template
     */
    public function documentsLinkTemplate(){
        require_once('templates/DocumentsLinkTemplate.php');
    }

    /**
     * include decouments view template
     */
    public function DocumentsAddTemplate(){
        require_once('templates/DocumentsAddTemplate.php');
    }

    /**
     * open documents page
     */
    public function showPage(){
        require_once('DocumentsPage.php');
    }
}
