<?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( 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") ));

    }



    ////////////////////////requests


      @$status  = "PENDING"; 

      if($decodeToken->UT== 'admin'){

        $requests = find_all_requests_for_business($decodeToken->BUSID, $status); 

      }else if($decodeToken->UT== 'manager' || $decodeToken->UT== 'technician'){

        $requests = find_all_requests_for_branch($decodeToken->BID, $status); 
      
      }


      if(empty($requests)){
        exit(json_encode([]));
      }

      foreach ($requests as $request[] ) {

        $item = $request;
        $json = json_encode($item);

      }


      exit($json) ;




  }else{
   

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


  }




