<?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']) || !isset($_GET['status']) || empty($_GET['status']) || !is_numeric($_GET['status'])){
    header('location: signDash.php?noid');
    die();
}

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


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

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

// change status for not found
$sqlForUpdate = "UPDATE `sign` SET `status` = (:status) WHERE `sign`.`id` = (:id)";
$queryForUpdate = $dbh->prepare($sqlForUpdate);
$queryForUpdate->bindParam(':id',$id,PDO::PARAM_STR);
$queryForUpdate->bindParam(':status',$status,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: signDash.php?successUpdate');
    die();