<!DOCTYPE html>
<html>
<head>
    <title><?= lang('payroll_summary') . ' ' . lang('report') ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <?php
    $direction = $this->session->userdata('direction');
    if (!empty($direction) && $direction == 'rtl') {
        $RTL = 'on';
    } else {
        $RTL = config_item('RTL');
    }

    $currency = $this->payroll_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
    $summary_rows = array();
    $gross_total = 0;
    $deduction_total = 0;
    $net_total = 0;
    $paid_total = 0;

    if (!empty($employee_payroll)) {
        foreach ($employee_payroll as $v_payroll) {
            $salary_payment_history = $this->db->where('salary_payment_id', $v_payroll->salary_payment_id)->get('tbl_salary_payment_details')->result();
            $total_salary_amount = 0;
            $rate = 0;
            if (!empty($salary_payment_history)) {
                foreach ($salary_payment_history as $v_payment_history) {
                    if (is_numeric($v_payment_history->salary_payment_details_value)) {
                        if ($v_payment_history->salary_payment_details_label == 'overtime_salary' || $v_payment_history->salary_payment_details_label == 'hourly_rates') {
                            $rate = $v_payment_history->salary_payment_details_value;
                        }
                        $total_salary_amount += $v_payment_history->salary_payment_details_value;
                    }
                }
            }
            $salary_allowance_info = $this->db->where('salary_payment_id', $v_payroll->salary_payment_id)->get('tbl_salary_payment_allowance')->result();
            $total_allowance = 0;
            if (!empty($salary_allowance_info)) {
                foreach ($salary_allowance_info as $v_salary_allowance_info) {
                    $total_allowance += $v_salary_allowance_info->salary_payment_allowance_value;
                }
            }
            $salary_deduction_info = $this->db->where('salary_payment_id', $v_payroll->salary_payment_id)->get('tbl_salary_payment_deduction')->result();
            $total_deduction = 0;
            if (!empty($salary_deduction_info)) {
                foreach ($salary_deduction_info as $v_salary_deduction_info) {
                    $total_deduction += $v_salary_deduction_info->salary_payment_deduction_value;
                }
            }

            $gross_salary = $total_salary_amount + $total_allowance - $rate;
            $fine_deduction = !empty($v_payroll->fine_deduction) ? $v_payroll->fine_deduction : 0;
            $net_salary = $gross_salary - $total_deduction;
            $paid_amount = $net_salary - $fine_deduction;

            $gross_total += $gross_salary;
            $deduction_total += $total_deduction + $fine_deduction;
            $net_total += $net_salary;
            $paid_total += $paid_amount;

            $summary_rows[] = array(
                'month' => date('F-Y', safe_strtotime($v_payroll->payment_month)),
                'date' => display_date($v_payroll->paid_date),
                'gross' => $gross_salary,
                'deduction' => $total_deduction,
                'net' => $net_salary,
                'fine' => $fine_deduction,
                'amount' => $paid_amount,
            );
        }
    }
    ?>
    <style>
        body { margin: 0; color: #172033; background: #ffffff; font: 12px/1.55 DejaVu Sans, Arial, Helvetica, sans-serif; min-width: 100%; min-height: 100%;<?php if (!empty($RTL)) { ?> text-align: right;<?php } ?> }
        table { width: 100%; border-collapse: collapse; border-spacing: 0;<?php if (!empty($RTL)) { ?> text-align: right;<?php } ?> }
        .page-wrap { padding: 18px 22px 24px; }
        .brand-shell { border: 1px solid #d7deea; padding: 18px 20px; background: #f8fafc; margin-bottom: 18px; }
        .brand-logo { width: 60px; height: 60px; background: #ffffff; border: 1px solid #d7deea; padding: 6px; }
        .brand-title { font-size: 24px; font-weight: 500; color: #0f172a; margin: 0 0 4px; }
        .brand-text { font-size: 12px; color: #64748b; margin: 0; }
        .report-meta { width: 300px;<?php if (!empty($RTL)) { ?> text-align: left;<?php } else { ?> text-align: right;<?php } ?> }
        .header-title { font-size: 20px; font-weight: 700; color: #0f172a; margin: 0 0 4px; line-height: 1.15; }
        .header-subtitle { font-size: 10px; color: #64748b; margin: 0; line-height: 1.35; }
        .range-chip { display: inline-block; padding-top: 4px; color: #475569; font-size: 9px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
        .metrics { margin: 0 0 18px; }
        .metric-card { width: 23.5%; border: 1px solid #d7deea; background: #ffffff; padding: 14px 16px; vertical-align: top; }
        .metric-gap { width: 2%; }
        .metric-label { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .12em; color: #64748b; margin-bottom: 6px; }
        .metric-value { font-size: 21px; font-weight: 700; color: #0f172a; margin: 0; }
        .metric-note { font-size: 11px; color: #94a3b8; margin-top: 4px; }
        .table-shell { border: 1px solid #d7deea; overflow: hidden; background: #ffffff; }
        .table-shell table { border-collapse: separate; border-spacing: 0; }
        .table-head td { background: #1e293b; color: #f8fafc; font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; padding: 12px 14px; border-bottom: 1px solid #1e293b; }
        .row td { padding: 12px 14px; border-bottom: 1px solid #e2e8f0; color: #1e293b; vertical-align: top; }
        .row-alt td { background: #f8fafc; }
        .number {<?php if (!empty($RTL)) { ?> text-align: left;<?php } else { ?> text-align: right;<?php } ?> }
        .muted { color: #64748b; }
        .total-row td { padding: 14px; background: #eef2ff; border-top: 1px solid #c7d2fe; font-weight: 700; color: #1e1b4b; }
    </style>
</head>
<body>
<?php
$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;
$img = $rootPath . '/' . config_item('company_logo');
if (!file_exists($img)) {
    $img = base_url() . config_item('company_logo');
}
if (!file_exists($img)) {
    $img = $rootPath . '/uploads/default_logo.png';
}
?>
<div class="page-wrap">
    <div class="brand-shell">
        <table>
            <tr>
                <td style="width: 84px; border: 0;"><img class="brand-logo" src="<?= $img ?>" alt="<?= config_item('company_name') ?>"></td>
                <td style="border: 0;"><p class="brand-title"><?= config_item('company_name') ?></p><p class="brand-text"><?= lang('payroll_summary') ?></p></td>
                <td class="report-meta" style="border: 0;"><p class="header-title"><?= lang('payroll_summary') ?></p><p class="header-subtitle">Payroll disbursement, deductions, and final paid amount<?= !empty($by) ? ' for ' . trim($by, ' -') : '' ?>.</p><span class="range-chip"><?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?></span></td>
            </tr>
        </table>
    </div>

    <table class="metrics">
        <tr>
            <td class="metric-card"><div class="metric-label"><?= lang('records') ?></div><p class="metric-value"><?= count($summary_rows) ?></p><div class="metric-note"><?= lang('salary_details') ?></div></td>
            <td class="metric-gap"></td>
            <td class="metric-card"><div class="metric-label"><?= lang('gross_salary') ?></div><p class="metric-value"><?= display_money($gross_total, $currency->symbol) ?></p><div class="metric-note"><?= lang('before') ?> <?= lang('deductions') ?></div></td>
            <td class="metric-gap"></td>
            <td class="metric-card"><div class="metric-label"><?= lang('total_deduction') ?></div><p class="metric-value"><?= display_money($deduction_total, $currency->symbol) ?></p><div class="metric-note"><?= lang('including') ?> <?= lang('fine_deduction') ?></div></td>
            <td class="metric-gap"></td>
            <td class="metric-card"><div class="metric-label"><?= lang('paid_amount') ?></div><p class="metric-value"><?= display_money($paid_total, $currency->symbol) ?></p><div class="metric-note"><?= lang('net_salary') ?>: <?= display_money($net_total, $currency->symbol) ?></div></td>
        </tr>
    </table>

    <div class="table-shell">
        <table>
            <thead>
            <tr class="table-head">
                <td><?= lang('month') ?></td>
                <td><?= lang('date') ?></td>
                <td class="number"><?= lang('gross_salary') ?></td>
                <td class="number"><?= lang('total_deduction') ?></td>
                <td class="number"><?= lang('net_salary') ?></td>
                <td class="number"><?= lang('fine_deduction') ?></td>
                <td class="number"><?= lang('amount') ?></td>
            </tr>
            </thead>
            <tbody>
            <?php if (!empty($summary_rows)): foreach ($summary_rows as $index => $row): ?>
                <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>">
                    <td><?= $row['month'] ?></td>
                    <td><?= $row['date'] ?></td>
                    <td class="number"><?= display_money($row['gross'], $currency->symbol) ?></td>
                    <td class="number"><?= display_money($row['deduction'], $currency->symbol) ?></td>
                    <td class="number"><?= display_money($row['net'], $currency->symbol) ?></td>
                    <td class="number"><?= display_money($row['fine'], $currency->symbol) ?></td>
                    <td class="number"><?= display_money($row['amount'], $currency->symbol) ?></td>
                </tr>
            <?php endforeach; ?>
                <tr class="total-row">
                    <td colspan="2"><?= lang('total') ?></td>
                    <td class="number"><?= display_money($gross_total, $currency->symbol) ?></td>
                    <td class="number"><?= display_money($deduction_total, $currency->symbol) ?></td>
                    <td class="number"><?= display_money($net_total, $currency->symbol) ?></td>
                    <td class="number">-</td>
                    <td class="number"><?= display_money($paid_total, $currency->symbol) ?></td>
                </tr>
            <?php else: ?>
                <tr class="row"><td colspan="7" class="muted"><?= lang('nothing_to_display') ?></td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
