<?php
namespace App\Models\Tarifas;

class TarifaManipuladoLineaModel extends \App\Models\BaseModel
{
    protected $table = "tarifa_manipulado_lineas";

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

    const SORTABLE = [
        0 => "t1.tirada_min",
        1 => "t1.tirada_max",
        2 => "t1.precio_min",
        3 => "t1.precio_max",
    ];

    protected $allowedFields = [
        "tarifa_manipulado_id",
        "tirada_min",
        "tirada_max",
        "precio_min",
        "precio_max",
        "user_created_id",
        "is_deleted",

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

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "tarifa_manipulado_id";

    protected $validationRules = [
        "precio_max" => [
            "label" => "TarifaManipuladoLineas.precioMax",
            "rules" => "required|decimal",
        ],
        "precio_min" => [
            "label" => "TarifaManipuladoLineas.precioMin",
            "rules" => "required|decimal",
        ],
        "tirada_max" => [
            "label" => "TarifaManipuladoLineas.tiradaMax",
            "rules" => "required|decimal",
        ],
        "tirada_min" => [
            "label" => "TarifaManipuladoLineas.tiradaMin",
            "rules" => "required|decimal",
        ],
        "margen" => [
            "label" => "TarifaManipuladoLineas.margen",
            "rules" => "required|decimal",
        ],
    ];

    protected $validationMessages = [
        "precio_max" => [
            "decimal" => "TarifaManipuladoLineas.validation.precio_max.decimal",
            "required" => "TarifaManipuladoLineas.validation.precio_max.required",
        ],
        "precio_min" => [
            "decimal" => "TarifaManipuladoLineas.validation.precio_min.decimal",
            "required" => "TarifaManipuladoLineas.validation.precio_min.required",
        ],
        "tirada_max" => [
            "decimal" => "TarifaManipuladoLineas.validation.tirada_max.decimal",
            "required" => "TarifaManipuladoLineas.validation.tirada_max.required",
        ],
        "tirada_min" => [
            "decimal" => "TarifaManipuladoLineas.validation.tirada_min.decimal",
            "required" => "TarifaManipuladoLineas.validation.tirada_min.required",
        ],
        "margen" => [
            "decimal" => "TarifaManipuladoLineas.validation.margen.decimal",
            "required" => "TarifaManipuladoLineas.validation.margen.required",
        ],
    ];

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "", $tarifa_manipulado_id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.tarifa_manipulado_id AS tarifa_manipulado_id, t1.tirada_min AS tirada_min, t1.tirada_max AS tirada_max, t1.precio_min AS precio_min, t1.precio_max AS precio_max, t1.margen AS margen, t2.id AS tarifa_manipulado"
            );
        //JJO
        $builder->where('tarifa_manipulado_id', $tarifa_manipulado_id);    
        $builder->where("t1.is_deleted", 0);

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


        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orLike("t1.tarifa_manipulado_id", $search)
                ->orLike("t1.tirada_min", $search)
                ->orLike("t1.tirada_max", $search)
                ->orLike("t1.precio_min", $search)
                ->orLike("t1.precio_max", $search)
                ->orLike("t1.id", $search)
                ->orLike("t1.tarifa_manipulado_id", $search)
                ->orLike("t1.tirada_min", $search)
                ->orLike("t1.tirada_max", $search)
                ->orLike("t1.precio_min", $search)
                ->orLike("t1.precio_max", $search)
                ->groupEnd();
    }

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

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

        $rows = $this->db
            ->table($this->table)
            ->select("id, tirada_min, tirada_max")
            ->where("is_deleted", 0)
            ->where("tarifa_manipulado_id", $id_tarifa_manipulado)
            ->get()->getResultObject();


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

        return "";
    }
}
