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

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

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

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

  // var_dump((getallheaders()));
  // exit();

  //exit(json_encode( array('headers' =>  getallheaders() )));


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

    }

    
    //All categories

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

      $categSet = find_all_products_categ(); 

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

      $categSet = find_all_products_categ_for_business($decodeToken->BUSID); 
    }

    //BRANCH USER
    else if($decodeToken->UT== 'manager' || $decodeToken->UT== 'technician' ){

      //your branch
      $branch = find_branch_by_id($decodeToken->BID); 

      $categSet = find_all_products_categ_for_business($branch->tbl_businessId); 
    }


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

    foreach ($categSet as $categ[]) {

      $item = $categ;

      $json = json_encode($item);

    }

    exit($json) ;

  }else{

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

  }