<?php
include("config.inc.php");
if(isset($_SESSION['user']) && $_SESSION['user'] == $admin_user){
	//
} else {
	header("location:login.php");
	exit;
}
include("header.php");
//
/*
$sql = "SELECT * FROM company WHERE STATUS_CODE = '1' ORDER BY COMPANY_ID ASC";

$result = $conn->query($sql);
*/
?>
<!--
<form name="c" action="">
  <label for="Companies">Companies:</label>
  <select name="company" id="company">
  <option value="">Select company</option>
<?php
	if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc())
		 {
      
        echo '<option value="'.$row['COMPANY_ID'].'">'.$row['COMP_NAME'].'</option>';
      
     }
    }
?>
  </select>
  <br><br>
  <input type="submit" value="Submit">
</form>
-->
<div style="padding-top: 50px;" ></div>
<?php
if(isset($_GET['company']) && $_GET['company'] != "") {
echo "<h1>".getComName($_GET['company'])."</h1>";

if(isset($department) && $department != ""){
    //DEPT_ID
    $sql = "SELECT * FROM EMPLOYEE WHERE COMPANY_ID = '".$_GET['company']."' AND DEPT_ID = '".$department."' ORDER BY EMPLOYEE_ID ASC";
    $result = $conn->query($sql);
}
else {
    $sql = "SELECT * FROM EMPLOYEE WHERE COMPANY_ID = '".$_GET['company']."' ORDER BY EMPLOYEE_ID ASC";
    $result = $conn->query($sql);
}

if(isset($_GET['employee']) && $_GET['employee'] != "") {
    $employee = $_GET['employee'];
    $employee_name = getEmpName($employee);
    $x = $_GET['from'];
    $y = $_GET['to'];
}
else {
    $employee = "";
    $employee_name = "Select Employee";
    $x = "";
    $y = "";
}
?>

<form name="e" action="" class="form-inline">
<div class="input-group">
  <select name="employee" id="employee" class="form-control">
  <option value="<?php echo $employee; ?>"><?php echo $employee_name; ?></option>
<?php
if(isset($department) && $department != ""){
    echo '<option value="all">All Employees - '. getDepName($department,$_GET['company']) .'</option>';
}
	if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc())
		 {
      
        echo '<option value="'.$row['EMPLOYEE_ID'].'">'.$row['FULL_NAME'].'</option>';
      
     }
    }

?>
  </select>
  <span class="input-group-addon"> </span>
  <input type="date" id="from" name="from" class="form-control" value="<?php echo $x; ?>" >
  <span class="input-group-addon">-</span>
  <input type="date" id="to" name="to" class="form-control" value="<?php echo $y; ?>">
  <input type="hidden" name="company" value="<?php echo $_GET['company']; ?>" />
  <input type="hidden" name="department" value="<?php echo $department; ?>" />
  <span class="input-group-btn">
  <button class="btn btn-primary" type="Submit" value="Submit" ><i class="fa fa-search"></i> Search</button>
  </span>
</div>
</form>
<br>

<?php
}
?>

      
        
<?php
if(isset($_GET['employee']) && $_GET['employee'] != "") {
    $x = $_GET['from'];
    $y = $_GET['to'];

    if($x == "" || $y == ""){

      echo '<div class="alert alert-warning">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
      <strong>Warning!</strong> Date range required!
    </div>';
    }

    if(isset($department) && $_GET['employee'] == "all") {

      $sqlx = "SELECT * FROM EMPLOYEE WHERE COMPANY_ID = '".$_GET['company']."' AND DEPT_ID = '".$department."' ORDER BY EMPLOYEE_ID ASC";
      $resultx = $conn->query($sqlx);

      if ($resultx->num_rows > 0) {

        while($rowx = $resultx->fetch_assoc())
         {
          
            //echo '<option value="'.$rowx['EMPLOYEE_ID'].'">'.$rowx['FULL_NAME'].'</option>';

          ?>
<div class="panel">
<div class="panel-heading">
          <h3 class="panel-title"><?php echo getEmpName($rowx['EMPLOYEE_ID']) ?></h3>
          <p><?php echo $x." - ".$y; ?></p>
        </div>
        <div class="panel-body">
          <div class="table-responsive">

          <table class="table table-bordered table-primary nomargin">
                  <thead>
                    <tr>
                      <th width="25%">Date</th>
                      <th width="20%" class="text-left">Time</th>
                      <th width="45%" class="text-left">Reason</th>
                      <th width="10%" class="text-center">Direction</th>
                    </tr>
                  </thead>
          </table>
<?php
    
    $sql = "SELECT ATT_DATE FROM ATTENDANCE WHERE EMPLOYEE_ID = '".$rowx['EMPLOYEE_ID']."' AND (CAST(ATT_DATE AS DATETIME)  BETWEEN '$x' AND '$y') GROUP BY ATT_DATE
     UNION SELECT ATT_DATE FROM ATTENDANCE_LOG WHERE EMPLOYEE_ID = '".$rowx['EMPLOYEE_ID']."' AND (CAST(ATT_DATE AS DATETIME)  BETWEEN '$x' AND '$y') GROUP BY ATT_DATE ORDER BY ATT_DATE";
    $result = $conn->query($sql);


    if ($result->num_rows > 0) {
        
        while($row = $result->fetch_assoc())
             {
          
                //echo $row['ATT_DATE_TIME']."<br>";
                $sql2 = "SELECT * FROM ATTENDANCE WHERE EMPLOYEE_ID = '".$rowx['EMPLOYEE_ID']."' AND ATT_DATE = '".$row['ATT_DATE']."' ORDER BY ATT_TIME ASC";
                $result2 = $conn->query($sql2);

                echo "<table class='table nomargin table-primary table-striped'>";
                while($row2 = $result2->fetch_assoc())
                {
                   //echo $row2['ATT_DATE_TIME']."<br>";
                   
                   if($row2['DIRECTION'] == 0)
                   {
                       if($row2['ATT_DATE_TIME']==NULL){
                        $direction = '<button type="button" class="btn btn-warning btn-block disabled">OUT</button>';
                       }
                       else {
                        $direction = '<button type="button" class="btn btn-primary btn-block disabled">IN</button>';
                       }
                        
                   }
                   else {
                    if($row2['ATT_DATE_TIME']==NULL){
                        $direction = '<button type="button" class="btn btn-primary btn-block disabled">IN</button>';
                       }
                       else {
                        $direction = '<button type="button" class="btn btn-warning btn-block disabled">OUT</button>';
                       }
                   }
                    $day = strtoupper(date("D", strtotime($row2['ATT_DATE'])));
                   echo "<tr><td width='25%'>".$row2['ATT_DATE']." ". $day."</td>
                   <td width='20%'>".$row2['ATT_TIME']."</td>
                   <td width='45%'> - </td>
                   <td width='10%'>".$direction."</td></tr>";
                }

                echo "</table>
                <table class='table nomargin table-warning table-striped'>";
                //echo $row['ATT_DATE_TIME']."<br>";
                $sql3 = "SELECT * FROM ATTENDANCE_LOG WHERE EMPLOYEE_ID = '".$rowx['EMPLOYEE_ID']."' AND ATT_DATE = '".$row['ATT_DATE']."'";
                $result3 = $conn->query($sql3);

                while($row3 = $result3->fetch_assoc())
                {
                   //echo $row2['ATT_DATE_TIME']."<br>";
                   if($row3['DIRECTION'] == 1)
                   {
                    $direction = '<button type="button" class="btn btn-primary btn-block disabled">IN</button>';
                   }
                   else {
                    $direction = '<button type="button" class="btn btn-warning btn-block disabled">OUT</button>';
                   }
                   $day = strtoupper(date("D", strtotime($row3['ATT_DATE'])));
                   echo "<tr><td width='25%'>".$row3['ATT_DATE']." ". $day."</td>
                   <td width='20%'>".$row3['ATT_TIME']." </td>
                   <td width='45%'>". $row3['REASON'] ." | ". $row3['REMARK']."</td>
                   <td width='10%'>".$direction."</td></tr>";
                }

                echo "</table><br>";
            
            
         }
         
        }

?>
</div>
        </div>
<hr>
<br>






<?php
          
         }
        }

    }
    else {
    
    

?>
<div class="panel">
<div class="panel-heading">
          <h3 class="panel-title"><?php echo getEmpName($_GET['employee']) ?></h3>
          <p><?php echo $x." - ".$y; ?></p>
        </div>
        <div class="panel-body">
          <div class="table-responsive">

          <table class="table table-bordered table-primary nomargin">
                  <thead>
                    <tr>
                      <th width="25%">Date</th>
                      <th width="20%" class="text-left">Time</th>
                      <th width="45%" class="text-left">Reason</th>
                      <th width="10%" class="text-center">Direction</th>
                    </tr>
                  </thead>
          </table>
<?php
    
    $sql = "SELECT ATT_DATE FROM ATTENDANCE WHERE EMPLOYEE_ID = '".$_GET['employee']."' AND (CAST(ATT_DATE AS DATETIME)  BETWEEN '$x' AND '$y') GROUP BY ATT_DATE
     UNION SELECT ATT_DATE FROM ATTENDANCE_LOG WHERE EMPLOYEE_ID = '".$_GET['employee']."' AND (CAST(ATT_DATE AS DATETIME)  BETWEEN '$x' AND '$y') GROUP BY ATT_DATE ORDER BY ATT_DATE";
    $result = $conn->query($sql);


    if ($result->num_rows > 0) {
        
        while($row = $result->fetch_assoc())
             {
          
                //echo $row['ATT_DATE_TIME']."<br>";
                $sql2 = "SELECT * FROM ATTENDANCE WHERE EMPLOYEE_ID = '".$_GET['employee']."' AND ATT_DATE = '".$row['ATT_DATE']."' ORDER BY ATT_TIME ASC";
                $result2 = $conn->query($sql2);

                echo "<table class='table nomargin table-primary table-striped'>";
                while($row2 = $result2->fetch_assoc())
                {
                   //echo $row2['ATT_DATE_TIME']."<br>";
                   
                   if($row2['DIRECTION'] == 0)
                   {
                       if($row2['ATT_DATE_TIME']==NULL){
                        $direction = '<button type="button" class="btn btn-warning btn-block disabled">OUT</button>';
                       }
                       else {
                        $direction = '<button type="button" class="btn btn-primary btn-block disabled">IN</button>';
                       }
                        
                   }
                   else {
                    if($row2['ATT_DATE_TIME']==NULL){
                        $direction = '<button type="button" class="btn btn-primary btn-block disabled">IN</button>';
                       }
                       else {
                        $direction = '<button type="button" class="btn btn-warning btn-block disabled">OUT</button>';
                       }
                   }
                    $day = strtoupper(date("D", strtotime($row2['ATT_DATE'])));
                   echo "<tr><td width='25%'>".$row2['ATT_DATE']." ". $day."</td>
                   <td width='20%'>".$row2['ATT_TIME']."</td>
                   <td width='45%'> - </td>
                   <td width='10%'>".$direction."</td></tr>";
                }

                echo "</table>
                <table class='table nomargin table-warning table-striped'>";
                //echo $row['ATT_DATE_TIME']."<br>";
                $sql3 = "SELECT * FROM ATTENDANCE_LOG WHERE EMPLOYEE_ID = '".$_GET['employee']."' AND ATT_DATE = '".$row['ATT_DATE']."'";
                $result3 = $conn->query($sql3);

                while($row3 = $result3->fetch_assoc())
                {
                   //echo $row2['ATT_DATE_TIME']."<br>";
                   if($row3['DIRECTION'] == 1)
                   {
                    $direction = '<button type="button" class="btn btn-primary btn-block disabled">IN</button>';
                   }
                   else {
                    $direction = '<button type="button" class="btn btn-warning btn-block disabled">OUT</button>';
                   }
                   $day = strtoupper(date("D", strtotime($row3['ATT_DATE'])));
                   echo "<tr><td width='25%'>".$row3['ATT_DATE']." ". $day."</td>
                   <td width='20%'>".$row3['ATT_TIME']." </td>
                   <td width='45%'>". $row3['REASON'] ." | ". $row3['REMARK']."</td>
                   <td width='10%'>".$direction."</td></tr>";
                }

                echo "</table><br>";
            
            
         }
         
        }

      }
} else {

  if(isset($_GET['company']) && $_GET['company'] != "") {
   
  echo '<div class="alert alert-info fade in nomargin">
  <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
  <h4>Welcome!</h4>
  <p>Please select employee name and date range above.</p>
  <div class="mb15"></div>
 
  </div>';
  } else {

    echo '<div class="alert alert-info fade in nomargin">
    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
    <h4>Welcome!</h4>
    <p>Please select your company name above.</p>
    <div class="mb15"></div>
   
    </div>';
  }
}
?>
          </div>
        </div>
<?php
include("footer.php");
?>
