<?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["phone"])  || !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 supplier
  if( isset($obj['firstName']) && isset($obj['phone'])  && isset($obj['businessId'])  )  { 

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

 	  @$lName =  mysql_prep($obj['lName']);
 	  @$idNum =  mysql_prep('123');

 	  @$phone =  mysql_prep($obj['phone']);
    @$businessId =  mysql_prep($obj['businessId']);
    
    //check if products is already exists
    $sql = $connectMe->prepare(" SELECT * FROM tbl_supplier WHERE (tbl_supplierPHone = ?) AND (tbl_businessId = ?) "); 

    $sql->bindParam(1, $phone);
    $sql->bindParam(2, $businessId);

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

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

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

        }else{ //English

        $error = array("error"=> $phone.' is already exist');
        exit( json_encode($error) ); 

      }


    }   

    else{

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

      $stmt = $connectMe->prepare("INSERT INTO  tbl_supplier (tbl_supplierFname, tbl_supplierMname, tbl_supplierLname, tbl_supplierIdNumber, tbl_supplierPHone, tbl_supplierToken, tbl_supplierStatus, tbl_businessId)  VALUES (?,?, ?,?, ?,?, ?,?) ");  

      $date = date("Y-m-d");
      $stmt->bindParam(1, $fName);

      $stmt->bindParam(2, $Mname);
      $stmt->bindParam(3, $lName);

      $stmt->bindParam(4, $idNum);
      $stmt->bindParam(5, $phone);

      $stmt->bindParam(6, $token);
      $stmt->bindParam(7, $status);

      $stmt->bindParam(8, $businessId);

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

          $message = array("message"=> $fName.' '.$Mname.' '.$lName.' yanditswe muri sistemu');
          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") ));

}


?>