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

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

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

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

  //exit(substr(getallheaders()["Authorization"], 7));
  //exit();

  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 businesses

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

      $busSet = all_businesses(1);

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

      $busSet = find_my_businesses($decodeToken->BUSID, 1); 

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

      $busSet = find_my_businesses($decodeToken->BUSID, 1); 
    }

    foreach ($busSet as $business[]) {

      $item = $business;

      $json = json_encode($item);

    }

    exit($json) ;

  }else{

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

  }