<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Payroll Total Summary Report</title>

    <style>
        @page {
            margin: 140px 40px 80px 40px;
        }

        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 12px;
            margin: 0;
        }

        /* =========================
           HEADER
        ========================== */
        .header {
            position: fixed;
            top: -120px;
            left: 0;
            right: 0;
            height: 100px;
        }

        .header-left {
            float: left;
            width: 70%;
        }

        .header-right {
            float: right;
            width: 30%;
            text-align: right;
        }

        .report-name {
            font-size: 15px;
            font-weight: bold;
        }

        .company-name {
            font-size: 13px;
            font-weight: bold;
        }

        .small-text {
            font-size: 11px;
        }

       .footer {
            position: fixed;
            bottom: -60px;
            left: 0;
            right: 0;
            height: 50px;
            font-size: 10px;
        }

        .footer-left {
            float: left;
            width: 33%;
            text-align: left;
        }

        .footer-center {
            float: left;
            width: 34%;
            text-align: center;
        }

        .footer-right {
            float: right;
            width: 33%;
            text-align: right;
        }

        .page-number:before {
            content: "Page " counter(page) " / " counter(pages);
        }

        /* =========================
           SUMMARY LAYOUT
        ========================== */
        .summary-section {
            margin-bottom: 20px;
            border: 1px solid #000;
            padding: 10px;
        }

        .section-title {
            font-weight: bold;
            font-size: 14px;
            margin-bottom: 10px;
            padding: 5px;
            background: #f2f2f2;
            border-left: 3px solid #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 10px;
        }

        .summary-table td {
            padding: 6px 8px;
            font-size: 11px;
            border-bottom: 1px solid #eee;
        }

        .summary-table .item-name {
            width: 70%;
        }

        .summary-table .amount {
            width: 30%;
            text-align: right;
            font-weight: bold;
        }

        .total-row {
            font-weight: bold;
            font-size: 13px;
            background: #f9f9f9;
            border-top: 2px solid #000;
            border-bottom: 2px solid #000;
        }

        .info-box {
            background: #f9f9f9;
            padding: 10px;
            margin-bottom: 20px;
            border-left: 3px solid #333;
        }

        .clearfix::after {
            content: "";
            display: table;
            clear: both;
        }
    </style>
</head>
<body>

<!-- ================= HEADER ================= -->
<div class="header">
    <div class="header-left">
        <div class="report-name">
            Payroll Total Summary Report
        </div>
        <div class="company-name">
            {{ $company->legal_name ?? $company->display_name ?? '' }}
        </div>
        <div class="small-text">
            {{ trim(implode(', ', array_filter([$company->address_line1 ?? null, $company->address_line2 ?? null]))) }}
        </div>
        <div class="small-text">
            @if(!empty($filters['from_date']) && !empty($filters['to_date']))
                Period: {{ \Carbon\Carbon::parse($filters['from_date'])->format('d M Y') }} to {{ \Carbon\Carbon::parse($filters['to_date'])->format('d M Y') }}
            @endif
        </div>
    </div>

    <div class="header-right">
        @if(!empty($company->logo))
            <img src="{{ public_path($company->logo) }}" height="70">
        @endif
    </div>
</div>

<!-- ================= FOOTER ================= -->
<div class="footer">
    <div class="footer-left">
        LaraOffice
    </div>

    <div class="footer-center">
        <span class="page-number"></span>
    </div>

    <div class="footer-right">
        Printed: {{ now()->format('Y-m-d H:i') }}
    </div>
</div>

<!-- ================= CONTENT ================= -->
@php
    $currency = optional($company->baseCurrency)->symbol ?? 'Rs.';
@endphp

<div class="info-box">
    <strong>Total Employees:</strong> {{ $totals['employee_count'] }}
</div>

<!-- EARNINGS SECTION -->
<div class="summary-section">
    <div class="section-title">Earnings</div>
    <table class="summary-table">
        <tr>
            <td class="item-name">Basic Salary</td>
            <td class="amount">{{ $currency }} {{ number_format($totals['basic_salary'], 2) }}</td>
        </tr>

        @foreach($totals['additions'] as $item)
        <tr>
            <td class="item-name">{{ $item['addition']->payslip_label ?: $item['addition']->name }}</td>
            <td class="amount">{{ $currency }} {{ number_format($item['total'], 2) }}</td>
        </tr>
        @endforeach

        <tr class="total-row">
            <td class="item-name">Total Gross Salary</td>
            <td class="amount">{{ $currency }} {{ number_format($totals['gross_salary'], 2) }}</td>
        </tr>
    </table>
</div>

<!-- DEDUCTIONS SECTION -->
<div class="summary-section">
    <div class="section-title">Deductions</div>
    <table class="summary-table">
        @foreach($totals['deductions'] as $item)
        <tr>
            <td class="item-name">{{ $item['deduction']->payslip_label ?: $item['deduction']->name }}</td>
            <td class="amount">{{ $currency }} {{ number_format($item['total'], 2) }}</td>
        </tr>
        @endforeach

        @if(count($totals['deductions']) == 0)
        <tr>
            <td colspan="2" style="text-align: center; color: #999;">No deductions</td>
        </tr>
        @endif
    </table>
</div>

<!-- NET SALARY SECTION -->
<div class="summary-section">
    <div class="section-title">Net Salary</div>
    <table class="summary-table">
        <tr class="total-row">
            <td class="item-name">Total Net Salary Payable</td>
            <td class="amount">{{ $currency }} {{ number_format($totals['net_salary'], 2) }}</td>
        </tr>
    </table>
</div>

<!-- COMPANY CONTRIBUTIONS SECTION -->
<div class="summary-section">
    <div class="section-title">Company Contributions</div>
    <table class="summary-table">
        <tr>
            <td class="item-name">EPF 12%</td>
            <td class="amount">{{ $currency }} {{ number_format($totals['epf_12'], 2) }}</td>
        </tr>
        <tr>
            <td class="item-name">ETF 3%</td>
            <td class="amount">{{ $currency }} {{ number_format($totals['etf_3'], 2) }}</td>
        </tr>
    </table>
</div>

</body>
</html>
