<?php
// Get month and year from URL parameters or use current month
$month = isset($_GET['month']) ? intval($_GET['month']) : date('n');
$year  = isset($_GET['year'])  ? intval($_GET['year'])  : date('Y');

// Validate month
if ($month < 1 || $month > 12) {
    $month = date('n');
}

// Get number of days in the month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);

// Fetch standard hours data from daily_production_summary
$std_hours_sql = "
    SELECT 
        DAY(record_date) as day,
        DAYNAME(record_date) as day_name,
        std_hours_vac,
        std_hours_cnc,
        std_hours_pu,
        std_hours_assbly
    FROM daily_production_summary
    WHERE MONTH(record_date) = $month AND YEAR(record_date) = $year
    ORDER BY record_date ASC
";

$std_hours_result = mysqli_query($conn, $std_hours_sql);
$std_hours_data = [];

while ($row = mysqli_fetch_assoc($std_hours_result)) {
    $std_hours_data[$row['day']] = $row;
}

// Daily target (adjust as needed - this is the target for weekdays)
$daily_target = 290.8;

// Month name
$month_name = date('F Y', strtotime("$year-$month-01"));

// Calculate cumulative values
$cumulative_target = 0;
$cumulative_actual = 0;
?>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }
        .container {
            background-color: white;
            padding: 20px;
            width: 100%;
            max-width: 100%;
            min-height: 100vh;
            margin: 0 auto;
        }
        h2 {
            color: #333;
            margin-bottom: 20px;
            text-align: center;
            font-size: 24px;
        }
        .month-selector {
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
            text-align: center;
        }
        .month-selector label {
            margin-right: 10px;
            font-weight: bold;
        }
        .month-selector select {
            padding: 5px 10px;
            margin-right: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .month-selector button {
            padding: 6px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .month-selector button:hover {
            background-color: #0056b3;
        }
        .table-responsive {
            overflow-x: auto;
            margin-top: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            min-width: 800px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
            font-size: 13px;
        }
        th {
            background-color: #4472C4;
            color: white;
            font-weight: bold;
            position: sticky;
            top: 0;
            z-index: 10;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f0f0f0;
        }
        .weekend {
            background-color: #d9d9d9;
        }
        .weekend td:nth-child(2),
        .weekend td:nth-child(3) {
            background-color: #d9d9d9;
        }
        .no-data {
            color: #999;
        }
        .date-col {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .target-col {
            background-color: #d9d9d9;
        }
        .actual-col {
            background-color: #ffffff;
        }
    </style>

    <div class="container">
        <h2>STD HOURS REPORT - <?php echo strtoupper($month_name); ?></h2>
        
        <div class="month-selector">
            <form method="GET" action="">
                <label for="month">Month:</label>
                <select name="month" id="month">
                    <?php for ($m = 1; $m <= 12; $m++): ?>
                        <option value="<?php echo $m; ?>" <?php echo ($m == $month) ? 'selected' : ''; ?>>
                            <?php echo date('F', mktime(0, 0, 0, $m, 1)); ?>
                        </option>
                    <?php endfor; ?>
                </select>
                
                <label for="year">Year:</label>
                <select name="year" id="year">
                    <?php for ($y = date('Y') - 2; $y <= date('Y') + 2; $y++): ?>
                        <option value="<?php echo $y; ?>" <?php echo ($y == $year) ? 'selected' : ''; ?>>
                            <?php echo $y; ?>
                        </option>
                    <?php endfor; ?>
                </select>
                
                <button type="submit">View Report</button>
            </form>
        </div>

        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th>DATE</th>
                        <th>DAILY TARGET</th>
                        <th>CUMV TARGET</th>
                        <th>DAILY ACTUAL</th>
                        <th>CUMV ACTUAL</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    for ($day = 1; $day <= $days_in_month; $day++): 
                        $date_str = sprintf("%04d-%02d-%02d", $year, $month, $day);
                        $day_name = date('l', strtotime($date_str));
                        $is_weekend = ($day_name == 'Saturday' || $day_name == 'Sunday');
                        
                        $std_hours = isset($std_hours_data[$day]) ? $std_hours_data[$day] : null;
                        
                        // Calculate total standard hours (VAC + CNC + PU + assbly)
                        $vac_std = $std_hours ? floatval($std_hours['std_hours_vac']) : 0;
                        $cnc_std = $std_hours ? floatval($std_hours['std_hours_cnc']) : 0;
                        $pu_std = $std_hours ? floatval($std_hours['std_hours_pu']) : 0;
                        $assbly_std = $std_hours ? floatval($std_hours['std_hours_assbly']) : 0;
                        
                        $daily_actual = $vac_std + $cnc_std + $pu_std + $assbly_std;
                        
                        // Calculate cumulative values (skip weekends for target)
                        if (!$is_weekend) {
                            $cumulative_target += $daily_target;
                        }
                        
                        if ($daily_actual > 0) {
                            $cumulative_actual += $daily_actual;
                        }
                        
                        $date_display = str_pad($day, 2, '0', STR_PAD_LEFT) . '-' . date('M', strtotime($date_str));
                        
                        $row_class = $is_weekend ? 'weekend' : '';
                    ?>
                        <tr class="<?php echo $row_class; ?>">
                            <td class="date-col"><?php echo $date_display; ?></td>
                            <td class="target-col">
                                <?php 
                                if ($is_weekend) {
                                    echo '0.0';
                                } else {
                                    echo number_format($daily_target, 1);
                                }
                                ?>
                            </td>
                            <td class="target-col">
                                <?php echo number_format($cumulative_target, 1); ?>
                            </td>
                            <td class="actual-col">
                                <?php echo number_format($daily_actual, 1); ?>
                            </td>
                            <td class="actual-col">
                                <?php echo number_format($cumulative_actual, 1); ?>
                            </td>
                        </tr>
                    <?php endfor; ?>
                </tbody>
            </table>
        </div>
    </div>