<?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["from"])  || !isset($_GET["to"])  || !isset($_GET["branchId"]) ) {

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

    }



    //sales report

    if (isset($_GET["from"]) && isset($_GET["to"])  && isset($_GET["branchId"])  ) {


      @$sellBranch = mysql_prep($_GET['branchId']); 
      @$status = 1;

      @$from = mysql_prep($_GET['from']);
      @$to = mysql_prep($_GET['to']);

      @$sellType = 1;
                   
      $soldProSet = find_all_sold_product_report_for_branch_and_type($from, $to, $status, $sellBranch, $sellType); 


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


      foreach ($soldProSet as $soldPro[]) {

        $item = $soldPro;

        $json = json_encode($item);

      }

      exit($json) ;


    }

  
  }else{

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

  }
