<?php
/**
 * SuppliersController controls the actions for suppliers
 */
class SuppliersController{
    private $model;

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

    /**
     * returns json array with table headers for suppliers
     * @return list all columns headers
     */
    public function getSuppliersHeaders(){
        echo json_encode($this->model->getSuppliersHeaders('array'));
    }

    /**
     * returns json response for suppliers
     * @return list suppliers json
     */
    public function getSuppliers(){
        $getArray = isset($_REQUEST['getArray']) ? $_REQUEST['getArray'] : false;
        $data = ['data' => $this->model->getSuppliers($getArray)];
        echo json_encode($data);
    }

    /**
     * returns json response for suppliers products
     * @return list all products for suppliers json
     */
    public function getSuppliersProducts(){
        $idCountry = isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0;
        $idProduct = isset($_REQUEST['idProduct']) ? $_REQUEST['idProduct'] : 0;
        $data = ['data' => $this->model->getSuppliersProducts($idCountry, $idProduct)];
        echo json_encode($data);
    }


    /**
     * returns json array with table headers for suppliers products
     * @return list all columns headers
     */
    public function getSuppliersProductsHeaders(){
        echo json_encode($this->model->getSuppliersProductsHeaders('array'));
    }

    /**
     * parse input for suppliers info
     * @return Array suppliers info
     */
    private function parseSuppliersData(){
        return [
            'idSupplier' => isset($_REQUEST['idSupplier']) ? $_REQUEST['idSupplier'] : 0,
            'name' => isset($_REQUEST['name']) ? $_REQUEST['name'] : '',
            'phone' => isset($_REQUEST['phone']) ? $_REQUEST['phone'] : '',
            'mail' => isset($_REQUEST['mail']) ? $_REQUEST['mail'] : '',
            'vatCode' => isset($_REQUEST['vatCode']) ? $_REQUEST['vatCode'] : ''
        ];
    }

    /**
     * edit supplier
     * @return json message for the update
     */
    public function editSupplier(){
        $supplierData = $this->parseSuppliersData();
        echo json_encode($this->model->editSupplier($supplierData));
    }

    /**
     * parse input for prodcut
     * @return Array product infos
     */
    private function parseProductData(){
        return [
            'idCountry' => isset($_REQUEST['idCountry']) ? $_REQUEST['idCountry'] : 0,
            'idProduct' => isset($_REQUEST['idProduct']) ? $_REQUEST['idProduct'] : 0,
            'idSupplier' => isset($_REQUEST['idSupplier']) ? $_REQUEST['idSupplier'] : 0,
            'idProductCategory' => isset($_REQUEST['productCategory']['id']) ? $_REQUEST['productCategory']['id'] : 0,
            'name' => isset($_REQUEST['name']) ? $_REQUEST['name'] : '',
            'description' => isset($_REQUEST['description']) ? $_REQUEST['description'] : '',
            'unitCostPrice' => isset($_REQUEST['unitCostPrice']) ? $_REQUEST['unitCostPrice'] : '',
            'unitVatCost' => isset($_REQUEST['unitVatCost']) ? $_REQUEST['unitVatCost'] : '',
            'manufacturerProductNo' => isset($_REQUEST['manufacturerProductNo']) ? $_REQUEST['manufacturerProductNo'] : '',
            'supplierProductNo' => isset($_REQUEST['supplierProductNo']) ? $_REQUEST['supplierProductNo'] : '',
            'unit' =>  isset($_REQUEST['unit']) ? $_REQUEST['unit'] : '',
            'isPriceRecurring' => isset($_REQUEST['isPriceRecurring']) ? $_REQUEST['isPriceRecurring'] : '',
            'payPeriod' => isset($_REQUEST['payPeriod']) ? $_REQUEST['payPeriod'] : 0,
            'isAvailable' => isset($_REQUEST['isAvailable']) ? $_REQUEST['isAvailable'] : 1
        ];
    }

    /**
     * controll for adding a new supplier
     * @return json message for the insert
     */
    public function addSupplierProduct(){
        $productData = $this->parseProductData();
        echo json_encode($this->model->addSupplierProduct($productData));
    }

    /**
     * controll for product edit
     * @return json message for the update
     */
    public function editSupplierProduct(){
        $productData = $this->parseProductData();
        echo json_encode($this->model->editSupplierProduct($productData));
    }

    /**
     * upload product documents
     * @return json messsage for upload documents
     */
    public function uploadProductDocument(){
        $file = isset($_FILES['file']) ? $_FILES['file'] : [];
        $idSupplierProduct = isset($_REQUEST['idSupplierProduct']) ? $_REQUEST['idSupplierProduct'] : 0;
        $idDocumentType = isset($_REQUEST['idDocumentType']) ? $_REQUEST['idDocumentType'] : 0;
        $documentName = isset($_REQUEST['documentName']) ? $_REQUEST['documentName'] : '';
        $visibleToCustomer = isset($_REQUEST['visibleToCustomer']) ? $_REQUEST['visibleToCustomer'] : null;
        echo json_encode($this->model->uploadProductDocument($idDocumentType, $idSupplierProduct, $documentName, $visibleToCustomer, $file));
    }

    /**
     * remove product documents
     * @return json remove documents for products
     */
    public function removeProductDocument(){
        $idDocument = isset($_REQUEST['idDocument']) ? $_REQUEST['idDocument'] : 0;
        echo json_encode($this->model->removeProductDocument($idDocument));
    }

    /**
     * include suppliers template
     */
    public function suppliersTemplate(){
        global $user;
        require_once('templates/SuppliersTemplate.php');
    }

    /**
     * include suppliers template
     */
    public function suppliersAddEditFormTemplate(){
        require_once('templates/SuppliersAddEditFormTemplate.php');
    }

    /**
     * include suppliers template
     */
    public function suppliersProductsAddEditFormTemplate(){
        require_once('templates/SuppliersProductsAddEditFormTemplate.php');
    }

    /**
     * get product categories
     */
    public function getProductCategories() {
        echo json_encode($this->model->getProductCategories());
    }

    /**
     * include upload product document template template
     */
    public function uploadProductDocumentTempalte() {
        require_once('templates/UploadProductDocumentTempalte.php');
    }

    public function showProductDocumentsTemplate() {
        require_once('templates/ShowProductDocumentsTemplate.php');
    }

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

}
?>
