<?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 : '$';
$company_name = config_item('company_name');
$company_logo = config_item('company_logo');
$company_logo_url = !empty($company_logo) ? base_url($company_logo) : '';
$company_email = config_item('company_email');
$is_email_render = $this->router->fetch_method() === 'send_payslip';

$user_info = $this->db->where('user_id', $employee_salary_info->user_id)->get('tbl_users')->row();
$employee_email = !empty($user_info->email) ? $user_info->email : '-';
$employee_name = !empty($employee_salary_info->fullname) ? $employee_salary_info->fullname : '-';
$salary_month = !empty($employee_salary_info->payment_month) ? date('F Y', safe_strtotime($employee_salary_info->payment_month)) : '-';
$payslip_no = !empty($payslip_number) ? $payslip_number : '-';
$employment_id = !empty($employee_salary_info->employment_id) ? $employee_salary_info->employment_id : '-';
$designation = !empty($employee_salary_info->designations) ? $employee_salary_info->designations : '-';
$department = !empty($employee_salary_info->deptname) ? $employee_salary_info->deptname : '-';
$mobile = !empty($employee_salary_info->mobile) ? $employee_salary_info->mobile : '-';
$joining_date = !empty($employee_salary_info->joining_date) ? display_date($employee_salary_info->joining_date) : '-';
$address = !empty($employee_salary_info->present_address) ? $employee_salary_info->present_address : '-';
$tagline = $company_email ? $company_email : 'ULTIMATE PROJECT MANAGER CRM PRO';

$payment_rows = array();
$total_hours_amount = 0;
$hourly_rate = 0;
if (!empty($salary_payment_details_info)) {
    foreach ($salary_payment_details_info as $payment_detail) {
        $label_key = $payment_detail->salary_payment_details_label;
        $label = $label_key;
        if ($label_key === 'overtime_salary') {
            $label = lang('overtime_salary') . ' (' . lang('per_hour') . ')';
        } elseif ($label_key === 'hourly_rates') {
            $label = lang('hourly_rates');
        } elseif (lang($label_key) !== $label_key) {
            $label = lang($label_key);
        }

        $raw_value = $payment_detail->salary_payment_details_value;
        $formatted_value = $raw_value;
        if (is_numeric($raw_value)) {
            $numeric_value = (float) $raw_value;
            if ($label_key === 'overtime_salary' || $label_key === 'hourly_rates') {
                $hourly_rate = $numeric_value;
            }
            $total_hours_amount += $numeric_value;
            $formatted_value = display_money($numeric_value, $currency_symbol);
        }

        $payment_rows[] = array(
            'label' => $label,
            'value' => $formatted_value,
            'numeric' => is_numeric($raw_value),
            'is_emphasis' => in_array($label_key, array('basic_salary', 'overtime_salary', 'overtime_amount'), true),
        );
    }
}

$allowance_total = 0;
$allowance_rows = array();
if (!empty($allowance_info)) {
    foreach ($allowance_info as $allowance) {
        $allowance_total += (float) $allowance->salary_payment_allowance_value;
        $allowance_rows[] = array(
            'label' => $allowance->salary_payment_allowance_label,
            'value' => display_money($allowance->salary_payment_allowance_value, $currency_symbol),
        );
    }
}

$deduction_total = 0;
$deduction_rows = array();
if (!empty($deduction_info)) {
    foreach ($deduction_info as $deduction) {
        $deduction_total += (float) $deduction->salary_payment_deduction_value;
        $deduction_rows[] = array(
            'label' => $deduction->salary_payment_deduction_label,
            'value' => display_money($deduction->salary_payment_deduction_value, $currency_symbol),
        );
    }
}

$gross_salary = $total_hours_amount + $allowance_total - $hourly_rate;
$net_salary = $gross_salary - $deduction_total;
$fine_deduction = !empty($employee_salary_info->fine_deduction) ? (float) $employee_salary_info->fine_deduction : 0;
$paid_amount = $net_salary - $fine_deduction;

$earnings_rows = array_merge($payment_rows, $allowance_rows);
$earnings_empty = empty($earnings_rows);
$deductions_empty = empty($deduction_rows);
?>

<div class="mx-auto w-full max-w-[1120px] space-y-4">
    <?php if (!$is_email_render) { ?>
        <div class="hidden-print flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
            <div>
                <p class="text-sm font-medium text-muted-foreground">Document Preview</p>
            </div>
            <div class="flex flex-wrap items-center gap-2">
                <a href="<?= base_url('admin/payroll/send_payslip/' . $employee_salary_info->salary_payment_id) ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground shadow-sm transition hover:bg-background" title="<?= lang('send_email') ?>">
                    <i data-lucide="mail" class="h-4 w-4"></i>
                    <span><?= lang('send_email') ?></span>
                </a>
                <button type="button" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground shadow-sm transition hover:bg-background" onclick="printPayslip()" title="<?= lang('print') ?>">
                    <i data-lucide="printer" class="h-4 w-4"></i>
                    <span><?= lang('print') ?></span>
                </button>
                <a href="<?= base_url('admin/payroll/salary_payment_details_pdf/' . $employee_salary_info->salary_payment_id) ?>" class="inline-flex items-center gap-2 rounded-lg bg-primary px-3 py-2 text-xs font-semibold text-primary-foreground shadow-sm transition hover:opacity-90" title="<?= lang('download') ?>">
                    <i data-lucide="download" class="h-4 w-4"></i>
                    <span>Download PDF</span>
                </a>
            </div>
        </div>
    <?php } ?>

    <div id="payslipPrintableArea" class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-5 py-5 sm:px-6 sm:py-6">
            <div class="flex flex-col gap-6 lg:flex-row lg:items-start lg:justify-between">
                <div class="flex min-w-0 items-center gap-4">
                    <div class="flex h-14 w-14 shrink-0 items-center justify-center overflow-hidden rounded-xl bg-primary text-primary-foreground shadow-sm">
                        <?php if (!empty($company_logo_url)) { ?>
                            <img src="<?= $company_logo_url ?>" alt="<?= html_escape($company_name) ?>" class="h-full w-full object-contain">
                        <?php } else { ?>
                            <span class="text-lg font-bold"><?= html_escape(strtoupper(substr($company_name, 0, 1))) ?></span>
                        <?php } ?>
                    </div>
                    <div class="min-w-0">
                        <h1 class="truncate text-2xl font-bold tracking-tight text-foreground"><?= html_escape($company_name) ?></h1>
                        <p class="mt-1 text-xs font-semibold uppercase tracking-[0.14em] text-muted-foreground"><?= html_escape($tagline) ?></p>
                    </div>
                </div>

                <div class="lg:text-right">
                    <p class="text-3xl font-light tracking-[0.16em] text-muted-foreground/80">PAYSLIP</p>
                    <div class="mt-2 space-y-1 text-sm text-foreground">
                        <p><span class="font-semibold"><?= lang('salary_month') ?>:</span> <?= html_escape($salary_month) ?></p>
                        <p><span class="font-semibold"><?= lang('payslip_no') ?>:</span> <?= html_escape($payslip_no) ?></p>
                    </div>
                </div>
            </div>
        </div>

        <div class="space-y-6 px-5 py-5 sm:px-6 sm:py-6">
            <section class="space-y-3">                
                <div class="rounded-2xl border border-border bg-background/40 p-4">
                    <div class="grid gap-x-6 gap-y-5 md:grid-cols-2 xl:grid-cols-3">
                        <div>
                            <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('name') ?></p>
                            <p class="mt-1.5 text-sm font-semibold text-foreground"><?= html_escape($employee_name) ?></p>
                        </div>
                        <div>
                            <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('employment_id') ?></p>
                            <p class="mt-1.5 text-sm font-semibold text-foreground"><?= html_escape($employment_id) ?></p>
                        </div>
                        <div>
                            <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('designation') ?></p>
                            <p class="mt-1.5 text-sm font-semibold text-foreground"><?= html_escape($designation) ?></p>
                        </div>
                        <div>
                            <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('departments') ?></p>
                            <p class="mt-1.5 text-sm font-semibold text-foreground"><?= html_escape($department) ?></p>
                        </div>
                        <div>
                            <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('email') ?></p>
                            <p class="mt-1.5 text-sm font-semibold text-foreground"><?= html_escape($employee_email) ?></p>
                        </div>
                        <div>
                            <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('mobile') ?></p>
                            <p class="mt-1.5 text-sm font-semibold text-foreground"><?= html_escape($mobile) ?></p>
                        </div>
                        <div>
                            <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('joining_date') ?></p>
                            <p class="mt-1.5 text-sm font-semibold text-foreground"><?= html_escape($joining_date) ?></p>
                        </div>
                        <div class="md:col-span-2 xl:col-span-2">
                            <p class="text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground"><?= lang('address') ?></p>
                            <p class="mt-1.5 text-sm font-semibold text-foreground"><?= html_escape($address) ?></p>
                        </div>
                    </div>
                </div>
            </section>

            <section class="grid gap-6 lg:grid-cols-2">
                <div class="space-y-2.5">
                    <h3 class="text-xs font-bold text-emerald-600 uppercase tracking-wider mb-3 pb-2 border-b border-emerald-200">
                        <?= lang('earning') ?>
                    </h3>
                    <div class="overflow-hidden rounded-2xl border border-border bg-background/30">
                        <div class="flex items-center justify-between gap-4 border-b border-border bg-background/50 px-4 py-2.5 text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground">
                            <span><?= lang('type_of_pay') ?></span>
                            <span><?= lang('amount') ?></span>
                        </div>
                        <?php if ($earnings_empty) { ?>
                            <div class="px-4 py-3.5 text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></div>
                        <?php } else { ?>
                            <?php foreach ($earnings_rows as $index => $row) { ?>
                                <div class="flex items-center justify-between gap-4 px-4 py-3 <?= $index !== count($earnings_rows) - 1 ? 'border-b border-border' : '' ?>">
                                    <span class="min-w-0 pr-3 text-sm <?= !empty($row['is_emphasis']) ? 'font-semibold text-foreground' : 'font-medium text-foreground' ?>"><?= html_escape($row['label']) ?></span>
                                    <span class="shrink-0 text-right text-sm font-semibold text-foreground"><?= $row['value'] ?></span>
                                </div>
                            <?php } ?>
                        <?php } ?>
                    </div>
                </div>

                <div class="space-y-2.5">
                    <div class="border-b border-rose-200 pb-2 dark:border-rose-100">
                        <h3 class="text-xs font-bold uppercase tracking-[0.16em] text-rose-600 dark:text-rose-400"><?= lang('deductions') ?></h3>
                    </div>
                    <div class="overflow-hidden rounded-2xl border border-border bg-background/30">
                        <div class="flex items-center justify-between gap-4 border-b border-border bg-background/50 px-4 py-2.5 text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground">
                            <span><?= lang('type_of_pay') ?></span>
                            <span><?= lang('amount') ?></span>
                        </div>
                        <?php if ($deductions_empty) { ?>
                            <div class="px-4 py-3.5 text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></div>
                        <?php } else { ?>
                            <?php foreach ($deduction_rows as $index => $row) { ?>
                                <div class="flex items-center justify-between gap-4 px-4 py-3 <?= $index !== count($deduction_rows) - 1 ? 'border-b border-border' : '' ?>">
                                    <span class="min-w-0 pr-3 text-sm font-medium text-foreground"><?= html_escape($row['label']) ?></span>
                                    <span class="shrink-0 text-right text-sm font-semibold text-rose-600 dark:text-rose-400"><?= $row['value'] ?></span>
                                </div>
                            <?php } ?>
                        <?php } ?>
                    </div>
                </div>
            </section>

            <section class="border-t border-border pt-6">
                <div class="grid gap-5 lg:grid-cols-12 lg:items-start">
                    <div class="rounded-2xl border border-border bg-background/40 p-4 lg:col-span-8">
                        <p class="text-sm font-semibold text-foreground">Important Note:</p>
                        <p class="mt-2 text-sm leading-5 text-muted-foreground">
                            This is a computer-generated document and does not require a physical signature. If you have any questions regarding your payslip, please contact the HR department.
                        </p>
                    </div>

                    <div class="space-y-2 lg:col-span-4">
                        <div class="flex items-center justify-between gap-4 px-2 py-1 text-sm">
                            <span class="font-medium text-foreground"><?= lang('gross_salary') ?></span>
                            <span class="shrink-0 font-semibold text-foreground"><?= display_money($gross_salary, $currency_symbol) ?></span>
                        </div>
                        <div class="flex items-center justify-between gap-4 px-2 py-1 text-sm">
                            <span class="font-medium text-foreground"><?= lang('total_deduction') ?></span>
                            <span class="shrink-0 font-semibold text-rose-600 dark:text-rose-400">- <?= display_money($deduction_total, $currency_symbol) ?></span>
                        </div>
                        <div class="flex items-center justify-between gap-4 px-2 py-1 text-sm">
                            <span class="font-medium text-foreground"><?= lang('net_salary') ?></span>
                            <span class="shrink-0 font-semibold text-foreground"><?= display_money($net_salary, $currency_symbol) ?></span>
                        </div>
                        <div class="flex items-center justify-between gap-4 border-b border-border px-2 py-1 text-sm">
                            <span class="font-medium text-foreground"><?= lang('fine_deduction') ?></span>
                            <span class="shrink-0 font-semibold <?= $fine_deduction > 0 ? 'text-rose-600 dark:text-rose-400' : 'text-muted-foreground' ?>">
                                <?= $fine_deduction > 0 ? '- ' . display_money($fine_deduction, $currency_symbol) : display_money(0, $currency_symbol) ?>
                            </span>
                        </div>

                        <div class="rounded-2xl bg-slate-900 px-4 py-3.5 text-slate-50 dark:bg-slate-950">
                            <div class="flex items-center justify-between gap-4">
                                <span class="text-sm font-bold uppercase tracking-[0.14em] text-slate-300"><?= lang('paid_amount') ?></span>
                                <span class="text-2xl font-bold tracking-tight text-emerald-400"><?= display_money($paid_amount, $currency_symbol) ?></span>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>

<?php if (!$is_email_render) { ?>
    <script type="text/javascript">
        function printPayslip() {
            window.print();
        }
    </script>
<?php } ?>
