<?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["c"]) ) {

    $error = array('details' => '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 club information

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

      @$clubId = mysql_prep($_GET['c']);

      $clubInfo = find_club_by_id($clubId); 

      if(!isset($clubInfo)){

        $error = array('details' => 'something went wrong');

        exit(json_encode($error));

      }


      //players
      $players  = find_all_club_players($clubInfo->id, 1);

      $clubInfo->players = $players;

      // club statistics
      $generalStat = find_club_by_general_stat_for_club($clubInfo->id, 1);

      $clubInfo->generalStat = $generalStat;


      // other statistics
      $otherStats = find_all_other_club_stats($clubInfo->id, 1);

      $clubInfo->otherStats = $otherStats;


      //club details
      $club = array("details" => $clubInfo);

      exit(json_encode($club));



    }  


  }else{

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


  }

