<?php
namespace App\Models\Tarifas;

class TarifaEnvioPrecioModel extends \App\Models\GoBaseModel
{
    protected $table = "tarifas_envios_precios";

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

    const SORTABLE = [
        0 => "t1.proveedor",
        1 => "t1.tipo_envio",
        2 => "t1.peso_min",
        3 => "t1.peso_max",
        4 => "t1.precio",
        5 => "t1.precio_adicional"
    ];

    protected $allowedFields = [
        "tarifa_envio_id",
        "proveedor_id",
        "tipo_envio",
        "peso_min",
        "peso_max",
        "precio",
        "precio_adicional",
        "deleted_at",
        "is_deleted",
    ];
    protected $returnType = "App\Entities\Tarifas\TarifaEnvioPrecioEntity";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "tarifa_envio_id";

    protected $validationRules = [
        "peso_max" => [
            "label" => "TarifasEnviosPrecios.pesoMax",
            "rules" => "required|decimal",
        ],
        "peso_min" => [
            "label" => "TarifasEnviosPrecios.pesoMin",
            "rules" => "required|decimal",
        ],
        "precio" => [
            "label" => "TarifasEnviosPrecios.precio",
            "rules" => "required|decimal",
        ],
        "precio_adicional" => [
            "label" => "TarifasEnviosPrecios.precioAdicional",
            "rules" => "required|decimal",
        ],
        "tipo_envio" => [
            "label" => "TarifasEnviosPrecios.tipoEnvio",
            "rules" => "required|in_list[a domicilio,cajas,palets]",
        ],
    ];

    protected $validationMessages = [
        "peso_max" => [
            "decimal" => "TarifasEnviosPrecios.validation.peso_max.decimal",
            "required" => "TarifasEnviosPrecios.validation.peso_max.required",
        ],
        "peso_min" => [
            "decimal" => "TarifasEnviosPrecios.validation.peso_min.decimal",
            "required" => "TarifasEnviosPrecios.validation.peso_min.required",
        ],
        "precio" => [
            "decimal" => "TarifasEnviosPrecios.validation.precio.decimal",
            "required" => "TarifasEnviosPrecios.validation.precio.required",
        ],
        "precio_adicional" => [
            "decimal" => "TarifasEnviosPrecios.validation.precio_adicional.decimal",
            "required" => "TarifasEnviosPrecios.validation.precio_adicional.required",
        ],
        "tipo_envio" => [
            "in_list" => "TarifasEnviosPrecios.validation.tipo_envio.in_list",
            "required" => "TarifasEnviosPrecios.validation.tipo_envio.required",
        ],
    ];
    public function findAllWithAllRelations(string $selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.id AS tarifa_envio,  t3.id AS proveedor FROM " .
            $this->table .
            " t1  LEFT JOIN lg_tarifas_envios t2 ON t1.tarifa_envio_id = t2.id LEFT JOIN lg_proveedores t3 ON t1.proveedor_id = t3.id";
        if (!is_null($limit) && intval($limit) > 0) {
            $sql .= " LIMIT " . intval($limit);
        }

        if (!is_null($offset) && intval($offset) > 0) {
            $sql .= " OFFSET " . intval($offset);
        }

        $query = $this->db->query($sql);
        $result = $query->getResultObject();
        return $result;
    }

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "")
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.tipo_envio AS tipo_envio, t1.peso_min AS peso_min, t1.peso_max AS peso_max, t1.precio AS precio, 
                t1.precio_adicional AS precio_adicional, t2.id AS tarifa_envio, t3.id AS proveedor_id, t3.nombre AS proveedor"
            );
        $builder->join("tarifas_envios_zonas t2", "t1.zona_envio_id = t2.id", "left");
        $builder->join("lg_proveedores t3", "t1.proveedor_id = t3.id", "left");

        //JJO
        $builder->where("t1.is_deleted", 0);

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.tipo_envio", $search)
                ->orLike("t1.peso_min", $search)
                ->orLike("t1.peso_max", $search)
                ->orLike("t1.precio", $search)
                ->orLike("t1.precio_adicional", $search)
                ->orLike("t3.nombre", $search)
                ->orLike("t1.tipo_envio", $search)
                ->orLike("t1.peso_min", $search)
                ->orLike("t1.peso_max", $search)
                ->orLike("t1.precio", $search)
                ->orLike("t1.precio_adicional", $search)
                ->orLike("t3.nombre", $search)
                ->groupEnd();
    }
}
