<!DOCTYPE html>
<html>
<head>
    <title><?= lang('manage_salary_details') . ' ' . lang('details') ?></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->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
    $rate = 0;
    $total_hours_amount = 0;
    if (!empty($salary_payment_details_info)) {
        foreach ($salary_payment_details_info as $v_payment_details) {
            if (is_numeric($v_payment_details->salary_payment_details_value)) {
                if ($v_payment_details->salary_payment_details_label == 'overtime_salary' || $v_payment_details->salary_payment_details_label == 'hourly_rates') {
                    $rate = $v_payment_details->salary_payment_details_value;
                }
                $total_hours_amount += $v_payment_details->salary_payment_details_value;
            }
        }
    }

    $total_allowance = 0;
    if (!empty($allowance_info)) {
        foreach ($allowance_info as $v_allowance) {
            $total_allowance += $v_allowance->salary_payment_allowance_value;
        }
    }

    $total_deduction = 0;
    if (!empty($deduction_info)) {
        foreach ($deduction_info as $v_deduction) {
            $total_deduction += $v_deduction->salary_payment_deduction_value;
        }
    }

    $gross_salary = $total_hours_amount + $total_allowance - $rate;
    $net_salary = $gross_salary - $total_deduction;
    $paid_amount = !empty($salary_payment_info->fine_deduction) ? $net_salary - $salary_payment_info->fine_deduction : $net_salary;
    ?>
    <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, .profile-shell { border: 1px solid #d7deea; padding: 18px 20px; background: #f8fafc; margin-bottom: 18px; }
        .brand-logo, .profile-photo { width: 60px; height: 60px; background: #ffffff; border: 1px solid #d7deea; padding: 6px; }
        .profile-photo { width: 108px; height: 108px; padding: 4px; }
        .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; }
        .employee-name { font-size: 22px; font-weight: 700; color: #0f172a; margin: 0 0 6px; }
        .detail-row td { padding: 5px 0; font-size: 11px; }
        .label { width: 34%; color: #64748b; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; }
        .value { color: #0f172a; }
        .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; margin-bottom: 18px; }
        .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; }
        .strong { font-weight: 700; }
        .notes-box { border: 1px solid #d7deea; background: #f8fafc; padding: 12px 14px; color: #334155; line-height: 1.55; }
    </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('manage_salary_details') ?></p></td>
                <td class="report-meta" style="border: 0;"><p class="header-title"><?= lang('manage_salary_details') ?></p><p class="header-subtitle">Salary payment breakdown with earnings, deductions, final payout, and payment method.</p><span class="range-chip"><?= lang('generated_on') ?> <?= display_date(date('Y-m-d')) ?></span></td>
            </tr>
        </table>
    </div>

    <div class="profile-shell">
        <table>
            <tr>
                <td style="width: 132px; border: 0;"><img class="profile-photo" src="<?= staffImage($salary_payment_info->user_id); ?>" alt="<?= $salary_payment_info->fullname ?>"></td>
                <td style="border: 0;">
                    <p class="employee-name"><?= $salary_payment_info->fullname ?></p>
                    <table>
                        <tr class="detail-row"><td class="label"><?= lang('emp_id') ?></td><td class="value"><?= $salary_payment_info->employment_id ?></td></tr>
                        <tr class="detail-row"><td class="label"><?= lang('departments') ?></td><td class="value"><?= $salary_payment_info->deptname ?></td></tr>
                        <tr class="detail-row"><td class="label"><?= lang('designation') ?></td><td class="value"><?= $salary_payment_info->designations ?></td></tr>
                        <tr class="detail-row"><td class="label"><?= lang('joining_date') ?></td><td class="value"><?= display_date($salary_payment_info->joining_date) ?></td></tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>

    <table class="metrics">
        <tr>
            <td class="metric-card"><div class="metric-label"><?= lang('salary_month') ?></div><p class="metric-value"><?= date('F Y', safe_strtotime($salary_payment_info->payment_month)) ?></p><div class="metric-note"><?= lang('payment_cycle') ?></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_salary, $currency->symbol) ?></p><div class="metric-note"><?= lang('allowances') ?>: <?= display_money($total_allowance, $currency->symbol) ?></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($total_deduction, $currency->symbol) ?></p><div class="metric-note"><?= !empty($salary_payment_info->fine_deduction) ? lang('fine_deduction') . ': ' . display_money($salary_payment_info->fine_deduction, $currency->symbol) : lang('standard') ?></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_amount, $currency->symbol) ?></p><div class="metric-note"><?= lang('net_salary') ?>: <?= display_money($net_salary, $currency->symbol) ?></div></td>
        </tr>
    </table>

    <div class="table-shell">
        <table>
            <thead><tr class="table-head"><td><?= lang('salary_details') ?></td><td class="number"><?= lang('amount') ?></td></tr></thead>
            <tbody>
            <?php if (!empty($salary_payment_details_info)): foreach ($salary_payment_details_info as $index => $v_payment_details): ?>
                <?php
                $label = $v_payment_details->salary_payment_details_label;
                if ($label == 'overtime_salary' || $label == 'hourly_rates') {
                    $small = ($label == 'overtime_salary') ? ' (' . lang('per_hour') . ')' : '';
                    $label = lang($label) . $small;
                }
                ?>
                <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>">
                    <td class="strong"><?= $label ?></td>
                    <td class="number"><?php if (is_numeric($v_payment_details->salary_payment_details_value)) { echo display_money($v_payment_details->salary_payment_details_value, $currency->symbol); } else { echo $v_payment_details->salary_payment_details_value; } ?></td>
                </tr>
            <?php endforeach; ?>
            <?php else: ?>
                <tr class="row"><td colspan="2" class="muted"><?= lang('nothing_to_display') ?></td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if (!empty($allowance_info)) { ?>
    <div class="table-shell">
        <table>
            <thead><tr class="table-head"><td><?= lang('allowances') ?></td><td class="number"><?= lang('amount') ?></td></tr></thead>
            <tbody>
            <?php foreach ($allowance_info as $index => $v_allowance): ?>
                <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>"><td class="strong"><?= $v_allowance->salary_payment_allowance_label ?></td><td class="number"><?= display_money($v_allowance->salary_payment_allowance_value, $currency->symbol) ?></td></tr>
            <?php endforeach; ?>
                <tr class="total-row"><td><?= lang('total') ?></td><td class="number"><?= display_money($total_allowance, $currency->symbol) ?></td></tr>
            </tbody>
        </table>
    </div>
    <?php } ?>

    <?php if (!empty($deduction_info)) { ?>
    <div class="table-shell">
        <table>
            <thead><tr class="table-head"><td><?= lang('deductions') ?></td><td class="number"><?= lang('amount') ?></td></tr></thead>
            <tbody>
            <?php foreach ($deduction_info as $index => $v_deduction): ?>
                <tr class="row<?= ($index % 2 !== 0) ? ' row-alt' : '' ?>"><td class="strong"><?= $v_deduction->salary_payment_deduction_label ?></td><td class="number"><?= display_money($v_deduction->salary_payment_deduction_value, $currency->symbol) ?></td></tr>
            <?php endforeach; ?>
                <tr class="total-row"><td><?= lang('total') ?></td><td class="number"><?= display_money($total_deduction, $currency->symbol) ?></td></tr>
            </tbody>
        </table>
    </div>
    <?php } ?>

    <div class="table-shell">
        <table>
            <thead><tr class="table-head"><td><?= lang('total_salary_details') ?></td><td class="number"><?= lang('amount') ?></td></tr></thead>
            <tbody>
                <tr class="row"><td class="strong"><?= lang('gross_salary') ?></td><td class="number"><?= display_money($gross_salary, $currency->symbol) ?></td></tr>
                <tr class="row row-alt"><td class="strong"><?= lang('total_deduction') ?></td><td class="number"><?= display_money($total_deduction, $currency->symbol) ?></td></tr>
                <tr class="row"><td class="strong"><?= lang('net_salary') ?></td><td class="number"><?= display_money($net_salary, $currency->symbol) ?></td></tr>
                <?php if (!empty($salary_payment_info->fine_deduction)) { ?><tr class="row row-alt"><td class="strong"><?= lang('fine_deduction') ?></td><td class="number"><?= display_money($salary_payment_info->fine_deduction, $currency->symbol) ?></td></tr><?php } ?>
                <tr class="total-row"><td><?= lang('paid_amount') ?></td><td class="number"><?= display_money($paid_amount, $currency->symbol) ?></td></tr>
            </tbody>
        </table>
    </div>

    <?php
    $payment_method = '';
    if (!empty($salary_payment_info->payment_type)) {
        $payment_method_value = !empty($salary_payment_info->payment_type) ? $salary_payment_info->payment_type : (!empty($salary_payment_info->payment_method) ? $salary_payment_info->payment_method : (!empty($salary_payment_info->payment_methods_id) ? $salary_payment_info->payment_methods_id : ''));
        if (is_numeric($payment_method_value)) {
            $payment_methods = get_row('tbl_payment_methods', array('payment_methods_id' => $payment_method_value));
        } else {
            $payment_methods = (object) array('method_name' => $payment_method_value);
        }
        if (!empty($payment_methods->method_name)) {
            $payment_method = $payment_methods->method_name;
        }
    }
    $account_name = '';
    $role = $this->session->userdata('user_type');
    if ($role == 1 && !empty($salary_payment_info->deduct_from)) {
        $account_info = $this->payroll_model->check_by(array('account_id' => $salary_payment_info->deduct_from), 'tbl_accounts');
        if (!empty($account_info)) {
            $account_name = $account_info->account_name;
        }
    }
    ?>
    <?php if (!empty($payment_method) || !empty($salary_payment_info->comments) || !empty($account_name)) { ?>
    <div class="table-shell">
        <table>
            <thead><tr class="table-head"><td colspan="2"><?= lang('payment_details') ?></td></tr></thead>
            <tbody>
                <?php if (!empty($payment_method)) { ?><tr class="row"><td class="strong"><?= lang('payment_method') ?></td><td><?= $payment_method ?></td></tr><?php } ?>
                <?php if (!empty($account_name)) { ?><tr class="row row-alt"><td class="strong"><?= lang('deduct_from') ?></td><td><?= $account_name ?></td></tr><?php } ?>
                <?php if (!empty($salary_payment_info->comments)) { ?><tr class="row"><td class="strong"><?= lang('comments') ?></td><td><?= $salary_payment_info->comments ?></td></tr><?php } ?>
            </tbody>
        </table>
    </div>
    <?php } ?>
</div>
</body>
</html>
