<?php
namespace App\Http\Controllers;

use DB;
use App\Models\Prueba;
use App\EmpresaModel;
use App\PersonaModel;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use App\Traits\Responser;

class ClientsController extends Controller
{
    use Responser;

    public function show($ruc)
    {
        try{
            $datosEmp = [];
            $persona = PersonaModel::where('num_documento', '=', $ruc)->get();
            if( count($persona) > 0 ){
                $persona = $persona->first()->empresa;
                if( !is_null($persona) ){
                    $datosEmp['host_BD'] = $persona->db_host;
                    $datosEmp['BD_sistema'] = $persona->db_name;
                    $datosEmp['usuario_BD'] = $persona->db_usuario;
                    $datosEmp['password_BD'] = $persona->db_contrasena;
                    $datosEmp['subdominio'] =$persona->sub_dominio;
                    $datosEmp['flg_reconexion']=$persona->flg_reconexion; 
                    $datosEmp['estado']=$persona->estado;
                    return $this->successResponse($datosEmp, "Cliente econtrado correctamente.", 201);
                } else {
                    return $this->errorResponse("La persona no tiene una empresa asociada.", 201);
                }
            } else {
                return $this->errorResponse("Cliente no encontrado", 201);
            }
        }catch(\Exception $ex){
            return $this->errorResponse($ex->getMessage(),500);
        }
    }

    public function listarEmpresaPorSubdominio(Request $request){
        $this->validate($request,['subdominio'=>'required'],['El subdominio es requerido']);
        $data = $request->all();
        $empresa = new EmpresaModel();
        $res = $empresa->listarEmpresaPorSubdominio($data['subdominio']);
        //return json_encode($res[0]->id_persona);
        $oPersona = new PersonaModel;
        $persona = $oPersona->obtenerRuc($res->id_persona);
        return $this->successResponse($persona,"Cliente encontrado correctamente",201);
    }

    public function obtenerDeuda($ruc){
		$oDeuda = new PersonaModel;
		$oDeuda = $oDeuda->obtenerEstadoDeuda($ruc);
        if($oDeuda){
            return $this->successResponse($oDeuda,"Deuda Encontrada",201);
        }else{
            return $this->successResponse([],"No tiene deuda",201);
        }
		
	}
}