<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Permission;
use App\Models\Admin;
use Auth;
use Session;

class PermissionController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $permissions = Permission::where('guard_name','admin')->get();
        return view('admin.permissions.list',compact('permissions'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $parents = Permission::where('guard_name', 'admin')->where('parent',0)->orderBy('name','ASC')->get();
        return view('admin.permissions.add', compact('parents'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'name' => 'required|unique:permissions,name'
        ]);
        $permission = new Permission;
        $permission->name = str_replace(' ', '_', strtolower(trim($request->name)));
        $permission->display_name = $request->name;
        if ($request->parent !="") {
            $permission->parent = $request->parent;
        }
        $permission->guard_name = 'admin';
        $permission->save();

        return redirect()->action('Admin\PermissionController@index')->with('success','Successfully Created!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $permission = Permission::find($id);
        return view('admin.permissions.view',compact('permission'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $permission = Permission::find($id);
        $parents = Permission::where('guard_name', 'admin')->where('parent',0)->orderBy('name','ASC')->get();
        return view('admin.permissions.edit',compact('permission','parents'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name' => 'required'
        ]);
        $permission = Permission::find($id);
        $permission->name = str_replace(' ', '_', strtolower(trim($request->name)));
        $permission->display_name = $request->name;
        if ($request->parent !="") {
            $permission->parent = $request->parent;
        }
        $permission->update();

        return redirect()->action('Admin\PermissionController@index')->with('success','Successfully Updated!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $permission = Permission::find($id); 

        Permission::whereId($id)->delete();
        return redirect()->action('Admin\PermissionController@index')->with('success','Successfully Deleted!');
    }
}
