<?php

namespace App\Http\Controllers;

use App\Models\Agenda;
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 SolicitacaoDeBloqueioController 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)
    {
        $nps = Nps::find($request['id']);

        $id_user = ['id_user' => Auth::user()['id']];
        $id_nps = ['id_nps' => $nps->id ];

        $data = $request->all();
        $data = array_merge($data, $id_user);
        $data = array_merge($data, $id_nps);

        if($request['encarregado'] != $request['substituto']){
            $nps->solicitacao_bloqueio()->create($data);

            $nps->historico()->create([
                'id_nps' => $nps->id,
                'id_user' => $id_user['id_user'],
                'descricao' => 'Emitiu uma Solicitação de bloqueio para o dia ' . date("d/m/Y", strToTime($data["data"])) . ', com horario de inicio as ' . $data["inicio"] . ' e termino as ' . $data["termino"]
        
            ]);

            $this->agenda($nps->id, $request->all());

            $request->session()->flash('success', 'Solicitação de bloqueio gerado com sucesso');

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

        }else{
            $request->session()->flash('warning', 'Para gerar a solicitação de bloqueio, o encarregado e substituto não podem ser o mesmo');
            return redirect()->back();
        }      
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        $nps = Nps::find($id);
        $bloqueio = $nps->solicitacao_bloqueio()->latest()->first();

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

        $pdf = new Dompdf($options);

        $pdf->loadHtml(view('pdfs.solicitacao_bloqueio', compact(['nps','bloqueio'])));
        $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)
    {
        //
    }

    private function agenda($nps, $request){
        if(
            Agenda::where('nps_id', $nps)->where('servico', 'Bloqueio')->count() == 0 
            && Agenda::where('nps_id', $nps)->where('servico', 'Desligamento')->count() == 0
        ){

            Agenda::create([
                'data' => $request['data'],
                'inicio' => $request['inicio'],
                'termino' => $request['termino'],
                'nps_id' => $nps,
                'servico' => 'Bloqueio',
                'encarregado' => $request['encarregado'],
                'substituto' => $request['substituto'],
            ]);

        }else if(
            Agenda::where('nps_id', $nps)->where('servico', 'Bloqueio')->count() == 1 
            && Agenda::where('nps_id', $nps)->where('servico', 'Desligamento')->count() == 0
        ){

            $agendamento = Agenda::where('nps_id', $nps);
            $agendamento->update([
                'data' => $request['data'],
                'inicio' => $request['inicio'],
                'termino' => $request['termino'],
                'nps_id' => $nps,
                'servico' => 'Bloqueio',
                'encarregado' => $request['encarregado'],
                'substituto' => $request['substituto']
            ]);

        }  
        
    }
}
