<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        /* === PAGE SETUP === */
        /* A4 portrait with 2cm margins and a footer frame at the bottom */
        @page {
            size: a4 portrait;
            margin: 2cm;
            @frame footer_frame {
                -pdf-frame-content: footerContent;
                bottom: 1cm;
                margin-left: 1cm;
                margin-right: 1cm;
                height: 1cm;
            }
        }

        /* === BODY STYLES === */
        body {
            font-family: Helvetica, sans-serif;
            font-size: 12pt;
            line-height: 1.5;
            color: #333;
        }

        /* === HEADER — worker name is the dominant element === */
        .header {
            text-align: center;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }
        .beneficiary-name {
            font-size: 24pt;
            font-weight: bold;
            text-transform: uppercase;
            color: #000;
        }
        .sub-header {
            font-size: 12pt;
            color: #666;
            margin-bottom: 5px;
        }
        .title {
            font-size: 18pt;
            font-weight: bold;
            color: #666;
        }

        /* === META BOX — beneficiary details === */
        .meta {
            margin-bottom: 20px;
            padding: 10px;
            border: 1px solid #ddd;
            background-color: #f9f9f9;
        }

        /* === ITEMS TABLE — base pay + adjustments === */
        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        .items-table th {
            border-bottom: 1px solid #000;
            padding: 8px;
            text-align: left;
            background-color: #eee;
            font-weight: bold;
        }
        .items-table td {
            border-bottom: 1px solid #eee;
            padding: 8px;
            text-align: left;
        }

        /* === TOTALS === */
        .totals {
            text-align: right;
            margin-top: 20px;
            border-top: 2px solid #333;
            padding-top: 10px;
        }
        .total-row {
            font-size: 16pt;
            font-weight: bold;
            color: #000;
        }

        /* === FOOTER — small print at bottom of page === */
        .footer {
            text-align: center;
            font-size: 10pt;
            color: #777;
        }

        /* Helpers */
        .text-right { text-align: right; }
        .positive { color: green; }
        .negative { color: red; }
    </style>
</head>
<body>
    <!-- Header: worker name is the biggest element (per CLAUDE.md rule) -->
    <div class="header">
        <div class="sub-header">PAYMENT TO BENEFICIARY</div>
        <div class="beneficiary-name">{{ record.worker.name }}</div>
        <div class="title">{% if is_advance %}Advance Payslip #{{ record.id }}{% elif is_loan %}Loan Payslip #{{ record.id }}{% else %}Payslip #{{ record.id }}{% endif %}</div>
    </div>

    <!-- Beneficiary details box -->
    <div class="meta">
        <strong>Beneficiary:</strong> {{ record.worker.name }}<br>
        <strong>ID Number:</strong> {{ record.worker.id_number }}<br>
        <strong>Date:</strong> {{ record.date }}
    </div>

    <!-- Line items: base pay + all adjustments -->
    <table class="items-table">
        <thead>
            <tr>
                <th>Description</th>
                <th class="text-right">Amount</th>
            </tr>
        </thead>
        <tbody>
            {% if is_advance %}
            <!-- Advance Payment — single line item -->
            <tr>
                <td>Advance Payment: {{ advance_description }}</td>
                <td class="text-right">R {{ advance_amount|floatformat:2 }}</td>
            </tr>
            {% elif is_loan %}
            <!-- Loan Payment — single line item -->
            <tr>
                <td>Loan Payment: {{ loan_description }}</td>
                <td class="text-right">R {{ loan_amount|floatformat:2 }}</td>
            </tr>
            {% else %}
            <!-- Base Pay — number of days worked × day rate -->
            <tr>
                <td>Base Pay ({{ logs_count }} days worked)</td>
                <td class="text-right">R {{ logs_amount|floatformat:2 }}</td>
            </tr>

            <!-- All payroll adjustments (bonuses, deductions, overtime, loan repayments) -->
            {% for adj in adjustments %}
            <tr>
                <td>{{ adj.get_type_display }}: {{ adj.description }}</td>
                <td class="text-right {% if adj.type in deductive_types %}negative{% else %}positive{% endif %}">
                    {% if adj.type in deductive_types %}- {% endif %}R {{ adj.amount|floatformat:2 }}
                </td>
            </tr>
            {% endfor %}
            {% endif %}
        </tbody>
    </table>

    <!-- Net pay total -->
    <div class="totals">
        <p class="total-row">Net Pay: R {{ record.amount_paid|floatformat:2 }}</p>
    </div>

    <!-- Footer frame (positioned at bottom of page by xhtml2pdf) -->
    <div id="footerContent" class="footer">
        Payer: Fox Fitt | Generated for {{ record.worker.name }} | {% now "Y-m-d H:i" %}
    </div>
</body>
</html>
