<?php
namespace App\Models\Configuracion;

class MaquinaModel extends \App\Models\GoBaseModel
{
    protected $table = "lg_maquinas";

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

    const SORTABLE = [
        //1 => "t1.id",
        0 => "t1.nombre",
        1 => "t2.nombre",
        2 => "t1.tipo",
        3 => "t1.velocidad",
        4 => "t1.duracion_jornada",
        5 => "t1.ancho",
        6 => "t1.alto",
        7 => "t1.ancho_impresion",
        8 => "t1.alto_impresion",
        9 => "t1.orden_planning",
        10 => "t1.min",
        11 => "t1.max",
        
        
    ];

    protected $allowedFields = [
        "nombre",
        "is_padre",
        "tipo",
        "velocidad",
        "ancho",
        "alto",
        "ancho_impresion",
        "alto_impresion",
        "alto_click",
        "padre_id",
        "min",
        "max",
        "duracion_jornada",
        "orden_planning",
        "is_rotativa",
        "precio_tinta_negro",
        "is_tinta",
        "precio_tinta_color",
        "velocidad_corte",
        "precio_hora_corte",
        "metrosxminuto",
        "forzar_num_formas_horizontales_portada",
        "forzar_num_formas_verticales_portada",
        "observaciones",
        "deleted_at",
        "is_deleted",
        "user_created_id",
        "user_updated_id"
    ];
    protected $returnType = "App\Entities\Configuracion\Maquina";

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

    protected $createdField = "created_at";

    public static $labelField = "nombre";

    protected $validationRules = [
        "alto" => [
            "label" => "Maquinas.alto",
            "rules" => "required|decimal|greater_than[0]",
        ],
        "alto_click" => [
            "label" => "Maquinas.altoClick",
            "rules" => "required|decimal|greater_than[0]",
        ],
        "alto_impresion" => [
            "label" => "Maquinas.altoImpresion",
            "rules" => "required|decimal|greater_than[0]",
        ],
        "ancho" => [
            "label" => "Maquinas.ancho",
            "rules" => "required|decimal|greater_than[0]",
        ],
        "ancho_impresion" => [
            "label" => "Maquinas.anchoImpresion",
            "rules" => "required|decimal|greater_than[0]",
        ],
        "duracion_jornada" => [
            "label" => "Maquinas.duracionJornada",
            "rules" => "required|integer",
        ],
        "forzar_num_formas_horizontales_portada" => [
            "label" => "Maquinas.forzarNumFormasHorizontalesPortada",
            "rules" => "integer|permit_empty",
        ],
        "forzar_num_formas_verticales_portada" => [
            "label" => "Maquinas.forzarNumFormasVerticalesPortada",
            "rules" => "integer|permit_empty",
        ],
        "max" => [
            "label" => "Maquinas.max",
            "rules" => "required|integer",
        ],
        "metrosxminuto" => [
            "label" => "Maquinas.metrosxminuto",
            "rules" => "max_length[31]",
        ],
        "min" => [
            "label" => "Maquinas.min",
            "rules" => "required|integer",
        ],
        "nombre" => [
            "label" => "Maquinas.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "observaciones" => [
            "label" => "Maquinas.observaciones",
            "rules" => "trim|max_length[16313]",
        ],
        "orden_planning" => [
            "label" => "Maquinas.ordenPlanning",
            "rules" => "required|integer",
        ],
        "precio_hora_corte" => [
            "label" => "Maquinas.precioHoraCorte",
            "rules" => "decimal",
        ],
        "precio_tinta_color" => [
            "label" => "Maquinas.precioTintaColor",
            "rules" => "decimal",
        ],
        "precio_tinta_negro" => [
            "label" => "Maquinas.precioTintaNegro",
            "rules" => "decimal",
        ],
        "tipo" => [
            "label" => "Maquinas.tipo",
            "rules" => "required|in_list[impresion,manipulado,acabado]",
        ],
        "velocidad" => [
            "label" => "Maquinas.velocidad",
            "rules" => "required|decimal",
        ],
        "velocidad_corte" => [
            "label" => "Maquinas.velocidadCorte",
            "rules" => "decimal",
        ],
    ];

    protected $validationMessages = [
        "alto" => [
            "decimal" => "Maquinas.validation.alto.decimal",
        ],
        "alto_click" => [
            "decimal" => "Maquinas.validation.alto_click.decimal",
            "required" => "Maquinas.validation.alto_click.required",
        ],
        "alto_impresion" => [
            "decimal" => "Maquinas.validation.alto_impresion.decimal",
            "required" => "Maquinas.validation.alto_impresion.required",
        ],
        "ancho" => [
            "decimal" => "Maquinas.validation.ancho.decimal",
        ],
        "ancho_impresion" => [
            "decimal" => "Maquinas.validation.ancho_impresion.decimal",
            "required" => "Maquinas.validation.ancho_impresion.required",
        ],
        "duracion_jornada" => [
            "integer" => "Maquinas.validation.duracion_jornada.integer",
            "required" => "Maquinas.validation.duracion_jornada.required",
        ],
        "forzar_num_formas_horizontales_portada" => [
            "integer" => "Maquinas.validation.forzar_num_formas_horizontales_portada.integer",
        ],
        "forzar_num_formas_verticales_portada" => [
            "integer" => "Maquinas.validation.forzar_num_formas_verticales_portada.integer",
        ],
        "max" => [
            "integer" => "Maquinas.validation.max.integer",
            "required" => "Maquinas.validation.max.required",
        ],
        "metrosxminuto" => [
            "max_length" => "Maquinas.validation.metrosxminuto.max_length",
            "required" => "Maquinas.validation.metrosxminuto.required",
        ],
        "min" => [
            "integer" => "Maquinas.validation.min.integer",
            "required" => "Maquinas.validation.min.required",
        ],
        "nombre" => [
            "max_length" => "Maquinas.validation.nombre.max_length",
            "required" => "Maquinas.validation.nombre.required",
        ],
        "observaciones" => [
            "max_length" => "Maquinas.validation.observaciones.max_length",
            //"required" => "Maquinas.validation.observaciones.required",
        ],
        "orden_planning" => [
            "integer" => "Maquinas.validation.orden_planning.integer",
            "required" => "Maquinas.validation.orden_planning.required",
        ],
        "precio_hora_corte" => [
            "decimal" => "Maquinas.validation.precio_hora_corte.decimal",
            "required" => "Maquinas.validation.precio_hora_corte.required",
        ],
        "precio_tinta_color" => [
            "decimal" => "Maquinas.validation.precio_tinta_color.decimal",
            "required" => "Maquinas.validation.precio_tinta_color.required",
        ],
        "precio_tinta_negro" => [
            "decimal" => "Maquinas.validation.precio_tinta_negro.decimal",
            "required" => "Maquinas.validation.precio_tinta_negro.required",
        ],
        "tipo" => [
            "in_list" => "Maquinas.validation.tipo.in_list",
            "required" => "Maquinas.validation.tipo.required",
        ],
        "velocidad" => [
            "decimal" => "Maquinas.validation.velocidad.decimal",
            "required" => "Maquinas.validation.velocidad.required",
        ],
        "velocidad_corte" => [
            "decimal" => "Maquinas.validation.velocidad_corte.decimal",
            "required" => "Maquinas.validation.velocidad_corte.required",
        ],
    ];

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

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

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

    public function getIdMaquinasForPapelImpresion($papel_impresion_id, $rotativa){
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "'".$papel_impresion_id."'". " as papel_impresion_id, t1.id AS maquina_id, '0' as active"
                );
        $builder->where("t1.is_rotativa", $rotativa);
        
        return $builder;
    }

    /**
     * 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.nombre AS nombre, t1.is_padre AS is_padre, t1.tipo AS tipo, t1.velocidad AS velocidad, t1.ancho AS ancho, t1.alto AS alto, t1.ancho_impresion AS ancho_impresion, t1.alto_impresion AS alto_impresion, t1.alto_click AS alto_click, t1.min AS min, t1.max AS max, t1.duracion_jornada AS duracion_jornada, t1.orden_planning AS orden_planning, t1.is_rotativa AS is_rotativa, t1.precio_tinta_negro AS precio_tinta_negro, t1.is_tinta AS is_tinta, t1.precio_tinta_color AS precio_tinta_color, t1.velocidad_corte AS velocidad_corte, t1.precio_hora_corte AS precio_hora_corte, t1.metrosxminuto AS metrosxminuto, t1.forzar_num_formas_horizontales_portada AS forzar_num_formas_horizontales_portada, t1.forzar_num_formas_verticales_portada AS forzar_num_formas_verticales_portada, t1.observaciones AS observaciones, t2.nombre AS padre"
            );
        $builder->join("lg_maquinas t2", "t1.padre_id = t2.id", "left");

        //JJO
        $builder->where("t1.is_deleted", 0);    

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.tipo", $search)
                ->orLike("t1.velocidad", $search)
                ->orLike("t1.ancho", $search)
                ->orLike("t1.alto", $search)
                ->orLike("t1.ancho_impresion", $search)
                ->orLike("t1.alto_impresion", $search)
                ->orLike("t1.alto_click", $search)
                ->orLike("t1.min", $search)
                ->orLike("t1.max", $search)
                ->orLike("t1.duracion_jornada", $search)
                ->orLike("t1.orden_planning", $search)
                ->orLike("t1.precio_tinta_negro", $search)
                ->orLike("t1.precio_tinta_color", $search)
                ->orLike("t1.velocidad_corte", $search)
                ->orLike("t1.precio_hora_corte", $search)
                ->orLike("t1.metrosxminuto", $search)
                ->orLike("t1.forzar_num_formas_horizontales_portada", $search)
                ->orLike("t1.forzar_num_formas_verticales_portada", $search)
                ->orLike("t1.observaciones", $search)
                ->orLike("t2.id", $search)
                ->orLike("t1.id", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.tipo", $search)
                ->orLike("t1.velocidad", $search)
                ->orLike("t1.ancho", $search)
                ->orLike("t1.alto", $search)
                ->orLike("t1.ancho_impresion", $search)
                ->orLike("t1.alto_impresion", $search)
                ->orLike("t1.alto_click", $search)
                ->orLike("t1.padre_id", $search)
                ->orLike("t1.min", $search)
                ->orLike("t1.max", $search)
                ->orLike("t1.duracion_jornada", $search)
                ->orLike("t1.orden_planning", $search)
                ->orLike("t1.precio_tinta_negro", $search)
                ->orLike("t1.precio_tinta_color", $search)
                ->orLike("t1.velocidad_corte", $search)
                ->orLike("t1.precio_hora_corte", $search)
                ->orLike("t1.metrosxminuto", $search)
                ->orLike("t1.forzar_num_formas_horizontales_portada", $search)
                ->orLike("t1.forzar_num_formas_verticales_portada", $search)
                ->orLike("t1.observaciones", $search)
                ->orLike("t2.nombre", $search)
                ->groupEnd();
    }
}
