<?php

/**
 * Created by PhpStorm.
 * User: lucasleaniz
 * Date: 26/4/17
 * Time: 11:36
 */
class EventType
{
    private $_id;
    protected $_client;
    protected $_nombre;
    protected $_tipo;
    protected $_estado;
    protected $_amarillo;
    protected $_rojo;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->_id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->_id = $id;
    }

    /**
     * @return mixed
     */
    public function getClient()
    {
        return $this->_client;
    }

    /**
     * @param mixed $client
     */
    public function setClient($client)
    {
        $this->_client = $client;
    }

    /**
     * @return mixed
     */
    public function getNombre()
    {
        return $this->_nombre;
    }

    /**
     * @param mixed $nombre
     */
    public function setNombre($nombre)
    {
        $this->_nombre = $nombre;
    }

    /**
     * @return mixed
     */
    public function getTipo()
    {
        return $this->_tipo;
    }

    /**
     * @param mixed $tipo
     */
    public function setTipo($tipo)
    {
        $this->_tipo = $tipo;
    }

    /**
     * @return mixed
     */
    public function getEstado()
    {
        return $this->_estado;
    }

    /**
     * @param mixed $estado
     */
    public function setEstado($estado)
    {
        $this->_estado = $estado;
    }


    /*** @return mixed */
    public function getAmarillo(){
        return $this->_amarillo;
    }
    public function setAmarillo($amarillo){
        $this->_amarillo = $amarillo;
    }

    /*** @return mixed */
    public function getRojo(){
        return $this->_rojo;
    }
    public function setRojo($rojo){
        $this->_rojo = $rojo;
    }


    public function delete() {
        $d_query = "DELETE FROM deve_eventos_tipos WHERE eventos_tipo_id = " . $this->getId();
        Database::query($d_query);
        if (!Database::affected_rows()) {
            return false;
        }
        return true;
    }

    public function save(){
        $id = $this->getId();
        if (isset($id)){
            $u_query = "UPDATE deve_eventos_tipos SET 
                          eventos_tipo_nombre   = '" .  $this->getNombre()      . "', 
                          eventos_dias_amarillo = '" .  $this->getAmarillo()    ."', 
                          eventos_dias_rojo     = '" .  $this->getRojo()        ."' 
                        WHERE eventos_tipo_id   = "  . $this->getId();
            if(!Database::query($u_query)){
                return false;
            }
        }else {
            $i_query = "INSERT INTO deve_eventos_tipos (
                              eventos_tipo_cliente, 
                              eventos_tipo_nombre, 
                              eventos_tipo_estado, 
                              eventos_tipo,
                              eventos_dias_amarillo,
                              eventos_dias_rojo
                        ) VALUES (
                              " . CLIENT_ID .", 
                              '".$this->getNombre()."', 
                              1, 
                              1,
                              ".$this->getAmarillo().", 
                              ".$this->getRojo().", 
                        )";
            if(!Database::query($i_query)) {
                return false;
            }
            $this->setId(Database::insert_id());
        }
        return true;
    }

    public static function getTypeByReason($reason_id){
        $type = false;
        $query = "SELECT  dt.eventos_tipo_id
                    FROM deve_eventos_tipos dt
                    INNER JOIN deve_eventos_motivos dm ON dt.eventos_tipo_id = dm.eventos_tipo_id 
                    WHERE dt.eventos_tipo_estado = 1
                      AND dm.eventos_motivo_estado = 1
                      AND dm.eventos_motivo_id = " . $reason_id." LIMIT 1";
        $q_query = Database::query($query);
        if (!$q_query){
            return false;
        }

        if (Database::num_rows($q_query)) {
            $fa_type = Database::fetch_assoc($q_query);
            $type = EventType::search($fa_type["eventos_tipo_id"]);
        }
        return $type;
    }

    public static function search($id){
        $type = new EventType();
        $s_query= "SELECT eventos_tipo_id, eventos_tipo_nombre , eventos_tipo, eventos_dias_amarillo, eventos_dias_rojo
				FROM deve_eventos_tipos 
				WHERE eventos_tipo_cliente = ".CLIENT_ID." AND eventos_tipo_estado = 1 AND eventos_tipo_id = ".$id;
        $q_query = Database::query($s_query);
        if (!$q_query){
            return false;
        }

        if (Database::num_rows($q_query)) {
            $fa_type = Database::fetch_assoc($q_query);

            $type->setClient(CLIENT_ID);
            $type->setId($fa_type["eventos_tipo_id"]);
            $type->setNombre($fa_type["eventos_tipo_nombre"]);
            $type->setTipo($fa_type["eventos_tipo"]);
            $type->setAmarillo($fa_type["eventos_dias_amarillo"]);
            $type->setRojo($fa_type["eventos_dias_rojo"]);
        }
        return $type;
    }

    public static function getAll(){
        $types = array();
        $s_query= "SELECT * 
                    FROM deve_eventos_tipos 
                    WHERE 
				      eventos_tipo_cliente = ".CLIENT_ID." AND 
				      eventos_tipo_estado = 1";

        $q_query = Database::query($s_query);
        if (!$q_query){
            return false;
        }
        while ($fa_type = Database::fetch_assoc($q_query)) {
            $type = new EventType();

            $type->setClient(CLIENT_ID);
            $type->setId($fa_type["eventos_tipo_id"]);
            $type->setNombre($fa_type["eventos_tipo_nombre"]);
            $type->setTipo($fa_type["eventos_tipo"]);
            $type->setAmarillo($fa_type["eventos_dias_amarillo"]);
            $type->setRojo($fa_type["eventos_dias_rojo"]);

            $types[] = $type;
        }
        return $types;
    }

}