<?php

namespace App\Http\Controllers;

use App\Models\Protocolo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class AnonimoController extends Controller
{
    public function index(){
        return view('protocolo.createAnonimo');
    }


    public function storeAnonima(Request $request)
    {

        //Tamanho máximo de upload 20 mil kB (20MB)
        $this->validate($request, [
            'tipo' => 'required',
            'descricao' => 'required',
            'telefone' => 'required',
            'anexo' => 'image|mimes:jpeg,jpg,gif,bmp,png,tiff,pdf,svg|max:20000'
        ]);

        /*Validator::make($request, [
            'anexo' => [
                Rule::exists();
            ]
        ]);*/

        $uC = new UserController();
        $requestData = $request->all();
        $id = $uC->storeAnonimo($requestData);

        $requestData['codigo'] = uniqid(8);
        $requestData['idUser'] = $id;
        $requestData['status'] = "aberta";
        $requestData['idSetor'] = 1;

        if (isset($requestData['anexo'])) {
            $caminho = Storage::put('public\\protocolos\\' . $id, $request->file('anexo'));
            $requestData['anexo'] = $caminho;
        }

        $codigo = $requestData['codigo'];

        Protocolo::create($requestData);

        return view('solicitante.sucessoAnonimo', compact( 'codigo'));
    }


}
