<?php

namespace App\Http\Controllers\Administrator;


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\PositionModel;
use Auth;
class PositionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct()
    {
         $this->middleware('permission:Position-List');
         $this->middleware('permission:Position-Create', ['only' => ['create','store']]);
         $this->middleware('permission:Position-Edit', ['only' => ['edit','update']]);
    }
    
    public function index()
    {
        $positions = PositionModel::where('branch_id', \Branch::branch_id())->get();
        return view('administrator.positions.index',compact('positions'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $positions = new PositionModel;
       
        $this->validate($request, [
            'name'      => 'required',
        
        ]);

        $positions->name     = $request->name;
        $positions->description     = $request->description;
        $positions->branch_id        = \Branch::branch_id();
        $positions->save();
        \LogActivity::addToLog('Position created successfully !');
        return redirect()->route('positions.index')
        ->with('success','Position created successfully !');
       
    }

    /**
     * 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($id)
    {
        $positions = PositionModel::find($id);
        return view('administrator.positions.edit',compact('positions'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $positions =PositionModel::find($id);
       
        $this->validate($request, [
            'name'      => 'required',
        
        ]);

        $positions->name     = $request->name;
        $positions->description    = $request->description;
        $positions->branch_id        = \Branch::branch_id();
       
        $positions->update();
        

        \LogActivity::addToLog('Position updated successfully !');
        return redirect()->route('positions.index')
                        ->with('success','Position updated successfully !');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
