<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);


/*The following code will check for pro user status using USER_ID  table */
// array for JSON response (default to failure)
$response = array();
$response["success"] = 0;
$response["message"] = "Failed";

//get json string
$json = file_get_contents('php://input');

//get json array
$jsonArr = json_decode($json, true);

// check for required fields
if ( isset($jsonArr['email']) && isset($jsonArr['pro_status'])) { 
    
     // include db config class
     require_once __DIR__ . '/db_config.php';
     // connecting to db
     $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
     mysqli_set_charset($mysqli, "utf8");

     $email=$jsonArr['email'];
     $pro_status=$jsonArr['pro_status'];

     if(updategopro($mysqli, $email, $pro_status) == "true"){         
    
         $response["success"] = 1;
         $response["message"] = "Success";
         // echoing JSON response
         echo json_encode($response);
 
     }   
     else{ 
         //else send generic failure message
         $response["success"] = 0;
         $response["message"] = "Failed";
         // echoing JSON response
         echo json_encode($response);
     }
     
     mysqli_close($mysqli);
 }                         
 
else{
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}

//function to update USER_ID  Table with PRO status
function updategopro($mysqli, $email, $pro_status){
    $stmt = $mysqli->prepare("UPDATE USER_ID SET PRO=? WHERE EMAIL=?");
    $stmt->bind_param("is", $pro_status, $email);
    $result = $stmt->execute(); 
    
    // Close the prepared statement
    if($stmt != null){
        $stmt->close(); 
    } 
              
    return $result;
  }       


?>