<?php 
require 'lock.php';

function checkTaskData($user_id, $role, $type,$con)
{
if($type=='Total')
{
if($role=='Manager')
{
$gettaskqry="select task_id from task_master where task_deleted='No'";
}
else{
$gettaskqry="select task_id from task_master where task_deleted='No' AND (task_userid='$user_id' OR task_cby='$user_id')";
}	
}
else{
if($role=='Manager')
{
$gettaskqry="select task_id from task_master where task_deleted='No' AND task_status='$type'";
}
else{
$gettaskqry="select task_id from task_master where task_deleted='No' AND task_status='$type' AND (task_userid='$user_id' OR task_cby='$user_id')";
}
}
$gettaskres=mysqli_query($con, $gettaskqry);
return $gettaskroe=mysqli_num_rows($gettaskres);
}


function checkTaskDataAll($user_id, $role, $type,$con)
{
$cdate = date('Y-m-d');
if($role=='Manager')
{
$gettaskqry="select task_master.*,user_master.user_name,user_photo from task_master 
inner join user_master ON user_master.user_id=task_master.task_userid 
where task_deleted='No' AND task_enddate <='$cdate' AND task_status!='Completed' order by task_id DESC";
}
else{
$gettaskqry="select task_master.*,user_master.user_name ,user_photo from task_master 
inner join user_master ON user_master.user_id=task_master.task_userid
where task_deleted='No' AND (task_userid='$user_id' OR task_cby='$user_id') AND task_enddate <='$cdate' AND task_status!='Completed' ";
}	

return $gettaskres=mysqli_query($con, $gettaskqry);
}
?>
<!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-->
	
<style>
.bg-pending {
    background-color: #fff3cd; /* light yellow */
}

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

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

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

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

.bg-default {
    background-color: #ffab91;
}
</style>

    <!-- 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">
          <!-- Container-fluid starts-->
          <div class="container-fluid dashboard-default-sec">
            <div class="row">
                  <div class="col-6 col-sm-6 col-md-3 col-xl-2 box-col-3 des-xl-25 rate-sec">
                    <div class="card income-card card-primary bg-default"><a href="task-list?type=total">                             
                      <div class="card-body text-center">                                  
                        <h5><?php echo $totaltask = checkTaskData($user_id, $user_type, "Total", $con); ?></h5>
                        <p class="text-dark">Total Task</p>
                      </div></a>
                    </div>
                  </div>
				  
                  <div class="col-6 col-sm-6 col-md-3 col-xl-2 box-col-3 des-xl-25 rate-sec">
                    <div class="card income-card card-secondary bg-pending"><a href="task-list?type=pending">                                    
                      <div class="card-body text-center">
                        <h5><?php echo $totaltask = checkTaskData($user_id, $user_type, "Pending", $con); ?></h5>
                        <p class="text-dark">Pending Task</p>
                      </div></a>
                    </div>
                  </div>
				  
				  <div class="col-6 col-sm-6 col-md-3 col-xl-2 box-col-3 des-xl-25 rate-sec">
                    <div class="card income-card card-secondary bg-progress"><a href="task-list?type=progress">                                    
                      <div class="card-body text-center">
                        <h5><?php echo $totaltask = checkTaskData($user_id, $user_type, "In Progress", $con); ?></h5>
                        <p class="text-dark">In Progress</p>
                      </div></a>
                    </div>
                  </div>
				  
				  <div class="col-6 col-sm-6 col-md-3 col-xl-2 box-col-3 des-xl-25 rate-sec">
                    <div class="card income-card card-secondary bg-hold"><a href="task-list?type=hold">                                    
                      <div class="card-body text-center">
                        <h5><?php echo $totaltask = checkTaskData($user_id, $user_type, "On Hold", $con); ?></h5>
                        <p class="text-dark">On Hold</p>
                      </div></a>
                    </div>
                  </div>
                  
                  <div class="col-6 col-sm-6 col-md-3 col-xl-2 box-col-3 des-xl-25 rate-sec">
                    <div class="card income-card card-secondary bg-review"><a href="task-list?type=review">                                    
                      <div class="card-body text-center">
                        <h5><?php echo $totaltask = checkTaskData($user_id, $user_type, "Review", $con); ?></h5>
                        <p class="text-dark">Review</p>
                      </div></a>
                    </div>
                  </div>
				  
				  <div class="col-6 col-sm-6 col-md-3 col-xl-2 box-col-3 des-xl-25 rate-sec">
                    <div class="card income-card card-secondary bg-completed"><a href="task-list?type=completed">                                    
                      <div class="card-body text-center">
                        <h5><?php echo $totaltask = checkTaskData($user_id, $user_type, "Completed", $con); ?></h5>
                        <p class="text-dark">Completed</p>
                      </div></a>
                    </div>
                  </div>
				  
				  <div class="col-xl-2 col-md-3 col-sm-6 box-col-3 des-xl-25 rate-sec">
                    <div class="card income-card card-secondary">                                    
                      <div class="card-body text-center">
                        <a class="btn btn-primary mb-1" href="task-add" style="padding:5px 10px;"> Add Task</a>
                        <a class="btn btn-secondary" href="task-list" style="padding:5px 10px;"> Task List</a>
                      </div>
                    </div>
                  </div>
                
            </div>
			  
			  
			<div class="row">
			  <div class="col-sm-12">
                <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">
						<?php
						$taskquery = checkTaskDataAll($user_id, $user_type, "Total", $con);
						if($taskquery && mysqli_num_rows($taskquery) > 0) 
						{
						while($taskgetdata=mysqli_fetch_assoc($taskquery))
						{
						$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);
						
						$task_cby = $taskgetdata['task_cby'];
                        $createduserqry=mysqli_query($con,"select * from user_master where user_id='$task_cby' ");
                        $createduserdata=mysqli_fetch_array($createduserqry);
						?>
						<div class="col-xxl-4 col-lg-6">
						<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="task-image/<?php echo $createduserdata['user_photo']; ?>" alt="" data-original-title="" title="">
						<div class="media-body"><p> Created By :  <?php echo $createduserdata['user_name'] ?></p></div>
						</div>
						
						<div class="media">
						<img class="img-20 me-2 rounded-circle" src="task-image/<?php echo $taskgetdata['user_photo']; ?>" alt="" data-original-title="" title="">
						<div class="media-body"><p> Assigned To :  <?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'])) ?> : <?php echo date('h:i A',strtotime($taskgetdata['task_endtime'])) ?> </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>
						</div>
						<?php
						}
						}
						else{
						?>
						<p> No Task Found </p>
						<?php
						}
						?>
                        </div>
                      </div>
					  <!-- ALL TASK SECTION END -->
					 
					 
                    </div>
                  </div>
                </div>
              </div>   
			</div>
           
          </div>
          <!-- Container-fluid Ends-->
        </div>
        <?php include 'footer.php'; ?>
  </body>
  
</html>