<?php

namespace App\Http\Controllers\Admin;

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

class LocationController extends Controller
{
    public function __construct()
    {
        $this->middleware(['permission:add_location'])->only(['create', 'store']);
        $this->middleware(['permission:edit_location'])->only(['edit', 'update']);
        $this->middleware(['permission:view_location'])->only(['index']);
        $this->middleware(['permission:delete_location'])->only(['delete']);
    }

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.locations.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', 'telephone' => 'required', 'fax' => 'required']);
        if($validator->fails()){
            toastr()->error(trans('locations.errors'));

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

    public function fetch($address){
        return Location::whereName($address)->first();
    }
}
