<?php
session_start();
ob_start();
include("../../pidbco/pidbjoin.php");

$searchemp = strip_tags($_GET['emp']);
$searchdat = strip_tags($_GET['dat']);
  $exp = explode("/",$searchdat);
  $searchdat22 = $exp[0].'-'.$exp[1].'-'.$exp[2]; 
  
  $exp = explode("/",$searchdat);
  $searchdat2 = $exp[2].'-'.$exp[1].'-'.$exp[0]; 
  
  $report_contnt = strip_tags($_GET['rep']);
  $tod_dat = date('d-m-Y h:i a');
  $ipp = $_SERVER['REMOTE_ADDR'];
  
  $fromdatenew = strip_tags($_GET['fromdatenew']);	//add new
  $exp2 = explode("/",$fromdatenew);
  $fromdatenew2 = $exp2[0].'-'.$exp2[1].'-'.$exp2[2]; 
  
  $sqemp2 = mysqli_query($con, "SELECT emp_name FROM `employee_registration` WHERE `id`='$searchemp' ");
  $resemp2 = mysqli_fetch_array($sqemp2);
  $empname = $resemp2['emp_name'];
  
  
   $report_by = $_SESSION['us_staff'];
   
   
    $message = '<div align="center"><u>
		  <strong style="float: left; color:red;"><u>"'. $empname .' :- Daily Tasks With Reports Dated '. $searchdat2 .'" Generated from '. $ipp .' on '. $tod_dat .'</u></strong></u><br><br><table  width="98%" border="1" cellpadding="3" cellspacing="0" >
                    <thead >
                    <tr >
                    <th>#</th>   
                    <th>Date/Time</th>    
                    <th>Age</th>             	
                    <th>From</th>
                    <th>To</th>
                    <th>Task Details</th>
                    <th>Status</th>
                    </tr>
                    </thead>
                    <tbody>';

  //  $select = mysqli_query($con, "SELECT * FROM `p_task` WHERE (`t_status`= '0' AND `t_to`='$searchemp') OR (`t_to`='$searchemp' AND `t_closedat`='$searchdat22')");   
 
  $select = mysqli_query($con, "SELECT * FROM `p_task` WHERE (`t_status`= '0' AND `t_to`='$searchemp') AND `t_dat` between '$fromdatenew2' and '$searchdat22'");    
    while($rstask = mysqli_fetch_array($select)){
   	$empfrom = $rstask['t_from'];
	$empto = $rstask['t_to']; 
	
	
	/* $task_report = $rstask['task_report'];
	if($task_report != ''){
		$task_report2 = "Report: &nbsp;".$task_report;
		$rep_count = "(1)";
	} */
	$task_id = $rstask['tid'];
	$sqw = mysqli_query($con, "SELECT * FROM `p_taswise_rep` WHERE `task_id`='$task_id'");
	$sqw_rwcount= mysqli_num_rows($sqw);
	if($sqw_rwcount != ''){
		$rep_count = "(".$sqw_rwcount.")";
	} else {
		$rep_count = "";
	}
	
	$datetime1 = new DateTime($rstask['t_dat']);
    $datetime2 = new DateTime($rstask['t_closedat']);
    $interval = $datetime1->diff($datetime2);
    $days_between = $interval->format('%a');
   	
   	$sqemp = mysqli_query($con, "SELECT emp_name FROM `employee_registration` WHERE `id`='$empfrom' ");
	$resemp = mysqli_fetch_array($sqemp);
   	$fromemp = $resemp['emp_name'];
	
	$sqemp2 = mysqli_query($con, "SELECT emp_name FROM `employee_registration` WHERE `id`='$empto' ");
	$resemp2 = mysqli_fetch_array($sqemp2);
   	$toemp = $resemp2['emp_name'];
	
	$ttid =$rstask['tid'];
	
	$taskdat = $rstask['t_dat'];
	$exp = explode("-",$taskdat);
	$yearr =  substr($exp[0], 2, 4);
	$taskdate = $exp[2].'/'.$exp[1].'/'.$yearr; 
	$th=$rstask['t_time'];
    $expd4 = explode(" ",$th);               	
	$tdat = $taskdate.'&nbsp;'.$expd4['0'];
	$tactn = $rstask['t_action'];
	if($rstask['t_status'] == 1){ $tstatus="Closed"; } else { $tstatus="Open"; }

           $message .= '<tr>
                    	<td>'. $ttid .'</td>
                    	<td>'. $tdat .' </td>
                    	<td>'. $days_between .'</td>
                    	<td>'. $fromemp .'</td>
                   	    <td>'. $toemp .'</td>
                    	<td>'. "Task : &nbsp;".$tactn .'<span style=" color:#F07747;">'. $rep_count .'</span>';
                    while($resw = mysqli_fetch_array($sqw)){
                    		
                     $message .= '<br><span style=" color:#F07747;">'. "Report : &nbsp;".$resw['reprt_date'].'&nbsp;'.$toemp.' Says : &nbsp;'.$resw['tas_report'] .'</span>';
					 }
					 
				    	 $message .= '</td>
                    	<td>'. $tstatus .'</td>
                    </tr>';	
       }    
	
		  
		
		  $message .= '</tbody>
                    </table>';
					
	   $sq2 = mysqli_query($con, "SELECT * FROM `p_task_report` WHERE `r_emp`='$searchemp' AND `task_emp`='$searchemp' AND `r_date`='$searchdat' ");
	   while($rs2 = mysqli_fetch_array($sq2)){
	   	    $rdate2 = $rs2['rdate'];
			$r_report2 = $rs2['r_report'];
			$r_emp = $rs2['r_emp'];
			$sqemp4 = mysqli_query($con, "SELECT emp_name FROM `employee_registration` WHERE `id`='$r_emp' ");
	        $resemp4 = mysqli_fetch_array($sqemp4);
   	        $empname4 = $resemp4['emp_name'];
					
		  $message .= '<br><br> <div style="clear:both;"></div>
		  <div style="float: left; ">
		  <p style="float: left;"><u style="color:#FF00FF;font-weight: bold;font-size: 14px;">'. $empname4 .': Report Summary of '. $rdate2 .': </u></p><br>
		  <span style="float: left;color:#1303FF;">'. $r_report2 .'</span><br><br></div>';
		}
		
		   	
			
			$message .= '<br><br> <div style="clear:both;"></div>
		  <div style="float: left; ">
		  <p style="float: left;"><u style="color:#FF00FF;font-weight: bold;font-size: 14px;">'. $empname .': Report Summary of '. $searchdat .': </u></p><br>
		  <span style="float: left;color:#1303FF;">'. $report_contnt .'</span><br><br></div>';
			
			
			
			
			
					
		$sq1 = mysqli_query($con, "SELECT * FROM `p_task_report` WHERE `r_emp`!='$searchemp' AND `r_date`='$searchdat' AND `task_emp`='$searchemp'");
		while($rs1 = mysqli_fetch_array($sq1)){
			$rdate = $rs1['rdate'];
			$r_report = $rs1['r_report'];
			$r_emp = $rs1['r_emp'];
			$sqemp3 = mysqli_query($con, "SELECT emp_name FROM `employee_registration` WHERE `id`='$r_emp' ");
	        $resemp3 = mysqli_fetch_array($sqemp3);
   	        $empname3 = $resemp3['emp_name'];
						
			$message .= '
           <br><br> <div style="clear:both;"></div>
           <div style="float: left; ">
           <p style="float: left;"><u style="color:#FF00FF;font-weight: bold; font-size: 14px;">'. $empname3 .': Reply '. $rdate .': </u></p><br>
           <span style="float: left;color:#1303FF;">'. $r_report .'</span><br><br></div>';		
		}
             
		 $message .= '</div>';
		 
		 echo $message;
		 

print "<script>window.print();window.close();</script>";	
	 
?>
  
 