<?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');
$month = isset($_GET['month']) ? intval($_GET['month']) : 1;
$year  = isset($_GET['year'])  ? intval($_GET['year'])  : 2026;

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


$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);

$production_sql = "
    SELECT 
        DAY(record_date) as day,
        vac_wip,
        cnc_wip,
        pu_wip,
        fg,
        fg_difference
    FROM daily_production_summary
    WHERE MONTH(record_date) = $month AND YEAR(record_date) = $year
    ORDER BY record_date ASC
";

$production_result = mysqli_query($conn, $production_sql);
$production_data = [];

while ($row = mysqli_fetch_assoc($production_result)) {
    $production_data[$row['day']] = $row;
}


$sales_sql = "
    SELECT 
        DAY(pp.req_date) as day,
        SUM(pp.req_qty * IFNULL(im.standard_cost_ebq, 0)) AS total_sales
    FROM production_planning pp
    LEFT JOIN item_management im ON im.id = pp.part_id
    WHERE MONTH(pp.req_date) = $month AND YEAR(pp.req_date) = $year
    GROUP BY DAY(pp.req_date)
    ORDER BY pp.req_date ASC
";

$sales_result = mysqli_query($conn, $sales_sql);
$sales_data = [];

while ($row = mysqli_fetch_assoc($sales_result)) {
    $sales_data[$row['day']] = $row['total_sales'];
}

// Fetch RM data if available (you may need to adjust based on your schema)
 $rm_sql = "
    SELECT 
        DAY(record_date) as day,
        rm
    FROM daily_production_summary
    WHERE MONTH(record_date) = $month AND YEAR(record_date) = $year
    ORDER BY record_date ASC
";

$rm_result = mysqli_query($conn, $rm_sql);
$rm_data = [];

while ($row = mysqli_fetch_assoc($rm_result)) {
    $rm_data[$row['day']] = $row['rm'];
}

// Stocktake target (you can adjust this or make it dynamic)
$stocktake_target = 615000;

// Month name
$month_name = date('F Y', strtotime("$year-$month-01"));
?>
    <style>
       
        .container {
    background-color: white;
    padding: 20px;
    border-radius: 0; /* remove rounded corners for full width */
    box-shadow: none; /* remove shadow */
    
    /* FULL SCREEN WIDTH */
    width: 100%;
    max-width: 100%;
    min-height: 100vh;
    margin: 0;
}
        h2 {
            color: #333;
            margin-bottom: 20px;
        }
        .month-selector {
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
        }
        .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 #ccc;
            padding: 8px;
            text-align: center;
            font-size: 14px;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
            position: sticky;
            top: 0;
            z-index: 10;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f0f0f0;
        }
        .over-target {
            background-color: #ffe6e6;
        }
        .under-target {
            background-color: #e6ffe6;
        }
        .no-data {
            color: #999;
        }
    </style>
    <div class="container">
        <h2>Production Summary - <?php echo $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') + 1; $y++): ?>
                        <option value="<?php echo $y; ?>" <?php echo ($y == $year) ? 'selected' : ''; ?>>
                            <?php echo $y; ?>
                        </option>
                    <?php endfor; ?>
                </select>
                
                <button type="submit">View</button>
            </form>
        </div>

        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Sales</th>
                        <th>FG</th>
                        <th>RM</th>
                        <th>VAC WIP</th>
                        <th>CNC WIP</th>
                        <th>PU WIP</th>
                        <th>TOTAL STOCK VALUE</th>
                        <th>Stocktake Target</th>
                    </tr>
                </thead>
                <tbody>
                    <?php for ($day = 1; $day <= $days_in_month; $day++): 
                        $prod = isset($production_data[$day]) ? $production_data[$day] : null;
                        $sales = isset($sales_data[$day]) ? $sales_data[$day] : 0;
                        $rm = isset($rm_data[$day]) ? $rm_data[$day] : 0;
                        
                        $vac_wip = $prod ? floatval($prod['vac_wip']) : 0;
                        $cnc_wip = $prod ? floatval($prod['cnc_wip']) : 0;
                        $pu_wip = $prod ? floatval($prod['pu_wip']) : 0;
                        $fg = $prod ? floatval($prod['fg']) : 0;
                        
                        $total_stock = $fg + $rm + $vac_wip + $cnc_wip + $pu_wip;
                        
                        $row_class = '';
                        if ($total_stock > 0) {
                            $row_class = $total_stock > $stocktake_target ? 'over-target' : 'under-target';
                        }
                        
                        $date_display = $day . '-' . date('M', strtotime("$year-$month-01"));
                    ?>
                        <tr class="<?php echo $row_class; ?>">
                            <td><?php echo $date_display; ?></td>
                            <td><?php echo $sales > 0 ? '£' . number_format($sales, 2) : '<span class="no-data">-</span>'; ?></td>
                            <td><?php echo $fg > 0 ? '£' . number_format($fg, 2) : '<span class="no-data">-</span>'; ?></td>
                            <td><?php echo $rm > 0 ? '£' . number_format($rm, 2) : '<span class="no-data">-</span>'; ?></td>
                            <td><?php echo $vac_wip > 0 ? '£' . number_format($vac_wip, 2) : '<span class="no-data">-</span>'; ?></td>
                            <td><?php echo $cnc_wip > 0 ? '£' . number_format($cnc_wip, 2) : '<span class="no-data">-</span>'; ?></td>
                            <td><?php echo $pu_wip > 0 ? '£' . number_format($pu_wip, 2) : '<span class="no-data">-</span>'; ?></td>
                            <td><strong><?php echo $total_stock > 0 ? '£' . number_format($total_stock, 2) : '<span class="no-data">-</span>'; ?></strong></td>
                            <td><?php echo '£' . number_format($stocktake_target, 2); ?></td>
                        </tr>
                    <?php endfor; ?>
                </tbody>
            </table>
        </div>
    </div>

