<?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($_GET["tr"]) ) {

    $error = array('purchaseDetails' => 'something went wrong');
    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") ));

    }

    //view all transaction information

    if (@$_GET['tr'] != "") {

      @$tId = mysql_prep($_GET['tr']);

      $purchaseInfo = find_transaction_by_id($tId);

      if(!$purchaseInfo){

        $error = array('purchaseDetails' => 'something went wrong');
        exit(json_encode($error));
      } 

      // last transfer info
      $lastTransferInfo = find_last_transfer_for_product(@$purchaseInfo->tbl_productId, 1);


      //PURCHASE details
      $purchaseDetails = array(
        "purchaseDetails" => $purchaseInfo,
        "price" => @$lastTransferInfo->tbl_branch_productPrice
      );

      exit(json_encode($purchaseDetails));
    
    }  

  
  }else{

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





