<?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 FG stock data from daily_production_summary
$fg_sql = "
    SELECT 
        DAY(record_date) as day,
        DAYNAME(record_date) as day_name,
        fg as daily_snapshot,
        fg_difference as daily_difference
    FROM daily_production_summary
    WHERE MONTH(record_date) = $month AND YEAR(record_date) = $year
    ORDER BY record_date ASC
";

$fg_result = mysqli_query($conn, $fg_sql);
$fg_data = [];

while ($row = mysqli_fetch_assoc($fg_result)) {
    $fg_data[$row['day']] = $row;
}

// Calculate daily target (you can adjust this logic)
$daily_target = 1875.00;

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

// Calculate cumulative values
$cumulative_target = 0;
$cumulative_difference = 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: 1000px;
        }
        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>FG STOCK 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 TARGET</th>
                        <th>CUMV TARGET</th>
                        <th>DAILY SNAPSHOT</th>
                        <th>DAILY + / -</th>
                        <th>CUMV + / -</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');
                        
                        $fg = isset($fg_data[$day]) ? $fg_data[$day] : null;
                        
                        $daily_snapshot = $fg ? floatval($fg['daily_snapshot']) : 0;
                        $daily_difference = $fg ? floatval($fg['daily_difference']) : 0;
                        
                        // Calculate cumulative target (skip weekends)
                        if (!$is_weekend) {
                            $cumulative_target += $daily_target;
                        }
                        
                        // Calculate cumulative difference
                        $cumulative_difference += $daily_difference;
                        
                        $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 ($is_weekend) {
                                    echo '<span class="no-data">-</span>';
                                } else {
                                    echo '£ ' . number_format($daily_target, 2);
                                }
                                ?>
                            </td>
                            <td>£ <?php echo number_format($cumulative_target, 2); ?></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>
                            <td>
                                <?php 
                                if ($cumulative_difference != 0) {
                                    $class = $cumulative_difference > 0 ? 'positive' : 'negative';
                                    $sign = $cumulative_difference > 0 ? '' : '-';
                                    echo '<span class="' . $class . '">£ ' . $sign . number_format(abs($cumulative_difference), 2) . '</span>';
                                } else {
                                    echo '<span class="no-data">-</span>';
                                }
                                ?>
                            </td>
                        </tr>
                    <?php endfor; ?>
                </tbody>
            </table>
        </div>
    </div>