<?
include_once 'inc/garden/q_trees.php';
include_once 'inc/garden/new_garden.php';
include_once 'inc/garden/change_soil.php';
include_once 'inc/garden/level.php';
include_once 'inc/garden/level_all.php';
if(isset($_GET['moderate'])){
include_once 'inc/garden/sell_all_tree.php';
}
include_once 'inc/garden/sell_animal.php';
include_once 'inc/garden/buy_animal.php';
include_once 'inc/garden/soil.php';
include_once 'inc/garden/soil_all.php';
include_once 'inc/garden/money.php';
include_once 'inc/garden/money_all.php';
if(isset($_SESSION['err']))
{
echo err_game("Не хватает овощей в амбаре");
unset($_SESSION['err']);
}

require_header ('Сады');

echo "<div class='content'>";
echo "<div class='block'>";
if(!isset($_GET['moderate'])){
if($ku['scarecrow_time']>$time)echo "<li><img width='16' height='16' src='images/icons/scarecrow.png' alt='o'> <a class='epic' href='/shop-scarecrow'>Пугало</a><span class='minor'> на <span>".time_left($ku['scarecrow_time']-$time)."</span></span></li>";
if($ku['dog_time']>$time)echo "<li><img width='16' height='16' src='images/icons/dog.png' alt=''> <a class='epic' href='/shop-dog'>Собака</a><span class='minor'> $ku[dog_type]% на <span>".time_left($ku['dog_time']-$time)."</span></span></li>";
if($ku['time_shmel']>$time)echo "<li><img width='16' height='16' alt='o' src='images/icons/heli1n.png'> <a class='epic' href='/shop-helicopter/1'>Шмель</a><span class='minor'> на <span>".time_left($ku['time_shmel']-$time)."</span></span></li>";
if($ku['time_drakon']>$time)echo "<li><img width='16' height='16' alt='o' src='images/icons/heli3n.png'> <a class='epic' href='/shop-helicopter/3'>Дракон</a><span class='minor'> на <span>".time_left($ku['time_drakon']-$time)."</span></span></li>";
if($ku['last_soil_garden']!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_plant` WHERE `id` = '$ku[last_soil_garden]'"),0)!=0)$lfid=mysql_fetch_array(mysql_query("SELECT `id` FROM `kolhoz_warehouse` WHERE `id_plant` = '$ku[last_soil_garden]' AND `id_user`='$ku[id]'"));
} else {
if($sell_sum>0)echo "<li><img width='16' height='16' src='images/icons/arrow.png' alt='*'><a href='/mygarden/moderate/sell_all'>Продать все деревья</a>  за <span><img width='16' height='16' src='images/icons/ruby.png' alt='*'> 
<span class='title'>$sell_sum</span></span></li>";
if($buy_sum>0)echo "<li><img width='16' height='16' src='images/icons/arrow.png' alt='*'><a href='/mygarden/moderate/buy_all'>Купить во все сады ".hsc($_SESSION['buy_tree_name'])."</a>  за <span><img width='16' height='16' src='images/icons/ruby.png' alt='*'> 
<span class='title'>$buy_sum</span></span></li>";
}

if($ku['avtofooder']>0 && !isset($_GET['moderate']))
{
$q=mysql_query("SELECT * FROM `kolhoz_garden` WHERE `id_user` = '$ku[id]' AND `id_pet` != '0'");
$count_gol=0;
while($post=mysql_fetch_array($q))
{
$max_time=$time+$post['level']*(3600*4);
$mezh_time=$max_time-$post['time'];
if($mezh_time>=4*3600)$count_gol++;
}
if($count_gol!=0)echo "<div><img width='16' height='16' src='images/icons/autosoil.png' alt='o'> <a href='mygarden/food_all'>Удобрить все</a></div>";
}

if($ku['avtoconveyor']>0 && !isset($_GET['moderate']))
{
$q=mysql_query("SELECT * FROM `kolhoz_garden` WHERE `id_user` = '$ku[id]' AND `id_pet` != '0'");
$money2=0;
while($post=mysql_fetch_array($q))
{
$pet=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_tree` WHERE `id` = '$post[id_pet]'"));
$d_money=floor($post['time_money2']/(4*3600));
if($d_money>0)
{
$nt=$post['time_money2']-($d_money*14400);
$count=1;
$count_conveyor_act=0;
$count_vet_act=0;
if(isset($avtoconveyor_act))$count_conveyor_act=($count/100)*$avtoconveyor_act;
if(isset($vet_act))$count_vet_act=($count/100)*$vet_ps_m;
$count=$count_conveyor_act+$count_vet_act+$count;

$money=($count*$pet['money'])*$d_money;
$money2=$money2+$money;
}
}
if($money2>0)echo "<div><img width='16' height='16' src='images/icons/money.png' alt='o'> <a href='mygarden/money_all'>Продать все</a> за <span><img width='16' height='16' src='images/icons/money.png' alt='*'> <span
class='title'>".(strkol($money2))."</span></span></div>";
}
echo "<div class='ptm'><ul>";
if($ku['compact']==1)$ku['str']=5;
if($ku['compact']==0)$ku['str']=500;
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_garden` WHERE `id_user` = '$ku[id]'"),0); 
if ($k_post==0) 
{ 
echo "Список пуст..."; 
} 
$k_page=k_page($k_post,$ku['str']); 
$page=page($k_page); 
$start=$ku['str']*$page-$ku['str']; 
$num=($page-1)*$ku['str']; 
$q=mysql_query("SELECT * FROM `kolhoz_garden` WHERE `id_user` = '$ku[id]' ORDER BY `id` DESC LIMIT $start, $ku[str]"); 
while($post=mysql_fetch_array($q))
{
$pet=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_tree` WHERE `id` = '$post[id_pet]'"));
echo "<li><img width='48' height='48' src='".($post['id_pet']==0?"images/sad_slot.png":"images/tree/$pet[id].png")."' alt='' class='portrait'>";
echo "<div>";
if($post['id_pet']==0)
{
echo "<img alt='' width='16' height='16' src='images/icons/bay_tree.png'> ".(isset($_SESSION['buy_tree'])?"<a href='/mygarden/buy-$post[id]/select-".hsc($_SESSION['buy_tree'])."'>Купить (".hsc($_SESSION['buy_tree_name'])." <img alt='' width='16' height='16' src='images/icons/ruby.png'>".hsc($_SESSION['buy_tree_cena']).")</a>, <a href='/mygarden/buy-$post[id]'>выбрать саженец</a>":"<a href='/mygarden/buy-$post[id]'>Купить дерево</a>")."";
}
else
{
if(!isset($_GET['moderate']))
{
echo "<span class='title'>".hsc($pet['name'])."</span>";
if($post['time']>$time)echo "<span class='small minor'> (Удобрено на ".time_left($post['time']-$time).")</span>";
}
if(isset($_GET['moderate']))
{
echo "<li><img alt='' width='16' height='16' src='images/icons/ruby.png'> 
<a href='/mygarden/sell-$post[id]'>Продать дерево</a> за <span><img width='16' height='16' src='images/icons/ruby.png' alt='*'> 
<span class='title'>".(floor($pet['cena']/3))."</span></span></li>";
if($post['level']<20)
{
$post['next_level']=$post['level']+1;
$money_all_levels=0;
for($kp=$post['next_level'];$kp<=20;$kp++)
{
$money_all_levels=$money_all_levels+$kp;
}
if(!empty($id_action) AND $id_action==1){$money_all_levels=round($money_all_levels/2,2);$post['next_level']=round($post['next_level']/2,2);}
echo "<li><img alt='' width='16' height='16' src='images/icons/up.png'> <a href='/mygarden/level-$post[id]'>Увеличить ёмкость</a>
 за <span><img width='16' height='16' src='images/icons/ruby.png' alt='o'> <span class='title'>$post[next_level] тыс.</span></span>
<span class='small minor'>(с $post[level] до $post[next_level])</span></li>
<li><img alt='' width='16' height='16' src='images/icons/up.png'> <a href='/mygarden/level_all-$post[id]'>Увеличить ёмкость</a> за 
<span><img width='16' height='16' src='images/icons/ruby.png' alt='o'> <span class='title'>$money_all_levels тыс.</span></span>
<span class='small minor'>(с $post[level] до 20)</span></li><li class='pt'></li>";
}
}
else
{
echo "</div><div>";
$d_money=floor($post['time_money2']/(4*3600));
if($d_money>0)
{
$nt=$post['time_money2']-($d_money*14400);
$count=1;
$count_conveyor_act=0;
$count_vet_act=0;
if(isset($avtoconveyor_act))$count_conveyor_act=($count/100)*$avtoconveyor_act;
if(isset($vet_act))$count_vet_act=($count/100)*$vet_ps_m;
$count=$count_conveyor_act+$count_vet_act+$count;

$money=($count*$pet['money'])*$d_money;
echo "<img width='16' height='16' src='images/icons/money.png' alt='*'> <a class='' href='/mygarden/money-$post[id]'>Продать</a> за <span><img width='16' height='16' src='images/icons/money.png' alt='o'> <span
class='title'>".(strkol($money))."</span></span>";
}
else
{
$max_time=$time+$post['level']*(3600*4);
$mezh_time=$max_time-$post['time'];
if($mezh_time>=4*3600)echo "<img width='16' height='16' src='images/icons/tomato.png' alt='*'> <a class='' href='mygarden/food-$post[id]".(!empty($lfid['id'])?"/select-$lfid[id]":null)."'><span>Удобрить</span></a>";
}
}
}
//echo "</div>";
echo "<div style='clear:both'></div></li>";
}
if ($k_page>1)str("/?mygarden&",$k_page,$page); // Вывод страниц 

if($mygarden<20 || ($ku['level']>=30 && $mygarden<25) || ($ku['level']>=50 && $mygarden<35) || ($ku['level']>=60 && $mygarden<40))echo "<li><img width='48' height='48' src='images/garden_new.png' alt='*' class='portrait'><div><span class='title'>Новый сад</span></div><div><img width='16' height='16' src='images/icons/cart.png' alt='*'> <a href='/mygarden/new_garden'>Купить</a>
за <span class='title'><img width='16' height='16' src='images/icons/money.png' alt=''> <span>".strkol(cpm($mygarden))."</span> монет</span></div><div style='clear:both'></div></li>";
echo "</ul></div><ul class='ptm'></ul>";

if($ku['last_soil_garden']!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_plant` WHERE `id` = '$ku[last_soil_garden]'"),0)!=0)$lf=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$ku[last_soil_garden]'"));

echo "<div class='ptm'><img width='16' height='16' src='images/icons/tomato.png' alt=''> <a href='/mygarden/change_soil'>Выбрать удобрение</a><span>".(isset($lf)?": ".hsc($lf['name'])."":null)."</span>
<li><img width='16' height='16' src='images/icons/sad.png' alt=''> <a href='/help-tree'>Саженцы</a></li>
 <img width='16' height='16' src='images/icons/refresh.png' alt=''> <a href='/mygarden/reflesh/".rand(99999,99999999)."'>Обновить</a> <img alt='o' width='16' height='16' src='images/icons/moderate.png'> <a href='/mygarden/moderate'>Управление</a></div>";
include_once 'inc/foot.php';
?>