<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Country;
use App\Models\Region;
use Illuminate\Http\Request;

class RegionController extends Controller
{
    public function index()
    {
        $regions = Region::orderBy('id','desc')->get();

        return view('admin.region.index', compact('regions'));
    }

    public function create()
    {
        $countries = Country::all();

        return view('admin.region.create', compact('countries'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'country_id' => 'required',
        ]);
        Region::create($request->post());

        return redirect()->route('admin.regions.index')->with('success','region has been created successfully.');
    }

    public function edit(Region $region)
    {
        $countries = Country::all();

        return view('admin.region.edit',compact('region', 'countries'));
    }

    public function update(Request $request, Region $region)
    {
        $request->validate([
            'country_id' => 'required',
        ]);

        $region->fill($request->post())->save();

        return redirect()->route('admin.regions.index')->with('success','regions Has Been updated successfully');
    }

    public function destroy(Region $region)
    {
        $region->delete();
        return redirect()->route('admin.regions.index')->with('success','regions has been deleted successfully');
    }
}
