<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");


$id_user = $_SESSION['UserInfo']['id'];
$user_type = $_SESSION['UserInfo']['user_type'];

$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

function getProjectName($id)
{
    global $db;
    $sql = "SELECT proj_name FROM _Projects WHERE id = ".$id;
    if (!$res = $db->SQLexecute($sql)) die($db->JsonError);
    $row = $db->fetchRow($res);
    return $row[0];
}

$sql = "SELECT id_project FROM _www_ids WHERE id_user = " . mysql_real_escape_string($id_user) . " AND user_type = 4 GROUP BY id_project";

if(!$res = $db->SQLexecute($sql)) die($db->JsonError());


$projects = array();
while ($row = $db->fetchRow($res)) {
    $projects[] = getProjectName($row[0]);
}
$data = array();
foreach ($projects as $key => $value) {
    $sql = "SELECT  SUM(CASE WHEN `flag_cli` = 1 THEN 1 ELSE 0 END) AS uploaded,
                    SUM(CASE WHEN `status` = 1 AND `status_rec` = 1 THEN 1 ELSE 0 END) AS Done
            FROM ".$value."_structure WHERE user_id = ". $id_user . " AND user_type = 4";
    if(!$res = $db->SQLexecute($sql)) die($db->JsonError());
    $row = $db->fetchRow($res);
    $data[] = array("project" => $value, "uploaded" => $row[0], "done" => $row[1]);
}



$html = '<table border=0><tr><td style="border:solid;border-color:000000;border-bottom-width:2px;border-top-width:0cm;border-left-width:0cm;border-right-width:0cm"><b>Project Information</b></td></tr></table>';

$html .= '<table border=0 width="100%" style=font-family:verdana;font-size:20px; cellpadding=2 cellspacing=0><tr><td colspan=2>&nbsp;</td></tr>';
    $html .= '<tr>';
        $html .= '<th align="left">';
            $html .= 'Project';
        $html .= '</th>';
        $html .= '<th align=right>';
            $html .= 'Done';
        $html .= '</th>';
        $html .= '<th align=right>';
            $html .= 'Uploaded';
        $html .= '</th>';
    $html .= '</tr>';
foreach ($data as $key => $value) {
    $html .= '<tr>';
        $html .= '<td align="left">';
            $html .= $value['project'];
        $html .= '</td>';
        $html .= '<td align=right>';
            $html .= $value['done'];
        $html .= '</td>';
        $html .= '<td align=right>';
            $html .= $value['uploaded'];
        $html .= '</td>';
    $html .= '</tr>';
}

$html .= '</table>';

echo $html;

?>
