<?php

require_once __DIR__ . '/db_config.php';

$response = array();
$response["projects"] = array();
$response["milestones"] = array();
$response["tasklists"] = array();
$response["clients"] = array();
$response["categories"] = array();
$response["tags"] = array();
$response["employees"] = array();
$response["tasks"] = array();
$response["assigns"] = array();



// check for post data
if ( isset($_POST["mail"]) && isset($_POST["cname"]) && isset($_POST["role"]) ) {
$mail= $_POST['mail'];
$cname=$_POST["cname"];
$role=$_POST["role"];

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());


$taskQuery="SELECT * FROM tm_all_tasks WHERE companyname='$cname' ";
$assignQuery="SELECT * FROM tm_all_assign ";
$tasklsitQuery="SELECT * FROM tm_all_tasklists WHERE companyname='$cname'";
$projectQuery="SELECT * FROM tm_all_projects WHERE companyname='$cname'";
$milestoneQuery="SELECT * FROM tm_all_milestones WHERE companyname='$cname' AND milestoneid IS NOT NULL ";
$employeeQuery="SELECT employeename,emailid FROM signupdetails WHERE companyname='$cname'";
$clientQuery="SELECT c_created_by_id AS cby,c_created_by_type as cbytype,c_datetime AS ccreateddatetime,c_company_id as cid,c_company_name as cname,c_responsible_person as rperson,c_responsible_mobile as rpersonmobile,c_responsible_mailid as remail,c_company_address as caddress,c_company_mobile  AS cmobile,c_company_mailid as cemail,c_company_phone as cphone,c_company_address as cweb FROM task_client_company WHERE c_companyname='$cname' ";

$catQuery="SELECT cat_id as catid,task_categoryname as catname,cat_description as catdescription,cat_created_by_mailid as catby,cat_created_by_type as catbytype,cat_datetime as catcreateddatetime FROM task_category WHERE cat_companyname='$cname' ";


if($role!="admin"){
    $taskQuery=$taskQuery." AND (presponsible='$mail' OR employee='$mail')";
    $assignQuery=$assignQuery." WHERE (presponsible='$mail' OR employee='$mail')";
    $tasklsitQuery=$tasklsitQuery." AND (presponsible='$mail' OR employee='$mail')";
    $projectQuery=$projectQuery." AND (presponsible='$mail' OR employee='$mail')";
    $milestoneQuery=$milestoneQuery." AND (presponsible='$mail' OR employee='$mail')";
}

    $taskQuery=$taskQuery." GROUP BY task_id";
    $tasklsitQuery=$tasklsitQuery."  GROUP BY tasklistid";
    $projectQuery=$projectQuery."  GROUP BY  projectid";
    $milestoneQuery=$milestoneQuery." GROUP BY  milestoneid";

/*$taskResult=mysqli_query($con,$taskQuery);
if(!empty($taskResult) && mysqli_num_rows($taskResult) > 0){
    while ($row = mysqli_fetch_array($taskResult,MYSQLI_ASSOC)) {
        array_push($response["tasks"], $row);
    }
}*/

$assignResult=mysqli_query($con,$assignQuery);
if(!empty($assignResult) && mysqli_num_rows($assignResult) > 0){
    while ($row = mysqli_fetch_array($assignResult,MYSQLI_ASSOC)) {
        array_push($response["assigns"], $row);
    }
}

$tasklsitResult=mysqli_query($con,$tasklsitQuery);
if(!empty($tasklsitResult) && mysqli_num_rows($tasklsitResult) > 0){
    while ($row = mysqli_fetch_array($tasklsitResult,MYSQLI_ASSOC)) {
        array_push($response["tasklists"], $row);
    }
}

$projectResult=mysqli_query($con,$projectQuery);
if(!empty($projectResult) && mysqli_num_rows($projectResult) > 0){
    while ($row = mysqli_fetch_array($projectResult,MYSQLI_ASSOC)) {
        array_push($response["projects"], $row);
    }
}

$milestoneResult=mysqli_query($con,$milestoneQuery);
if(!empty($milestoneResult) && mysqli_num_rows($milestoneResult) > 0){
    while ($row = mysqli_fetch_array($milestoneResult,MYSQLI_ASSOC)) {
        array_push($response["milestones"], $row);
    }
}

$clinetResult=mysqli_query($con,$clientQuery);
if(!empty($clinetResult) && mysqli_num_rows($clinetResult) > 0){
    while ($row = mysqli_fetch_array($clinetResult,MYSQLI_ASSOC)) {
        array_push($response["clients"], $row);
    }
}

$employeeResult=mysqli_query($con,$employeeQuery);
if(!empty($employeeResult) && mysqli_num_rows($employeeResult) > 0){
    while ($row = mysqli_fetch_array($employeeResult,MYSQLI_ASSOC)) {
        array_push($response["employees"], $row);
    }
}

$catResult=mysqli_query($con,$catQuery);
if(!empty($catResult) && mysqli_num_rows($catResult) > 0){
    while ($row = mysqli_fetch_array($catResult)) {
        array_push($response["categories"], $row);
    }
}

$tagQuery="SELECT tag_id as tagid,tagname,wieghtage as weightage,tag_created_by_mailid as tabby,tag_created_by_type as tagbytype,tag_datetime as tagcreateddatetime FROM task_tag WHERE tag_companyname='$cname' ";
$tagResult=mysqli_query($con,$tagQuery);
if(!empty($tagResult) && mysqli_num_rows($tagResult) > 0){
    while ($row = mysqli_fetch_array($tagResult)) {
        array_push($response["tags"], $row);
    }
}
echo json_encode($response);

}


?>