<?php

namespace App\Http\Controllers\HR;

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

class HrRecommendationController extends Controller
{

    function __construct()
    {
      $this->middleware('permission:Recommendation Create', ['only' => ['create', 'store']]);
      $this->middleware('permission:Recommendation List', ['only' => ['index']]);
      $this->middleware('permission:Recommendation Edit', ['only' => ['update','edit']]);
    }
    public function index()
    {
        $recommend = RecommendationModel::get();
        return view('hr.recommendation.index', compact('recommend'));
    }


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


    public function store(Request $request)
    {
        $this->validate($request, [
            'emp_id' => 'required',
            'request_to' => 'required'
        ]);
        //dd($request->all());
        try{
            $recommend = new RecommendationModel();
            $recommend->emp_id = $request->emp_id;
            $recommend->request_to = $request->request_to;

            $recommend->increment_comment = $request->in_comment;
            $recommend->increment_date = date('Y-m-d',strtotime($request->in_date));
            $recommend->increment_description = $request->in_description;

            $recommend->bonus_comment = $request->bonus_comment;
            $recommend->bonus_date = date('Y-m-d',strtotime($request->bonus_date));
            $recommend->bonus_description = $request->bonus_description;

            $recommend->promote_comment = $request->pro_comment;
            $recommend->promote_description = $request->pro_description;

            $recommend->opportunity_comment = $request->opp_comment;
            $recommend->opportunity_description = $request->opp_description;

            $recommend->save();

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

    public function show(RecommendationModel $recommendationModel)
    {
        //
    }


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


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

        try{
            $recommend = RecommendationModel::find($id);
            $recommend->emp_id = $request->emp_id;
            $recommend->request_to = $request->request_to;

            $recommend->increment_comment = $request->increment_comment;
            $recommend->increment_date = date('Y-m-d',strtotime($request->increment_date));
            $recommend->increment_description = $request->increment_description;

            $recommend->bonus_comment = $request->bonus_comment;
            $recommend->bonus_date = date('Y-m-d',strtotime($request->bonus_date));
            $recommend->bonus_description = $request->bonus_description;

            $recommend->promote_comment = $request->promote_comment;
            $recommend->promote_description = $request->promote_description;

            $recommend->opportunity_comment = $request->opportunity_comment;
            $recommend->opportunity_description = $request->opportunity_description;

            $recommend->update();

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


    public function destroy(RecommendationModel $recommendationModel)
    {
        //
    }
}
