<?php
namespace App\Models\Tarifas;

class TarifaEncuadernacionModel extends \App\Models\GoBaseModel
{
    protected $table = "tarifa_encuadernacion";

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

    const SORTABLE = [
        0 => "t1.nombre",
        1 => "t1.precio_min",
        2 => "t1.importe_fijo",
        3 => "t1.mostrar_en_presupuesto",

    ];

    protected $allowedFields = [
        "nombre",
        "precio_min",
        "importe_fijo",
        "mostrar_en_presupuesto",
        "deleted_at",
        "is_deleted",
        "user_created_id",
        "user_updated_id"
    ];
    protected $returnType = "App\Entities\Tarifas\TarifaEncuadernacionEntity";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "nombre";

    protected $validationRules = [
        "nombre" => [
            "label" => "Tarifaencuadernacion.nombre",
            "rules" => "trim|required|max_length[255]",
        ],

        "precio_min" => [
            "label" => "Tarifaencuadernacion.precioMin",
            "rules" => "required|decimal",
        ],
        "importe_fijo" => [
            "label" => "Tarifaencuadernacion.importeFijo",
            "rules" => "required|decimal",
        ],
    ];

    protected $validationMessages = [
        "nombre" => [
            "max_length" => "Tarifaencuadernacion.validation.nombre.max_length",
            "required" => "Tarifaencuadernacion.validation.nombre.required",
        ],
        "precio_min" => [
            "required" => "Tarifaencuadernacion.validation.precio_min.required",
            "decimal" => "Tarifaencuadernacion.validation.precio_min.decimal",
        ],
        "importe_fijo" => [
            "required" => "Tarifaencuadernacion.validation.importe_fijo.required",
            "decimal" => "Tarifaencuadernacion.validation.importe_fijo.decimal",
        ],
    ];

    /**
     * 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.precio_min AS precio_min, t1.importe_fijo AS importe_fijo, t1.mostrar_en_presupuesto AS mostrar_en_presupuesto");

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

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