<?php
namespace App\Models\Clientes;

class ClienteDistribuidorModel extends \App\Models\GoBaseModel
{
    protected $table = "cliente_distribuidores";

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

    const SORTABLE = [
        1 => "t1.id",
        2 => "t1.nombre",
        3 => "t1.cif",
        4 => "t1.direccion",
        5 => "t1.ciudad",
        6 => "t1.cp",
        7 => "t1.email",
        8 => "t1.pais_id",
        9 => "t1.provincia_id",
        10 => "t1.comunidad_autonoma_id",
        11 => "t1.telefono",
        12 => "t1.horarios_entrega",
        13 => "t1.persona_contacto",
        14 => "t1.precio",
        15 => "t2.nombre",
        16 => "t3.nombre",
        17 => "t4.nombre",
    ];

    protected $allowedFields = [
        "nombre",
        "cif",
        "direccion",
        "ciudad",
        "cp",
        "email",
        "pais_id",
        "provincia_id",
        "comunidad_autonoma_id",
        "telefono",
        "horarios_entrega",
        "persona_contacto",
        "precio",
    ];
    protected $returnType = "App\Entities\Clientes\ClienteDistribuidorEntity";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "nombre";

    protected $validationRules = [
        "cif" => [
            "label" => "ClienteDistribuidores.cif",
            "rules" => "trim|max_length[50]",
        ],
        "ciudad" => [
            "label" => "ClienteDistribuidores.ciudad",
            "rules" => "trim|max_length[100]",
        ],
        "cp" => [
            "label" => "ClienteDistribuidores.cp",
            "rules" => "trim|max_length[10]",
        ],
        "direccion" => [
            "label" => "ClienteDistribuidores.direccion",
            "rules" => "trim|max_length[300]",
        ],
        "email" => [
            "label" => "ClienteDistribuidores.email",
            "rules" => "trim|max_length[150]|valid_email|permit_empty",
        ],
        "horarios_entrega" => [
            "label" => "ClienteDistribuidores.horariosEntrega",
            "rules" => "trim|max_length[300]",
        ],
        "nombre" => [
            "label" => "ClienteDistribuidores.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "persona_contacto" => [
            "label" => "ClienteDistribuidores.personaContacto",
            "rules" => "trim|max_length[100]",
        ],
        "precio" => [
            "label" => "ClienteDistribuidores.precio",
            "rules" => "decimal|permit_empty",
        ],
        "telefono" => [
            "label" => "ClienteDistribuidores.telefono",
            "rules" => "trim|max_length[60]",
        ],
    ];

    protected $validationMessages = [
        "cif" => [
            "max_length" => "ClienteDistribuidores.validation.cif.max_length",
        ],
        "ciudad" => [
            "max_length" => "ClienteDistribuidores.validation.ciudad.max_length",
        ],
        "cp" => [
            "max_length" => "ClienteDistribuidores.validation.cp.max_length",
        ],
        "direccion" => [
            "max_length" => "ClienteDistribuidores.validation.direccion.max_length",
        ],
        "email" => [
            "max_length" => "ClienteDistribuidores.validation.email.max_length",
            "valid_email" => "ClienteDistribuidores.validation.email.valid_email",
        ],
        "horarios_entrega" => [
            "max_length" => "ClienteDistribuidores.validation.horarios_entrega.max_length",
        ],
        "nombre" => [
            "max_length" => "ClienteDistribuidores.validation.nombre.max_length",
            "required" => "ClienteDistribuidores.validation.nombre.required",
        ],
        "persona_contacto" => [
            "max_length" => "ClienteDistribuidores.validation.persona_contacto.max_length",
        ],
        "precio" => [
            "decimal" => "ClienteDistribuidores.validation.precio.decimal",
        ],
        "telefono" => [
            "max_length" => "ClienteDistribuidores.validation.telefono.max_length",
        ],
    ];
    public function findAllWithAllRelations(string $selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.nombre AS pais,  t3.nombre AS provincia,  t4.nombre AS comunidad_autonoma FROM " .
            $this->table .
            " t1  LEFT JOIN lg_paises t2 ON t1.pais_id = t2.id LEFT JOIN lg_provincias t3 ON t1.provincia_id = t3.id LEFT JOIN lg_comunidades_autonomas t4 ON t1.comunidad_autonoma_id = t4.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.nombre AS nombre, t1.cif AS cif, t1.direccion AS direccion, t1.ciudad AS ciudad, t1.cp AS cp, t1.email AS email, t1.telefono AS telefono, t1.horarios_entrega AS horarios_entrega, t1.persona_contacto AS persona_contacto, t1.precio AS precio, t2.nombre AS pais, t3.nombre AS provincia, t4.nombre AS comunidad_autonoma"
            );
        $builder->join("lg_paises t2", "t1.pais_id = t2.id", "left");
        $builder->join("lg_provincias t3", "t1.provincia_id = t3.id", "left");
        $builder->join("lg_comunidades_autonomas t4", "t1.comunidad_autonoma_id = t4.id", "left");

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.cif", $search)
                ->orLike("t1.direccion", $search)
                ->orLike("t1.ciudad", $search)
                ->orLike("t1.cp", $search)
                ->orLike("t1.email", $search)
                ->orLike("t1.telefono", $search)
                ->orLike("t1.horarios_entrega", $search)
                ->orLike("t1.persona_contacto", $search)
                ->orLike("t1.precio", $search)
                ->orLike("t2.id", $search)
                ->orLike("t3.id", $search)
                ->orLike("t4.id", $search)
                ->orLike("t1.id", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.cif", $search)
                ->orLike("t1.direccion", $search)
                ->orLike("t1.ciudad", $search)
                ->orLike("t1.cp", $search)
                ->orLike("t1.email", $search)
                ->orLike("t1.pais_id", $search)
                ->orLike("t1.provincia_id", $search)
                ->orLike("t1.comunidad_autonoma_id", $search)
                ->orLike("t1.telefono", $search)
                ->orLike("t1.horarios_entrega", $search)
                ->orLike("t1.persona_contacto", $search)
                ->orLike("t1.precio", $search)
                ->orLike("t2.nombre", $search)
                ->orLike("t3.nombre", $search)
                ->orLike("t4.nombre", $search)
                ->groupEnd();
    }
}
