<?php

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

use Illuminate\Contracts\Encryption\DecryptException;

use Illuminate\Http\Request;
use App\Http\Requests\ContractRequest;
use App\Contract;
use App\ContractDetail;
use App\User;
use App\Designation;
use App\Scale;
use App\AssignAllowance;
use Crypt;
use EF;
use App\Asset;
use App\Branch;

use App\Allowance;

class ContractController extends Controller
{
    public function index()
    {
    	$contract    = new Contract;
    	$employee    = new User;
    	$designation = new Designation;
    	$scale       = new Scale;
        $branch      = new Branch;


    	return view('hr.contract.manage_contract',
    		[
                'contract_list'    => $contract->getContractList(),
                'contract_list'    => $contract->getContractList(),
                'branch'           => $branch->getBranchList(),
                'employee_list'    => $employee->getUserListByType(['Employee']),
                'designation_list' => $designation->getDesignationList(),
                'scale_list'       => $scale->getScaleList(),

    		]
    	);
    }

    public function storeContract(ContractRequest $request)
    {
    	$form_collect = $request->input();


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

      $form_collect['branch']      = decrypt($form_collect[\EF::branchWhiteLabel()['module']]);


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

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

        $form_collect['contract_form'] = $this->uploadfile($request->contract_form);

    	$contract      = new Contract;


    	$curr_contract = $contract->storeContract($form_collect);

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

            $user = new User;

            $user = User::find($curr_contract->user_id);

            if(isset($user->id) && $user->designation_id != $curr_contract->designation_id)
            {
               $update_designation = $user->updateUserDesignation($form_collect['employee'],$form_collect['designation']);
            }


             $contract_detail = new ContractDetail;

             $curr_contract_detail = $contract_detail->storeContractDetail($form_collect,$curr_contract->id);

    	   parent::successMessage('Contract has been added');
    	}

    	return redirect()->back();

    }

  public function uploadfile($file,$loaded_image = '/graphics/logo/user.png')
  {
      if((isset($file) && trim(strlen($file)) > 0))
      {
          $file =  \EF::uploadFile($file);

      }
      else
      {
          if(strlen(trim($loaded_image)) > 0)
          {
              $file =  $loaded_image;
          }
          else
          {
             $file = '/graphics/logo/user.png';
          }
      }


      return $file;
  }

  public function assignAllowances(ContractRequest $request)
  {

    try
    {
      $form_collect = $request->input();

      if(@count($form_collect['allowance_id']) == 0)
      {
       parent::errorMessage('Please select an allowance to assign');

       return redirect()->back();
      }

      $contract_detail = new ContractDetail;


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

      foreach($form_collect['allowance_id'] as $rows)
      {
         $form_collect['allowances'][] = decrypt($rows);
      }

      $curr_details = $contract_detail->storeContractDetailByAllowance($form_collect);

      if(isset($curr_details))
      {
        parent::successMessage('Allowances  has been successfully assigned');

      }

           return redirect('contract');


    }
    catch(DecryptException $e)
    {

    }



  }

    public function assignAssets(ContractRequest $request)
  {

    try
    {

      $contract_detail = new ContractDetail;

      $form_collect = $request->input();

       if(@count($form_collect['asset_id']) == 0)
      {
       parent::errorMessage('Please select an asset to assign');

       return redirect()->back();
      }


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

      foreach($form_collect['asset_id'] as $rows)
      {
         $form_collect['assets'][] = decrypt($rows);
      }

      $curr_details = $contract_detail->storeContractDetailByAsset($form_collect);

      if(isset($curr_details))
      {
        parent::successMessage('Assets  has been successfully assigned');

      }

           return redirect('contract');


    }
    catch(DecryptException $e)
    {

    }



  }


  public function contractAssignment($id)
  {

    try
    {

      $contract_id = decrypt($id);

      $asset = new Asset;

      $new_assets = $asset->getUnAssignedAssetList($contract_id);

      $allowance = new Allowance;

       $new_allowances = $allowance->getUnAssignedAllowanceList($contract_id);

    }
    catch(DecryptException $e)
    {

    }
      return view('hr.contract.view_contract_assignment',
      [
        'new_assets'     => $new_assets,

        'new_allowances' => $new_allowances,

        'contract_id'    => $contract_id,


      ]
    );
  }

    public function updateContract(ContractRequest $request)
    {
    	try
    	{

            $form_collect = $request->input();


            $form_collect['contract']               = decrypt($form_collect['contract']);
      $form_collect['branch']      = decrypt($form_collect[\EF::branchWhiteLabel()['module']]);


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

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

             $form_collect['contract_form'] = $this->uploadfile($request->contract_form);

    		$contract      = new Contract;

    		$curr_contract = $contract->updateContract($form_collect);

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

    	}
    	catch(DecryptException $e)
    	{

    	}

    	return redirect()->back();
    }
    // CONTRACT DETAIL STORE BY CONTRACT
    public function storeDetailByContract(ContractRequest $request)
    {
      $contract_list = $request->checkbox;

        $form_collect = $request->input();

        $contract_detail = new ContractDetail;

        try
        {
            $form_collect['assign_allowance'] = decrypt($form_collect['assign_allowance']);

            for($i = 0; $i< @count($form_collect['contract_list']); $i++)
            {
                $form_collect['contract_list'][$i] = decrypt($form_collect['contract_list'][$i]);
            }

            $contract_details = $contract_detail->storeDetailByContract($form_collect);

          if($contract_details == 1)
          {
            parent::successMessage('Allowance has been alloted');
          }



        }
        catch(DecryptException $e)
        {

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

    public function getContracts($id)
    {
        try
        {
           if(\Request::ajax())
            {
                $assign_id = decrypt($id);

                $assign_allowance = new AssignAllowance;

                $contract         = new Contract;

                $current_assign_allowance = $assign_allowance->getAssignAllowanceById($assign_id);

                if(isset($current_assign_allowance->id))
                {
                    $form_collect['assign_id'] = $current_assign_allowance->id;
                     $form_collect['designation_id'] = $current_assign_allowance->designation_id;

                     $form_collect['scale_id'] = $current_assign_allowance->scale_id;

                    $contract_list = $contract->getContracts($form_collect);

             $view = '<input type = "hidden" name = "assign_allowance" value ="'.Crypt::encrypt($current_assign_allowance->id).'"';
             foreach($contract_list as $rows)
             {
                $view .= '<tr><td><input type="checkbox" class="checked_contract" name ="contract_list[]" value="'.Crypt::encrypt($rows->id).'"></td><td>'.ucfirst($rows->user->name).'</td><td>'.$rows->scale->title.'</td><td>'.$rows->designation->title.'</td><td>'.EF::numberFormat($rows->basic_salary).'</td><td>'.EF::dateFormat($rows->start_date).'-'. EF::dateFormat($rows->end_date).'</td></tr>';

             }
             return $view;


                }




            }

        }
        catch(DecryptException $e)
        {

        }


    }

    public function getEmployeeInfo($id)
    {

        try
        {
           if(\Request::ajax())
            {
                $data = [];

                $user = new User;

                $designation = new Designation;

                $user_id = decrypt($id);

                $user_details = $user->getUserById($user_id);

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

                    $curr_designation = $designation->getDesignationById($user_details->designation_id);
                    if(isset($curr_designation->id))
                    {


                    $data = ['designation_id' => Crypt::encrypt($user_details->designation_id),'user_designation' => $curr_designation->title,'employee_type'=> $user_details->employee_type];

                    }
                    else
                    {
                        $data = ['employee_type'=> $user_details->employee_type];
                    }



                }


                return $data;
            }
        }

        catch(DecryptException $e)
        {

        }

    }


}
