<?php

namespace App\Http\Controllers\Admin;

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

class PrefixController extends Controller
{
    public function __construct()
    {
        $this->middleware(['permission:add_prefix'])->only(['create', 'store']);
        $this->middleware(['permission:edit_prefix'])->only(['edit', 'update']);
        $this->middleware(['permission:view_prefix'])->only(['index']);
        $this->middleware(['permission:delete_prefix'])->only(['delete']);
    }

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.prefixes.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('prefixes.errors'));

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

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

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

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

        return redirect()->route('prefixes.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(Prefix $prefix)
    {
        return view('admin.prefixes.edit', ['prefix' => $prefix]);
    }

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

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


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

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

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

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

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

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

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

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