<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Permission;
use App\Role;
use App\Admin;
use Illuminate\Support\Facades\Auth;

class RoleController extends Controller
{


    public function __construct()
    {
        $this->middleware('auth:admin');
    }




    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index(Role $role)
    {

        //$this->authorize('view', $role);

        $data = [];
        $data['role']  = $role;
        $data['roles'] = $role::all();

        return view('admin.roles.index',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Role $role)
    {
        $this->authorize('create', $role);

        $data = [];
        $data['role']                = $role;
        $data['curentpermissions']   = $role->permissions()->pluck('id')->toArray();
        $data['create'] = 1;
        $data['permissions'] = Permission::all();
        return view('admin.roles.form', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Role $role)
    {

        $this->authorize('create', $role);


        if( $request->isMethod('post') )
        {

            $this->validate(
                $request,
                [
                    'name' => 'required|string|min:5',
                    'description' => 'required|string|min:5',
                    'permissions' => 'required|min:1',
                    'permissions.*' => 'required|integer|exists:permissions,id',

                ]
            );

            $role = new Role();


            $role->name        = $request->name;
            $role->description = $request->description;

            $role->save();

            $role->permissions()->sync($request->permissions);


            return redirect()->route('admin.role.index')->with('result', 'Dodat novi nivo pristupa: "'.$role->name.'"');
        }





    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function show(Role $role)
    {
        //
        $this->authorize('view', $role);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function edit(Role $role)
    {
        //
        $this->authorize('update', $role);

        $data = [];
        $data['role']                = $role;
        $data['curentpermissions']   = $role->permissions()->pluck('id')->toArray();
        $data['create']              = 0;
        $data['permissions']         = Permission::all();
        return view('admin.roles.form', $data);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Role $role)
    {

        $this->authorize('update', $role);

        $this->validate(
            $request,
            [
                'name' => 'required|string|min:5',
                'description' => 'required|string|min:5',
                'permissions' => 'required|min:1',
                'permissions.*' => 'required|integer|exists:permissions,id',

            ]
        );

        $role->name = $request->name;
        $role->description = $request->description;

        $role->save();

        $role->permissions()->sync($request->permissions);


        return redirect()->route('admin.role.index')->with('result', 'Nivo pristupa je izmenjen');;


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function destroy(Role $role)
    {
        $this->authorize('delete', $role);

        $name = $role->name;
        $this->authorize('delete', $role);
        $role->destroy($role->id);
        return redirect()->route('admin.role.index')->with('result', 'Nivo pristupa "'.$name.'" je obrisan');

    }
}
