<?php
namespace App\Models\Tarifas;

class TarifaEncuadernacionTiradaModel extends \App\Models\BaseModel
{
    protected $table = "tarifa_encuadernacion_tiradas";

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

    const SORTABLE = [
        0 => "t3.nombre",
        1 => "t1.tirada_min",
        2 => "t1.tirada_max",
        4 => "t1.importe_fijo",        
    ];

    protected $allowedFields = [
        "tarifa_encuadernacion_id",
        "tirada_min",
        "tirada_max",
        "proveedor_id", 
        "importe_fijo",
        "user_created_id",
        "user_updated_id",
        "is_deleted",
        "deleted_at",

    ];
    protected $returnType = "App\Entities\Tarifas\TarifaEncuadernacionTirada";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "tarifa_encuadernacion_id";

    protected $validationRules = [
        "tirada_max" => [
            "label" => "TarifaEncuadernacionTiradas.tiradaMax",
            "rules" => "required|integer",
        ],
        "tirada_min" => [
            "label" => "TarifaEncuadernacionTiradas.tiradaMin",
            "rules" => "required|integer",
        ],
        "importe_fijo" => [
            "label" => "Tarifaencuadernacion.importeFijo",
            "rules" => "required|decimal",
        ],
    ];

    protected $validationMessages = [
        "tirada_max" => [
            "integer" => "TarifaEncuadernacionTiradas.validation.tirada_max.integer",
            "required" => "TarifaEncuadernacionTiradas.validation.tirada_max.required",
        ],
        "tirada_min" => [
            "integer" => "TarifaEncuadernacionTiradas.validation.tirada_min.integer",
            "required" => "TarifaEncuadernacionTiradas.validation.tirada_min.required",
        ],
        "importe_fijo" => [
            "required" => "Tarifaencuadernacion.validation.importe_fijo.required",
            "decimal" => "Tarifaencuadernacion.validation.importe_fijo.decimal",
        ],
    ];

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "", $tarifa_encuadernacion_id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.tarifa_encuadernacion_id AS tarifa_encuadernacion_id, 
                t1.importe_fijo AS importe_fijo,
                t1.tirada_min AS tirada_min, t1.tirada_max AS tirada_max, 
                t3.nombre AS proveedor, t3.id AS proveedor_id, t2.id AS tarifa_encuadernacion"
            );
        //JJO
        $builder->where('tarifa_encuadernacion_id', $tarifa_encuadernacion_id);    
        $builder->where("t1.is_deleted", 0);

        $builder->join("tarifa_encuadernacion t2", "t1.tarifa_encuadernacion_id = t2.id", "left");
        $builder->join("lg_proveedores t3", "t1.proveedor_id = t3.id", "left");


        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->Like("t1.tirada_min", $search)
                ->orLike("t1.tirada_max", $search)
                ->orLike("t1.precio_min", $search)
                ->orLike("t1.importe_fijo", $search)
                ->orLike("t3.nombre", $search)
                ->orLike("t1.tirada_min", $search)
                ->orLike("t1.tirada_max", $search)
                ->orLike("t3.nombre", $search)
                ->orLike("t1.precio_min", $search)
                ->orLike("t1.importe_fijo", $search)
                ->groupEnd();
    }

    public function checkIntervals($data = [], $id_tirada = null, $tarifa_encuadernacion_id = null){
        
        helper('general');

        if(floatval($data["tirada_min"])>= floatval($data["tirada_max"])){
            return lang('TarifaEncuadernacionTiradas.validation.error_tirada_range');
        }

        $rows = $this->db
            ->table($this->table)
            ->select("id, tirada_min, tirada_max")
            ->where("is_deleted", 0)
            ->where("tarifa_encuadernacion_id", $tarifa_encuadernacion_id)
            ->where("proveedor_id", $data["proveedor_id"])
            ->get()->getResultObject();


        foreach ($rows as $row) {
            if (!is_null($id_tirada)){
                if($row->id == $id_tirada){
                    continue;
                }
            }
            if(check_overlap(floatval($data["tirada_min"]), floatval($data["tirada_max"]),
                                $row->tirada_min, $row->tirada_max)){
                    return lang('TarifaEncuadernacionTiradas.validation.error_tirada_overlap');
            }
        }

        return "";
    }
}
