<?php
// attendance_report.php
session_start();
require_once("../config/config.php");

$month = $_GET['month'] ?? date('Y-m');
$year = substr($month, 0, 4);
$month_num = substr($month, 5, 2);

$month_start = "$year-$month_num-01";
$month_end = date('Y-m-t', strtotime($month_start));

$report = $conn->query("SELECT 
    e.employee_id, e.full_name, e.designation,
    COUNT(CASE WHEN a.status = 'Present' THEN 1 END) as present,
    COUNT(CASE WHEN a.status = 'Half Day' THEN 1 END) as half,
    COUNT(CASE WHEN a.status = 'Absent' THEN 1 END) as absent,
    COUNT(CASE WHEN a.status = 'Leave' THEN 1 END) as leave_days,
    COUNT(CASE WHEN a.status = 'Holiday' THEN 1 END) as holiday,
    COALESCE(SUM(a.overtime_hours), 0) as overtime
    FROM employee_master e
    LEFT JOIN attendance a ON e.employee_id = a.employee_id 
        AND a.attendance_date BETWEEN '$month_start' AND '$month_end'
    WHERE e.status = 'active'
    GROUP BY e.id
    ORDER BY e.employee_id");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Attendance Report</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        /* Same exact style as add_employee.php */
        :root { --primary: #8b5cf6; --primary-light: #a78bfa; --text: #e0e7ff; }
        * { margin:0; padding:0; box-sizing:border-box; }
        body { font-family:'Segoe UI',sans-serif; min-height:100vh; background:linear-gradient(rgba(0,0,0,0.6),rgba(0,0,0,0.6)),url('../background.jpeg') center/cover no-repeat fixed; color:var(--text); padding:20px; }
        .container { max-width:1400px; margin:0 auto; background:rgba(255,255,255,0.1); backdrop-filter:blur(16px); border-radius:24px; border:1px solid rgba(255,255,255,0.2); box-shadow:0 20px 50px rgba(0,0,0,0.4); overflow:hidden; }
        .header { background:rgba(255,255,255,0.15); padding:40px; text-align:center; }
        .header h1 { font-size:2.5rem; margin-bottom:10px; }
        .form-area { padding:40px; }
        .section-title { font-size:1.8rem; margin:50px 0 25px; padding-bottom:12px; border-bottom:1px solid rgba(255,255,255,0.3); }
        .table-area { overflow-x:auto; }
        table { width:100%; border-collapse:collapse; }
        th, td { padding:18px; text-align:center; border-bottom:1px solid rgba(255,255,255,0.15); }
        th { background:rgba(139,92,246,0.3); }
        .filter { text-align:center; margin-bottom:40px; }
        .filter input { padding:16px; font-size:1.1rem; background:rgba(255,255,255,0.15); border:1px solid rgba(255,255,255,0.3); border-radius:14px; color:white; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Monthly Attendance Report</h1>
            <p><?= date('F Y', strtotime($month_start)) ?></p>
        </div>

        <div class="form-area">
            <div class="filter">
                <form method="get">
                    <input type="month" name="month" value="<?= $month ?>" required>
                    <button type="submit" style="padding:16px 30px; background:var(--primary); border:none; border-radius:14px; color:white; font-size:1.1rem; margin-left:10px;">
                        View Report
                    </button>
                </form>
            </div>

            <h2 class="section-title"><i class="fas fa-chart-bar"></i> Attendance Summary</h2>

            <div class="table-area">
                <table>
                    <thead>
                        <tr>
                            <th>Employee ID</th>
                            <th>Full Name</th>
                            <th>Designation</th>
                            <th>Present</th>
                            <th>Half Day</th>
                            <th>Absent</th>
                            <th>Leave</th>
                            <th>Holiday</th>
                            <th>Overtime (hrs)</th>
                            <th>Payable Days</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = $report->fetch_assoc()): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($row['employee_id']) ?></strong></td>
                            <td><?= htmlspecialchars($row['full_name']) ?></td>
                            <td><?= htmlspecialchars($row['designation']) ?></td>
                            <td><?= $row['present'] ?></td>
                            <td><?= $row['half'] ?></td>
                            <td><?= $row['absent'] ?></td>
                            <td><?= $row['leave_days'] ?></td>
                            <td><?= $row['holiday'] ?></td>
                            <td><?= number_format($row['overtime'], 2) ?></td>
                            <td><strong><?= $row['present'] + ($row['half'] * 0.5) ?></strong></td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>

            <div style="text-align:center; margin-top:50px;">
                <a href="attendance_daily.php" style="padding:18px 40px; background:var(--primary); border-radius:14px; color:white; text-decoration:none; font-size:1.2rem;">
                    <i class="fas fa-calendar-alt"></i> Mark Daily Attendance
                </a>
            </div>
        </div>
    </div>
</body>
</html>