<?php

$response = array();
$response["emp"] = array();
$response["present"]= array();
$response["leaves"]= array();
require_once __DIR__ . '/db_config.php';


if (isset($_POST["companyname"]) ) {
    
    $companyname = $_POST['companyname'];
    $startdate = $_POST['startdate'];
    $enddate = $_POST['enddate'];
   
    $deleted="no";
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());
    
    $query ="SELECT emailid,mobilenumber,employeename,designation FROM signupdetails WHERE ( companyname = '$companyname' and deleted='$deleted' ) ";
    $result = mysqli_query($con,$query);

    if(!empty($result) && mysqli_num_rows($result) > 0){
	   $response["emp"] = mysqli_fetch_all($result,MYSQLI_ASSOC);
    }
    
    $query ="SELECT date, count(*) as present FROM attendance WHERE companyname = '$companyname' AND date <= '$enddate' AND date >='$startdate' group by date order by date ASC";
    $result = mysqli_query($con,$query);

    if(!empty($result) && mysqli_num_rows($result) > 0){
	   $response["present"] = mysqli_fetch_all($result,MYSQLI_ASSOC);
    }
    
    $query ="SELECT leavefrom,leaveto FROM leavetable WHERE companyname = '$companyname' AND ((leavefrom<='$startdate' and leaveto>='$startdate')  or (leavefrom<='$enddate' and leaveto>='$enddate')) order by leavefrom ASC";
    if(!empty($result) && mysqli_num_rows($result) > 0){
	   $response["leaves"] = mysqli_fetch_all($result,MYSQLI_ASSOC);
    }
    
    mysqli_close($con);
}
echo json_encode($response);

?>