<?php


namespace App\Http\Controllers\Hr;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use App\Http\Requests\ContractDetailRequest;
use App\ContractDetail;
use App\Contract;
use Crypt;
use App\DB;

class ContractDetailController extends Controller
{
    
    public function index($id)
    {

    	try 
      {
            
    	$contract_detail    = new ContractDetail;
    	$contract           = new Contract;

    	$id = decrypt($id);

    	$curr_contract = $contract->getContractById($id);

      if(isset($curr_contract->id))
      {
          
         $contract_detail_list    = $contract_detail->getContractDetailByContractId($curr_contract->id);

       $view = '';

       $remove_key_word = "'remove'";

        foreach($contract_detail_list as $rows)
        {
            $update_key_word = uniqid();

            $view .= '<tr><td>'.$rows->allowance->title.'</td><td>'.$rows->allowance->type.'</td><td> <small class="form-errors pull-right req" value=""></small><input class = "form-control form-control-sm percentage_'.$update_key_word.'" name = "percentage"  value = "'.$rows->percentage.'"</td><td> <small class="form-errors pull-right req" value=""></small><input class = "form-control form-control-sm amount_'.$update_key_word.'" name = "amount"  value = "'.$rows->amount.'"</td><td><abbr title = "Remove Allowance"><i style = "color: red; width:20px;" class="fa fa-trash btn" id = "'.Crypt::encrypt($rows->id).'" onclick = "removeContractDetail(event,this)"></i></abbr><abbr title = "Update Allowance"><i style = "color: green;" class="fa fa-pencil-alt btn" onclick = "updateContractDetail(event,this,'."'amount_".$update_key_word."'".','."'percentage_".$update_key_word."'".')" id = "'.Crypt::encrypt($rows->id).'"></i></abbr></td></tr>';

         }

                
    	
        }

      return $view;



        } 
        catch (DecryptException $e) 
        {
            
        }

    }

    public function uploadAssetImage(Request $request)
    {
      try
      {


        $request->validate([
          'asset_form'        =>  'nullable|mimes:pdf,png,jpeg,jpg|max:10240',
           
        ]);
         $collect_form['asset_form'] = $this->uploadfile($request->asset_form);

         $collect_form['id'] = decrypt($request->employee_asset);

         $contract_detail = new ContractDetail;

         $upload_image = $contract_detail->uploadAssetFile($collect_form);


         if($upload_image)
         {
           parent::successMessage('Asset Form successfully uploaded.'); 

           return redirect()->back();
         }




      }
      catch(DecryptException $e)
      {

      }
    }

      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 removeContractDetail($id)
  {
        parent::errorMessage('Contract Detail not found.');

        try 
        {
          $id = decrypt($id);

          $contract_detail = new ContractDetail;
         $curr_contract_detail = $contract_detail->removeContractDetail($id);

          if(isset($curr_contract_detail))
           {
            $contract_id = Crypt::encrypt($curr_contract_detail->contract_id);
         
             parent::setLogMessage($curr_contract_detail->assignAllowance->allowance->title.'  has been removed.');

                parent::successMessage('Assigned Allowance against contract has been removed.',parent::getLogMessage()); 
           }  
        } 
        catch (DecryptException $e)
        {
          
        }

       
  }
     public function updateContractDetail(ContractDetailRequest $request)
    {
        try 
        {
            if(\Request::ajax())
            {
               
dd($request->input());
                $id = decrypt($request->id);

                $contract_detail = new ContractDetail;


                $curr_detail = $contract_detail->updateContractDetail($request->input(),$id);

                if(isset($curr_detail))
                {
                    
                    parent::setLogMessage('Contract Detail "'.$curr_detail->assignAllowance->allowance->title.'" has been updated');

                    parent::successMessage('Contract Detail has been updated.',parent::getLogMessage());

                    return 1;
                }

                return 0;
            }
            
        } 
        catch (DecryptException $e) 
        {
            
        }

        
    }


}
