<?php
namespace App\Models\Configuracion;

class MaquinasCallesModel extends \App\Models\BaseModel
{
    protected $table = "maquinas_calles";

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

    const SORTABLE = [
        0 => "t1.formas",
        1 => "t1.internas",
        2 => "t1.externas",
    ];

    protected $allowedFields = [
        "maquina_id", 
        "formas",
        "internas", 
        "externas", 
        "user_created_id", 
        "user_updated_id", 
        "is_deleted", 
        "deleted_at", 
        "created_at", 
        "updated_at"];
    protected $returnType = "App\Entities\Configuracion\MaquinasCallesEntity";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "maquina_id";

    protected $validationRules = [
        "formas" => [
            "rules" => "required|integer",
        ],
        "internas" => [
            "rules" => "required|decimal",
        ],
        "externas" => [
            "rules" => "required|decimal",
        ],
    ];

    
    /**
     * 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.maquina_id as maquina, t1.formas AS formas, 
                t1.internas AS internas, t1.externas AS externas,"
            );

        //JJO
        $builder->where('maquina_id', $maquina_id);    
        $builder->where("t1.is_deleted", 0);
    
        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.formas", $search)
                ->orLike("t1.internas", $search)
                ->orLike("t1.externas", $search)
                ->orLike("t1.formas", $search)
                ->orLike("t1.internas", $search)
                ->orLike("t1.externas", $search)
                ->groupEnd();
    }

    public function getCallesForMaquina($maquina_id=-1, $num_formas = 0)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.formas AS formas, 
                t1.internas AS internas, t1.externas AS externas,"
            );

        //JJO
        $builder->where('t1.maquina_id', $maquina_id);    
        $builder->where('t1.formas', $num_formas);    
        $builder->where("t1.is_deleted", 0);
    
        return $builder->get()->getResultObject();
    }

    public function checkDuplicatedFormas($data = [], $id = null){

        helper('general');
        
        $rows = $this->db
            ->table($this->table)
            ->select("id, formas")
            ->where("is_deleted", 0)
            ->where("maquina_id", $data['maquina_id'])
            ->get()->getResultObject();


        foreach ($rows as $row) {
            if (!is_null($id)){
                if($row->id == $id){
                    continue;
                }
            }
            if(intval($data["formas"]) == $row->formas){
                return lang('MaquinasCalles.validation.error_formas_exists');
            }
        }
    }
}
