<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
$currency_symbol = !empty($currency->symbol) ? $currency->symbol : '$';

$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;
    }
}

$total_hours_amount = 0;
$rate = 0;
if (!empty($salary_payment_details_info)) {
    foreach ($salary_payment_details_info as $payment_detail) {
        if (is_numeric($payment_detail->salary_payment_details_value)) {
            if ($payment_detail->salary_payment_details_label == 'overtime_salary' || $payment_detail->salary_payment_details_label == 'hourly_rates') {
                $rate = (float) $payment_detail->salary_payment_details_value;
            }
            $total_hours_amount += (float) $payment_detail->salary_payment_details_value;
        }
    }
}

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

$total_deduction = 0;
if (!empty($deduction_info)) {
    foreach ($deduction_info as $deduction) {
        $total_deduction += (float) $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;
$employee_id = !empty($salary_payment_info->employment_id) ? $salary_payment_info->employment_id : (!empty($salary_payment_info->user_id) ? 'EMP-' . str_pad($salary_payment_info->user_id, 5, '0', STR_PAD_LEFT) : '-');
$joining_date = !empty($salary_payment_info->joining_date) ? display_date($salary_payment_info->joining_date) : '-';
$department_name = !empty($salary_payment_info->deptname) ? $salary_payment_info->deptname : '-';
$designation_name = !empty($salary_payment_info->designations) ? $salary_payment_info->designations : '-';
$salary_month = !empty($salary_payment_info->payment_month) ? date('F Y', safe_strtotime($salary_payment_info->payment_month)) : '-';
$logo_path = base_url() . config_item('company_logo');
$company_name = config_item('company_name');
?>

<div id="printableArea">
    <div class="border-b border-border bg-card px-5 py-4">
        <div class="flex items-start justify-between gap-4">
            <div class="flex min-w-0 items-center gap-3">
                <div class="flex h-10 w-10 items-center justify-center overflow-hidden rounded-lg bg-primary/10 text-primary">
                    <?php if (!empty(config_item('company_logo'))) : ?>
                        <img src="<?= $logo_path ?>" alt="<?= html_escape($company_name) ?>" class="h-full w-full object-contain">
                    <?php else : ?>
                        <span class="text-sm font-bold"><?= strtoupper(substr($company_name, 0, 1)) ?></span>
                    <?php endif; ?>
                </div>
                <div class="min-w-0">
                    <h4 class="truncate text-[1rem] font-semibold text-foreground"><?= html_escape($company_name) ?></h4>
                    <p class="text-[0.85rem] text-muted-foreground"><?= config_item('company_email') ? html_escape(config_item('company_email')) : 'Ultimate Project Manager CRM PRO' ?></p>
                </div>
            </div>

            <div class="flex items-center gap-2">
                <a href="<?= base_url('admin/payroll/send_payslip/' . $salary_payment_info->salary_payment_id) ?>"
                    class="inline-flex h-9 w-9 items-center justify-center rounded-lg border border-border bg-background text-muted-foreground transition hover:bg-muted hover:text-foreground"
                    title="<?= lang('send_email') ?>">
                    <i data-lucide="mail" class="h-4 w-4"></i>
                </a>
                <button class="inline-flex h-9 w-9 items-center justify-center rounded-lg border border-border bg-background text-muted-foreground transition hover:bg-muted hover:text-foreground"
                    type="button"
                    title="<?= lang('print') ?>"
                    onclick="printDiv('printableArea')">
                    <i data-lucide="printer" class="h-4 w-4"></i>
                </button>
                <a href="<?= base_url('admin/payroll/salary_payment_details_pdf/' . $salary_payment_info->salary_payment_id) ?>"
                    class="inline-flex items-center gap-2 rounded-xl border border-border bg-background px-4 py-2 text-[0.95rem] font-medium text-foreground shadow-sm transition hover:bg-muted"
                    title="<?= lang('download') ?>">
                    <i data-lucide="download" class="h-4 w-4"></i>
                    <span>Export PDF</span>
                </a>
            </div>
        </div>
    </div>

    <div class="bg-background px-5 py-6">
        <div class="space-y-6">
            <div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
                <div class="flex flex-col gap-5 lg:flex-row lg:items-center lg:justify-between">
                    <div class="flex min-w-0 items-center gap-5">
                        <div class="flex h-24 w-24 shrink-0 items-center justify-center overflow-hidden rounded-full border-4 border-background bg-muted shadow-sm">
                            <?php if (!empty($salary_payment_info->avatar)) : ?>
                                <img src="<?= base_url() . $salary_payment_info->avatar ?>" alt="<?= html_escape($salary_payment_info->fullname) ?>" class="h-full w-full object-cover">
                            <?php else : ?>
                                <img src="<?= base_url('img/user.png') ?>" alt="Employee" class="h-14 w-14 object-contain opacity-70">
                            <?php endif; ?>
                        </div>

                        <div class="min-w-0">
                            <h3 class="truncate text-[1.55rem] font-semibold text-foreground"><?= html_escape($salary_payment_info->fullname) ?></h3>
                            <div class="mt-2 inline-flex rounded-full border border-primary/15 bg-primary/10 px-3 py-1 text-[0.85rem] font-semibold text-primary">
                                <?= html_escape($salary_month) ?>
                            </div>
                        </div>
                    </div>

                    <div class="lg:text-right">
                        <div class="text-[0.8rem] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('emp_id') ?></div>
                        <div class="mt-1 text-[1.2rem] font-semibold text-foreground"><?= html_escape($employee_id) ?></div>
                    </div>
                </div>

                <div class="mt-5 border-t border-border pt-5">
                    <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
                        <div>
                            <div class="flex items-center gap-2 text-[0.8rem] font-semibold uppercase tracking-[0.1em] text-muted-foreground">
                                <i data-lucide="briefcase-business" class="h-4 w-4"></i>
                                <span><?= lang('departments') ?></span>
                            </div>
                            <div class="mt-2 text-[1.1rem] font-semibold text-foreground"><?= html_escape($department_name) ?></div>
                        </div>

                        <div>
                            <div class="flex items-center gap-2 text-[0.8rem] font-semibold uppercase tracking-[0.1em] text-muted-foreground">
                                <i data-lucide="building-2" class="h-4 w-4"></i>
                                <span><?= lang('designation') ?></span>
                            </div>
                            <div class="mt-2 text-[1.1rem] font-semibold text-foreground"><?= html_escape($designation_name) ?></div>
                        </div>

                        <div>
                            <div class="flex items-center gap-2 text-[0.8rem] font-semibold uppercase tracking-[0.1em] text-muted-foreground">
                                <i data-lucide="calendar-days" class="h-4 w-4"></i>
                                <span><?= lang('joining_date') ?></span>
                            </div>
                            <div class="mt-2 text-[1.1rem] font-semibold text-foreground"><?= html_escape($joining_date) ?></div>
                        </div>

                        <div>
                            <div class="flex items-center gap-2 text-[0.8rem] font-semibold uppercase tracking-[0.1em] text-muted-foreground">
                                <i data-lucide="clock-3" class="h-4 w-4"></i>
                                <span><?= lang('payment_month') ?></span>
                            </div>
                            <div class="mt-2 text-[1.1rem] font-semibold text-foreground"><?= html_escape($salary_month) ?></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="grid grid-cols-1 gap-6 lg:grid-cols-2">
                <div>
                    <div class="flex items-center gap-2 text-[1.05rem] font-semibold uppercase tracking-[0.08em] text-foreground">
                        <i data-lucide="badge-dollar-sign" class="h-4 w-4 text-primary"></i>
                        <span><?= lang('salary_details') ?></span>
                    </div>
                    <div class="mt-3 border-t border-border pt-4">
                        <div class="space-y-3">
                            <?php if (!empty($salary_payment_details_info)) : ?>
                                <?php foreach ($salary_payment_details_info as $payment_detail) : ?>
                                    <?php
                                    $label = $payment_detail->salary_payment_details_label;
                                    if ($label == 'overtime_salary' || $label == 'hourly_rates') {
                                        $label = lang($label) . ($label == lang('overtime_salary') ? ' (' . lang('per_hour') . ')' : '');
                                    }
                                    $value = is_numeric($payment_detail->salary_payment_details_value)
                                        ? display_money($payment_detail->salary_payment_details_value, $currency_symbol)
                                        : $payment_detail->salary_payment_details_value;
                                    ?>
                                    <div class="flex items-center justify-between gap-4 text-[1rem] text-foreground">
                                        <span><?= html_escape($label) ?></span>
                                        <span class="font-medium"><?= $value ?></span>
                                    </div>
                                <?php endforeach; ?>
                            <?php else : ?>
                                <div class="text-[0.95rem] text-muted-foreground"><?= lang('nothing_to_display') ?></div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <div>
                    <div class="flex items-center gap-2 text-[1.05rem] font-semibold uppercase tracking-[0.08em] text-foreground">
                        <i data-lucide="receipt-text" class="h-4 w-4 text-primary"></i>
                        <span><?= lang('payment_details') ?></span>
                    </div>
                    <div class="mt-3 border-t border-border pt-4">
                        <div class="space-y-3">
                            <div class="flex items-center justify-between gap-4 text-[1rem] text-foreground">
                                <span><?= lang('paid_amount') ?></span>
                                <span class="font-medium"><?= display_money($paid_amount, $currency_symbol) ?></span>
                            </div>
                            <?php if (!empty($salary_payment_info->fine_deduction)) : ?>
                                <div class="flex items-center justify-between gap-4 text-[1rem] text-foreground">
                                    <span><?= lang('fine_deduction') ?></span>
                                    <span class="font-medium text-destructive">- <?= display_money($salary_payment_info->fine_deduction, $currency_symbol) ?></span>
                                </div>
                            <?php endif; ?>
                            <?php if (!empty($payment_method)) : ?>
                                <div class="flex items-center justify-between gap-4 text-[1rem] text-foreground">
                                    <span><?= lang('payment_method') ?></span>
                                    <span class="font-medium"><?= html_escape($payment_method) ?></span>
                                </div>
                            <?php endif; ?>
                            <?php if (!empty($account_name)) : ?>
                                <div class="flex items-center justify-between gap-4 text-[1rem] text-foreground">
                                    <span><?= lang('deduct_from') ?></span>
                                    <span class="font-medium"><?= html_escape($account_name) ?></span>
                                </div>
                            <?php endif; ?>
                            <?php if (!empty($salary_payment_info->comments)) : ?>
                                <div class="rounded-xl border border-border bg-muted/30 p-3 text-[0.95rem] text-muted-foreground">
                                    <div class="mb-1 text-[0.8rem] font-semibold uppercase tracking-[0.1em] text-foreground/80"><?= lang('comments') ?></div>
                                    <div><?= nl2br(html_escape($salary_payment_info->comments)) ?></div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <?php if (!empty($allowance_info) || !empty($deduction_info)) : ?>
                <div class="grid grid-cols-1 gap-6 lg:grid-cols-2">
                    <div>
                        <div class="flex items-center gap-2 text-[1.05rem] font-semibold uppercase tracking-[0.08em] text-foreground">
                            <i data-lucide="circle-dollar-sign" class="h-4 w-4 text-primary"></i>
                            <span><?= lang('allowances') ?></span>
                        </div>
                        <div class="mt-3 border-t border-border pt-4">
                            <?php if (!empty($allowance_info)) : ?>
                                <div class="space-y-3">
                                    <?php foreach ($allowance_info as $allowance) : ?>
                                        <div class="flex items-center justify-between gap-4 text-[1rem] text-foreground">
                                            <span><?= html_escape($allowance->salary_payment_allowance_label) ?></span>
                                            <span class="font-medium"><?= display_money($allowance->salary_payment_allowance_value, $currency_symbol) ?></span>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php else : ?>
                                <div class="text-[0.95rem] text-muted-foreground"><?= lang('nothing_to_display') ?></div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div>
                        <div class="flex items-center gap-2 text-[1.05rem] font-semibold uppercase tracking-[0.08em] text-foreground">
                            <i data-lucide="badge-percent" class="h-4 w-4 text-primary"></i>
                            <span><?= lang('deductions') ?></span>
                        </div>
                        <div class="mt-3 border-t border-border pt-4">
                            <?php if (!empty($deduction_info)) : ?>
                                <div class="space-y-3">
                                    <?php foreach ($deduction_info as $deduction) : ?>
                                        <div class="flex items-center justify-between gap-4 text-[1rem] text-foreground">
                                            <span><?= html_escape($deduction->salary_payment_deduction_label) ?></span>
                                            <span class="font-medium text-destructive">- <?= display_money($deduction->salary_payment_deduction_value, $currency_symbol) ?></span>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php else : ?>
                                <div class="text-[0.95rem] text-muted-foreground"><?= lang('nothing_to_display') ?></div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endif; ?>

            <div class="rounded-2xl border border-border bg-card px-6 py-5 shadow-sm">
                <div class="grid grid-cols-1 gap-6 lg:grid-cols-[minmax(0,1fr)_220px] lg:items-center">
                    <div class="grid gap-3 text-[1rem]">
                        <div class="flex items-center justify-between gap-4">
                            <span class="text-muted-foreground"><?= lang('gross_salary') ?></span>
                            <span class="font-semibold text-foreground"><?= display_money($gross_salary, $currency_symbol) ?></span>
                        </div>
                        <div class="flex items-center justify-between gap-4">
                            <span class="text-muted-foreground"><?= lang('total_deduction') ?></span>
                            <span class="font-semibold text-destructive">- <?= display_money($total_deduction, $currency_symbol) ?></span>
                        </div>
                    </div>

                    <div class="border-t border-border pt-4 text-right lg:border-l lg:border-t-0 lg:pl-8 lg:pt-0">
                        <div class="text-[0.8rem] font-semibold uppercase tracking-[0.14em] text-primary"><?= lang('net_salary') ?></div>
                        <div class="mt-2 text-[2.25rem] font-semibold tracking-[-0.03em] text-foreground"><?= display_money($net_salary, $currency_symbol) ?></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal-footer hidden-print flex items-center justify-end border-t border-border bg-card px-5 py-4">
    <button type="button" class="inline-flex items-center gap-2 rounded-xl border border-border bg-background px-4 py-2 text-[0.95rem] font-medium text-foreground shadow-sm transition hover:bg-muted" data-dismiss="modal">
        <i data-lucide="x" class="h-4 w-4"></i>
        <?= lang('close') ?>
    </button>
</div>

<script type="text/javascript">
    function printDiv(printableArea) {
        var printContents = document.getElementById(printableArea).innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        window.print();
        document.body.innerHTML = originalContents;
    }
</script>
