<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Statement;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;

class StatementController extends Controller
{

    public function addstate(Request $request)
    {
        $state = Statement::create([
            'user_id' => Auth::id(),
            'RegistrNumber' => $request->RegistrNumber,
            'comment' => $request->comment,
        ]);
        
        return redirect(route('homepage'));
    }

    public function states(){
        $mystates = Statement::where('user_id', Auth::id())->get();
        $pstates = Statement::where('status', 'Рассматривается')->get();
        $astates = Statement::where('status', 'Принято')->get();
        $dstates = Statement::where('status', 'Отклонено')->get();
        return view('homepage', ['mystates' => $mystates,'pstates' => $pstates, 'astates' => $astates, 'dstates' => $dstates]);
    }

    public function statestatus($id, Request $request)
    {
        $state = Statement::where('id', $id)->first();
        if(Auth::user() && Auth::user()->role == 'admin'){
            if($request->route()->getName() == 'false'){
                $state->status = "Отклонено";
            }
            else{
                $state->status = "Принято";
            }
            $state->save();
        }
        return redirect()->route('homepage');
    }
}
