<?php

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

use Illuminate\Http\Request;
use App\Http\Requests\DeductionRequest;
use App\User;
use App\Deduction;
use Crypt;

class DeductionController extends Controller
{
    public function index()
    {
    	$deduction   = new Deduction;
    	$employee    = new User;
    	
        $deduction_titles = $deduction->getDistinctDeduction('title',['title']);
    	return view('hr.deduction.manage_deduction',
    		[
                'deduction_list'        => $deduction->getDeductionList(),
                 'titles'               => $deduction_titles,
               
    		]
    	);
    }

    public function storeDeduction(DeductionRequest $request)
    {
    	$form_collect = $request->input();


    	$deduction      = new Deduction;

    	$curr_deduction = $deduction->storeDeduction($form_collect);

    	if (isset($curr_deduction->id)) 
    	{
    	   parent::successMessage('Deduction has been added');	

            if($request->ajax())
            {
               $data['amount'] = $curr_deduction->amount;
               $data['title']     = $curr_deduction->title;
           

               parent::clearMessageLog(1);
               
               
               return $data;
            }
    	}

    	return redirect()->back();

    }

    public function updateDeduction(DeductionRequest $request)
    {
    	try
    	{
            $form_collect         = $request->input();
            
            $form_collect['deduction'] = decrypt($form_collect['deduction']);
   
    		$deduction      = new Deduction;

    		$curr_deduction = $deduction->updateDeduction($form_collect);

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

    	}
    	catch(DecryptException $e)
    	{

    	}

    	return redirect()->back();
    }

       public function getDeduction($id)
    {
        try
        {

            if(\Request::ajax())
            {
                $data = '';

                $deduction = new Deduction;

               
                 $id = decrypt($id);



            $curr_deduction = $deduction->getDeductionById($id);

            if (isset($curr_deduction->id)) 
            {

    

                 $data  = ['title'=> $curr_deduction->title , 'amount' => $curr_deduction->amount,'id' => Crypt::encrypt($curr_deduction->id)];

             
            }

          return response()->json($data);

        }

          
          }
            
        catch(DecryptException $e)
        {

        }

    
    }


}
