<?php

namespace App\Http\Controllers\HR;

use App\Model\Hr\DevelopmentPlanModel;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HrDevelopmentPlanController extends Controller
{
    function __construct()
    {
      $this->middleware('permission:Development plan Create', ['only' => ['create', 'store']]);
      $this->middleware('permission:Development plan List', ['only' => ['index']]);
      $this->middleware('permission:Development plan Edit', ['only' => ['update','edit']]);
    }
    public function index()
    {
        $dev = DevelopmentPlanModel::get();
        return view('hr.development-plan.index', compact('dev'));
    }


    public function create()
    {
        $emp = User::where('status', 1)->pluck('name', 'id');
        return view('hr.development-plan.create', compact('emp'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'emp_id' => 'required',
            'request_to' => 'required'
        ]);

        try{
            $dev = new DevelopmentPlanModel();
            $dev->emp_id = $request->emp_id;
            $dev->request_to = $request->request_to;
            $dev->description_department = $request->de_description;
            $dev->suggestion_training = $request->training;
            $dev->suggestion_hard_skill = $request->hard_skill;
            $dev->suggestion_soft_skill = $request->soft_skill;
            $dev->description = $request->description;

            $dev->save();

            return redirect()->route('hr-development-plan.index')->with('success', 'Added Successful!');
        }catch(\Exception $ex){
            return back()->withErrors($ex->getMessage())->withInput();
        }
    }


    public function show(DevelopmentPlanModel $developmentPlanModel)
    {
        //
    }

    public function edit($id)
    {
        $dev = DevelopmentPlanModel::find($id);
        $emp = User::where('status', 1)->pluck('name', 'id');
        return view('hr.development-plan.edit', compact('dev','emp'));
    }


    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'emp_id' => 'required',
            'request_to' => 'required'
        ]);

        try{
            $dev = DevelopmentPlanModel::find($id);
            $dev->emp_id = $request->emp_id;
            $dev->request_to = $request->request_to;
            $dev->description_department = $request->de_description;
            $dev->suggestion_training = $request->training;
            $dev->suggestion_hard_skill = $request->hard_skill;
            $dev->suggestion_soft_skill = $request->soft_skill;
            $dev->description = $request->description;

            $dev->update();

            return redirect()->route('hr-development-plan.index')->with('success', 'Updated Successful!');
        }catch(\Exception $ex){
            return back()->withErrors($ex->getMessage())->withInput();
        }
    }


    public function destroy(DevelopmentPlanModel $developmentPlanModel)
    {
        //
    }
}
