<?php

namespace App\Models\Presupuestos;

class PresupuestoDireccionesModel extends \App\Models\BaseModel
{
    protected $table = "presupuesto_direcciones";

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

    const SORTABLE = [
        0 => "t1.cantidad",
        1 => "t1.att",
        2 => "t1.email",
        3 => "t1.direccion",
        4 => "t1.cp",
        5 => "t1.municipio",
        6 => "t1.provincia",
        7 => "t2.nombre",
        8 => "t1.telefono",
        9 => "t1.precio",
        10 => "t1.proveedor",
    ];

    protected $allowedFields = [
        "tarifa_id",
        "presupuesto_id",
        "cantidad",
        "peso",
        "att", 
        "email", 
        "direccion", 
        "pais_id", 
        "provincia", 
        "municipio", 
        "cp", 
        "telefono",
        'precio',
        'margen',
        "proveedor_id",
        "proveedor",
        "entregaPieCalle",
    ];

    protected $returnType = "App\Entities\Presupuestos\PresupuestoDireccionesEntity";

    public static $labelField = "id";

    
    
    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "", $presupuesto_id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.presupuesto_id AS presupuesto_id, t1.tarifa_id AS tarifa_id, t1.att AS att, 
                t1.email AS email, t1.direccion AS direccion, t1.pais_id AS pais_id, t2.nombre AS pais, 
                t1.municipio AS municipio, t1.provincia AS provincia, t1.cp AS cp, t1.telefono AS telefono,
                t1.peso AS peso, t1.cantidad AS cantidad, t1.precio AS precio, t1.margen AS margen, 
                t1.proveedor_id AS proveedor_id, t1.proveedor AS proveedor, t1.entregaPieCalle AS entregaPieCalle"
            );

        $builder->where('t1.presupuesto_id', $presupuesto_id);
        $builder->join("lg_paises t2", "t1.pais_id = t2.id", "left");
        $builder->orderBy('t1.id', 'DESC');
        
        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.att", $search)
                ->orLike("t1.email", $search)
                ->orLike("t1.direccion", $search)
                ->orLike("t2.nombre", $search)
                ->orLike("t1.municipio", $search)
                ->orLike("t1.provincia", $search)
                ->orLike("t1.cp", $search)
                ->orLike("t1.telefono", $search)
                ->groupEnd();
    }


    public function updateLineaEnvio($lineaEnvio){

        $lineaTemp = $lineaEnvio;
        unset($lineaTemp->pais);

        $this->save($lineaTemp);
    }


    /**
     * Get resource data for pdf generation.
     *
     * @param int $presupuesto_id
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResourceForPdf($presupuesto_id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.direccion AS direccion, t2.nombre AS pais, 
                t1.municipio AS municipio, t1.provincia AS provincia, t1.cp AS cp, t1.telefono AS telefono,
                t1.cantidad AS cantidad"
            );

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

        return $builder;
    }

    public function getDireccion($id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.att AS att, 
                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("lg_paises t3", "t1.pais_id = t3.id", "left");
        

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

}
