<?php header("Access-Control-Allow-Origin: *");
  header("Access-Control-Allow-Headers: *");  

  header('Content-Type: application/json');

  $json = file_get_contents('php://input');
  $obj = json_decode($json, true);

  include '../db/connectMe.php';   
  include '../function/function.php';  

  // (C) GENERATE JWT TOKEN
  require "../vendor/autoload.php";
  use Firebase\JWT\JWT;

  // (A) NO data
  if (!isset($obj['firstName'])  && !isset($obj['nid']) && !isset($obj['gender'])  && !isset($obj['levelId'])  && !isset($obj['villageId']) && !isset($obj['positionId'])  && !isset($obj['phone'])  && !isset($obj['salary'])  && !isset($obj['businessId'])  ){

    $error = array("error"=> "no");
    exit( json_encode($error) ); 

  }



  if( AuthorizeMe(@substr(getallheaders()["Authorization"], 7)) ){

    $jwt = substr(getallheaders()["Authorization"], 7);

    //Decoding data
    $decodeToken = (json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $jwt)[1]))))->data);

    if(!isset($decodeToken)){

      exit(json_encode(array("error" => "unauthorised") ));

    }



  // add employeee

  if (isset($obj['firstName'])  && isset($obj['nid']) && isset($obj['gender'])  && isset($obj['levelId'])  && isset($obj['villageId']) && isset($obj['positionId'])  && isset($obj['phone'])  && isset($obj['salary'])  && isset($obj['businessId'])  ){


 	@$fName =  mysql_prep($obj['firstName']);
 	@$mName=  mysql_prep($obj['middleName']);

 	@$lName =  mysql_prep($obj['lastName']);
 	@$idN =  mysql_prep($obj['nid']);

 	@$sex =  mysql_prep($obj['gender']);
  @$level = mysql_prep($obj['levelId']);

  @$vill =  mysql_prep($obj['villageId']);
  @$type = mysql_prep($obj['positionId']);

  @$phone = mysql_prep($obj['phone']);
  @$token =  mysql_prep(password_hash(uniqid(), PASSWORD_BCRYPT, ['cost' => 10]));

  @$addEmpSalary = mysql_prep($obj['salary']);
  @$addEmpBus = mysql_prep($obj['businessId']);

    
    //check if products is already exists
    $sql = $connectMe->prepare(" SELECT * FROM tbl_employee WHERE (tbl_employeeIdNumber = ?) AND (tbl_businessId = ?) "); 

    $sql->bindParam(1, $idN);
    $sql->bindParam(2, $addEmpBus);

    $sql->execute();
    if($sql->rowCount() > 0){ 


      if($decodeToken->LANGUAGE == 2){    //KINYARWANDA

        $error = array("error"=> $idN.' isanzwemo');
        exit( json_encode($error) ); 

        }else{ //English

        $error = array("error"=> $idN.' is already registered');
        exit( json_encode($error) ); 

      }



    }   

    else{

      $status = 1;
      $date = date("Y-m-d");

      $stmt = $connectMe->prepare("INSERT INTO  tbl_employee ( tbl_emp_typeId	, tbl_employeeFname	,  tbl_employeeMnane	,  tbl_employeeLname	, tbl_employeeIdNumber, tbl_employeeSex	, tbl_degreeId	 , tbl_villageId, tbl_employeeToken, tbl_employeeTel, tbl_employeeStatus,tbl_employeeSalary, tbl_businessId	 )  VALUES (?,?,?, ?,?,?, ?,?,?, ?,?,?, ?) ");  
       
      $stmt->bindParam(1, $type);
      $stmt->bindParam(2, $fName);

      $stmt->bindParam(3, $mName);
      $stmt->bindParam(4, $lName);

      $stmt->bindParam(5, $idN);
      $stmt->bindParam(6, $sex);
    
      $stmt->bindParam(7, $level);
      $stmt->bindParam(8, $vill);

      $stmt->bindParam(9, $token);
      $stmt->bindParam(10, $phone);

      $stmt->bindParam(11, $status);
      $stmt->bindParam(12, $addEmpSalary);

      $stmt->bindParam(13, $addEmpBus);

      if($stmt->execute()){

        if($decodeToken->LANGUAGE == 2){    //KINYARWANDA

          $message = array("message"=> $fName.' '.$mName.' '.$lName.' yanditswe nkumukozi');
          exit( json_encode($message) ); 

        }else{  //English

          $message = array("message"=> $fName.' '.$mName.' '.$lName.' registered');
          exit( json_encode($message) ); 

        }

      
      }else{

        if($decodeToken->LANGUAGE == 2){    //KINYARWANDA

          $error = array("error"=> 'Igikorwa ntabwo kibashije kurangira, ongera ugerageze');
          exit( json_encode($error) ); 

        }else{


          $error = array("error"=> 'Something went wrong, please try again');
          exit( json_encode($error) ); 

        }

      }
    }
    

}


}else{

  exit(json_encode(array("error" => "unauthorised") ));

}






?>