<?php 
require_once('db_connect.php');
session_start();
    //ini_set('display_errors', '1');
    //ini_set('display_startup_errors', '1');
    //error_reporting(E_ALL);
    $varNumberOfDays = 31;
    $monthOf30 = array('6','11','4','9');
    
   function get_user_name($user_id)
	{
		GLOBAL $conn;
		
		$query = "SELECT firstname, lastname FROM users WHERE id = '".$user_id."' LIMIT 1";
		$result = mysqli_query($conn, $query);
		$rowcount=mysqli_num_rows($result);
		
		if($rowcount>0)
		{
		  $row   = mysqli_fetch_array($result);
		  $ssfullname = ucfirst($row['firstname'])." ".ucfirst($row['lastname']); 
		}else{
		    $ssfullname  = 'NA';
		}
		
		
		return $ssfullname;
		
	}
if(isset($_POST['action']) && $_POST['action']=='progress_report' )
    {
        extract($_POST);
        //echo "<pre>"; print_r($_POST); die();
        $from_date = $_POST['from_date']; 
        $to_date = $_POST['to_date']; 
        $user_id = $_POST['user_id']; 
        $whrstr = '';
        if(!empty($from_date) && $to_date)
        {
            $whrstr .= " AND t.start_date BETWEEN '$from_date'AND '$to_date'";
            
        }
        if(!empty($user_id))
        {
            
                $whrstr .= " AND t.user_ids = '".$user_id."'";
            
            
        }else{
            if($_SESSION['login_type']=='2')
            {
               $whrstr .=  " AND u.manager_id = '".$_SESSION['login_id']."'";
            }
        }
          $query = "SELECT DISTINCT(user_ids) as user_ids FROM `users` as u INNER JOIN task_list as t ON u.id=t.user_ids WHERE t.status='1' $whrstr";
         
          $stmt = $conn->prepare($query);
          $stmt->execute();
          $result = $stmt->get_result();
         ?>
        <table class="table m-0 table-bordered">
            <thead>
                  <th>#</th>
                  <th>User Name</th>
                    <?php
                    if(in_array(date('m'),$monthOf30 ))
                    {
                     $varNumberOfDays = 30;
                    }
                    if(date('m') == 2)
                    {
                    $varNumberOfDays = 29;
                    }
                        for($i = 0; $i < $varNumberOfDays; $i++) {
                    ?>  <th class="th-day-date">
                        <table class="table-day-date">
                            <tr>
                                <td class="month-days">Week Days</td>
                            </tr>
                            <tr><td class="month-date"><?php echo ($i+1) ?> Apr 2024</td></tr>
                        </table>
                        </th>
                    <?php
                        }
                    ?>
                  <!--<th>Task</th>
                  <th>Completed Task</th>
                  <th>Work Duration</th>
                  <th>Progress</th>
                  <th>Status</th>-->
                </thead>
                <tbody>
                <?php
                $i = 1;
                $stat = array("Pending","Started","On-Progress","On-Hold","Over Due","Done");
                $where = "";
                if($_SESSION['login_type'] == 2){
                  $where = " where manager_id = '{$_SESSION['login_id']}' ";
                }elseif($_SESSION['login_type'] == 3){
                  $where = " where concat('[',REPLACE(user_ids,',','],['),']') LIKE '%[{$_SESSION['login_id']}]%' ";
                }
                 $qry = $conn->query("SELECT p.*,CONCAT(u.firstname, ' ',u.lastname) as uname,u.id FROM users u inner join project_list p on p.user_ids = u.id order 
                  by u.firstname asc");
                while($row= $qry->fetch_assoc()):
                $tprog = $conn->query("SELECT * FROM task_list where project_id = {$row['id']}")->num_rows;
                $cprog = $conn->query("SELECT * FROM task_list where project_id = {$row['id']} and status = 3")->num_rows;
                $prog = $tprog > 0 ? ($cprog/$tprog) * 100 : 0;
                $prog = $prog > 0 ?  number_format($prog,2) : $prog;
                $prod = $conn->query("SELECT * FROM user_productivity where project_id = {$row['id']}")->num_rows;
                $dur = $conn->query("SELECT sum(time_rendered) as duration FROM user_productivity where project_id = {$row['id']}");
                $dur = $dur->num_rows > 0 ? $dur->fetch_assoc()['duration'] : 0;
                if($row['status'] == 0 && strtotime(date('Y-m-d')) >= strtotime($row['start_date'])):
                if($prod  > 0  || $cprog > 0)
                  $row['status'] = 2;
                else
                  $row['status'] = 1;
                elseif($row['status'] == 0 && strtotime(date('Y-m-d')) > strtotime($row['end_date'])):
                $row['status'] = 4;
                endif;
                  ?>
                  <tr>
                      <td>
                         <?php echo $i++ ?>
                      </td>
                      <td>
                          <a>
                              <?php echo ucwords($row['uname']) ?>
                          </a>
                          <br>
                          <small>
                             <b> Project: </b><?php echo ($row['name']) ?>
                          </small>
                      </td>
                      <?php
                            for($i = 0; $i < $varNumberOfDays; $i++) {
                        ?>  <td></td>
                        <?php
                            }
                        ?>
                      
                      
                      
                  </tr>
                <?php endwhile; ?>
                </tbody>  
        </table>
         
         
         
         
           
           <?php
            
    
        
    }
?>