<?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');
}

// Fetch monthly totals from daily_production_summary
$summary_sql = "
    SELECT 
        SUM(std_hours_vac) as total_std_vac,
        SUM(std_hours_cnc) as total_std_cnc,
        SUM(std_hours_assbly) as total_std_assbly,
        SUM(std_hours_pu) as total_std_pu,
        SUM(clock_hours_vac) as total_clock_vac,
        SUM(clock_hours_cnc) as total_clock_cnc,
        SUM(clock_hours_assbly) as total_clock_assbly,
        SUM(clock_hours_pu) as total_clock_pu
    FROM daily_production_summary
    WHERE MONTH(record_date) = $month AND YEAR(record_date) = $year
";

$summary_result = mysqli_query($conn, $summary_sql);
$summary = mysqli_fetch_assoc($summary_result);

// Extract values
$std_vac = floatval($summary['total_std_vac']);
$std_cnc = floatval($summary['total_std_cnc']);
$std_assbly = floatval($summary['total_std_assbly']);
$std_pu = floatval($summary['total_std_pu']);

$clock_vac = floatval($summary['total_clock_vac']);
$clock_cnc = floatval($summary['total_clock_cnc']);
$clock_assbly = floatval($summary['total_clock_assbly']);
$clock_pu = floatval($summary['total_clock_pu']);

// Calculate productivities
$prod_vac = ($clock_vac > 0) ? ($std_vac / $clock_vac) * 100 : 0;
$prod_cnc = ($clock_cnc > 0) ? ($std_cnc / $clock_cnc) * 100 : 0;
$prod_assbly = ($clock_assbly > 0) ? ($std_assbly / $clock_assbly) * 100 : 0;
$prod_pu = ($clock_pu > 0) ? ($std_pu / $clock_pu) * 100 : 0;

// Calculate totals
$total_std = $std_vac + $std_cnc + $std_assbly + $std_pu;
$total_clock = $clock_vac + $clock_cnc + $clock_assbly + $clock_pu;
$total_prod = ($total_clock > 0) ? ($total_std / $total_clock) * 100 : 0;

// 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: 30px;
            max-width: 800px;
            margin: 0 auto;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            border-radius: 5px;
        }
        h2 {
            color: #333;
            margin-bottom: 20px;
            text-align: center;
            font-size: 24px;
        }
        .month-selector {
            margin-bottom: 30px;
            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 {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: center;
            font-size: 14px;
        }
        th {
            background-color: #4472C4;
            color: white;
            font-weight: bold;
        }
        td:first-child {
            text-align: left;
            font-weight: bold;
            background-color: #f8f9fa;
        }
        tr:hover {
            background-color: #f0f0f0;
        }
        .total-row {
            background-color: #e8f4f8;
            font-weight: bold;
        }
        .total-row td {
            background-color: #e8f4f8;
            border-top: 2px solid #333;
        }
    </style>

    <div class="container">
        <h2>MONTHLY PRODUCTIVITY SUMMARY - <?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>

        <table>
            <thead>
                <tr>
                    <th>SECTION</th>
                    <th>STD HOURS</th>
                    <th>Clock Hours</th>
                    <th>Productivity</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>VAC</td>
                    <td><?php echo number_format($std_vac, 1); ?></td>
                    <td><?php echo number_format($clock_vac, 1); ?></td>
                    <td><?php echo number_format($prod_vac, 0); ?>%</td>
                </tr>
                <tr>
                    <td>CNC</td>
                    <td><?php echo number_format($std_cnc, 1); ?></td>
                    <td><?php echo number_format($clock_cnc, 1); ?></td>
                    <td><?php echo number_format($prod_cnc, 0); ?>%</td>
                </tr>
                <tr>
                    <td>ASSY</td>
                    <td><?php echo number_format($std_assbly, 1); ?></td>
                    <td><?php echo number_format($clock_assbly, 1); ?></td>
                    <td><?php echo number_format($prod_assbly, 0); ?>%</td>
                </tr>
                <tr>
                    <td>PU ML</td>
                    <td><?php echo number_format($std_pu, 1); ?></td>
                    <td><?php echo number_format($clock_pu, 1); ?></td>
                    <td><?php echo number_format($prod_pu, 0); ?>%</td>
                </tr>
                <tr class="total-row">
                    <td>TOTAL</td>
                    <td><?php echo number_format($total_std, 1); ?></td>
                    <td><?php echo number_format($total_clock, 1); ?></td>
                    <td><?php echo number_format($total_prod, 0); ?>%</td>
                </tr>
            </tbody>
        </table>
    </div>