<?php

namespace App\Http\Controllers;

use App\Models\Nps;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;


class SearchController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = User::find(Auth::user()['id']);
        $pesquisa = $request['search'];

        if(empty($user->getRoleNames()[0]) || $user->getRoleNames()[0] == "Todas as unidades"){
            $notasPs = Nps::select('nps.nps', 'nps.id', 'nps.alimentador', 'nps.chave' , 'nps.circuito', 'nps.quemabre', 'nps.status', 'nps.finalidade','nps.prazo_execucao', 'nps.cliente', 'nps.logradouro', 'nps.bairro', 'nps.municipio', 'solicitacao_desligamentos.id AS id_desl', 'solicitacao_desligamentos.data', 'solicitacao_desligamentos.inicio', 'solicitacao_desligamentos.termino', 'solicitacao_desligamentos.encarregado')
                        ->leftJoin('solicitacao_desligamentos', 'id_nps', '=', 'nps.id')
                        ->where('cliente', 'like', "$pesquisa%")
                        ->orWhere('nps', 'like', "$pesquisa%")
                        ->orderBy('nps.prazo_execucao', 'asc')
                        ->orderBy('solicitacao_desligamentos.data', 'asc')
                        ->orderBy('solicitacao_desligamentos.inicio', 'asc')
                        ->get();
        }else{
            $notasPs = Nps::select('nps.nps', 'nps.id', 'nps.alimentador', 'nps.chave', 'nps.circuito', 'nps.quemabre', 'nps.status', 'nps.finalidade', 'nps.prazo_execucao', 'nps.cliente', 'nps.logradouro', 'nps.bairro', 'nps.municipio', 'solicitacao_desligamentos.id AS id_desl', 'solicitacao_desligamentos.data', 'solicitacao_desligamentos.inicio', 'solicitacao_desligamentos.termino', 'solicitacao_desligamentos.encarregado')
                        ->leftJoin('solicitacao_desligamentos', 'id_nps', '=', 'nps.id')
                        ->where('nps.cidade', '=', $user->getRoleNames()[0])
                        ->where('cliente', 'like', "$pesquisa%")
                        ->orWhere('nps', 'like', "$pesquisa%")
                        ->orderBy('nps.prazo_execucao', 'asc')
                        ->orderBy('solicitacao_desligamentos.data', 'asc')
                        ->orderBy('solicitacao_desligamentos.inicio', 'asc')
                        ->get();
        }

        
        if(count($notasPs) > 1){
            foreach($notasPs as $key => $nps){
                $desligamento = Nps::find($nps->id);
                $desligamento = $desligamento->solicitacao_desligamento()->latest()->first();
                
                if(!empty($desligamento['data']) && $nps['data'] != $desligamento['data'])
                {
                    unset($notasPs[$key]);
                }   
                
                if(!empty($desligamento['id']) && $nps['id_desl'] != $desligamento['id'])
                {
                    unset($notasPs[$key]);
                }

            }
        }

        $nps = $notasPs;

        return view('search.index', compact('nps', 'pesquisa'));
    }

    /**
     * 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)
    {
        //
    }

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

    /**
     * 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)
    {
        //
    }
}
