<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Auth;
use App\Models\Nps;
use Dompdf\Dompdf;
use Dompdf\Options;

class ComunicadoInicioObraController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $dia = $request['date'];
        $nps = Nps::find($request['id']);
        $id_user = Auth::user()['id'];

        $nps->comunicado_inicio_obra()->create([
            'data' => $dia,
            'id_nps' => $nps->id,
            'id_user' => $id_user
        ]);

        $nps->historico()->create([
            'id_nps' => $nps->id,
            'id_user' => $id_user,
            'descricao' => 'Emitiu o Comunicado de início da obra para o dia ' . date('d/m/Y', strToTime($dia))
      
        ]);

        $request->session()->flash('success', 'Comunicado de inicio de obra gerado com sucesso');

        return redirect()->route('nps.show',['np' => $nps->id]);
    }

    /** 
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $nps = Nps::find($id);
        $dia = $nps['data'];

        $options = new Options();
        $options->set('isRemoteEnabled', TRUE);

        $pdf = new Dompdf($options);

        $pdf->loadHtml(view('pdfs.inicio_obra', compact(['nps','dia'])));
        $pdf->setPaper('A4', 'potrait');
        $pdf->setBasePath(public_path()); // This line resolve
        $pdf->render();
        ob_get_clean();
        $pdf->stream("", array("Attachment" => false));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
