<?php

namespace App\Http\Controllers\Hr;
use App\Http\Controllers\Controller;

use Illuminate\leaves\Encryption\DecryptException;

use Illuminate\Http\Request;
use App\Http\Requests\LeaveRequest;

use App\Leave;
use App\User;

use EF;

class LeaveController extends Controller
{
    public function index()
    {
    	$leave    = new Leave;
    	$employee    = new User;

    	return view('hr.leave.manage_leave',
    		[
                'leave_list'    => $leave->getleaveList(),
                'employee_list'    => $employee->getUserListByType(['Employee']),
    		]
    	);
    }

    public function storeLeave(LeaveRequest $request)
    {
        //dd($request);
    	$form_collect = $request->input();

    	$form_collect['employee']    = decrypt($form_collect['employee']);

    	$leave      = new Leave;

    	$curr_leave = $leave->storeLeave($form_collect);

    	if (isset($curr_leave->id)) 
    	{
    	   parent::successMessage('Leave has been added');	
    	}

    	return redirect()->back();

    }

    public function updateLeave(LeaveRequest $request)
    {
    	try
    	{
            $form_collect = $request->input();
            
            $form_collect['leave'] = decrypt($form_collect['leave']);

           
    		$leave      = new Leave;

    		$curr_leave = $leave->updateLeave($form_collect);

	    	if (isset($curr_leave->id)) 
	    	{
	    	 parent::successMessage('Leave has been updated');	
	    	}

    	}
    	catch(DecryptException $e)
    	{

    	}

    	return redirect()->back();
    }
}
