<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Custom\SQLConnection;  
use App\Custom\Misc;
use Session;
use DB;

class TelefonosController extends Controller{    
    
    //FUNCION QUE DEVUELVE LA VISTA DE BUSQUEDA
    public static function buscarTelefono(){
        return view("/abm-telefonos/telefonos");
    }

    //FUNCION QUE LLAMA A TABLATELEFONOS PARA ARMAR LA TABLA
    public function mostarTablaPersona(){        
        $id_padron = request()->input('id_padron');        
        
        if ($id_padron != NULL) {                        
            $tabla = self::tablaTelefonos($id_padron);                        
            return $tabla;                                    
        }else{            
            return 'El cuit es invalido o no fue recibido';
        }
    }

    //FUNCION QUE TRAE LOS DATOS PARA ARMAR LA TABLA    
    public static function tablaTelefonos($id_padron){
        $data = [];

        if( $id_padron != NULL ){
            try {
                $data = SQLConnection::backendStoreProcedure('abm_telefonos', array('SELECCIONAR',NULL, $id_padron), 'select');                                
            } catch (\Throwable $th) {
                return 'error_datos_tabla';
            }            
        }
        
        $tabla = \View::make('/abm-telefonos/tabla_telefonos')->with(compact(['data'],'id_padron'))->render();
		
        return $tabla;
    }

    //FUNCION QUE DA DE ALTA UN REGISTRO
    public function altaRegistro(){        
        if( isset($_POST['id_padron']) && (isset($_POST['tipoTelefono']) && $_POST['tipoTelefono'] != 'Seleccione una opcion...') && isset($_POST['numeroTelefono']) ){
            
            $id_padron = request()->input('id_padron');
            $numero_telefono = request()->input('numeroTelefono');
            $tipo_telefono = request()->input('tipoTelefono');

            SQLConnection::backendStoreProcedure('abm_telefonos', array('ALTA',NULL, $id_padron,$numero_telefono, $tipo_telefono, 1), 'select');

            $tabla = self::tablaTelefonos($id_padron);
            
            return $tabla;            
        }else{
            return 'error_alta';
        }      
    }

    //FUNCION QUE EDITA UN REGISTRO
    public function editarRegistro(){                    
        if( isset($_POST['id_telefono_edit']) && isset($_POST['id_padron_edit']) && (isset($_POST['tipoTelefonoEdit']) && $_POST['tipoTelefonoEdit'] != 'Seleccione una opcion...') && isset($_POST['numeroTelefonoEdit']) ){
            
            $id_padron = request()->input('id_padron_edit');
            $id_telefono = request()->input('id_telefono_edit');            
            $numero_telefono = request()->input('numeroTelefonoEdit');
            $tipo_telefono = request()->input('tipoTelefonoEdit');
            $estado = request()->input('registro_estado');
            
            SQLConnection::backendStoreProcedure('abm_telefonos', array('EDITAR',$id_telefono, NULL,$numero_telefono, $tipo_telefono, $estado), 'select');            

            $tabla = self::tablaTelefonos($id_padron);
            
            return $tabla;
        }else{
            return 'error_editar';
        }
    }

    //FUNCION QUE CAMBIA EL ESTADO DE UN REGISTRO
    public function estadoRegistro(){                  
        if (isset($_POST['id_telefono']) && isset($_POST['activo']) && isset($_POST['id_padron']) ) {
            
            $id_telefono = request()->input('id_telefono');
            $estado = request()->input('activo');
            $id_padron = request()->input('id_padron');

            SQLConnection::backendStoreProcedure('abm_telefonos', array('ESTADO',$id_telefono, $estado), 'select');
            
            $tabla = self::tablaTelefonos($id_padron);
            
            return $tabla;
        }else{
            return 'error_estado';
        }
    } 
}