<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use App\Http\Controllers\CorridaAnalitoController;
use App\Http\Controllers\ResultadoCorridaController;
use App\Libraries\fpdf\EstructuraPDF;

class PDFController extends Controller{

    private $carpetaTemp;
    public $urlImgB1;
    public $urlImgB2;
    public $urlImgB3;
    public $urlImgB4;
    private $req;
    private $idReporte;

    public function __construct() {
        $this->middleware('auth');
        $this->carpetaTemp = '/home/quikappspane/public_html/v2m-v2/public/reports_temp/'; // Externo
        // $this->carpetaTemp = 'c://xamppNuevo/htdocs/v2m/public/reports_temp/'; // Local
        $this->idReporte = uniqid();
    }

    public function setAttribute($atributo, $valor){
        $this->$atributo = $valor;
    }

    public function saveImages(){
        file_put_contents($this->urlImgB1, $this->procesarImg($this->req->base64_grafico_B1));
        file_put_contents($this->urlImgB2, $this->procesarImg($this->req->base64_grafico_B2));
        file_put_contents($this->urlImgB3, $this->procesarImg($this->req->base64_grafico_B3));
        file_put_contents($this->urlImgB4, $this->procesarImg($this->req->base64_grafico_B4));
    }

    public function generarInformeFinal(Request $req) {

        $this->setAttribute("req", $req);
        $this->setNomsImages();
        $this->saveImages();
        $this->generarEstructuraPDF();
        $this->deleteTempImages();

        return $this->idReporte;
    }

    public function generarEstructuraPDF(){

        $info_analito = CorridaAnalitoController::listItem($this->req->id);
        $resultados = ResultadoCorridaController::listByAnalitoCorrida($this->req->id);
        $promedios = ResultadoCorridaController::listPromediosByAnalitoCorrida($resultados);
        $resultadosC5 = ResultadoCorridaController::listC5ByAnalitoCorrida($this->req->id, $promedios->formula_b, $promedios->intercepcion_a);
        $promediosC5 = ResultadoCorridaController::listPromediosC5ByAnalitoCorrida($resultadosC5, $promedios);

        $pdf = new EstructuraPDF('P','mm','letter');
        $pdf->CreateDocument($info_analito,$resultados,$promedios,$resultadosC5,$promediosC5, $this);
        $pdf->Close();
        $pdf->Output("F", $this->carpetaTemp . $this->idReporte . ".pdf");
    }


    public function seeReport(Request $req){
        return view("reporte.contenedorReporteFinal")
            ->with("id_reporte", $req->referencia);
    }

    public function eliminarInforme(Request $req){
        unlink($this->carpetaTemp . $req->id . ".pdf");
    }

    public function setNomsImages(){
        $this->urlImgB1 = $this->getUrlImg();
        $this->urlImgB2 = $this->getUrlImg();
        $this->urlImgB3 = $this->getUrlImg();
        $this->urlImgB4 = $this->getUrlImg();
    }

    public function deleteTempImages(){
        unlink($this->urlImgB1);
        unlink($this->urlImgB2);
        unlink($this->urlImgB3);
        unlink($this->urlImgB4);
    }

    public function procesarImg($base64){
        $base_to_php = explode(',', $base64);
        $data = base64_decode($base_to_php[1]);
        return $data;
    }

    public function getUrlImg(){
        return $this->carpetaTemp . uniqid() . ".png";
    }


    public function retornoVistaEdicion(Request $req){

        $info_analito = CorridaAnalitoController::listItem($req->id);
        $resultados = ResultadoCorridaController::listByAnalitoCorrida($req->id);

        return view("reporte.reporteFinal")
        ->with("id", $req->id)
        ->with("analitoCorrida", $info_analito)
        ->with("resultados", $resultados);
    }
}
