<?php

    include('../../config/config.php');
    date_default_timezone_set('Africa/Lagos');
    
    // Set CORS headers to allow requests from any origin (*)
    header("Access-Control-Allow-Origin: *");
    
    // Allow specific HTTP methods
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
    
    // Allow specific HTTP headers
    header("Access-Control-Allow-Headers: Content-Type, Authorization");
    
    // Allow cookies to be included in the request (if needed)
    header("Access-Control-Allow-Credentials:true");
        
        
    // SQL query to select categories
    $version = floatval($_POST['version']);
    
    $sql = "SELECT * FROM appupdates WHERE version_code > '$version'";
    $result = mysqli_query($link, $sql);
    $row_cnt = mysqli_num_rows($result);
    
    if ($row_cnt > 0) 
    {
        
        // Create an array to store the categories
        $version = array();
        
        // Fetch and add each category to the array
        while ($row = mysqli_fetch_assoc($result)) {
            $version[] = array(
                'android_app_url' => $row['android_app_url'],
                'ios_app_url' => $row['ios_app_url']
            );
        }
    
    }
    else
    {
        
        // Create an array to store the categories
        $version = array();
        
        // Fetch and add each category to the array
        while ($row = mysqli_fetch_assoc($result)) {
            $version[] = array(
                'android_app_url' => 0,
                'ios_app_url' => 0
            );
        }
    
    }
    
    // Close the database connection (assuming you have already established it)
    
    // Convert the array to JSON
    $json_data = json_encode($version, JSON_PRETTY_PRINT);
    
    // Output the JSON data
    echo $json_data;
    ?>
