<?php

namespace App\Models\Configuracion;

class PapelFormatoModel extends \App\Models\BaseModel
{
    protected $table = "lg_papel_formato";

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

    const SORTABLE = [
        1 => "t1.id",
        2 => "t1.ancho",
        3 => "t1.alto",
        4 => "t1.created_at",
        5 => "t1.updated_at",
    ];

    protected $allowedFields = ["ancho", "alto", "orden_select"];
    protected $returnType = "App\Entities\Configuracion\PapelFormatoEntity";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "ancho";

    protected $validationRules = [
        "alto" => [
            "label" => "LgPapelFormatoes.alto",
            "rules" => "required|decimal",
        ],
        "ancho" => [
            "label" => "LgPapelFormatoes.ancho",
            "rules" => "required|decimal",
        ],
    ];

    protected $validationMessages = [
        "alto" => [
            "decimal" => "LgPapelFormatoes.validation.alto.decimal",
            "required" => "LgPapelFormatoes.validation.alto.required",
        ],
        "ancho" => [
            "decimal" => "LgPapelFormatoes.validation.ancho.decimal",
            "required" => "LgPapelFormatoes.validation.ancho.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.ancho AS ancho, t1.alto AS alto, t1.created_at AS created_at, t1.updated_at AS updated_at"
            );

        return empty($search)
            ? $builder
            : $builder
            ->groupStart()
            ->like("t1.id", $search)
            ->orLike("t1.ancho", $search)
            ->orLike("t1.alto", $search)
            ->orLike("t1.created_at", $search)
            ->orLike("t1.updated_at", $search)
            ->orLike("t1.id", $search)
            ->orLike("t1.ancho", $search)
            ->orLike("t1.alto", $search)
            ->orLike("t1.created_at", $search)
            ->orLike("t1.updated_at", $search)
            ->groupEnd();
    }

    public function getElementsForMenu()
    {
        return $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, CONCAT(t1.ancho, ' x ', t1.alto) AS tamanio"
            )->where('is_deleted', 0)->orderBy('orden_select', 'asc')->get()->getResultObject();
    }

    public function getElementsForMenu2($search = "")
    {
        $builder =  $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, CONCAT(t1.ancho, ' x ', t1.alto) AS name"
            )
            ->where('is_deleted', 0)
            ->where('id>', 0)
            ->orderBy('orden_select', 'asc');


        return empty($search)
            ? $builder->get()->getResultObject()
            : $builder
            ->groupStart()
            ->like('t1.ancho', $search)
            ->orLike('t1.alto', $search)
            ->groupEnd()->get()->getResultObject();
    }


    public function getNombre($id = -1)
    {
        try {
            $builder =  $this->db
                ->table($this->table . " t1")
                ->select(
                    "CONCAT(t1.ancho, ' x ', t1.alto) AS name"
                )
                ->where('t1.id', $id);

            return $builder->get()->getResultObject()[0]->name;
        } catch (\Exception $e) {
            return null;
        }
    }
}
