<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use App\Http\Controllers\AnalitoEP15Controller;
use App\Http\Controllers\ResultadoEP15Controller;
use App\Libraries\fpdf\EstructuraPDFEP15;

class PDFEP15Controller extends Controller {

    private $carpetaTemp;
    public $urlBases64;
    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();
        $this->urlBases64 = array();
    }


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


    public function setNomsImages(){
        foreach($this->req->bases64 as $base64){
            $this->urlBases64[$base64["id"]] = $this->getUrlImg();
        }
    }


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


    public function saveImages(){
        foreach($this->req->bases64 as $base64){
            file_put_contents(
                $this->urlBases64[$base64["id"]],
                $this->procesarImg($base64["val"])
            );
        }
    }


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


    public function generarEstructuraPDF(){
        $info_analito = AnalitoEP15Controller::listItem($this->req->id);
        $instResultadoEP15 = new ResultadoEP15Controller();
        $obj_json = $instResultadoEP15->valoresEstadisticosEP15($this->req->id);

        $pdf = new EstructuraPDFEP15('P','mm','letter');
        $pdf->CreateDocument($info_analito, $obj_json, $this);
        $pdf->Close();
        $pdf->Output("F", $this->carpetaTemp . $this->idReporte . ".pdf");
    }


    public function deleteTempImages(){
        foreach($this->req->bases64 as $base64){
            unlink($this->urlBases64[$base64["id"]]);
        }
    }


    public function generarInformeFinal(Request $req) {
        $this->setAttribute("req", $req);
        $this->setNomsImages();
        $this->saveImages();
        $this->generarEstructuraPDF();
        $this->deleteTempImages();
        return $this->idReporte;
    }


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

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


    /*
    public function retornoVistaEdicion(Request $req){

        $info_analito = AnalitoEP15Controller::listItem($req->id);
        $resultados = ResultadoEP15Controller::listByAnalitoCorrida($req->id);

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