<?php
namespace App\Models\Configuracion;

class PapelGenericoModel extends \App\Models\GoBaseModel
{
    protected $table = "lg_papel_generico";

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

    const SORTABLE = [
        //0 => "t1.id",
        0 => "t1.nombre",
        1 => "t1.code",
        2 => "t1.code_ot",
        3 => "t1.show_in_client",
    ];

    protected $allowedFields = ["nombre", "code", "code_ot", "show_in_client","deleted_at","is_deleted"];
    protected $returnType = "App\Entities\Configuracion\PapelGenerico";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "nombre";

    protected $validationRules = [
        "code" => [
            "label" => "PapelGenericoes.code",
            "rules" => "trim|max_length[5]",
        ],
        "code_ot" => [
            "label" => "PapelGenericoes.codeOt",
            "rules" => "trim|max_length[5]",
        ],
        "nombre" => [
            "label" => "PapelGenericoes.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
    ];

    protected $validationMessages = [
        "code" => [
            "max_length" => "PapelGenericoes.validation.code.max_length",
        ],
        "code_ot" => [
            "max_length" => "PapelGenericoes.validation.code_ot.max_length",
        ],
        "nombre" => [
            "max_length" => "PapelGenericoes.validation.nombre.max_length",
            "required" => "PapelGenericoes.validation.nombre.required",
        ],
    ];

    /**
     * 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.code AS code, t1.code_ot AS code_ot, t1.show_in_client AS show_in_client"
            )
            ->where("is_deleted", 0);

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.code", $search)
                ->orLike("t1.code_ot", $search)
                ->orLike("t1.id", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.code", $search)
                ->orLike("t1.code_ot", $search)
                ->groupEnd();
    }
}
