<?php

class OrderProjects{
    public function getOrderProjects($available = 1){
        global $database;
        $whereSql = intval($available) === 1 ? " AND op.isAvailable=1" : "";

        $sql = "SELECT
                    op.id AS idProject,
                    op.name AS projectName,
                    op.isAvailable
                FROM ".TABLES['order_projects']." op
                WHERE 1=1 $whereSql";

        return $database->fetchResultArray($sql);
    }

    private function validateProjectData($projectData){
        global $database;
        $data = [];

        $checkMessage = $database->isEmpty('projectName', $projectData->projectName);
        if($checkMessage){
            $data['messages'][] = $checkMessage;
        }

        $checkMessage = $database->invalidLength('projectName', $projectData->projectName, 100);
        if($checkMessage){
            $data['messages'][] = $checkMessage;
        }

        return $data;
    }

    public function editOrderProject($projectData){
        global $database;
        $data = [];
        $projectData = json_decode($projectData);

        if(empty($projectData)){
            $data['messages'][] = [
                'code' => 'error',
                'message' => 'INVALID_DATA'
            ];

            return $data;
        }

        $checkMessages = $this->validateProjectData($projectData);
        if(!empty($checkMessages)){
            return $checkMessages;
        }

        $sql = "UPDATE ".TABLES['order_projects']."
                SET name='".$database->escapeValue($projectData->projectName)."',
                    isAvailable=".$database->escapeValue($projectData->isAvailable)."
                WHERE id=".$database->escapeValue($projectData->idProject);
        $query = $database->query($sql);

        if($database->affectedRows() < 1){
            $data['messages'][] = [
                'code' => 'warning',
                'message' => 'NO_CHANGES'
            ];

        }else{
            $data['messages'][] = [
                'code' => 'success',
                'message' => 'PROJECT_UPDATED'
            ];
        }

        return $data;
    }

    public function addOrderProject($projectData){
        global $database;
        $data = [];
        $projectData = json_decode($projectData);

        if(empty($projectData)){
            $data['messages'][] = [
                'code' => 'error',
                'message' => 'INVALID_DATA'
            ];

            return $data;
        }

        $checkMessages = $this->validateProjectData($projectData);
        if(!empty($checkMessages)){
            return $checkMessages;
        }

        $sql = "INSERT INTO ".TABLES['order_projects']."
                (name)
                VALUES('".$database->escapeValue($projectData->projectName)."')";
        $query = $database->query($sql);

        if($database->affectedRows() < 1){
            $data['messages'][] = [
                'code' => 'warning',
                'message' => 'INVALID_DATA'
            ];

        }else{
            $data['messages'][] = [
                'code' => 'success',
                'message' => 'PROJECT_ADDED'
            ];
        }

        return $data;
    }
}
