<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Apply;
use Illuminate\Support\Facades\Auth;
use App\Models\Category;
use App\Http\Requests\create_category_request;

class AdminController extends Controller
{

    public function adminpanel()
    {
        $user = Auth::user();
        $aps = Apply::all();
        return view('ap', ['aps' => $aps, 'user' => $user]);
    }

    public function addcategory(create_category_request $request) 
    {
        if (Auth::user()->role !== 'admin') {
            return redirect('index');
        }
        $category = new Category();
        $category->name = $request->name;
        $category->save();
        return redirect()->route('account');
    }


    public function addcategoryAPI(create_category_request $request) 
    {
        if (Auth::user()->role !== 'admin') {
            return redirect('index');
        }
        $category = new Category();
        $category->name = $request->name;
        $category->save();
        return ['success' => 'Категория создана']; 
    }

    public function status($id, Request $request)
    {
        $apply = Apply::where('id', $id)->first();
        if($request->route()->getName() == 'false'){
            $apply->status = "Услуга отменена";
        }
        else{
            $apply->status = "Услуга оказана";
        }
        $apply->save();
        return redirect()->route('account');
    }

    public function applytrue($id, Request $request)
    {
        $apply = Apply::where('id', $id)->first();
        $apply->status = "Услуга оказана";
        $apply->save();
        return ['success' => 'Заявка принята']; 
    }

    public function applyfalse($id, Request $request)
    {
        $apply = Apply::where('id', $id)->first();
        $apply->status = "Услуга отменена";
        $apply->save();
        return ['success' => 'Заявка отклонена']; 
    }
}
