<?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 WIP data from daily_production_summary
$wip_sql = "
    SELECT 
        DAY(record_date) as day,
        DAYNAME(record_date) as day_name,
        vac_wip,
        cnc_wip,
        pu_wip
    FROM daily_production_summary
    WHERE MONTH(record_date) = $month AND YEAR(record_date) = $year
    ORDER BY record_date ASC
";

$wip_result = mysqli_query($conn, $wip_sql);
$wip_data = [];
$previous_total = 0;

while ($row = mysqli_fetch_assoc($wip_result)) {
    $wip_data[$row['day']] = $row;
}

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

    <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: 600px;
        }
        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: #e8e8e8;
        }
        .positive {
            color: #008000;
            font-weight: bold;
        }
        .negative {
            color: #FF0000;
            font-weight: bold;
        }
        .no-data {
            color: #999;
        }
        .date-col {
            background-color: #f8f9fa;
            font-weight: bold;
        }
    </style>

    <div class="container">
        <h2>WIP 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>DAY</th>
                        <th>DAILY SNAPSHOT</th>
                        <th>DAILY + / -</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');
                        
                        $wip = isset($wip_data[$day]) ? $wip_data[$day] : null;
                        
                        // Calculate total WIP (VAC + CNC + PU)
                        $vac_wip = $wip ? floatval($wip['vac_wip']) : 0;
                        $cnc_wip = $wip ? floatval($wip['cnc_wip']) : 0;
                        $pu_wip = $wip ? floatval($wip['pu_wip']) : 0;
                        
                        $daily_snapshot = $vac_wip + $cnc_wip + $pu_wip;
                        
                        // Calculate daily difference
                        $daily_difference = 0;
                        if ($daily_snapshot > 0 && $previous_total > 0) {
                            $daily_difference = $daily_snapshot - $previous_total;
                        }
                        
                        // Update previous total for next iteration
                        if ($daily_snapshot > 0) {
                            $previous_total = $daily_snapshot;
                        }
                        
                        $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><?php echo $day_name; ?></td>
                            <td>
                                <?php 
                                if ($daily_snapshot > 0) {
                                    echo '£ ' . number_format($daily_snapshot, 2);
                                } else {
                                    echo '<span class="no-data">-</span>';
                                }
                                ?>
                            </td>
                            <td>
                                <?php 
                                if ($daily_difference != 0) {
                                    $class = $daily_difference > 0 ? 'positive' : 'negative';
                                    $sign = $daily_difference > 0 ? '' : '-';
                                    echo '<span class="' . $class . '">£ ' . $sign . number_format(abs($daily_difference), 2) . '</span>';
                                } else {
                                    echo '<span class="no-data">-</span>';
                                }
                                ?>
                            </td>
                        </tr>
                    <?php endfor; ?>
                </tbody>
            </table>
        </div>
    </div>
