<?php
namespace App\Models\Configuracion;

class MaquinasDefectoModel extends \App\Models\BaseModel
{
    protected $table = "lg_maquina_por_defecto";

    /**
     * Whether primary key uses auto increment.
     *
     * @var bool
     */
    protected $useAutoIncrement = true;

    const SORTABLE = [
        0 => "t1.tipo",
        1 => "t2.nombre",
        2 => "t1.ancho_min",
        3 => "t1.ancho_max",
        4 => "t1.alto_min",
        5 => "t1.alto_max",
        6 => "t1.tirada_min",
        7 => "t1.tirada_max",
    ];

    protected $allowedFields = [
        "tipo",
        "ancho_min",
        "ancho_max",
        "alto_min",
        "alto_max",
        "tirada_min",
        "tirada_max",
        "maquina_id",
        "deleted_at",
        "is_deleted",
        "user_created_id",
        "user_updated_id"        
    ];
    protected $returnType = "App\Entities\Configuracion\MaquinasDefectoEntity";

    protected $useTimestamps = true;
    protected $useSoftDeletes = false;

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "tipo";

    protected $validationRules = [
        "alto_max" => [
            "label" => "MaquinasPorDefecto.altoMax",
            "rules" => "required|decimal",
        ],
        "alto_min" => [
            "label" => "MaquinasPorDefecto.altoMin",
            "rules" => "required|decimal",
        ],
        "ancho_max" => [
            "label" => "MaquinasPorDefecto.anchoMax",
            "rules" => "required|decimal",
        ],
        "ancho_min" => [
            "label" => "MaquinasPorDefecto.anchoMin",
            "rules" => "required|decimal",
        ],
        "tipo" => [
            "label" => "MaquinasPorDefecto.tipo",
            "rules" => "required|in_list[bn,bnhq,color,portada,cubierta,rotativa]",
        ],
        "tirada_max" => [
            "label" => "MaquinasPorDefecto.tiradaMax",
            "rules" => "required|integer",
        ],
        "tirada_min" => [
            "label" => "MaquinasPorDefecto.tiradaMin",
            "rules" => "required|integer",
        ],
        "maquina_id" => [
            "label" => "MaquinasPorDefecto.maquinaId",
            "rules" => "required",
        ],
    ];

    protected $validationMessages = [
        "alto_max" => [
            "decimal" => "MaquinasPorDefecto.validation.alto_max.decimal",
            "required" => "MaquinasPorDefecto.validation.alto_max.required",
        ],
        "alto_min" => [
            "decimal" => "MaquinasPorDefecto.validation.alto_min.decimal",
            "required" => "MaquinasPorDefecto.validation.alto_min.required",
        ],
        "ancho_max" => [
            "decimal" => "MaquinasPorDefecto.validation.ancho_max.decimal",
            "required" => "MaquinasPorDefecto.validation.ancho_max.required",
        ],
        "ancho_min" => [
            "decimal" => "MaquinasPorDefecto.validation.ancho_min.decimal",
            "required" => "MaquinasPorDefecto.validation.ancho_min.required",
        ],
        "tipo" => [
            "in_list" => "MaquinasPorDefecto.validation.tipo.in_list",
            "required" => "MaquinasPorDefecto.validation.tipo.required",
        ],
        "tirada_max" => [
            "integer" => "MaquinasPorDefecto.validation.tirada_max.integer",
            "required" => "MaquinasPorDefecto.validation.tirada_max.required",
        ],
        "tirada_min" => [
            "integer" => "MaquinasPorDefecto.validation.tirada_min.integer",
            "required" => "MaquinasPorDefecto.validation.tirada_min.required",
        ],
        "maquina_id" => [
            "required" => "MaquinasPorDefecto.validation.tirada_min.required",
        ],
    ];

    public function findAllWithMaquinas(string $selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.nombre AS maquina FROM " .
            $this->table .
            " t1  LEFT JOIN lg_maquinas t2 ON t1.maquina_id = t2.id";
        if (!is_null($limit) && floatval($limit) > 0) {
            $sql .= " LIMIT " . $limit;
        }

        if (!is_null($offset) && floatval($offset) > 0) {
            $sql .= " OFFSET " . $offset;
        }

        $query = $this->db->query($sql);
        $result = $query->getResultObject();
        return $result;
    }

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "")
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.tipo AS tipo, t1.ancho_min AS ancho_min, t1.ancho_max AS ancho_max, t1.alto_min AS alto_min, t1.alto_max AS alto_max, t1.tirada_min AS tirada_min, t1.tirada_max AS tirada_max, t2.nombre AS maquina"
            );
        $builder->where('t1.is_deleted', 0);
        $builder->join("lg_maquinas t2", "t1.maquina_id = t2.id", "left");

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orLike("t1.tipo", $search)
                ->orLike("t1.ancho_min", $search)
                ->orLike("t1.ancho_max", $search)
                ->orLike("t1.alto_min", $search)
                ->orLike("t1.alto_max", $search)
                ->orLike("t1.tirada_min", $search)
                ->orLike("t1.tirada_max", $search)
                ->orLike("t2.id", $search)
                ->orLike("t1.id", $search)
                ->orLike("t1.tipo", $search)
                ->orLike("t1.ancho_min", $search)
                ->orLike("t1.ancho_max", $search)
                ->orLike("t1.alto_min", $search)
                ->orLike("t1.alto_max", $search)
                ->orLike("t1.tirada_min", $search)
                ->orLike("t1.tirada_max", $search)
                ->orLike("t1.maquina_id", $search)
                ->orLike("t2.nombre", $search)
                ->groupEnd();
    }

    public function checkIntervals($data = [], $id = null){

        helper('general');

        if(floatval($data["ancho_min"])>= floatval($data["ancho_max"])){
            return lang('MaquinasPorDefecto.validation.error_ancho_range');
        }

        if(floatval($data["tirada_min"])>= floatval($data["tirada_max"])){
            return lang('MaquinasPorDefecto.validation.error_tirada_range');
        }

        if(floatval($data["alto_min"])>= floatval($data["alto_max"])){
            return lang('MaquinasPorDefecto.validation.error_alto_range');
        }

        $rows = $this->db
            ->table($this->table)
            ->select("id, ancho_min, ancho_max, alto_min, alto_max, tirada_min, tirada_max")
            ->where("is_deleted", 0)
            ->where("tipo", $data["tipo"])
            ->get()->getResultObject();


        foreach ($rows as $row) {
            if (!is_null($id)){
                if($row->id == $id){
                    continue;
                }
            }
            if(check_overlap(floatval($data["ancho_min"]), floatval($data["ancho_max"]),
                                $row->ancho_min, $row->ancho_max)){
                return lang('MaquinasPorDefecto.validation.error_ancho_overlap');
            }
            if(check_overlap(floatval($data["alto_min"]), floatval($data["alto_max"]),
                                $row->alto_min, $row->alto_max)){
                return lang('MaquinasPorDefecto.validation.error_alto_overlap');
            }
            if(check_overlap(floatval($data["tirada_min"]), floatval($data["tirada_max"]),
                                $row->tirada_min, $row->tirada_max)){
                    return lang('MaquinasPorDefecto.validation.error_tirada_overlap');
            }
        }


        return "";
    }

}


