<?php
namespace App\Models\Configuracion;

class PapelImpresionTipologiaModel extends \App\Models\BaseModel
{
    protected $table = "lg_papel_impresion_tipologias";

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

    protected $allowedFields = [
        "papel_impresion_id",
        "tipo",
        "negro",
        "cyan",
        "magenta",
        "amarillo",
        "cg",
        "gota_negro",
        "gota_color",
    ];
    protected $returnType = "App\Entities\Configuracion\PapelImpresionTipologia";

    public static $labelField = "tipo";

    protected $validationRules = [
        "cg"=> [
            "label" => "ImpresionTipologias.cg",
            "rules" => "required|decimal",
        ],

        "amarillo" => [
            "label" => "ImpresionTipologias.amarillo",
            "rules" => "required|decimal",
        ],
        "cyan" => [
            "label" => "ImpresionTipologias.cyan",
            "rules" => "required|decimal",
        ],
        "gota_color" => [
            "label" => "ImpresionTipologias.gotaColor",
            "rules" => "required|decimal",
        ],
        "gota_negro" => [
            "label" => "ImpresionTipologias.gotaNegro",
            "rules" => "required|decimal",
        ],
        "magenta" => [
            "label" => "ImpresionTipologias.magenta",
            "rules" => "required|decimal",
        ],
        "negro" => [
            "label" => "ImpresionTipologias.negro",
            "rules" => "required|decimal",
        ],
        "tipo" => [
            "label" => "ImpresionTipologias.tipo",
            "rules" => "required|in_list[negro,color,bicolor]",
        ],
    ];

    protected $validationMessages = [
        "amarillo" => [
            "decimal" => "ImpresionTipologias.validation.amarillo.decimal",
            "required" => "ImpresionTipologias.validation.amarillo.required",
        ],
        "cyan" => [
            "decimal" => "ImpresionTipologias.validation.cyan.decimal",
            "required" => "ImpresionTipologias.validation.cyan.required",
        ],
        "gota_color" => [
            "decimal" => "ImpresionTipologias.validation.gota_color.decimal",
            "required" => "ImpresionTipologias.validation.gota_color.required",
        ],
        "gota_negro" => [
            "decimal" => "ImpresionTipologias.validation.gota_negro.decimal",
            "required" => "ImpresionTipologias.validation.gota_negro.required",
        ],
        "magenta" => [
            "decimal" => "ImpresionTipologias.validation.magenta.decimal",
            "required" => "ImpresionTipologias.validation.magenta.required",
        ],
        "negro" => [
            "decimal" => "ImpresionTipologias.validation.negro.decimal",
            "required" => "ImpresionTipologias.validation.negro.required",
        ],
        "tipo" => [
            "in_list" => "ImpresionTipologias.validation.tipo.in_list",
            "required" => "ImpresionTipologias.validation.tipo.required",
        ],
    ];

    public function findAllWithPapelImpresion(string $selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.nombre AS papel_impresion_id FROM " .
            $this->table .
            " t1  LEFT JOIN lg_papel_impresion t2 ON t1.papel_impresion_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;
    }

    public function findTipologiasForPapelImpresion(int $papelImpresionID, $tipo=null){
        
        $builder = $this->db
            ->table($this->table )
            ->select("*")
            ->where("papel_impresion_id", $papelImpresionID);
        
        if(!is_null($tipo)){
            $builder->where("tipo", $tipo);
        }

        return $builder;        
    }


    public function removeForPapelImpresion($papel_impresion_id){
        $builder = $this->db
            ->table($this->table . " t1")
            ->where('papel_impresion_id', $papel_impresion_id)
            ->delete();

    }

    public function getResource($id)
    {
        $builder = $this->db
            ->table($this->table)
            ->select("*")
            ->where("id", $id);

        return $builder;
            
    }

    public function checkTipo($data, $id){
        
        $builder = $this->db
            ->table($this->table)
            ->select("id")
            ->where("papel_impresion_id", $data["papel_impresion_id"])
            ->where("tipo", $data["tipo"]);
            
        if ($builder->countAllResults(false) > 0){
            $found_id = $builder->get()->getResultObject()[0]->id;
            if($found_id != $id){
                return lang('ImpresionTipologias.validation.error_tipo_duplicado');
            }
        }
        

        return "";
    }
}
