<?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($obj['name']) || !isset($obj['description']) || !isset($obj['image']) || !isset($obj['clubId'])  ) {

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

    }


    if( isset($obj['name']) && isset($obj['description']) && isset($obj['image']) && isset($obj['clubId']) ){

      @$name =  mysql_prep($obj['name']);
      @$description =  mysql_prep($obj['description']);

      @$image =  ($obj['image']);
      @$clubId =  mysql_prep($obj['clubId']);

      @$userId =  mysql_prep(@$decodeToken->ID);  clubId

      $status = 1;
      $date = date("Y-m-d");


        $stmt = $connectMe->prepare("UPDATE tbl_club SET name =?, description =?, image =?, date =?, userId =?, status =? WHERE id =? ");  

        $stmt->bindParam(1, $name);
        $stmt->bindParam(2, $description);

        $stmt->bindParam(3, $image);
        $stmt->bindParam(4, $date);

        $stmt->bindParam(5, $userId);
        $stmt->bindParam(6, $status);

        $stmt->bindParam(7, $status);

        if($stmt->execute()){

          $message = array("message"=> "Club updated successfully");
          exit( json_encode($message) ); 

        }else{

          $error = array("error"=> 'Something went wrong, please try again');
          exit( json_encode($error) ); 

          
        }

      
      
      


    }




  }else{

    $error = array("error" => "UNAUTHORIZED");

    exit(json_encode($error));
  }