<!doctype html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <title>Absences</title>

    <style>
        @page { size: A3 landscape; margin: 8mm; }

        html, body {
            margin: 0;
            padding: 4mm;
            font-family: Helvetica, sans-serif;
            font-size: 8px;
        }

        table.calendar {
            width: 100%;
            border-collapse: collapse;
            table-layout: auto;
            border: 4px solid #0a0a0a;
        }

        th, td {
            border: 2px solid #0a0a0a;
            padding: 2px 1px;
            vertical-align: middle;
            overflow: hidden;
            white-space: nowrap;
        }

        thead th {
            text-align: center;
            font-weight: 700;
        }

        .col-employee {
            font-size: 10px;
            font-weight: bold;
            width: 10mm;
            text-align: left;
        }

        .col-day {
            font-size: 8px;
            text-align: center;
        }

        .month {
            font-size: 14px;
            font-weight: bold;
        }

        .site-title td {
            font-weight: bold;
            font-size: 12px;
            text-transform: uppercase;
            border-color: #0a0a0a;
        }

        .site-title .label {
            text-align: left;
        }

        .code {
            font-weight: bold;
            font-size: 8px;
        }

        .holiday-code {
            font-weight: bold;
            font-size: 4px;
        }

        .month-separator {
            border-right: 4px solid #0a0a0a !important;
        }

        .weekend {
            background: grey;
        }

        .holiday {
            background: #b3e5fc;
        }

        .formation {
            background: #6366f1;
            color: #fff;
        }

        .body-cell {
            height: 6mm;
            padding: 0 !important;
            vertical-align: middle;
            text-align: center;
        }

        .full-cell {
            display: block;
            text-align: center;
            margin: 0;
            padding: 0;
        }

        .half-table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
            border: 0;
            border-spacing: 0;
            margin: 0;
        }

        .half-td {
            height: 3mm;
            line-height: 3mm;
            text-align: center;
            font-weight: bold;
            font-size: 7px;
            padding: 0;
            border: 0;
            vertical-align: middle;
        }

        td.body-cell table,
        td.body-cell td {
            padding: 0;
        }

    </style>
</head>
<body>

{% set months = {
    1:'Janvier', 2:'Février', 3:'Mars', 4:'Avril', 5:'Mai', 6:'Juin',
    7:'Juillet', 8:'Août', 9:'Septembre', 10:'Octobre', 11:'Novembre', 12:'Décembre'
} %}
{% set dow = ['Lu','Ma','Me','Je','Ve','Sa','Di'] %}

{% set dayColWidthMm = 5 %}

<table class="calendar">
    <thead>
    {# Ligne 1 : mois #}
    <tr>
        <th class="col-employee" rowspan="4"></th>

        {% set currentMonth = '' %}
        {% set monthCount = 0 %}

        {% for day in days %}
            {% set m = day.date|date('n') %}
            {% set monthLabel = months[m] ~ ' ' ~ (day.date|date('Y')) %}

            {% if monthLabel != currentMonth %}
                {% if not loop.first %}
                    <th class="month month-separator" colspan="{{ monthCount }}">{{ currentMonth }}</th>
                {% endif %}
                {% set currentMonth = monthLabel %}
                {% set monthCount = 1 %}
            {% else %}
                {% set monthCount = monthCount + 1 %}
            {% endif %}

            {% if loop.last %}
                <th class="month" colspan="{{ monthCount }}">{{ currentMonth }}</th>
            {% endif %}
        {% endfor %}
    </tr>

    {# Ligne 2 : numéro de semaine #}
    <tr>
        {% set currentWeek = '' %}
        {% set weekCount = 0 %}
        {% for day in days %}
            {% set weekLabel = 'S' ~ (day.date|date('W')) %}
            {% if weekLabel != currentWeek %}
                {% if not loop.first %}
                    <th class="col-day" colspan="{{ weekCount }}">{{ currentWeek }}</th>
                {% endif %}
                {% set currentWeek = weekLabel %}
                {% set weekCount = 1 %}
            {% else %}
                {% set weekCount = weekCount + 1 %}
            {% endif %}

            {% if loop.last %}
                <th class="col-day" colspan="{{ weekCount }}">{{ currentWeek }}</th>
            {% endif %}
        {% endfor %}
    </tr>

    {# Ligne 3 : jour semaine #}
    <tr>
        {% for day in days %}
            {% set idx = (day.date|date('N') - 1) %}
            {% set isMonthEnd = (not loop.last) and (days[loop.index].date|date('n') != day.date|date('n')) %}
            {% set isWeekend = day.date|date('N') in [6, 7] %}
            <th class="col-day{% if isMonthEnd %} month-separator{% endif %}{% if isWeekend %} weekend{% endif %}" style="width: {{ dayColWidthMm }}mm;">{{ dow[idx] }}</th>
        {% endfor %}
    </tr>

    {# Ligne 4 : numéro #}
    <tr>
        {% for day in days %}
            {% set isMonthEnd = (not loop.last) and (days[loop.index].date|date('n') != day.date|date('n')) %}
            {% set isWeekend = day.date|date('N') in [6, 7] %}
            <th class="col-day{% if isMonthEnd %} month-separator{% endif %}{% if isWeekend %} weekend{% endif %}" style="width: {{ dayColWidthMm }}mm;">{{ day.label }}</th>
        {% endfor %}
    </tr>
    </thead>

    <tbody>
    {% set currentSiteId = null %}

    {% for employee in employees %}
        {% set site = employee.site %}
        {% set siteId = site ? site.id : 'none' %}
        {% set siteName = site ? site.name : 'Sans site' %}

        {# couleur de fond du site (si tu as un champ color) #}
        {% set siteColor = '#ffd7d7' %}
        {% if site and attribute(site, 'color') is defined and site.color %}
            {% set siteColor = site.color %}
        {% endif %}

        {# Nouvelle section site #}
        {% if siteId != currentSiteId %}
            <tr class="site-title">
                <td class="label" style="background: {{ siteColor }};" colspan="{{ 1 + (days|length) }}">
                    {{ siteName }}
                </td>
            </tr>
            {% set currentSiteId = siteId %}
        {% endif %}

        {# Ligne employé #}
        <tr>
            <td class="col-employee">
                {{ employee.firstName }}{% if employee.lastName %} {{ employee.lastName|first }}.{% endif %}
            </td>

            {% for day in days %}
                {% set isHoliday = holidayMap[day.date] ?? null %}
                {% set info = absenceMap[employee.id][day.date] ?? null %}
                {% set hasFormation = formationMap[employee.id][day.date] ?? false %}
                {% set isFormationOnly = hasFormation and not info and not isHoliday %}
                {% set isMonthEnd = (not loop.last) and (days[loop.index].date|date('n') != day.date|date('n')) %}
                {% set isWeekend = day.date|date('N') in [6, 7] %}
                <td class="col-day body-cell{% if isMonthEnd %} month-separator{% endif %}{% if isWeekend %} weekend{% endif %}{% if isHoliday %} holiday{% endif %}{% if isFormationOnly %} formation{% endif %}" style="width: {{ dayColWidthMm }}mm;{% if info and not isHoliday and not info.half %} background-color: {{ info.color }};{% endif %}">
                    {% if isHoliday %}
                        <span class="full-cell code">Férié</span>
                    {% elseif isFormationOnly %}
                        <span class="full-cell code">F</span>
                    {% elseif info %}
                        {% if info.half %}
                            <table class="half-table">
                                <tr>
                                    <td class="half-td" style="{% if info.halfLabel == 'AM' %}background-color: {{ info.color }};{% endif %}">
                                        {% if info.halfLabel == 'AM' %}{{ info.code }}{% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td class="half-td" style="{% if info.halfLabel == 'PM' %}background-color: {{ info.color }};{% endif %}">
                                        {% if info.halfLabel == 'PM' %}{{ info.code }}{% endif %}
                                    </td>
                                </tr>
                            </table>
                        {% else %}
                            <span class="full-cell code">{{ info.code }}{% if hasFormation %}*{% endif %}</span>
                        {% endif %}
                    {% endif %}
                </td>
            {% endfor %}
        </tr>
    {% else %}
        <tr>
            <td colspan="{{ 1 + (days|length) }}">Aucun employé.</td>
        </tr>
    {% endfor %}
    </tbody>
</table>

</body>
</html>
