<?php

namespace App\Models\Configuracion;

class MaquinasTarifasImpresionModel extends \App\Models\BaseModel
{
    protected $table = "lg_maquinas_tarifas_impresion";

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

    const SORTABLE = [
        1 => "t1.tipo",
        2 => "t1.uso",
        3 => "t1.precio",
        4 => "t1.margen"
    ];

    protected $allowedFields = ["maquina_id", "tipo", "precio", "uso", "margen", "user_created_id", "user_updated_id", "is_deleted", "deleted_at", "created_at", "updated_at"];
    protected $returnType = "App\Entities\Configuracion\MaquinasTarifasImpresionEntity";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "maquina_id";

    protected $validationRules = [
        "precio" => [
            "label" => "MaquinasTarifasImpresions.precio",
            "rules" => "required|decimal",
        ],
        "tipo" => [
            "label" => "MaquinasTarifasImpresions.tipo",
            "rules" => "permit_empty|in_list[negro,color,negrohq,bicolor,colorhq]",
        ],
        "uso" => [
            "label" => "MaquinasTarifasImpresions.uso",
            "rules" => "trim|required|max_length[255]",
        ],
    ];

    protected $validationMessages = [
        "precio" => [
            "decimal" => "MaquinasTarifasImpresions.validation.precio.decimal",
            "required" => "MaquinasTarifasImpresions.validation.precio.required",
        ],
        "tipo" => [
            "in_list" => "MaquinasTarifasImpresions.validation.tipo.in_list",
        ],
        "uso" => [
            "max_length" => "MaquinasTarifasImpresions.validation.uso.max_length",
            "required" => "MaquinasTarifasImpresions.validation.uso.required",
        ],
    ];


    public function findAllWithMaquinas(string $selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.nombre AS maquina FROM " .
            $this->table .
            " t1  LEFT JOIN lg_maquinas t2 ON t1.maquina_id = t2.id";
        if (!is_null($limit) && intval($limit) > 0) {
            $sql .= " LIMIT " . $limit;
        }

        if (!is_null($offset) && intval($offset) > 0) {
            $sql .= " OFFSET " . $offset;
        }

        $query = $this->db->query($sql);
        $result = $query->getResultObject();
        return $result;
    }

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "", $maquina_id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.tipo AS tipo, t1.precio AS precio, t1.uso AS uso, t1.margen AS margen, t1.is_deleted AS is_deleted, t2.nombre AS maquina"
            );

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

        $builder->join("lg_maquinas t2", "t1.maquina_id = t2.id", "left");

        return empty($search)
            ? $builder
            : $builder
            ->groupStart()
            ->like("t1.id", $search)
            ->orLike("t1.tipo", $search)
            ->orLike("t1.precio", $search)
            ->orLike("t1.uso", $search)
            ->orLike("t1.margen", $search)
            ->orLike("t1.id", $search)
            ->orLike("t1.maquina_id", $search)
            ->orLike("t1.tipo", $search)
            ->orLike("t1.precio", $search)
            ->orLike("t1.uso", $search)
            ->groupEnd();
    }

    public function getTarifa($maquina_id = -1, $uso = "", $tipo = "")
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.precio AS precio, t1.margen AS margen"
            );

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

        $tarifas = $builder->get()->getResultArray();
        if (count($tarifas)>0){
            
            return [$tarifas[0]['id'], floatval($tarifas[0]['precio']), floatval($tarifas[0]['margen'])];
        }
        return $tarifas;
    }
}
