<?php
session_start();
error_reporting(1);
include('../includes/config.php');
include('../includes/db.php');

if (strlen($_SESSION['alogin']) == 0) {
    header('location: ../index.php');
    
    die();
}


if(!isset($_GET['orderId']) || empty($_GET['orderId']) || !is_numeric($_GET['orderId'])){
    header('location: ../server_copy.php');
    $_SESSION['Error'] = "ভুল রিকুয়েস্ট";
    die();
}

$orderId = $_GET['orderId'];
//get user banalce
$userAmountSql = "SELECT balance FROM users where email = (:email) LIMIT 1";
$userAmountQuery = $dbh->prepare($userAmountSql);
$userAmountQuery->bindParam(':email',$_SESSION['alogin'],PDO::PARAM_STR);
$userAmountQuery->execute();
$userAmountDetail = $userAmountQuery->fetchAll(PDO::FETCH_OBJ);

if($userAmountQuery->rowCount() <= 0){
    header('location: ../server_copy.php');
    $_SESSION['Error'] = "আপনার একাউন্ট এ পর্যাপ্ত ব্যালেন্স নেই।";
    die();
}

if($userAmountDetail[0]->balance < 10){
    header('location: ../server_copy.php');
    $_SESSION['Error'] = "আপনার একাউন্ট এ পর্যাপ্ত ব্যালেন্স নেই।";
    die();
}


//get order info
//get user banalce
$userDownloadSql = "SELECT * FROM server_copy where id = (:id) LIMIT 1";
$userDownloadQuery = $dbh->prepare($userDownloadSql);
$userDownloadQuery->bindParam(':id',$orderId,PDO::PARAM_STR);
$userDownloadQuery->execute();
$userDownloadDetail = $userDownloadQuery->fetchAll(PDO::FETCH_OBJ);

if($userDownloadQuery->rowCount() <= 0){
    header('location: ../server_copy.php');
    $_SESSION['Error'] = "অর্ডার খুজে পাওয়া যায়নি।";
    die();
}


if($userDownloadDetail[0]->status != 2){
    header('location: ../server_copy.php');
    $_SESSION['Error'] = "আপনি ভুল রিকুয়েস্ট করেছেন।";
    die();
}



//minas 10 taka
if($userDownloadDetail[0]->total_download >=10){
    $sqlBalanceUpdate = "UPDATE `users` SET `balance` = balance - 10 WHERE `users`.`email` = (:email)";
    $queryBalanceUpdate = $dbh->prepare($sqlBalanceUpdate);
    $queryBalanceUpdate->bindParam(':email',$_SESSION['alogin'],PDO::PARAM_STR);
    $queryBalanceUpdate->execute();

}
//update download
$sqlDownloadUpdate = "UPDATE `server_copy` SET `total_download` = total_download + 1 WHERE `server_copy`.`id` = (:id)";
$queryDownloadUpdate = $dbh->prepare($sqlDownloadUpdate);
$queryDownloadUpdate->bindParam(':id',$orderId,PDO::PARAM_STR);
$queryDownloadUpdate->execute();




?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Download</title>
</head>
<body>
    <script>
        function downloadFile() {
        var fileUrl = '../a-dmin/serverCopy/<?php echo $userDownloadDetail[0]->url ?>';

        var link = document.createElement('a');
        link.href = fileUrl;
        link.download = fileUrl.split('/').pop(); // Set the filename

        // Simulate a click event to trigger the download
        link.dispatchEvent(new MouseEvent('click'));
        }

        // Call the function to initiate the download
        downloadFile();

        <?php
            $_SESSION['Success'] = "ফাইল ডাউনলোড হয়েছে।";
        ?>

        location.href = "../server_copy.php";
    </script>
</body>
</html>
