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

  }


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

    }

    //exit(json_encode(array("total" => number_format(0), "profit" => 0)));



    //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= 0;
                   
      if($decodeToken->UT== 'admin' ){  // admin

        //paid cash  ////////////////////////////////////////////
        $sellCashInfo = find_sell_type_by_name("cash");

        $sellCashId = $sellCashInfo->tbl_sell_typeId;

        $cash = calculate_sold_product_report_for_branch_by_type($from, $to, $status, $sellBranch, $sellCashId)->amount;

        //paid momo  ////////////////////////////////////////////
        $sellMomoInfo = find_sell_type_by_name("momo");

        $sellMomoId = $sellMomoInfo->tbl_sell_typeId;

        $momo = calculate_sold_product_report_for_branch_by_type($from, $to, $status, $sellBranch, $sellMomoId)->amount;

        // total amount
        $amount = calculate_sold_product_report_for_branch_and_type($from, $to, $status, $sellBranch, $sellType)->amount;

        // profit
        $profit = calculate_profit_for_branch_and_type($from, $to, $status, $sellBranch, $sellType)->amount;
        

        if($decodeToken->STOCKSTATUS == 1){

          exit(json_encode(
            array(
              "total" => ($amount), 
              "profit" => ($profit),
              "cash" => ($cash),
              "momo" => ($momo)
            )
          ));

        }else {

          exit(json_encode(
            array(
              "total" => ($amount), 
              "profit" => 0,
              "cash" => ($cash),
              "momo" => ($momo)
            )
          ));    
        }



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


        //paid cash  ////////////////////////////////////////////
        $sellCashInfo = find_sell_type_by_name("cash");

        $sellCashId = $sellCashInfo->tbl_sell_typeId;

        $cash = calculate_sold_product_report_for_branch_by_type($from, $to, $status, $sellBranch, $sellCashId)->amount;

        //paid momo  ////////////////////////////////////////////
        $sellMomoInfo = find_sell_type_by_name("momo");

        $sellMomoId = $sellMomoInfo->tbl_sell_typeId;

        $momo = calculate_sold_product_report_for_branch_by_type($from, $to, $status, $sellBranch, $sellMomoId)->amount;


        $amount = calculate_sold_product_report_for_branch_and_type($from, $to, $status, $sellBranch, $sellType)->amount;

        exit(json_encode(
          array(
            "total" => $amount, 
            "profit" => 0,
            "cash" => ($cash),
            "momo" => ($momo)
          )
        ));


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


        exit(json_encode(
          array(
            "total" => 0, 
            "profit" => 0,
            "cash" => 0,
            "momo" => 0
          )
        ));


      }


    }else{

      exit(json_encode(
        array(
          "total" => 0, 
          "profit" => 0,
          "cash" => 0,
          "momo" => 0
        )
      ));
    }

  
  }else{

    //http_response_code(401);

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

  }
