<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use App\Http\Requests\add_apply_request;
use App\Http\Requests\add_city_request;
use App\Http\Requests\add_category_request;
use App\Models\Job;
use App\Models\Apply;
use App\Models\City;
use App\Models\User;
use App\Models\Category;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    
    public function admin()
    {
        $categories = Category::paginate(3, ['*'], 'category');
        $cities = City::paginate(3, ['*'], 'city');
        $users = User::all();
        $jobs = Job::all();
        $apply = Apply::all();
        return view('admin', ['categories' => $categories, 'cities' => $cities, 'users' => $users, 'jobs' => $jobs, 'apply' => $apply]);
    }

    public function editcategory(Category $category, Request $request)
    {
        $category->name = $request->categoryname;
        $category->save();
        return redirect()->route('admin');  
    }

    public function applys($job)
    {
        $apply = Apply::where('job_id', $job)->get();

        return view('applys', ['apply' => $apply]);    
    }

    public function addcategory(add_category_request $request)
    {
        $category = new Category();
        $category->name = $request->categoryname;
        $category->save();
        return redirect()->route('admin');   
    }

    public function edit_category(Category $category)
    {
        return view('adminedit', ['category' => $category]); 
    }
    
    public function addapply(Job $job, add_apply_request $request)
    {
        $ap = new Apply();
        $ap->user_id = Auth::id();
        $ap->job_id = $job->id;
        $ap->website = $request->website;
        $ap->comment = $request->comment;
        $ap->save();

        return redirect()->route('index');
    }

    public function addcity(add_city_request $request)
    {
        $city = new City();
        $city->name = $request->cityname;
        $city->save();
        return redirect()->route('admin'); 
    }

    public function edit_city(City $city)
    {
        return view('adminedit', ['city' => $city]); 
    }

    public function editcity(City $city, Request $request)
    {
        $city->name = $request->cityname;
        $city->save();
        return redirect()->route('admin');  
    }
}
