<?php

namespace App\Http\Controllers\Hr;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\AllowanceRequest;
use App\Allowance;
use Illuminate\Contracts\Encryption\DecryptException;

class AllowanceController extends Controller
{
    public function index()
    {   
        $allowance = new Allowance;

        $allowance_types = $allowance->getDistinctAllowance('type',['type']);
        
        return view('hr.allowance.manage_allowance',
        	[

        		'allowance_list'       => $allowance->getAllowanceList(),
                'types'                => $allowance_types,
        		
        	]
    );
       
    }

    public function storeAllowance(AllowanceRequest $request)
    {




        $allowance = new Allowance;

        $form_collect = $request->input();


        if($form_collect['amount'] == null && $form_collect['percentage'] == null)
        {

            parent::errorMessage('Please enter Allowance Amount or Percentage');

             return redirect('allowance');

        }


        if($form_collect['amount'] != null && $form_collect['percentage'] != null)
        {

            parent::errorMessage('You cant enter amount and percentage at same time');

             return redirect('allowance');

        }




        if($form_collect['allowance_type'] === "Other")
        {
            $form_collect['type'] =  $form_collect['type'];
        }
        else
        {
            $form_collect['type'] =  $form_collect['allowance_type'];
        }

        if(isset($form_collect['is_taxable']))
        {
             $form_collect['is_taxable'] =  1;
        }
        else
        {
            $form_collect['is_taxable'] =  0;
        }

        $curr_allowance = $allowance->storeAllowance($form_collect);

        if(isset($curr_allowance->id))
        {
            parent::setLogMessage('Allowance "'.$curr_allowance->title.' Amount: '.$curr_allowance->amount.'" has been created');

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

        return redirect('allowance');
    }


     public function updateAllowance(AllowanceRequest $request)
    {
        try 
        {
            

            $form_collect = $request->input();

            $form_collect['id'] = decrypt($request->allowance);

        if($form_collect['amount'] == null && $form_collect['percentage'] == null)
        {

            parent::errorMessage('Please enter Allowance Amount or Percentage');

             return redirect('allowance');

        }

           if($form_collect['amount'] != null && $form_collect['percentage'] != null)
        {

            parent::errorMessage('You cannot enter amount and percentage at same time');

             return redirect('allowance');

        }

          if(isset($form_collect['is_taxable']))
        {
             $form_collect['is_taxable'] =  1;
        }
        else
        {
            $form_collect['is_taxable'] =  0;
        }

            $allowance = new Allowance;


            $curr_allowance = $allowance->updateAllowance($form_collect);

            if(isset($curr_allowance))
            {
                parent::setLogMessage('Allowance "'.$curr_allowance->title.' Amount: '.$curr_allowance->amount.'" has been updated');

                parent::successMessage('Allowance has been updated.',parent::getLogMessage());
            }
        } 
        catch (DecryptException $e) 
        {
            
        }

        return redirect('allowance');
    }
}
