<?php 
require_once('db_connect.php');
session_start();

$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;
    
}

function get_weekday_name($date) {
    return date("l", strtotime($date));
}

if(isset($_POST['action']) && $_POST['action'] == 'monthly_report') {
    extract($_POST);
    
    $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();
?>
 <div style="font-size: 26px;text-align: center;font-weight: bold;">Monthly Report</div>
<table class="table m-0 table-bordered report-type_monthly>">
    <thead>
        <th>#</th>
        <th>User Name</th>
        <?php
       
        for($i = 0; $i < $varNumberOfDays; $i++) {
            $current_date = date('Y-m-d', strtotime("$from_date +$i days"));
            $weekday_name = get_weekday_name($current_date);
            if(in_array($weekday_name, array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'))) {
        ?>
        <th class="th-day-date">
            <table class="table-day-date">
                <tr>
                    <td class="month-days"><?= $weekday_name ?></td>
                </tr>
                <tr>
                    <td class="month-date"><?= date("j M Y", strtotime($current_date)) ?></td>
                </tr>
            </table>
        </th>
        <?php
            }
        }
        ?>
    </thead>
    <tbody>
    <?php
    if($result->num_rows > 0) {
        $cnt = 0;
        while ($row = $result->fetch_assoc()) {
            ?>
            <tr>
                <td><?= ++$cnt ?></td>
                <td><a><?= get_user_name($row['user_ids']) ?></a></td>
                <?php
                for($i = 0; $i < $varNumberOfDays; $i++) {
                    $current_date = date('Y-m-d', strtotime("$from_date +$i days"));
                    $weekday_name = get_weekday_name($current_date);
                    // Adjust the condition to include week and filter  by ashish pandey
                    if(in_array($weekday_name, array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'))) {
                        $querySql = "SELECT * FROM task_list WHERE start_date = '".$current_date."' AND user_ids='".$row['user_ids']."'";
                        $stmt_rep = $conn->prepare($querySql);
                        $stmt_rep->execute();
                        $result_rep = $stmt_rep->get_result();
                        
                        if($result_rep->num_rows > 0) {
                            $total_time_taken_all = 0;
                            while ($row_rep = $result_rep->fetch_assoc()) {
                                $tasks = unserialize($row_rep['task']);
                                $time_taken_values = unserialize($row_rep['time_taken']); 
                                $total_seconds = 0;
                                
                                foreach ($time_taken_values as $time_taken) {
                                    list($hours, $minutes) = explode(':', $time_taken);
                                    $total_seconds += $hours * 60 + $minutes;
                                }
                                
                                $total_hours = floor($total_seconds / 60);
                                $total_minutes = $total_seconds % 60;
                                $total_time_taken = sprintf('%02d:%02d', $total_hours, $total_minutes);
                                $total_time_taken_cal = floatval($total_time_taken);
                                $total_time_taken_all += $total_time_taken_cal;
                            }
                            echo "<td>" . number_format($total_time_taken_all, 2) . "</td>";
                        } else {
                            echo "<td>0.00</td>";
                        }
                    }
                }
                ?>
            </tr>
            <?php
        }
    }
    ?>
    </tbody>
</table>

<?php
}
?>