<?php

require_once('system/func.php');
auth(); // Закроем от не авторизованных
$title = 'Задания';
require_once('system/header.php');
if (isset($_GET['take'])) {
    $id = num($_GET['take']);
    $quest = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest_user` WHERE `id` = '$id'"));
    $q = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest` WHERE `id` = '$quest[id_quest]'"));
    if ($quest['koll'] >= $quest['max_koll']) {
        $log = 'Задание ' . $q['name'] . ' завершено';
        mysql_query('INSERT INTO `quest_log` ( `user`, `text` , `login`) VALUES ("' . $user['id'] . '", "' . $log . '" , "' . $user['login'] . '")');


        //Кампания
        if ($campaign['id_camp'] == 1) {
            mysql_query("UPDATE `campaign`  SET  `quest`='" . ($campaign['quest'] + 1) . "'  WHERE `id_user`='" . $myID . "'");
        }

        //Кампания
        if ($campaign['id_camp'] == 4) {
            mysql_query("UPDATE `campaign`  SET  `quest`='" . ($campaign['quest'] + 1) . "'  WHERE `id_user`='" . $myID . "'");
        }


        mysql_query("update `quest_user` set `koll` = '0', `last` = '" . (time() + 60 * 60 * 24) . "' where (`id` = '" . $quest['id'] . "')");
        mysql_query("update `users` set `almaz` = '" . ($user['almaz'] + $q['almaz']) . "', `gold` = '" . ($user['gold'] + $q['exp']) . "' where (`id` = '" . $myID . "')");
        $_SESSION['msg'] = 'Задание завершено!<br>Награда: ' . ico('icons', 'almaz.png') . ' ' . $q['almaz'] . ' алмазов и ' . ico('icons', 'gold.png') . ' ' . $q['exp'] . ' золота';


        $quest = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest` WHERE `id` = '523'"));
        $q = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest_user` WHERE `id_quest` = '$quest[id]' AND `id_user` = '$myID'"));
        if ($q['last'] < time()) {
            mysql_query("update `quest_user` set `koll` = `koll`+'1' where (`id` = '" . $q['id'] . "')");
        }


        header('Location: ?');
        exit();
    } else {
        header('Location: ?');
        exit();
    }
}
//echo "<div class='header'> <img src='/images/other/calendar.png'> </span></div>";

$q = mysql_query("SELECT * FROM `quest_user` WHERE `id_user` = '$myID' ORDER BY `koll` DESC, `last` ASC");
while ($post = mysql_fetch_assoc($q)) {
    $quest = mysql_fetch_assoc(mysql_query("SELECT * FROM `quest` WHERE `id` = '$post[id_quest]'"));


    echo "<div class='text_p1'><div class='oh'><center><img src='/images/other/calendar.png'></center></div>";
    if ($post['last'] < time()) {
        $status = ($post['koll'] >= $quest['koll'] ? "<br><font color='lime'>Задание выполнено!</font>" : " <span style = 'float:right;'> $post[koll] / $quest[koll]");

        echo '<span class="almaz">' . $quest['name'] . ' <br></span><small>' . $quest['text'] . '</small><br><font color="lime">Награда:</font><span class="gold"> ' . ico('icons', 'almaz.png') . ' ' . $quest['almaz'] . '  ' . ico('icons', 'gold.png') . ' ' . $quest['exp'] . '</span> ' . $status . '</div>';

        if ($post['koll'] >= $quest['koll']) {
            echo "<center><div class='header'><div class='btn_grey'><span class='cb_end'><span class='cb_ttl'><a class ='llb' href='?take=$post[id]' >Получить награду</div></div></span></span></center></a>";
        }
    } else {
        echo "<span class='almaz'><b>$quest[name]</b></span> <br> Задание завершено ";
    }

    echo "</div>";
}

//echo'<div class="block_c center llb"><small><center><font color="dreen">Задания Обнуляются в 00:00 по московскому времени</font></center></small></div>';

require_once('system/footer.php');
