<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Sector;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class SectorController extends Controller
{
    public function __construct()
    {
        $this->middleware(['permission:add_sector'])->only(['create', 'store']);
        $this->middleware(['permission:edit_sector'])->only(['edit', 'update']);
        $this->middleware(['permission:view_sector'])->only(['index']);
        $this->middleware(['permission:delete_sector'])->only(['delete']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.sectors.index', ['sectors' => Sector::all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.sectors.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), ['name' => 'required']);
        if($validator->fails()){
            toastr()->error(trans('sectors.errors'));

            return redirect()->back()->withErrors($validator);
        }

        Sector::create($request->all());

        activity()->log('قام '.auth()->user()->name.'باضافة قطاع جديد '.$request->name);

        toastr()->success(trans('sectors.add_success'));

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

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Sector $sector)
    {
        return view('admin.sectors.edit', ['sector' => $sector]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Sector $sector)
    {
        $validator = Validator::make($request->all(), ['name' => 'required']);
        if($validator->fails()){
            toastr()->error(trans('sectors.errors'));

            return redirect()->back()->withErrors($validator);
        }


        $sector->update($request->all());

        activity()->log('قام '.auth()->user()->name.'بالتعديل على القطاع '.$sector->name);

        toastr()->success(trans('sectors.updated_success'));

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Sector $sector)
    {
        $sector->delete();

        activity()->log('قام '.auth()->user()->name.'بحذف القطاع '.$sector->name);

        toastr()->success(trans('sectors.deleted_success'));

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