<?php
require_once("../../autoloader.php");
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header("Access-Control-Allow-Methods: POST");
header("Allow: POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Origin");

$headers = apache_request_headers();
$response = array();
$controller = new SubscriberModel;

date_default_timezone_set('Africa/Lagos');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id = $_POST['user_id'];
    $number = (int)$_POST['page_num'];
    $serviceName = $_POST['servicename'];  // Service name filter
   $startDate = $_POST['start_date']; // Start date filter
$endDate = $_POST['end_date'];     // End date filter
file_put_contents("eyeyeog.txt", $startDate);
// Convert start date
$startDateObject = DateTime::createFromFormat('Y/m/d', $startDate);
$startDateFormatted = $startDateObject->format('Y-m-d');

// Convert end date
$endDateObject = DateTime::createFromFormat('Y/m/d', $endDate);
$endDateFormatted = $endDateObject->format('Y-m-d');
       // End date filter

    // Pass serviceName, startDate, and endDate to the controller
    $result = $controller->getUserTransactionFilter($id, $startDateFormatted, $endDateFormatted);
file_put_contents("eyeyeyeyail_log.txt", $result);
    $response["status"] = "success";

    // Limit the number of transactions if page number is greater than 0
    if ($number > 0) {
        $result = array_slice($result, 0, $number);
    }

   

    $response["transactions"] = $result;
    $modifiedJsonString = json_encode($response, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

    

    echo $modifiedJsonString;
    exit();
}
?>
