<?php

namespace App\Models\Tarifas;

class TarifaEncuadernacionDimensionesModel extends \App\Models\BaseModel
{
    protected $table = "tarifa_encuadernacion_dimensiones";

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

    protected $allowedFields = [];
    protected $returnType = "App\Entities\Tarifas\TarifaEncuadernacionDimensionesEntity";

    public static $labelField = "descripcion";

    protected $validationRules = [];

    protected $validationMessages = [];

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getDimensiones()
    {

        $builder = $this->db
            ->table($this->table . " t1")
            ->select("t1.id AS value, t1.keyword AS label")
            ->orderBy('t1.id', 'asc');

        $result = $builder->get()->getResultObject();

        // Convertir el resultado en un array
        $resultArray = json_decode(json_encode($result), true);

        // Buscar y mover el objeto con value == 3 al principio
        foreach ($resultArray as $key => $item) {
            if ($item['value'] == 4) {
                $generico = $item;
                unset($resultArray[$key]);
                array_unshift($resultArray, $generico);
                break;
            }
        }

        // Convertir de nuevo a objetos
        $result = json_decode(json_encode($resultArray));

        return $result;
    }
}
