<?php

namespace App\Models\Configuracion;

class PapelImpresionMargenModel extends \App\Models\BaseModel
{
    protected $table = "papel_impresion_margenes";

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

    const SORTABLE = [
        0 => "t1.paginas_min",
        1 => "t1.paginas_max",
        2 => "t1.margen",
    ];

    protected $allowedFields = [
        "papel_impresion_id",
        "paginas_min",
        "paginas_max",
        "margen",
        "user_created_id",
        "user_updated_id",
        "is_deleted",
        "deleted_at",

    ];
    protected $returnType = "App\Entities\Configuracion\PapelImpresionMargen";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "papel_impresion_id";

    protected $validationRules = [
        "paginas_max" => [
            "label" => "PapelImpresionMargenes.paginasMax",
            "rules" => "required|decimal",
        ],
        "paginas_min" => [
            "label" => "PapelImpresionMargenes.paginasMin",
            "rules" => "required|decimal",
        ],
        "margen" => [
            "label" => "PapelImpresionMargenes.margen",
            "rules" => "required|decimal",
        ],
    ];

    protected $validationMessages = [
        "paginas_max" => [
            "decimal" => "PapelImpresionMargenes.validation.paginas_max.decimal",
            "required" => "PapelImpresionMargenes.validation.paginas_max.required",
        ],
        "paginas_min" => [
            "decimal" => "PapelImpresionMargenes.validation.paginas_min.decimal",
            "required" => "PapelImpresionMargenes.validation.paginas_min.required",
        ],
        "margen" => [
            "decimal" => "PapelImpresionMargenes.validation.margen.decimal",
            "required" => "PapelImpresionMargenes.validation.margen.required",
        ],
    ];

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

        return empty($search)
            ? $builder
            : $builder
            ->groupStart()
            ->like("t1.paginas_min", $search)
            ->orLike("t1.paginas_max", $search)
            ->orLike("t1.margen", $search)
            ->orLike("t1.paginas_min", $search)
            ->orLike("t1.paginas_max", $search)
            ->orLike("t1.margen", $search)
            ->groupEnd();
    }

    public function checkIntervals($data = [], $id_linea = null, $papel_impresion_id = null)
    {

        helper('general');

        if (floatval($data["paginas_min"]) >= floatval($data["paginas_max"])) {
            return lang('PapelImpresionMargenes.validation.error_paginas_range');
        }

        $rows = $this->db
            ->table($this->table)
            ->select("id, paginas_min, paginas_max")
            ->where("is_deleted", 0)
            ->where("papel_impresion_id", $papel_impresion_id)
            ->get()->getResultObject();


        foreach ($rows as $row) {
            if (!is_null($id_linea)) {
                if ($row->id == $id_linea) {
                    continue;
                }
            }
            if (check_overlap(
                floatval($data["paginas_min"]),
                floatval($data["paginas_max"]),
                $row->paginas_min,
                $row->paginas_max
            )) {
                return lang('PapelImpresionMargenes.validation.error_paginas_overlap');
            }
        }

        return "";
    }

    public function getMargenFormPags($papel_impresion_id = -1, $num_paginas = 0)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.margen AS margen"
            );
        //JJO
        $builder->where('papel_impresion_id', $papel_impresion_id);
        $builder->where("t1.is_deleted", 0);
        $builder->where("t1.paginas_min <=", $num_paginas);
        $builder->where("t1.paginas_max >=", $num_paginas);
        

        return $builder->get()->getResultArray();
    }
}
