<?php
namespace App\Models\Compras;

class ProveedorModel extends \App\Models\BaseModel
{
    protected $table = "lg_proveedores";

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

    const SORTABLE = [
        0 => "t1.nombre",
        1 => "t2.nombre",
    ];

    protected $allowedFields = [
        "nombre",
        "tipo_id",
        "razon_social",
        "cif",
        "direccion",
        "cp",
        "ciudad",
        "provincia_id",
        "pais_id",
        "persona_contacto",
        "email",
        "telefono",
        "propiedades",
        "is_deleted",
        "deleted_at"
    ];
    protected $returnType = "App\Entities\Compras\ProveedorEntity";

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "nombre";

    protected $validationRules = [
        "cif" => [
            "label" => "Proveedores.cif",
            "rules" => "trim|max_length[15]",
        ],
        "tipo_id" => [
            "label" => "Proveedores.tipoId",
            "rules" => "required",
        ],
        "ciudad" => [
            "label" => "Proveedores.ciudad",
            "rules" => "trim|max_length[255]",
        ],
        "cp" => [
            "label" => "Proveedores.cp",
            "rules" => "trim|max_length[10]",
        ],
        "direccion" => [
            "label" => "Proveedores.direccion",
            "rules" => "trim|max_length[255]",
        ],
        "email" => [
            "label" => "Proveedores.email",
            "rules" => "trim|max_length[255]|valid_email|permit_empty",
        ],
        "nombre" => [
            "label" => "Proveedores.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "persona_contacto" => [
            "label" => "Proveedores.personaContacto",
            "rules" => "trim|max_length[255]",
        ],
        "razon_social" => [
            "label" => "Proveedores.razonSocial",
            "rules" => "trim|max_length[255]",
        ],
        "telefono" => [
            "label" => "Proveedores.telefono",
            "rules" => "trim|max_length[60]",
        ],
    ];

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

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

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

    public function getProvList(int $tipoId = -1){

        $builder = $this->db
        ->table($this->table . " t1")
        ->select(
            "t1.id AS value, t1.nombre AS label, t1.propiedades AS options")
        ->where("tipo_id", $tipoId)
        ->where("is_deleted", 0)
        ->orderBy('t1.nombre', 'asc');

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