<?php

include_once __DIR__ . '/../secrets.php';

header('Content-Type: application/json; charset=utf-8');
include '../db_connect.php'; 

$json = file_get_contents('php://input');
$jsonArray = json_decode($json, true);

$data = $jsonArray[0];
$type = $data['type'];
$operator = $data['operator'];
$key = $data['key'];

$response = array();

$security_key = decrypData($key);

if ($security_key === false) {
    echo 'Invalid encryption data';
    exit;
}

if ($security_key == SECURITY_KEY_PLAIN) {

    $con = dbConnect();
    
    $regularArray = array();
    $regularSql = "SELECT * FROM regular_pack WHERE operator = '$operator' AND type = '$type'";
    $regularResult = mysqli_query($con, $regularSql);

    while ($item = mysqli_fetch_assoc($regularResult)) {
        $regularArray[] = array(
            'id' => $item['id'],
            'title' => $item['title'],
            'amount' => $item['amount'],
            'day' => $item['day'],
            'operator' => $item['operator'],
            'type' => $item['type'],
            'text' => $item['text']
        );
    }
    

    $response[] = array(
        'regularpack' => $regularArray
    );

    echo json_encode($response); 
}else {
    echo json_encode(['error' => 'Invalid key or password']);
}


?>
