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




if(!isset($_SESSION['jay_proj_mngmnt']) && empty($_SESSION['jay_proj_mngmnt']))
{
    echo '<script type="text/javascript">alert("You Want To Login Now.."); window.location.href="../Login";</script>';
}

/* --------- Add Task Register  -------- */

if(isset($_POST['add_new_taskreg'])){
	
	$taskfrom = strip_tags($_POST['taskfrom']);
	$taskto = strip_tags($_POST['taskto']);
	$taskaction = strip_tags($_POST['taskaction']);
	$taskstatus = strip_tags($_POST['taskstatus']);
	date_default_timezone_set('Asia/Calcutta');

	$dat = date('Y-m-d');
	$tim = date('h:i a', time());
	$taskadd = mysqli_query($con, "INSERT INTO `p_task` (`t_from` ,`t_to` ,`t_action` , `t_status`, `t_time`,`t_dat`) VALUES ('$taskfrom', '$taskto', '$taskaction', '$taskstatus', '$tim', '$dat')");
	
}
/* -------- Delete Task Register ------- */

if(isset($_POST['btn_delete_taskk'])){
	$id = strip_tags(base64_decode($_POST['id']));
	$sqdel = mysqli_query($con, "DELETE FROM `p_task` WHERE `tid` = '$id'");	
}
/* ------ Edit Task Register  ------ */

if(isset($_POST['edit_new_taskreg'])){
	
	$taskfrom   = strip_tags($_POST['taskfrom']);
	$taskto     = strip_tags($_POST['taskto']);
	$taskaction = strip_tags($_POST['taskaction']);
	$taskstatus = strip_tags($_POST['taskstatus']);
	$transferto = strip_tags($_POST['transferto']);
	$taskreport = strip_tags($_POST['taskreport']);
	$hidvall = strip_tags($_POST['hidvall']);
	$log_pers = $_SESSION['us_staff'];
	
	if($taskstatus == 2){
	   $dat = date('Y-m-d');
	   $tim = date("h:i a");
	   
	   $report_dat = date('d/m/Y h:i a');
       if($taskreport != ''){
          $insert = mysqli_query($con, "INSERT INTO `p_taswise_rep` (`task_id` ,`log_id` ,`tas_report` ,`reprt_date`)
VALUES ('$hidvall', '$log_pers', '$taskreport', '$report_dat')");
	 
	   }
	     $upp = mysqli_query($con, "UPDATE `p_task` SET `t_from` = '$taskto',
`t_to` = '$transferto',`t_action` = '$taskaction',`t_status` = '$taskstatus',`t_sign` = '$tasksign', `t_closedat`='$close_dat', `t_trans_to`='$transferto',`task_report`='$taskreport',`tas_reprt_dat`='$report_dat' WHERE `tid` = '$hidvall' ");
 
	} else {
		
	if($taskstatus != 0) {
	   $close_dat = date('Y-m-d');
	}
		
	$tasksign   = $_POST['tasksign'];
	$report_dat = date('d/m/Y h:i a');
    if($taskreport != ''){
    $insert = mysqli_query($con, "INSERT INTO `p_taswise_rep` (`task_id` ,`log_id` ,`tas_report` ,`reprt_date`)
VALUES ('$hidvall', '$log_pers', '$taskreport', '$report_dat')");
	 
	}
	
	$upp = mysqli_query($con, "UPDATE `p_task` SET `t_from` = '$taskfrom',
`t_to` = '$taskto',`t_action` = '$taskaction',`t_status` = '$taskstatus',`t_sign` = '$tasksign', `t_closedat`='$close_dat', `t_trans_to`='$transferto',`task_report`='$taskreport',`tas_reprt_dat`='$report_dat' WHERE `tid` = '$hidvall' ");
 
 }

}






/*  --- Add InterConnected Task Register  --- */
if(isset($_POST['add_new_interconct'])){
		
	$interconnect = $_POST['interconnect'];	
	$idd = $_POST['idd'];
	$sq1 = mysqli_query($con, "UPDATE `p_task` SET `t_connect` = '$interconnect' WHERE `tid` = '$idd'");
}


/* ----- Reminder ----------- */
if(isset($_POST['add_new_reminder'])){
	$reminderdat = $_POST['reminderdat'];
	$idd = $_POST['idd'];
	$sq1 = mysqli_query($con, "UPDATE `p_task` SET `remind_set` = '$reminderdat' WHERE `tid` = '$idd'");
}


/* ------ Create Report -------- */

if(isset($_POST['create_reports'])){
				
$searchemp = strip_tags($_POST['searchemp']);		
$searchdat = strip_tags($_POST['searchdat']);	

$ddat = date('Y/m/d');
$yesturday = date('Y/m/d',(strtotime ( '-1 day' , strtotime ( $ddat) ) ));

$exp = explode("/",$searchdat);
$searchdat2 = $exp[0].'-'.$exp[1].'-'.$exp[2]; 
	
$select = mysqli_query($con, "SELECT * FROM `p_task` WHERE (`t_status`= '0' AND `t_to`='$searchemp') OR ( `t_to`='$searchemp' AND `t_closedat`='$searchdat2')"); 
$selec_count = mysqli_num_rows($select);	

if($selec_count == 0){ ?>
 <span style=" color: red;"><?php echo "No Reports."; ?></span>	
<?php } else { ?>
	
<div class="box" >
   <div class="box-body" style=" overflow: auto;  " >
        <div class="box-body table-responsive no-padding" >
        		
	 <table id="example1" class="table table-bordered table-striped" >
                    <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>
         <?php 
      
        
    while($rstask = mysqli_fetch_array($select)){
   	$empfrom = $rstask['t_from'];
	$empto =   $rstask['t_to'];
	
	$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'];
   ?>           	
                  <tr>
                    	<td><?php echo $rstask['tid']; ?></td>
                    	<td><?php echo $rstask['t_dat']; ?> &nbsp; <?php echo $rstask['t_time']; ?></td>
                    	<td><?php echo $days_between; ?></td>
                    	<td><?php echo $fromemp; ?></td>
                   	    <td><?php echo $toemp; ?></td>
                    	<td><?php echo $rstask['t_action']; ?><span style=" color:#F07747;"><?php echo $rep_count; ?></span>
                    		<?php while($resw = mysqli_fetch_array($sqw)){ ?>
                            <br><span style=" color:#F07747;"><?php echo "Report : &nbsp;".$resw['reprt_date'].'&nbsp;'.$toemp.' Says : &nbsp;'.$resw['tas_report']; ?></span>
					         <?php } ?>
                    	</td>
                    	<td><?php if($rstask['t_status'] == 1){ echo "Closed"; } else { echo "Open"; } ?></td>
                  </tr>	
  <?php } ?>              	
                    </tbody>
                    </table>
</div></div></div>
<?php 
$sqs = mysqli_query($con, "SELECT * FROM `p_task_report` WHERE `r_emp`='$searchemp' AND `r_date`='$searchdat'");
while($res3 = mysqli_fetch_array($sqs)){
?>  
 <div class="col-md-12" style=" background-color: #D3D3D3; margin-top: 3px;" >
    <div class="col-md-10"  style=" padding-left: 2px; padding-right: 0px; ">	
       <div class="form-group">
    <small class="label label-warning" > <?php echo $res3['rdate']; ?> </small>  <br>
     	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $res3['r_report']; ?> 
</div></div></div>
<?php } ?>


<?php if(($searchdat == $ddat) || ($searchdat == $yesturday)){ ?>
  <div class="col-md-12" style="margin-top: 10px;" >
    <div class="col-md-10"  style=" padding-left: 2px; padding-right: 0px; ">					
         <div class="form-group">
          <textarea name="report_contnt" id="report_contnt" placeholder="Report" class="form-control"  style=" height: 100px;"></textarea>	
  </div></div>
  
    <div class="col-md-2"  style=" padding-left: 2px; padding-right: 0px; ">					
         <div class="form-group">
   <input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Save " onclick="report_sent_save();" style=" float: right; width: 100px; height: 28px;" />
  <!-- <input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Save & Email" onclick="report_sent();" style=" float: right; width: 100px; height: 29px; margin-top: 4px;" /> -->
  <a href="" data-toggle="modal" data-target="#myModal9data9" style=" cursor: pointer;">
  <input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Save & Email" style=" float: right; width: 100px; height: 28px; margin-top: 4px;" />
  </a>
  
  <input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Print " onclick="report_print();" style=" float: right; width: 100px; height: 28px; margin-top: 4px;" />
  
  
    </div></div></div>   	 
    <?php } ?>
                    	
<?php	
} }


if(isset($_POST['report_sent_save'])){
	
  $searchemp = strip_tags($_POST['searchemp']);
  $searchdat = strip_tags($_POST['searchdat']);
  $report_contnt = strip_tags($_POST['report_contnt']);
  $tod_dat = date('d-m-Y h:i a');
  $report_by = $_SESSION['us_staff'];
  
  $sqins = mysqli_query($con, "INSERT INTO `p_task_report` (`r_emp` ,`task_emp`,`r_date` ,`r_report` ,`rdate`)
VALUES ('$report_by','$searchemp', '$searchdat', '$report_contnt', '$tod_dat')");
  
  
  
}
















if(isset($_POST['report_sent_email'])){
	
  $searchemp = strip_tags($_POST['searchemp']);
  $searchdat = strip_tags($_POST['searchdat']);
  
  $exp = explode("/",$searchdat);
  $searchdat22 = $exp[0].'-'.$exp[1].'-'.$exp[2]; 
  
  $exp = explode("/",$searchdat);
  $searchdat2 = $exp[2].'-'.$exp[1].'-'.$exp[0]; 
  
  $report_contnt = strip_tags($_POST['report_contnt']);
  $tod_dat = date('d-m-Y h:i a');
  $ipp = $_SERVER['REMOTE_ADDR'];
  
  $sqemp2 = mysqli_query($con, "SELECT emp_name FROM `employee_registration` WHERE `id`='$searchemp' ");
  $resemp2 = mysqli_fetch_array($sqemp2);
  $empname = $resemp2['emp_name'];
	
  $emailset1 = strip_tags($_POST['emailset1']);
  $emailset2 = strip_tags($_POST['emailset2']);
  $emailset3 = strip_tags($_POST['emailset3']);
  
  $cemail = '';
  if($emailset1 != ''){
  	$cemail .=$emailset1;
  } 
  if($emailset2 != ''){
  	$cemail .=','.$emailset2;
  }
  if($emailset3 != ''){
  	$cemail .=','.$emailset3;
  }
  
  $report_by = $_SESSION['us_staff'];
  $sqins = mysqli_query($con, "INSERT INTO `p_task_report` (`r_emp` , `task_emp`,`r_date` ,`r_report` ,`rdate`,`email1`,`email2`,`email3`)
  VALUES ('$report_by', '$searchemp','$searchdat', '$report_contnt', '$tod_dat','$emailset1','$emailset2','$emailset3')");
  $last_insert = mysqli_insert_id($con);
  
  $cemail  = $cemail;
          
          $headers  = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\nFrom: Task Report <programerstest@gmail.com>\r\n";

$headers .= "X-Mailer: PHP/" . phpversion();
$headers .= "X-Priority: 1" . "\r\n"; 
             $cemail2 = 'programerstest@gmail.com';		
		  $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')");    
    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; 
	
	$tdat = $taskdate.'&nbsp;'.$rstask['t_time'];
	$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>';
		}
		
		   			
		$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>';
 	     
 	     $subject = "$empname :- Daily Tasks With Reports Dated $searchdat2 Generated from $ipp on $tod_dat ";  
	     
	     @mail($cemail,$subject,$message,$headers,"-f $cemail2");
	 
	 
       
       
       
       
      


  	
 } 
 
 
 if(isset($_POST['btn_change_pass'])){
	
   $pwd_old = strip_tags(sha1($_POST['pwd_old']));
   $pwd_new = strip_tags(sha1($_POST['pwd_new']));
   
   $sqq= mysqli_query($con, "SELECT * FROM `pi_log_data` WHERE `pass`='$pwd_old' ");
   $rss= mysqli_num_rows($sqq);
      if($rss != 0){
           $sq = mysqli_query($con,"UPDATE `pi_log_data` SET `pass` = '$pwd_new' WHERE `pass` = '$pwd_old'");
           echo "Password Changed Successfully";
      } else {
	       echo "Invalid Password";
      }
 } 
 
 
 
 
 
 
 /* ---- Todo List Search Result ---- */
 
 if(isset($_POST['create_todoresult'])){
				
$search_todo = strip_tags($_POST['search_todo']);	
$idd = $_POST['idd'];	
$select = mysqli_query($con, "SELECT * FROM `p_task` WHERE `t_action` LIKE '%$search_todo%' "); 
$selec_count = mysqli_num_rows($select);	
if($selec_count == 0){ ?>
 <span style=" color: red;"><?php echo "No Reports."; ?></span>	
<?php } else {
	?>
<div class="box" >
   <div class="box-body" style=" overflow: auto;  " >
        <div class="box-body table-responsive no-padding" >
        		
	 <table id="example11" class="table table-bordered table-striped" >
                    <thead >
                    <tr >
                    <th></th>	
                    <th>#</th>   
                    <th>Date/Time</th>                 	
                    <th>From</th>
                    <th>To</th>
                    <th>Todo</th>
                    <th>Status</th>
                    </tr>
                    </thead>
                    <tbody>
         <?php 
      
     $k=1;    
    while($rstask = mysqli_fetch_array($select)){
   	$empfrom = $rstask['t_from'];
	$empto = $rstask['t_to'];
   	
   	$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'];
   ?>           	
                    <tr>
                    	<td><input type="checkbox" name="selector[]" id="ad_Checkbox1<?php echo $k; ?>" class="ads_Checkbox" value="<?php echo $rstask['tid']; ?>" /></td>
                    	<td><?php echo $rstask['tid']; ?></td>
                    	<td><?php echo $rstask['t_dat']; ?> &nbsp; <?php echo $rstask['t_time']; ?></td>
                    	<td><?php echo $fromemp; ?></td>
                   	    <td><?php echo $toemp; ?></td>
                    	<td><?php echo $rstask['t_action']; ?></td>
                    	<td><?php if($rstask['t_status'] == 1){ echo "Closed"; } else { echo "Open"; } ?></td>
                    </tr>	
      <?php  $k= $k+1; } ?>              	
                    	
                    </tbody>
                    </table>
</div></div>
<input type="submit" class="btn btn-primary" name="frasubmit" id="frasubmit" value="Submit" style=" float: right;margin-right: 5px; " onclick="add_interconnect('<?php echo $idd; ?>');" />
</div>
   	 
                    	
<?php	
} }
 
 
 
if(isset($_POST['add_inter_conn_todo'])){
	
	$regno = $_POST['regno'];
	$idd = $_POST['idd'];
	$sqq = mysqli_query($con, "UPDATE `p_task` SET `t_connect` = '$regno' WHERE `tid` = '$idd'");
	
} 




/* ------ Transfer All ------ */

if(isset($_POST['transfer_all']))
{
	
	$taskfrom = $_POST['taskfrom'];
	$taskto   = $_POST['taskto'];
	
	$sqemp = mysqli_query($con, "SELECT tid FROM `p_task` WHERE `t_to`='$taskfrom' AND `t_status`='0' ");
	while($resemp = mysqli_fetch_array($sqemp)){
			
		$tid = $resemp['tid'];
		
		$upp = mysqli_query($con, "UPDATE `p_task` SET `t_from` = '$taskfrom',
       `t_to` = '$taskto',`t_status` = '0', `t_trans_to`='$taskfrom' WHERE `tid` = '$tid' ");
	}
}



 ?>
