<?php
define('NTLCMS', 1);
$module = 'checktask';
include ('../system/start.php');
include ('../system/function.php');
include ('../system/antidos.php');
$page_title = 'Страница проверки выполнения - клик клуба ' . strtoupper($_SERVER['HTTP_HOST']);
include ('../system/head.php');
if (!$active){
header('Location: '.URL);
$_SESSION['fail'] = 'Вы не авторизированы!';
exit;
}
echo '<div class="title"><b>'.razd.' Проверка выполнения</b></div>';
if (isset($_GET['pay_here'])){
$payed = isset($_GET['pay_here']) ? filter_var($_GET['pay_here'], FILTER_SANITIZE_NUMBER_INT) : '0';
$rows = DB :: run() -> query("select (`id_job`) from `jobs_use` where `pay` = '0' and `id` = '$payed'") -> fetch(PDO::FETCH_OBJ);
$task = DB :: run() -> querySingle("select count(`id_job`) from `jobs_use` where `pay` = '0' and `id` = '$payed'");
$payed2 = $rows -> id_job;
if ($task > 0){
$row = DB :: run() -> query("select * from `jobs` where `id` = '$payed2' and `user` = '$ulogin' and `accept` = '1' limit 1") -> fetch(PDO::FETCH_OBJ);
$summa = $row -> pay;
if ($summa <= $_USER['bank']){
$tasks = DB :: run() -> querySingle("select count(`id_job`) from `jobs_use` where `pay` = '0' and `id` = '$payed'");
if ($tasks > 0){
$user = DB :: run() -> query("select (`user`) from `jobs_use` where `pay` = '0' and `id_job` = '$payed2' limit 1") -> fetch(PDO::FETCH_OBJ);
$to = $user -> user;
$per = DB :: run() -> querySingle("select count(`period`) from `jobs` where `pay` = '0' and `id` = '$payed2' limit 1");
DB::run() -> query("update `users` set `bank` = `bank` -? where `login`=?;", array($summa, $ulogin));
DB::run() -> query("update `users` set `bank` = `bank` +? where `login`=?;", array($summa, $to));
DB :: run() -> query("insert into `notice` (`to`, `date`, `status`, `tema`, `text`) values (?, ?, ?, ?, ?)", array($to, TIME, 1, 'Задания юзеров', 'Выполненное вами задание [url='.URL.'/showtask'.$payed.'] № '.$payed.'[/url] оплачено рекламодателем!'));
DB :: run() -> query("update `jobs_use` set `pay` = '1' where `id` = '$payed'");
if ($per == 0){
}
echo '<div class="post"><center><i class="fa fa-check-circle fa-fw"></i> Заявка успешно принята!</center></div>';
}
} else echo '<div class="error"><center><i class="fa fa-close fa-fw"></i> У вас недостаточно средств для оплаты!</center></div>';	
}
}
else
if (isset($_GET['del_here'])){
$payed = isset($_GET['del_here']) ? filter_var($_GET['del_here'], FILTER_SANITIZE_NUMBER_INT) : '0';
$payed2 = DB :: run() -> query("select (`id_job`) from `jobs_use` where `pay` = '0' and `id` = '$payed'") -> fetch(PDO::FETCH_OBJ);
$task = DB :: run() -> querySingle("select (`id_job`) from `jobs_use` where `pay` = '0' and `id` = '$payed'");
if ($task > 0){
$taskes = DB :: run() -> querySingle("select (`pay`) from `jobs` where `id` = '$payed' and `user` = '$ulogin' and `accept` = '1' limit 1");
if ($taskes > 0){
$sum = DB :: run() -> query("select (`pay`) from `jobs` where `id` = '$payed' and `user` = '$ulogin' and `accept` = '1' limit 1") -> fetch(PDO::FETCH_OBJ);
DB :: run() -> query("update `jobs_use` set `pay` = '2' where `id` = '$payed'");
echo '<div class="error"><center><i class="fa fa-check-circle fa-fw"></i> Заявка успешно отменена!</center></div>';
}
}
}
$sql = DB :: run() -> query("select * from `jobs_category`");
while ($data = $sql -> fetch(PDO::FETCH_OBJ)){
$names[$data -> id] = $data -> name;
}
$sql = DB :: run() -> query("select * from `jobs` where `user` = '$ulogin'");
$task = DB :: run() -> querySingle("select count(`id`) from `jobs_use` where  `pay` = '0' ");
if ($task == 0){
echo '<div class="error"><center><i class="fa fa-close fa-fw"></i> Задания на проверку отсутствуют!</center></div>';
}else{
while ($sdata = $sql -> fetch()){
$sdatas[] = $sdata;
}
for ($i = 0; $i < count($sdatas); $i++){
$wname = $sdatas[$i]['id'];
$sql = DB :: run() -> query("select * from `jobs_use` where `pay` = '0' and `id_job` = '$wname' limit 10");
while($data = $sql -> fetch(PDO::FETCH_OBJ)){
echo '<div class="menu">';	
echo '#'.$sdatas[$i]['id'].'. <b>'.$sdatas[$i]['name'].'</b> (Оплата '.money($sdatas[$i]['pay']).')<br/>';
echo '<i class="fa fa-list-alt fa-fw"></i> <b>Сообщение:</b> '.$data -> message;
echo '<br/><i class="fa fa-folder-o fa-fw"></i> <b>Категория:</b> '.$names[$sdatas[$i]['category']];

echo'<center><a href="'.URL.'/paycheck'.((int) $data -> id).'">Оплатить</a> <b>|</b> <a href="'.URL.'/delcheck'.((int) $data -> id).'"> Отменить</a></center></div>';
echo '</div>';
}
}
}
echo '<div class="block"><i class="fa fa-home fa-fw"></i> <a href="'.URL.'/cabinet">В кабинет</a> | <a href="'.URL.'/tasks">Задания пользователей</a></div>';
include ('../system/foot.php');
?>