<?php

function get_table($id)
{
	include("config.php");
	$s = "<table cellpadding=10 border=2>";
	$s .= "<tr><th>Дата</th><th>Начало рабочего дня</th><th>Конец рабочего дня</th><th>Кол-во генеральных уборок</th><th>Кол-во текущих уборок</th><th>Кол-во заездов</th><th>Сумма оплаты за день</th></tr>";
	$res1 = mysqli_query($mysql,"SELECT c.start, c.end, sum(case when d.work=1 then 1 else 0 end) as w1, sum(case when d.work=2 then 1 else 0 end) as w2, sum(case when d.work=3 then 1 else 0 end) as w3,sum(d.bed*30 + d.towels*10 + f.price) as price FROM statistics as c left join statistics as d on c.start < d.start and c.end > d.end left join rooms as e on e.id = d.room left join prices as f on f.room_type = e.type and f.work = d.work WHERE c.work=0 and c.staff='".$id."' group by c.start");
	while($all = mysqli_fetch_array($res1))
	{
		$s .= "<tr><td class=link>".substr($all["start"],0,strpos($all["start"]," "))."</td><td>".$all["start"]."</td><td>".$all["end"]."</td><td>".$all["w2"]."</td><td>".$all["w3"]."</td><td>".$all["w1"]."</td><td>".$all["price"]."</td></tr>";
	}
	$s .= "</table>";
	return $s;
}

function get_day($date)
{
	include("config.php");
	$s = "<table cellpadding=10 border=2>";
	$s .= "<tr><th>Начало уборки</th><th>Конец уборки</th><th>Тип уборки</th><th>Номер</th><th>Категория номера</th><th>Корпус</th><th>Сумма оплаты</th></tr>";
	$res1 = mysqli_query($mysql,"SELECT c.start,c.end,f.name as work,d.num,g.name as build,d.type,sum(c.bed*30 + c.towels*10 + e.price) as price FROM `statistics` as c left join `rooms` as d on c.room = d.id left join `prices` as e on e.room_type = d.type and e.work = c.work left join `works` as f on f.id = c.work left join `builds` as g on g.id = d.build WHERE c.start like '2020-09-02%' and c.work != 0 group by c.start");
	while($all = mysqli_fetch_array($res1))
	{
		$s .= "<tr><td>".$all["start"]."</td><td>".$all["end"]."</td><td>".$all["work"]."</td><td>".$all["num"]."</td><td>".$all["type"]."</td><td>".$all["build"]."</td><td>".$all["price"]."</td></tr>";
	}
	$s .= "</table>";
	return $s;
}
	
if($_GET["id"]!="")
{
	echo get_table($_GET["id"]);
	
	?>

	<script>
	$('.link, .unlink').click(function(){
		var a = $(this);
		if(a.attr('class') == 'link')
			$.ajax({url:'.', type:'get', data:'date='+a.html(), cache:false, async:false, success:function(d) {
				a.parent().after("<tr><td colspan=7 align=center>"+d+"</td></tr>");
				}
			});
		else
			a.parent().next().remove();
		a.toggleClass('link unlink');
	});
	</script>
	
	<?php

	die();
}
	
if($_GET["date"]!="")
{
	echo get_day($_GET["date"]);
	die();
}
	
?>

<html>
<head>
<style>
.link, .unlink {cursor:pointer; text-decoration:underline; color:blue;}
</style>
</head>
<body>
	
<?php
include("config.php");


	echo "<select id=users><option disabled selected>Выберите пользователя</option>";
	$res1 = mysqli_query($mysql,"SELECT `id`,`name` FROM `users` WHERE 1");
	while($all = mysqli_fetch_array($res1))
	{
		echo "<option value='".$all["id"]."'>".$all["name"]."</option>";
	}
	echo "<select><br><br>";
	
?>
	
<div id='table'></div><br>
<a href='code.php'>Код</a>
<script src='jquery-3.2.1.min.js'></script>
<script>
$("#users").change(function(){
	var a = $("#users").val();
	$.ajax({url:".", type:'get', data:"id="+a, cache:false, async:false, success:function(d) {$("#table").html(d);}});
});
</script>
</body>
</html>