<?php 
require 'lock.php';

$task_userid = $_POST['task_userid'] ?? '';
$task_status = $_POST['task_status'] ?? '';
$fromdate    = $_POST['fromdate'] ?? '';
$enddate     = $_POST['enddate'] ?? '';
$searchdata     = $_POST['searchdata'] ?? '';

$where = [];
// Always condition
$where[] = "task_deleted='No'";

// Dynamic conditions
// Status filter
if(!empty($task_status) && $task_status != 'All'){
    $where[] = "task_status='$task_status'";
}

// User filter
if(!empty($task_userid) && $task_userid != 'All'){
    $where[] = "(task_userid='$task_userid' OR task_cby='$task_userid')";
}

if(!empty($fromdate) && !empty($enddate)){
    $where[] = "task_enddate BETWEEN '$fromdate' AND '$enddate'";
}

// 🔍 Search filter (NEW)
if(!empty($searchdata)){
    $searchdata = mysqli_real_escape_string($con, $searchdata);
    $where[] = "(task_title LIKE '%$searchdata%' OR task_detail LIKE '%$searchdata%')";
}

// 👤 Role-based filter
if ($user_type == 'User') {
    $where[] = "task_userid = '$user_id'";
}

// Convert array to string
$where_sql = implode(" AND ", $where);

// Final Query
$gettaskqry = "SELECT task_master.*, user_master.user_name FROM task_master 
INNER JOIN user_master ON user_master.user_id = task_master.task_userid
WHERE $where_sql";
$gettaskres = mysqli_query($con, $gettaskqry);
$gettaskrow = mysqli_num_rows($gettaskres);

?>
<!DOCTYPE html>
<html lang="en">
<?php require 'head.php'; ?>
<body>
    <!-- Loader starts-->
    <div class="loader-wrapper">
      <div class="theme-loader">    
        <div class="loader-p"></div>
      </div>
    </div>
    <!-- Loader ends-->
    <!-- page-wrapper Start-->
    <div class="page-wrapper" id="pageWrapper">
      <?php include 'top.php'; ?>
      <!-- Page Body Start-->
      <div class="page-body-wrapper horizontal-menu">
        <?php include 'header.php'; ?>
        <!-- Page Sidebar Ends-->
        <div class="page-body">
          <div class="container-fluid">
            <div class="page-header">
              <div class="row">
                <div class="col-sm-6">
                  <h3>Task Report</h3>
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index">Home</a></li>
                    <li class="breadcrumb-item active">Task Report</li>
                  </ol>
                </div>
              </div>
            </div>
          </div>
	
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">		  
<style>
.bg-pending {
    background-color: #fff3cd; /* light yellow */
}

.bg-progress {
    background-color: #cce5ff; /* light blue */
}

.bg-hold {
    background-color: #e2e3e5; /* gray */
}

.bg-completed {
    background-color: #d4edda; /* light green */
}

.bg-default {
    background-color: #f8f9fa;
}

.badge-review {
    background-color: #f4b400;
    color: #fff;
}

.bg-review {
    background-color: #fff8e1; /* light golden background */
}

</style>	

          <!-- Container-fluid starts-->
          <div class="container-fluid">
            <div class="row project-cards">
              <div class="col-md-12 project-list">
                <div class="card">
                  <div class="row">
                    <div class="col-md-12 p-0">
					<p> Total Search Record : <?php echo $gettaskrow ?> </p>
                    </div>
                  </div>
                </div>
              </div>
              <div class="col-sm-12" id="printableArea">
                <div class="card">
                  <div class="card-body">
                    <div class="tab-content" id="top-tabContent">
                      <div class="tab-pane fade show active" id="top-all" role="tabpanel" aria-labelledby="all-tab">
                        <div class="row">
						<div class="col-12 col-lg-12 col-xl-12 col-xxl-12">
						<?php
						while($taskgetdata=mysqli_fetch_assoc($gettaskres))
						{
						$task_id = $taskgetdata['task_id'];
						$status = $taskgetdata['task_status'] ?? '';

                        switch($status){
                        case 'Pending':
                        $badgeClass = 'badge-warning';
                        break;
                        
                        case 'In Progress':
                        $badgeClass = 'badge-info';
                        break;
                        
                        case 'On Hold':
                        $badgeClass = 'badge-secondary';
                        break;
						
						case 'Review':
                        $badgeClass = 'badge-review';
                        break;
                        
                        case 'Completed':
                        $badgeClass = 'badge-success';
                        break;
                        
                        default:
                        $badgeClass = 'badge-primary';
                        }
						
                        switch($status){
                        case 'Pending':
                        $bgClass = 'bg-pending';
                        break;
                        
                        case 'In Progress':
                        $bgClass = 'bg-progress';
                        break;
                        
                        case 'On Hold':
                        $bgClass = 'bg-hold';
                        break;
						
						case 'Review':
                        $bgClass = 'bg-review';
                        break;
                        
                        case 'Completed':
                        $bgClass = 'bg-completed';
                        break;
                        
                        default:
                        $bgClass = 'bg-default';
                        }
						
						$encrypted = encryptId($task_id);
						?>
						<div class="project-box <?php echo $bgClass; ?>">
						<span class="badge <?php echo $badgeClass; ?>"><?php echo $taskgetdata['task_status'] ?></span>
						<h6><a href="task-track?task_id=<?php echo $encrypted ?>" class="text-dark">
						<?php echo $taskgetdata['task_title'] ?></a>
						</h6>
						<div class="media">
						<img class="img-20 me-2 rounded-circle" src="assets/img/1.png" alt="" data-original-title="" title="">
						<div class="media-body">
						<p><?php echo $taskgetdata['user_name'] ?></p>
						</div>
						</div>
						<p><?php echo $taskgetdata['task_detail'] ?></p>
						<div class="row details">
						<div class="col-6"><span>Task Priority </span></div>
						<div class="col-6 font-primary"><?php echo $taskgetdata['task_priority'] ?> </div>
						<div class="col-6"><span>Type </span></div>
						<div class="col-6 font-primary"><?php echo $taskgetdata['task_type'] ?> </div>
						<?php
						if($taskgetdata['task_enddate']!='')
						{
						?>
						<div class="col-6"> <span>Task Deadline</span></div>
						<div class="col-6 font-primary"><?php echo date('d-M-Y',strtotime($taskgetdata['task_enddate'])) ?></div>
						<?php
						}

						if($taskgetdata['task_file']!='')
						{
						?>
						<div class="col-6"> <span>File</span></div>
						<div class="col-6 font-primary"><a href="task-image/<?php echo $taskgetdata['task_file'] ?>" target="_blank">
						<i data-feather="file-plus"></i></a></div>
						<?php
						}
						?>
						</div>
						</div>
						<?php
						}
						?>
						</div>
                        </div>
                      </div>
					  <!-- ALL TASK SECTION END -->
					 
					 
                    </div>
                  </div>
                </div>
              </div>
			  
			  <div class="col-md-12">
                <div class="card">
                  <div class="row">
                    <div class="col-md-12 p-0">
					<button type="button" class="btn btn-primary" onclick="printDiv('printableArea')"> <i class="fa fa-download"></i> Generate PDF </button>	
                    </div>
                  </div>
                </div>
              </div>
			  
			  
            </div>
          </div>
          <!-- Container-fluid Ends-->
        </div>
        <?php include 'footer.php'; ?>
		
		 	<script type="text/javascript">

function printDiv(divName) {

     var printContents = document.getElementById(divName).innerHTML;

     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;

}

</script>

  </body>
  
</html>
