{% extends 'base.html' %}

{% block title %}Payslip #{{ record.id }} | Fox Fitt{% endblock %}

{% block content %}
<!-- === PAYSLIP DETAIL PAGE ===
     Shows a completed payment with work logs, adjustments, and totals.
     Reached from the Payment History tab on the payroll dashboard.
     Has a Print button that uses the browser's native print dialog. -->

<div class="container py-5">
    <!-- Action buttons (hidden when printing) -->
    <div class="d-print-none mb-4 d-grid gap-2 d-md-flex">
        <a href="{% url 'payroll_dashboard' %}?status=paid" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-1"></i> Back to Payment History
        </a>
        <button onclick="window.print()" class="btn btn-accent">
            <i class="fas fa-print me-1"></i> Print Payslip
        </button>
    </div>

    <!-- Payslip card -->
    <div class="card border-0 shadow-sm" id="payslip-card">
        <div class="card-body p-5">

            <!-- === HEADER — worker name is the dominant element === -->
            <div class="row mb-5 border-bottom pb-4 align-items-center">
                <div class="col-md-6">
                    <h6 class="text-uppercase text-muted fw-bold small mb-1">Payment To Beneficiary:</h6>
                    <h2 class="fw-bold text-dark mb-0 text-uppercase">{{ record.worker.name }}</h2>
                    <p class="text-muted small mb-0">{% if is_advance %}Advance{% elif is_loan %}Loan{% endif %} Payslip No. #{{ record.id|stringformat:"06d" }}</p>
                </div>
                <div class="col-md-6 text-md-end mt-3 mt-md-0">
                    <h3 class="fw-bold text-uppercase text-secondary mb-1">{% if is_advance %}Advance{% elif is_loan %}Loan{% endif %} Payslip</h3>
                    <div class="fw-bold">{{ record.date|date:"F j, Y" }}</div>
                    <div class="text-muted small">Payer: Fox Fitt</div>
                </div>
            </div>

            <!-- === WORKER DETAILS + NET PAY === -->
            <div class="row mb-5">
                <div class="col-md-6">
                    <h6 class="text-uppercase text-muted fw-bold small mb-3">Beneficiary Details:</h6>
                    <h4 class="fw-bold">{{ record.worker.name }}</h4>
                    <p class="mb-0">ID Number: <strong>{{ record.worker.id_number }}</strong></p>
                    <p class="mb-0">Phone: {{ record.worker.phone_number|default:"—" }}</p>
                </div>
                <div class="col-md-6 text-md-end mt-4 mt-md-0">
                    <h6 class="text-uppercase text-muted fw-bold small mb-3">Net Payable Amount:</h6>
                    <div class="display-6 fw-bold text-dark">R {{ record.amount_paid|floatformat:2 }}</div>
                    <p class="text-success small fw-bold mt-2">
                        <i class="fas fa-check-circle me-1"></i> PAID
                    </p>
                </div>
            </div>

            {% if is_advance %}
            <!-- === ADVANCE PAYMENT DETAIL === -->
            <h6 class="text-uppercase text-muted fw-bold small mb-3">Advance Details</h6>
            <div class="table-responsive mb-4">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Description</th>
                            <th class="text-end">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>{{ advance_adj.date|date:"M d, Y" }}</td>
                            <td><span class="badge bg-info text-dark text-uppercase">Advance Payment</span></td>
                            <td>{{ advance_adj.description|default:"Salary advance" }}</td>
                            <td class="text-end text-success fw-bold">R {{ advance_adj.amount|floatformat:2 }}</td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <!-- === ADVANCE TOTAL === -->
            <div class="row justify-content-end mt-4">
                <div class="col-md-5">
                    <table class="table table-sm border-0">
                        <tr class="border-top border-dark">
                            <td class="text-end border-0 fw-bold fs-5">Amount Advanced:</td>
                            <td class="text-end border-0 fw-bold fs-5">R {{ advance_adj.amount|floatformat:2 }}</td>
                        </tr>
                    </table>
                </div>
            </div>

            {% elif is_loan %}
            <!-- === LOAN PAYMENT DETAIL === -->
            <h6 class="text-uppercase text-muted fw-bold small mb-3">Loan Details</h6>
            <div class="table-responsive mb-4">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Description</th>
                            <th class="text-end">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>{{ loan_adj.date|date:"M d, Y" }}</td>
                            <td><span class="badge bg-warning text-dark text-uppercase">Loan Payment</span></td>
                            <td>{{ loan_adj.description|default:"Worker loan" }}</td>
                            <td class="text-end text-success fw-bold">R {{ loan_adj.amount|floatformat:2 }}</td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <!-- === LOAN TOTAL === -->
            <div class="row justify-content-end mt-4">
                <div class="col-md-5">
                    <table class="table table-sm border-0">
                        <tr class="border-top border-dark">
                            <td class="text-end border-0 fw-bold fs-5">Loan Amount:</td>
                            <td class="text-end border-0 fw-bold fs-5">R {{ loan_adj.amount|floatformat:2 }}</td>
                        </tr>
                    </table>
                </div>
            </div>

            {% else %}
            <!-- === WORK LOG TABLE — each day worked === -->
            <h6 class="text-uppercase text-muted fw-bold small mb-3">Work Log Details (Attendance)</h6>
            <div class="table-responsive mb-4">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Date</th>
                            <th>Project</th>
                            <th>Notes</th>
                            <th class="text-end">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for log in logs %}
                        <tr>
                            <td>{{ log.date|date:"M d, Y" }}</td>
                            <td>{{ log.project.name }}</td>
                            <td>{{ log.notes|default:"—"|truncatechars:50 }}</td>
                            <td class="text-end">R {{ record.worker.daily_rate|floatformat:2 }}</td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="4" class="text-center text-muted">
                                <i class="fas fa-info-circle me-1"></i> No work logs in this period.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                    <tfoot class="table-light">
                        <tr>
                            <td colspan="3" class="text-end fw-bold">Base Pay Subtotal</td>
                            <td class="text-end fw-bold">R {{ base_pay|floatformat:2 }}</td>
                        </tr>
                    </tfoot>
                </table>
            </div>

            <!-- === ADJUSTMENTS TABLE — bonuses, deductions, overtime, loan repayments === -->
            {% if adjustments %}
            <h6 class="text-uppercase text-muted fw-bold small mb-3 mt-4">Adjustments (Bonuses, Deductions, Loans)</h6>
            <div class="table-responsive mb-4">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Description</th>
                            <th class="text-end">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for adj in adjustments %}
                        <tr>
                            <td>{{ adj.date|date:"M d, Y" }}</td>
                            <td>
                                <span class="badge bg-secondary text-uppercase">{{ adj.get_type_display }}</span>
                            </td>
                            <td>{{ adj.description }}</td>
                            <td class="text-end {% if adj.type in deductive_types %}text-danger{% else %}text-success{% endif %}">
                                {% if adj.type in deductive_types %}
                                - R {{ adj.amount|floatformat:2 }}
                                {% else %}
                                + R {{ adj.amount|floatformat:2 }}
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% endif %}

            <!-- === GRAND TOTAL SUMMARY === -->
            <div class="row justify-content-end mt-4">
                <div class="col-md-5">
                    <table class="table table-sm border-0">
                        <tr>
                            <td class="text-end border-0 text-muted">Base Pay:</td>
                            <td class="text-end border-0" width="140">R {{ base_pay|floatformat:2 }}</td>
                        </tr>
                        {% if adjustments %}
                        <tr>
                            <td class="text-end border-0 text-muted">Adjustments Net:</td>
                            <td class="text-end border-0">
                                {% if adjustments_net >= 0 %}
                                + R {{ adjustments_net|floatformat:2 }}
                                {% else %}
                                - R {{ adjustments_net_abs|floatformat:2 }}
                                {% endif %}
                            </td>
                        </tr>
                        {% endif %}
                        <tr class="border-top border-dark">
                            <td class="text-end border-0 fw-bold fs-5">Net Payable:</td>
                            <td class="text-end border-0 fw-bold fs-5">R {{ record.amount_paid|floatformat:2 }}</td>
                        </tr>
                    </table>
                </div>
            </div>
            {% endif %}

            <!-- === FOOTER === -->
            <div class="text-center text-muted small mt-5 pt-4 border-top">
                <p>This is a computer-generated document and does not require a signature.</p>
                <p>Payer: Fox Fitt &copy; 2026</p>
            </div>
        </div>
    </div>
</div>
{% endblock %}
