<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests;

use App\Models\Setor;
use Illuminate\Http\Request;

class SetorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\View\View
     */
    public function index(Request $request)
    {
        $keyword = $request->get('search');
        $perPage = 25;

        if (!empty($keyword)) {
            $setor = Setor::where('nome', 'LIKE', "%$keyword%")
                ->orWhere('sigla', 'LIKE', "%$keyword%")
                ->orWhere('idSetorPai', 'LIKE', "%$keyword%")
                ->latest()->paginate($perPage);
        } else {
            $setor = Setor::latest()->paginate($perPage);
        }

        return view('setor.setor.index', compact('setor'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('setor.setor.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        $this->validate($request, [
			'nome' => 'required',
			'sigla' => 'required'
		]);
        $requestData = $request->all();

        Setor::create($requestData);

        return redirect('setor/setor')->with('flash_message', 'Setor added!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        $setor = Setor::findOrFail($id);

        return view('setor.setor.show', compact('setor'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function edit($id)
    {
        $setor = Setor::findOrFail($id);

        return view('setor.setor.edit', compact('setor'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
			'nome' => 'required',
			'sigla' => 'required'
		]);
        $requestData = $request->all();

        $setor = Setor::findOrFail($id);
        $setor->update($requestData);

        return redirect('setor/setor')->with('flash_message', 'Setor updated!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy($id)
    {
        Setor::destroy($id);

        return redirect('setor/setor')->with('flash_message', 'Setor deleted!');
    }

    //Este método PRECISA das aspas simples nos índices
    public function imprimeArvoreWrapper(){
        $arraySetores = array();
        $setor = Setor::findOrFail(1);
        $arraySetores = array_merge($arraySetores, ["'1'" =>  $setor->sigla]);
        return $this->imprimeArvore($arraySetores, $setor->sigla, $setor->id);
    }

    //Este método PRECISA das aspas simples nos índices
    public function imprimeArvore($arraySetores, $nomeSetorPai, $idSetorPai){
        $setores = Setor::where("idSetorPai", "=",$idSetorPai)->where('id','<>',1)->get();

        if(sizeof($setores) > 0){
            foreach($setores as $setor){
                $arraySetores = array_merge($arraySetores, ["'".$setor->id."'" => $nomeSetorPai."/".$setor->sigla]);
                $arraySetores = array_merge($arraySetores, $this->imprimeArvore(array(), $nomeSetorPai."/".$setor->sigla, $setor->id));
            }
            return $arraySetores;
        }else{
            return [];
        }
    }
}
