<?php
require_once('../wu_init.php');
include('wu_checkadmin.php');
if (!wu_token()) { exit('wu-error'); }
if (!USER_LOGGED) { exit('wu-error'); }
require('../wu-functions/wu_functions.php');

if(isset($_POST['id']) && isset($_POST['reas'])){
if(!empty($_POST['id']) && !empty($_POST['reas'])){
$id = intval($_POST['id']);
$reas = $_POST['reas'];

$cash_out = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT ".DB_PREFIX."_adm_cash_out.id,usr,n,s,dt,ti FROM `".DB_PREFIX."_adm_cash_out` INNER JOIN ".DB_PREFIX."_pay_systems ON ".DB_PREFIX."_adm_cash_out.ps = ".DB_PREFIX."_pay_systems.id WHERE ".DB_PREFIX."_adm_cash_out.id='$id' LIMIT 1"));
if (empty($cash_out['id'])) { exit('0'); }
$usr = $cash_out['usr'];
$sum = $cash_out['s'];

mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_users` SET `bal` = `bal`+'$sum' WHERE uid = '$usr' LIMIT 1");
mysqli_query($connect_db, "DELETE FROM `".DB_PREFIX."_adm_cash_out` WHERE id = '$id' LIMIT 1");

//Отправка уведомления в личный кабинет и уведомления на E-mail
$msg = 'Вывод средств на кошелёк '.$cash_out['n'].' через платёжную систему '.$cash_out['ti'].' был отклонён по следующей причине: '.$reas;
$msg_real = mysqli_real_escape_string($connect_db, $msg);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_notif` (usr,msg,st,dt) VALUES ('$usr','$msg_real','0','$dt')");
wu_email_send_usr($usr, 'Отклонение заявки на вывод', $msg);

exit('1');
} else { exit('wu-error'); }
} else { exit('wu-error'); }
?>