<?php
session_start();
error_reporting(0);
include('includes/config.php');
include('includes/db.php');
if (strlen($_SESSION['adminlogin']) == 0) {
	header('location:index.php');
    die();
}

if(!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])){
    header('location: serverDash.php?noid');
    die();
}

$id = $_GET['id'];
//get order info
$orderInfoSql = "SELECT * FROM server_copy where id = (:id) LIMIT 1";
$orderInfoQuery = $dbh->prepare($orderInfoSql);
$orderInfoQuery->bindParam(':id',$id,PDO::PARAM_STR);
$orderInfoQuery->execute();


if($orderInfoQuery->rowCount() <= 0){
    header('location: serverDash.php?OrderNotFound');
    die();
}
//get order info
$orderInfoDetail = $orderInfoQuery->fetchAll(PDO::FETCH_OBJ);

if($orderInfoDetail[0]->status != 1){
    header('location: serverDash.php?notDeletable');
    die();
}

// change status for not found
$sqlForUpdate = "UPDATE `server_copy` SET `status` = '3' WHERE `server_copy`.`id` = (:id)";
$queryForUpdate = $dbh->prepare($sqlForUpdate);
$queryForUpdate->bindParam(':id',$id,PDO::PARAM_STR);
$queryForUpdate->execute();


//get cost
$select = "SELECT * FROM control";
$g = mysqli_query($link,$select);
$data=mysqli_fetch_assoc($g);
$ssprice = $data['serverAndSign'];
$refund = $ssprice - 0;

//code for balance add
$sqlBalanceUpdate = "UPDATE `users` SET `balance` = balance + $refund WHERE `users`.`email` = (:email)";
$queryBalanceUpdate = $dbh->prepare($sqlBalanceUpdate);
$queryBalanceUpdate->bindParam(':email',$orderInfoDetail[0]->user,PDO::PARAM_STR);
$queryBalanceUpdate->execute();

//unlink file
//unlink('sainCopy/'.$orderInfoDetail[0]->url);
//redirect
header('location: serverDash.php?successNotFound');
    die();