<?php 

include 'db_connect.php';

$varNumberOfDays = 30;

?>
 <div class="col-md-12">
        <div class="card card-outline card-success">
          <div class="card-header">
            <div class="row">
                <div class="col-md-3">
                    <div class="form-group">
                        <label>From Date</label>
                        <input type="date" name="from_date" id="from_date" value="<?php if(isset($_GET['from_date'])){ echo $_GET['from_date']; } ?>" class="form-control">
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label>To Date</label>
                        <input type="date" name="to_date" id="to_date" value="<?php if(isset($_GET['to_date'])){ echo $_GET['to_date']; } ?>" class="form-control">
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group">
                        <label>Users<?php //echo "<pre>"; print_r($_SESSION['login_id']);?></label>
                        
                        <select name="user_id" id="user_id" class="form-control">
                             <option value="">Select users</option>
                            <?php 
                             $role_str = '';
                             if($_SESSION['login_type'] !='1')
                             {
                                 $role_str = "  AND manager_id='".$_SESSION['login_id']."'";
                             }
                            $qry_user = $conn->query("SELECT id, concat(firstname, ' ',lastname) as name  FROM `users` WHERE type!='1' $role_str");
                             if ($qry_user->num_rows > 0)
                             { 
                                while($row_users = $qry_user->fetch_assoc()) 
                                { ?>
                                   <option value="<?=$row_users['id']?>" <?php if(isset($_GET['user_id'])){ echo 'selected'; } ?>><?=$row_users['name']?></option>
                           <?php  }
                             }
                         
                            ?>
                            
                            
                        </select>
                    </div>
                </div>
                
                <div class="col-md-3">
                    <div class="form-group">
                        <label></label> <br>
                        <button type="button" id="filter_btn" class="btn btn-primary" style="margin-top:24px;">Filter Date</button>
                    </div>
                </div>
            </div>
            
            <div class="card-tools">
            	<button class="btn btn-flat btn-sm bg-gradient-success btn-success" id="print"><i class="fa fa-print"></i> Print</button>
            </div>
          </div>
          <div class="card-body p-0">
            <div class="table-responsive" id="printable">
              <table class="table m-0 table-bordered">
               <!--  <colgroup>
                  <col width="5%">
                  <col width="30%">
                  <col width="35%">
                  <col width="15%">
                  <col width="15%">
                </colgroup> -->
                <thead>
                  <th>#</th>
                  <th>User Name</th>
                    <?php
                        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
                            }
                        ?>
                      <td class="text-center">
                      	<?php echo number_format($tprog) ?>
                      </td>
                      <td class="text-center">
                      	<?php echo number_format($cprog) ?>
                      </td>
                      <td class="text-center">
                      	<?php echo number_format($dur).' Hr/s.' ?>
                      </td>
                      <td class="project_progress">
                          <div class="progress progress-sm">
                              <div class="progress-bar bg-green" role="progressbar" aria-valuenow="57" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $prog ?>%">
                              </div>
                          </div>
                          <small>
                              <?php echo $prog ?>% Complete
                          </small>
                      </td>
                      <td class="project-state">
                          <?php
                            if($stat[$row['status']] =='Pending'){
                              echo "<span class='badge badge-secondary'>{$stat[$row['status']]}</span>";
                            }elseif($stat[$row['status']] =='Started'){
                              echo "<span class='badge badge-primary'>{$stat[$row['status']]}</span>";
                            }elseif($stat[$row['status']] =='On-Progress'){
                              echo "<span class='badge badge-info'>{$stat[$row['status']]}</span>";
                            }elseif($stat[$row['status']] =='On-Hold'){
                              echo "<span class='badge badge-warning'>{$stat[$row['status']]}</span>";
                            }elseif($stat[$row['status']] =='Over Due'){
                              echo "<span class='badge badge-danger'>{$stat[$row['status']]}</span>";
                            }elseif($stat[$row['status']] =='Done'){
                              echo "<span class='badge badge-success'>{$stat[$row['status']]}</span>";
                            }
                          ?>
                      </td>
                  </tr>
                <?php endwhile; ?>
                </tbody>  
              </table>
            </div>
          </div>
        </div>
        </div>
    <script>
      $(document).ready(function(){
          progress_report();
          
	  $('#filter_btn').click(function(){
            var from_date = $('#from_date').val();
            var to_date = $('#to_date').val();
            var user_id = $('#user_id').val();
            progress_report(from_date,to_date,user_id);
			
	        });
      
      });
      
      function progress_report(from_date='',to_date='',user_id='')
      {
        //  alert(from_date);
          
         $.ajax({
                url: 'filter_report.php',
                method: 'POST',
                data: {'action':'progress_report','from_date':from_date,'to_date':to_date,'user_id':user_id},
                success: function(response){
                    alert(response);
                   // $('#printable').html(response);
                }
            }); 
      }
     </script>
	 
<script>
	$('#print').click(function(){
		start_load()
		var _h = $('head').clone()
		var _p = $('#printable').clone()
		var _d = "<p class='text-center'><b>Project Progress Report as of (<?php echo date("F d, Y") ?>)</b></p>"
		_p.prepend(_d)
		_p.prepend(_h)
		var nw = window.open("","","width=900,height=600")
		nw.document.write(_p.html())
		nw.document.close()
		nw.print()
		setTimeout(function(){
			nw.close()
			end_load()
		},750)
	})
</script>