<?php

namespace App\Models\Clientes;

class ClienteDireccionesModel extends \App\Models\BaseModel
{
    protected $table = "cliente_direcciones";

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

    const SORTABLE = [
        0 => "t1.alias",
        1 => "t1.att",
        2 => "t1.email",
        3 => "t1.direccion",
        4 => "t1.cp",
        5 => "t1.municipio",
        6 => "t1.provincia",
        7 => "t4.nombre",
        8 => "t3.nombre",
        9 => "t1.telefono",
    ];

    protected $allowedFields = [
        "alias",
        "cliente_id", 
        "att", 
        "email", 
        "direccion", 
        "pais_id", 
        "provincia",
        "municipio", 
        "cp", 
        "telefono",
    ];

    protected $returnType = "App\Entities\Clientes\ClienteDireccionesEntity";

    public static $labelField = "id";

    protected $validationRules = [
        "att" => [
            "label" => "ClienteDirecciones.att",
            "rules" => "trim|max_length[100]",
        ],
        "alias" => [
            "label" => "ClienteDirecciones.att",
            "rules" => "trim|max_length[100]",
        ],
        "email" => [
            "label" => "ClienteDirecciones.email",
            "rules" => "trim|max_length[100]",
        ],
        "cp" => [
            "label" => "ClienteDirecciones.cp",
            "rules" => "trim|max_length[20]",
        ],
        "telefono" => [
            "label" => "ClienteDirecciones.telefono",
            "rules" => "trim|max_length[40]",
        ],
    ];

    protected $validationMessages = [
        "att" => [
            "max_length" => "ClienteDirecciones.validation.max_length",
        ],
        "alias" => [
            "max_length" => "ClienteDirecciones.validation.max_length",
        ],
        "email" => [
            "max_length" => "ClienteDirecciones.validation.max_length",
        ],
        "cp" => [
            "max_length" => "ClienteDirecciones.validation.max_length",
            "valid_email" => "ClienteDirecciones.validation.valid_email",
        ],
        "telefono" => [
            "max_length" => "ClienteDirecciones.validation.max_length",
        ],
    ];

    
    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "", $cliente_id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.cliente_id AS cliente_id, t2.nombre as cliente_nombre, t1.att AS att, t1.alias AS alias,
                t1.email AS email, t1.direccion AS direccion, t1.pais_id AS pais_id, t3.nombre AS pais, t3.nombre AS pais, 
                t1.municipio AS municipio, t1.provincia AS provincia, t1.cp AS cp, t1.telefono AS telefono"
            );

        $builder->where('t1.cliente_id', $cliente_id);
        $builder->join("clientes t2", "t1.cliente_id = t2.id", "left");
        $builder->join("lg_paises t3", "t1.pais_id = t3.id", "left");
        

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.att", $search)
                ->orLike("t1.email", $search)
                ->orLike("t1.direccion", $search)
                ->orLike("t3.nombre", $search)
                ->orLike("t4.nombre", $search)
                ->orLike("t1.municipio", $search)
                ->orLike("t1.provincia", $search)
                ->orLike("t1.cp", $search)
                ->orLike("t1.telefono", $search)
                ->groupEnd();
    }

    public function getDireccion($id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.cliente_id AS cliente_id, t2.nombre as cliente_nombre, t1.att AS att, t1.alias AS alias,
                t1.email AS email, t1.direccion AS direccion, t1.pais_id AS pais_id, t3.nombre AS pais, 
                t1.municipio AS municipio, t1.provincia AS provincia, t1.cp AS cp, t1.telefono AS telefono"
            );

        $builder->where('t1.id', $id);
        $builder->join("clientes t2", "t1.cliente_id = t2.id", "left");
        $builder->join("lg_paises t3", "t1.pais_id = t3.id", "left");
        

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

    public function getIdForPresupuestoCliente($cliente_id = -1, $att = "", $email = "", $direccion = "", $cp = "", $pais_id = -1, $telefono = "")
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select("t1.id AS id")
            ->where("t1.cliente_id", $cliente_id)
            ->where("t1.att", $att)
            ->where("t1.email", $email)
            ->where("t1.direccion", $direccion)
            ->where("t1.cp", $cp)
            ->where("t1.pais_id", $pais_id)
            ->where("t1.telefono", $telefono)
            ->limit(1);


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

    public function getMenuDirecciones($cliente_id=-1){
        
        $builder = 
            $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.alias AS text")
            ->where("t1.cliente_id", $cliente_id);

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

    }

    public function nuevaDireccion($data)
    {
        $builder = $this->db->table($this->table);
        $builder->insert($data);
        return $this->db->insertID();
    }
}
