<?php

require 'connect.php';
mysqli_query($dbC, "set @tid=0;");
date_default_timezone_set("Europe/Athens");
$filterCols = ["id","etairia","kwd_sus","monada_paxunshs","elegkths"];
$filters = "";
if (isset($_POST['today']) && $_POST['today'] == 'true') {
    $filters .= " AND hm_elegxou='" .date('Y-m-d')."'";
} 
if(isset($_POST['from']) && $_POST['from']!=""){
	$from = date('Y-m-d',strtotime($_POST['from']));
	$filters .= " AND hm_elegxou>=\"${from}\"";
}
if(isset($_POST['to']) && $_POST['to']!=""){
	$to = date('Y-m-d',strtotime($_POST['to']));
	$filters .= " AND hm_elegxou<=\"${to}\"";
}
if ($_SESSION['susername'] == 'sel' || $_SESSION['susername'] == 'maroudas') {
    $filters .= " AND etairia='ΣΕΛΟΝΤΑ'";
}
if($_POST['filter']!=""){
    $filters .= " AND (";
    foreach($filterCols as $index => $col){
        $filters .= ($index!=0 ? " OR " : "")."${col} LIKE '%{$_POST['filter']}%'";
    }
    $filters.=")";
}
if(isset($_POST['origin']) && $_POST['origin']==2){
    $control = "control_form4";
    $production = "production2";
} else {
    $control = "control_form";
    $production = "production";
}

$baseQuery = "select *, @tid:=@tid+1 as tid from ${control} left join users ON ${control}.elegkths=users.ALogin WHERE isactive=1 ${filters} order by id DESC";
$qry = "select * from (
            ${baseQuery}
        ) cf
        ".($_POST['page']>1 ? " WHERE cf.tid>".( ($_POST['page']-1) * $_POST['limit'] ) : "" )." order by cf.tid ASC limit {$_POST['limit']}";

$res = mysqli_query($dbC, $qry);
$table = "";
while($row=mysqli_fetch_array($res)){
	$parid = "";
	$pardate = "";
	$pareidos = "";
	$pr = mysqli_query($dbC, "SELECT * FROM ${production} WHERE proeleush LIKE '%\"{$row['id']}\"%'");
	while($prow = mysqli_fetch_array($pr)){
		$parid .= ($parid!="" ? "<br /> " : "").$prow['id'];
		$pardate .= ($pardate!="" ? "<br /> " : "").$prow['hm_anaxwrhshs'];
		$pareidos .= ($pareidos!="" ? "<br /> " : "").$prow['proion1'].($prow['proion']!="" ? " - ".$prow['proion'] : "");
	}
	if($row['flag']==1){
		$table.= "<tr class=\"blue\">";
	} elseif($row['apodekth']=="0"){
		$table.= "<tr class=\"red\">";
	} else {
		$table.= "<tr>";
	}
    $table.= "<td>".$row['id']."</td>
	<td>".$row['etairia']."</td>
	<td>".$row['elegkths']."</td>
	<td>".$row['eidos'].$row['megethos']."</td>
	<td>".$row['monada_paxunshs']."</td>
	<td>".date('d-m-Y',strtotime($row['hm_elegxou']))."</td>
	<td>".($row['hm_lixpar']!="" ? date('d-m-Y',strtotime($row['hm_lixpar'])) : ' - ')."</td>
	<td>".$parid."</td>
	<td>".$pardate."</td>
	<td>".$pareidos."</td>
	<td>".((empty($row['picture']) || $row['picture']=="[]") ? '' : '<img src="img/img.png" height="25px" />')."</td>
	<td>".($row['flag']==1? $row['approve_by'] : '')."</td>
	<td>
		<div class='btn-group'>
			<button class='btn btn-default' onclick=\"document.location = 'Cview".($_POST['origin']==2 ? 2 : '').".php?cID=".$row['id']."';\">View</button> 
			<button class='btn btn-default' onclick=\"approve(".$row['id'].");\">Έγκριση</button> 
		</div>";
	$table.= "</td></tr>";
}
$numPages = ceil(mysqli_num_rows(mysqli_query($dbC, $baseQuery)) / $_POST['limit']);
echo json_encode(["table"=>$table, "qry"=>$qry, "numPages"=>$numPages]);


?>