<?
if(isset($_GET['add']) && intval($_GET['add'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `zadaniy` WHERE `id` = '".intval($_GET['add'])."' AND (`level` = '$ku[level]' OR `level` > '".($ku['level']-4)."')"),0)!=0)
{
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `zadaniy` WHERE `id` = '".intval($_GET['add'])."'"));

mysql_query("UPDATE `kolhoz_user` SET `last_zadanie` = '$plant[id_plant]' WHERE `id` = '$ku[id]'");



header("Location:?quest");
}
if($ku['last_zadanie']!=NULL)
{
echo "<div><h3>Текущее задание</h3></div><div class='block'>";

$qq=mysql_query("SELECT * FROM `zadaniy`  WHERE `id_plant` = '$ku[last_zadanie]'");
echo "<ul>";
while($zadanie=mysql_fetch_array($qq))
{
$nada=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id_plant` = '$zadanie[id_plant]' AND `shop` != '1'"));
$post=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$zadanie[id_plant]'"),0);
if(isset($_GET['gotovo']) && $nada['count']>=$zadanie['count'])
{
mysql_query("UPDATE kolhoz_user SET rubies = '".($ku['rubies']+$zadanie['priz'])."' WHERE id = '$ku[id]'");
$countt=$nada['count']-$zadanie['count'];
if($countt!=0)
{
mysql_query("UPDATE `kolhoz_warehouse` SET `count` = '".($nada['count']-$zadanie['count'])."' WHERE `id_user` = '$ku[id]' AND `id_plant` = '$zadanie[id_plant]'");
} else {
mysql_query("DELETE FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id_plant`='$zadanie[id_plant]'");
}
header("Location:?quest");
}

if(isset($_GET['gotovo_all']) && $nada['count']>=$zadanie['count'])
{
$umnoj=$nada['count']/$zadanie['count'];
mysql_query("UPDATE kolhoz_user SET rubies = '".($ku['rubies']+$zadanie['priz']*$umnoj)."' WHERE id = '$ku[id]'");
$countt=$nada['count']-$zadanie['count'];
if($countt!=0)
{
mysql_query("UPDATE `kolhoz_warehouse` SET `count` = '".($nada['count']-$zadanie['count']*$umnoj)."' WHERE `id_user` = '$ku[id]' AND `id_plant` = '$zadanie[id_plant]'");
} else {
mysql_query("DELETE FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id_plant`='$zadanie[id_plant]'");
}
header("Location:?quest");
}
$umnoj=$nada['count']/$zadanie['count'];
echo "<li style='padding-bottom:8px'>";
echo "<img width='48' height='48' src='images/plants/$zadanie[id_plant].png' alt='o' class='portrait'>";
echo "<div class='small'>";
echo "<span>".hsc($post['name'])."</span><br><span class='title'>".($nada['count']==NULL?"0":"$nada[count]")."/$zadanie[count]</span><br>".($nada['count']>=$zadanie['count']?"".($nada['count']>=$zadanie['count']*2?"<img width='16' height='16' src='images/icons/harvest.png' alt='o'> <a href='?quest&gotovo_all'>Получить максимальную награду</a> <span class='title'>(<img width='16' height='16' src='images/icons/ruby.png' alt='o'> ".number_format($zadanie['priz']*$umnoj).")</span>":"<img width='16' height='16' src='images/icons/harvest.png' alt='o'> <a href='?quest&gotovo'>Получить награду</a> <span class='title'>(<img width='16' height='16' src='images/icons/ruby.png' alt='o'> $zadanie[priz])</span>")."":"<span class='minor'>Прибыль:</span> <img width='16' height='16' src='images/icons/ruby.png' alt='o'> <span class='title'>$zadanie[priz]</span>")."</div></li>";
}
echo "<br><span class='minor'>Купленные растения не учитываются! При наличии растения в амбаре и докупке его оно также перестает учитываться! Чтобы растения засчитывались продайте его из амбара и собирайте снова!</span></div>";

}
if(isset($_GET['sort']) && $_GET['sort']<2)
{
mysql_query("UPDATE kolhoz_user SET zad_set = '".intval($_GET['sort'])."' WHERE id = '$ku[id]'");
header("Location:?quest");
}
echo "<div><h3>Задания</h3></div>";
echo '<li class="block">
<label>
<select onchange="location.href=this.value">
  <option>Сортировка</option>
  <option value="?quest&sort=0">Все задания</option>
  <option value="?quest&sort=1">Доступные задания</option>
</select>
</label>
</li>';
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `zadaniy` WHERE `id_plant` != '$ku[last_zadanie]'".($ku['zad_set']!=0?" AND (`level` > '".($ku['level']-4)."' AND `level` < '".($ku['level']+1)."')":null).""),0);
if ($k_post==0)
{
echo "<div class='block'>Список пуст...</div>";
}
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];

$q=mysql_query("SELECT * FROM `zadaniy` WHERE `id_plant` != '$ku[last_zadanie]'".($ku['zad_set']!=0?" AND (`level` > '".($ku['level']-4)."' AND `level` < '".($ku['level']+1)."')":null)." ORDER BY `level` ASC LIMIT $start, $set[p_str]");
echo "<ul>";
while($post=mysql_fetch_array($q))
{
echo "<div class='block'>";
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$post[id_plant]'"));
echo "<li style='padding-bottom:8px'>";
echo "<img width='48' height='48' src='images/plants/$plant[id].png' alt='o' class='portrait'>";
echo "<div class='small'>";
echo "".($ku['level']>=$plant['level'] && $plant['level']>$ku['level']-4?"<a href='?quest&add=$post[id]'>":null)."<span>".hsc($plant['name'])."</span>".($ku['level']>=$plant['level']?"</a>":null)."<br><span class='minor'>Нужно собрать: <img width='16' height='16' src='images/icons/harvest.png' alt='o'> <span class='title'>$post[count]</span><br>Прибыль: <img width='16' height='16' src='images/icons/ruby.png' alt='o'> <span class='title'>$post[priz]</span></span></div></li>";
echo "</div>";
}
echo "</ul>";
if ($k_page>1)str("?quest&",$k_page,$page); // Вывод страниц

include_once 'inc/foot.php';
?>