<?php
namespace App\Models\Tarifas;

class TarifaEnvioZonaModel extends \App\Models\BaseModel
{
    protected $table = "tarifas_envios_zonas";

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

    const SORTABLE = [
        0 => "t1.nombre",
        1 => "t1.cp_inicial",
        2 => "t1.cp_final",
        3 => "t1.importe_fijo",
    ];

    protected $allowedFields = ["tarifa_envio_id", "nombre", "cp_inicial", "cp_final", "importe_fijo", "is_deleted", "deleted_at"];
    protected $returnType = "App\Entities\Tarifas\TarifaEnvioZonaEntity";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "tarifa_envio_id";

    protected $validationRules = [
        "nombre" => [
            "label" => "TarifasEnviosZonas.nombre",
            "rules" => "trim|required|max_length[50]",
        ],

        "cp_final" => [
            "label" => "TarifasEnviosZonas.cpFinal",
            "rules" => "trim|required|max_length[10]",
        ],
        "cp_inicial" => [
            "label" => "TarifasEnviosZonas.cpInicial",
            "rules" => "trim|required|max_length[10]",
        ],
        "importe_fijo" => [
            "label" => "TarifasEnviosZonas.importeFijo",
            "rules" => "required|integer",
        ],
    ];

    protected $validationMessages = [
        "nombre" => [
            "max_length" => "TarifasEnviosZonas.validation.nombre.max_length",
            "required" => "TarifasEnviosZonas.validation.nombre.required",
        ],
        "cp_final" => [
            "max_length" => "TarifasEnviosZonas.validation.cp_final.max_length",
            "required" => "TarifasEnviosZonas.validation.cp_final.required",
        ],
        "cp_inicial" => [
            "max_length" => "TarifasEnviosZonas.validation.cp_inicial.max_length",
            "required" => "TarifasEnviosZonas.validation.cp_inicial.required",
        ],
        "importe_fijo" => [
            "integer" => "TarifasEnviosZonas.validation.importe_fijo.integer",
            "required" => "TarifasEnviosZonas.validation.importe_fijo.required",
        ],
    ];

    public function findAllWithTarifasEnvios(string $selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.id AS tarifa_envio FROM " .
            $this->table .
            " t1  LEFT JOIN lg_tarifas_envios t2 ON t1.tarifa_envio_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;
    }

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "", $tarifa_envio_id=-1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id as id, t1.nombre AS nombre, t1.cp_inicial AS cp_inicial, t1.cp_final AS cp_final, t1.importe_fijo AS importe_fijo, t2.id AS tarifa_envio"
            );
        $builder->join("lg_tarifas_envios t2", "t1.tarifa_envio_id = t2.id", "left");
        $builder->where("t1.tarifa_envio_id", $tarifa_envio_id);

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

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.nombre", $search)
                ->orLike("t1.cp_inicial", $search)
                ->orLike("t1.cp_final", $search)
                ->orLike("t1.importe_fijo", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.cp_inicial", $search)
                ->orLike("t1.cp_final", $search)
                ->orLike("t1.importe_fijo", $search)
                ->groupEnd();
    }

    public function removeAllZonasLineas($tarifa_envio_id = -1, $datetime = null, $delete_flag=1){
        
        $builder = $this->db
            ->table($this->table)
            ->set(['deleted_at' =>  $datetime->format('Y-m-d H:i:s'),
                    'is_deleted' => $delete_flag])
            ->where('tarifa_envio_id',$tarifa_envio_id)
            ->update();

        return $builder;
    }
}
