<?php

namespace App\Http\Controllers\Hr;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\AssignAllowanceRequest;
use Illuminate\Contracts\Encryption\DecryptException;
use App\AssignAllowance;
use App\Scale;
use App\Designation;
use App\Allowance;
class AssignAllowanceController extends Controller
{
     public function index()
    {   
        $assign_allowance = new AssignAllowance;

        $scale            = new Scale;

        $designation      = new Designation;

        $allowance        = new Allowance;
     
        
        return view('hr.assign_allowance.manage_assigned_allowance',
        	[

        		'assign_allowance_list'       => $assign_allowance->getAssignedAllowanceList(),
                'scale_list'                  => $scale->getScaleList(),
                'designation_list'            => $designation->getDesignationList(),
                'allowance_list'              => $allowance->getAllowanceList()
        		
        	]
    );
       
    }


    public function storeAssignAllowance(AssignAllowanceRequest $request)
    {

        $assign_allowance = new AssignAllowance;

        $form_collect = $request->input();

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

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

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

        $curr_assigned_allowance = $assign_allowance->storeAssignAllowance($form_collect);

        if(isset($curr_assigned_allowance->id))
        {
            parent::setLogMessage('Allowance "'.$curr_assigned_allowance->allowance->title.'" has been assigned');

            parent::successMessage('Allowance has been assigned.',parent::getLogMessage());
        }

        return redirect('viewassignallowance');
    }

    public function updateAssignAllowance(AssignAllowanceRequest $request)
    {

        $assign_allowance = new AssignAllowance;

        $form_collect = $request->input();

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

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

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

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

        $curr_assigned_allowance = $assign_allowance->updateAssignAllowance($form_collect);

        if(isset($curr_assigned_allowance->id))
        {
            parent::setLogMessage('Allowance "'.$curr_assigned_allowance->allowance->title.'" has been re assigned');

            parent::successMessage('Allowance has been re assigned.',parent::getLogMessage());
        }

        return redirect('viewassignallowance');
    }

}
