<?php
namespace App\Models\Configuracion;

class ImposicionModel extends \App\Models\GoBaseModel
{
    protected $table = "lg_imposiciones";

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

    const SORTABLE = [
        1 => "t1.id",
        2 => "t1.ancho",
        3 => "t1.alto",
        4 => "t1.unidades",
        5 => "t1.orientacion",
        6 => "t1.maquina",
        7 => "t1.etiqueta",
    ];

    protected $allowedFields = ["ancho", "alto", "unidades", "orientacion", "maquina", "etiqueta"];
    protected $returnType = "App\Entities\Configuracion\Imposicion";

    public static $labelField = "ancho";

    protected $validationRules = [
        "alto" => [
            "label" => "Imposiciones.alto",
            "rules" => "required|integer",
        ],
        "ancho" => [
            "label" => "Imposiciones.ancho",
            "rules" => "required|integer",
        ],
        "etiqueta" => [
            "label" => "Imposiciones.etiqueta",
            "rules" => "trim|max_length[100]",
        ],
        "maquina" => [
            "label" => "Imposiciones.maquina",
            "rules" => "trim|max_length[100]",
        ],
        "orientacion" => [
            "label" => "Imposiciones.orientacion",
            "rules" => "permit_empty|in_list[H,V]",
        ],
        "unidades" => [
            "label" => "Imposiciones.unidades",
            "rules" => "integer|permit_empty",
        ],
    ];

    protected $validationMessages = [
        "alto" => [
            "integer" => "Imposiciones.validation.alto.integer",
            "required" => "Imposiciones.validation.alto.required",
        ],
        "ancho" => [
            "integer" => "Imposiciones.validation.ancho.integer",
            "required" => "Imposiciones.validation.ancho.required",
        ],
        "etiqueta" => [
            "max_length" => "Imposiciones.validation.etiqueta.max_length",
        ],
        "maquina" => [
            "max_length" => "Imposiciones.validation.maquina.max_length",
        ],
        "orientacion" => [
            "in_list" => "Imposiciones.validation.orientacion.in_list",
        ],
        "unidades" => [
            "integer" => "Imposiciones.validation.unidades.integer",
        ],
    ];

    /**
     * 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.ancho AS ancho, t1.alto AS alto, t1.unidades AS unidades, t1.orientacion AS orientacion, t1.maquina AS maquina, t1.etiqueta AS etiqueta"
            );

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orlike("t1.ancho", $search)
                ->orLike("t1.alto", $search)
                ->orLike("t1.unidades", $search)
                ->orLike("t1.orientacion", $search)
                ->orLike("t1.maquina", $search)
                ->orLike("t1.etiqueta", $search)
                ->orlike("t1.id", $search)
                ->orLike("t1.ancho", $search)
                ->orLike("t1.alto", $search)
                ->orLike("t1.unidades", $search)
                ->orLike("t1.orientacion", $search)
                ->orLike("t1.maquina", $search)
                ->orLike("t1.etiqueta", $search)
                ->groupEnd();
    }
}
