<?php
include_once '../../sys/inc/start.php';
include_once '../../sys/inc/compress.php';
include_once '../../sys/inc/sess.php';
include_once '../../sys/inc/home.php';
include_once '../../sys/inc/settings.php';
include_once '../../sys/inc/db_connect.php';
include_once '../../sys/inc/ipua.php';
include_once '../../sys/inc/fnc.php';
include_once '../../sys/inc/user.php';

$titles='Палаты';
include_once 'head.php';

if (!isset($user)){
echo '</div><div class="major_event"><ul><li><div class="exp_event"><span><center>Чтобы играть надо <a href="/reg.php">зарегистрироваться</a></center></span></div></li></ul></div>';
exit;
}


if (isset($_GET['vipolneno']))echo '</div><div class="major_event"><ul><li><div class="exp_event"><span><center>Успешно выполнено!</center></span></div></li></ul></div><div class="block"> ';

echo '<div>
        <img width="16" height="16" src="icon/refresh.png"/>
        <a href="?">Обновить</a>
    </div>';
$palata = mysql_result(mysql_query("select count(*) from `inter_pal` WHERE  `id_user` = '$user[id]'  LIMIT 1"),0);
if($palata<4){
mysql_query("INSERT INTO `inter_pal` (`id_user`) VALUES  ( '".$user['id']."') ");
mysql_query("INSERT INTO `inter_pal` (`id_user`) VALUES  ( '".$user['id']."') ");
mysql_query("INSERT INTO `inter_pal` (`id_user`) VALUES  ( '".$user['id']."') ");
mysql_query("INSERT INTO `inter_pal` (`id_user`) VALUES  ( '".$user['id']."') ");
}
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `inter` WHERE `id_user` = '$user[id]'"),0) == '0'){
mysql_query("INSERT INTO `inter` (`id_user`) VALUES  ( '".$user['id']."') ");
}

/////////
function sictime($timediff){
$oneMinute=60; 
$oneHour=60*60; 
$oneDay=60*60*24; 
$dayfield=floor($timediff/$oneDay); 
$hourfield=floor(($timediff-$dayfield*$oneDay)/$oneHour); 
$minutefield=floor(($timediff-$dayfield*$oneDay-$hourfield*$oneHour)/$oneMinute); 
$secondfield=floor(($timediff-$dayfield*$oneDay-$hourfield*$oneHour-$minutefield*$oneMinute)); 
if($dayfield>0)$day=$dayfield.'д. ';
if($hourfield>0)$hour=$hourfield.'ч. ';

$time_1="".$day.$hour." ".$minutefield."м. ".$secondfield." сек.";
return $time_1;
}
/////////


if (isset($_GET['palata']))
{
if($inter_user['almaz']>$inter_user['palat_dop']*15){
mysql_query("UPDATE `inter` SET `palat_dop` = '".($inter_user['palat_dop']+1)."', `almaz` = '".($inter_user['almaz']-$inter_user['palat_dop']*15)."' WHERE `id_user` = '".$user['id']."' LIMIT 1");
mysql_query("INSERT INTO `inter_pal` (`id_user`) VALUES  ( '".$user['id']."') ");
header("Location: index.php?vipolneno");
}else
echo '<div class="major_event"><ul><li><div class="exp_event"><span><center>Недостаточно алмазов!</center></span></div></li></ul></div>';
}

if (isset($_GET['osmotr']))
{
$inter_pal=mysql_fetch_array(mysql_query("SELECT * FROM `inter_pal` WHERE `id`='".$_GET['osmotr']."'"));
if($time>$inter_pal['time_dop']&&$inter_pal['time_dop']!=-1){
mysql_query("UPDATE `inter` SET `opit` = '".($inter_user['opit']+20)."' WHERE `id_user` = '".$user['id']."' LIMIT 1");
mysql_query("UPDATE `inter_pal` SET `time_dop` = '-1', `time` = '".($inter_pal['time']-60*20)."' WHERE `id` = '".$_GET['osmotr']."' LIMIT 1");
header("Location: index.php?vipolneno");
}
}

if (isset($_GET['vipis']))
{
$inter_pal=mysql_fetch_array(mysql_query("SELECT * FROM `inter_pal` WHERE `id`='".$_GET['vipis']."'"));
$inter_pac=mysql_fetch_array(mysql_query("SELECT * FROM `inter_pac` WHERE `id`='".$inter_pal['pac']."'"));
if($time>$inter_pal['time']&&$inter_pal['time']!=-1){
mysql_query("UPDATE `inter` SET `opit` = '".($inter_user['opit']+$inter_pac['opit'])."', `monet` = '".($inter_user['monet']+$inter_pac['monet'])."', `almaz` = '".($inter_user['almaz']+5)."' WHERE `id_user` = '".$user['id']."' LIMIT 1");
mysql_query("UPDATE `inter_pal` SET  `pac` = '-1', `time` = '-1' WHERE `id` = '".$_GET['vipis']."' LIMIT 1");
echo '<div class="major_event"><ul><li><div class="exp_event"><span><center>Опыт '.$inter_pac['opit'].', монет +'.$inter_pac['monet'].' и алмаз +5</center></span></div></li></ul></div>';
}
}

if (isset($_GET['ubrat']))
{
$inter_pal=mysql_fetch_array(mysql_query("SELECT * FROM `inter_pal` WHERE `id`='".$_GET['ubrat']."'"));
if($inter_pal['time']==-1){
mysql_query("UPDATE `inter` SET `opit` = '".($inter_user['opit']+50)."' WHERE `id_user` = '".$user['id']."' LIMIT 1");
mysql_query("UPDATE `inter_pal` SET  `time` = '0', `pac` = '0' WHERE `id` = '".$_GET['ubrat']."' LIMIT 1");
header("Location: index.php?vipolneno");
}
}

$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `inter_pal` WHERE `id_user` = '$user[id]'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];

echo "<ul class='delim-list padtop_s'>";
$res = mysql_query("select * from `inter_pal` WHERE `id_user` = '$user[id]'  LIMIT $start, $set[p_str];");
while ($post = mysql_fetch_array($res)){


echo "<li class='padtop_s first-li'>";
if ($post['pac']==-1) echo "<img class='icon' width='48' height='48' src='img/palata/dirtybed.png'>"; elseif ($post['pac']==0) echo "<img class='icon' width='48' height='48' src='img/palata/bed.png'>"; else echo "<img class='icon' width='48' height='48' src='img/pac/$post[pac].png'>";
$inter_pac=mysql_fetch_array(mysql_query("SELECT * FROM `inter_pac` WHERE `id`='".$post['pac']."'"));

echo "<div>";
if ($post['pac']==-1)echo "<span class='patienttitle'>Грязная палата</span>";
if ($post['pac']==0)echo "<span class='patienttitle'>Свободная палата</span><br /><img width='16' height='16' src='icon/diagnosis.png'/><a href='pac.php?id=$post[id]'>Принять пациента</a>";
else {
echo ''.$inter_pac['name'].' <br /></div><div class="smallfont minor" style="margin-left:52px;"><div><span class="smallfont minor">Выписка через :</span> ';
if($time<$post['time'])echo '<span class="ylwtitle">'.sictime($post['time']-$time).'</span></div>'; else echo '<span class="ylwtitle">окончена</span></div>';
if($post['time_dop']!=-1&&$time<$post['time']){
if($time<$post['time_dop'])echo '<span class="smallfont minor">Осмотр через '.sictime($post['time_dop']-$time).'</span></div>'; else echo '<img width="16" height="16" src="icon/eye.png"/> <a href="?osmotr='.$post['id'].'">Осмотреть</a> (+20 опыта,-20м. времени)</div>';
} elseif($time>$post['time']&&$post['time']!=-1) {
echo '<img width="16" height="16" src="icon/diagnosis.png"/><a href="?vipis='.$post['id'].'">Выписать</a> (+'.$inter_pac['opit'].' опыта,+'.$inter_pac['monet'].' монет и +5 алмаза)';
}else if($post['time']!=-1&&$post['time_napit']<=$time)
echo '<div><img width="16" height="16" src="icon/pill.png"/> <a href="pac.php?a=nap&id='.$post['id'].'">Напоить соком</a></div>';
else if($post['time']!=-1)echo '<div><img width="16" height="16" src="icon/pill.png"/> Напоить через : '.sictime($post['time_napit']-$time).'</div>';


}
if ($post['pac']==-1)echo "<img width='16' height='16' src='icon/hand-icon.png'/><a href='?ubrat=$post[id]'>Убрать</a>(+50 опыта)";

echo "<div style='clear: both'></div></li>";
}
echo '<li class="padtop_m">
<div><img class="icon" width="48" height="48" src="img/palata/bed.png"/><span class="drugtitle">Новая палата</span><div><img width="16" height="16" src="icon/cart.png"/><a href="?palata">Купить</a>
за <img width="16" height="16" src="icon/diamond.png"/> <span class="ylwtitle">'.($inter_user['palat_dop']*15).' алмазов</span></div>
<div style="clear: both"></div></div></li>
';
if ($k_page>1)str("?",$k_page,$page); // Вывод страниц


echo "</div>";
echo "</ul>";
include_once 'foot.php';



?>
