<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php
$emp_salary_info = $this->payroll_model->get_emp_salary_list($id);
$salary_allowance_info = array();
$salary_deduction_info = array();
$basic_salary = 0;
$curency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();

if (!empty($emp_salary_info)) {
    $this->payroll_model->_table_name = "tbl_salary_allowance";
    $this->payroll_model->_order_by = "salary_allowance_id";
    $salary_allowance_info = $this->payroll_model->get_by(array('salary_template_id' => $emp_salary_info->salary_template_id), FALSE);

    $this->payroll_model->_table_name = "tbl_salary_deduction";
    $this->payroll_model->_order_by = "salary_deduction_id";
    $salary_deduction_info = $this->payroll_model->get_by(array('salary_template_id' => $emp_salary_info->salary_template_id), FALSE);

    if (!empty($emp_salary_info->salary_template_id)) {
        $basic_salary = $emp_salary_info->basic_salary;
    } else {
        $basic_salary = $emp_salary_info->hourly_rate;
    }
    if (empty($basic_salary)) {
        $basic_salary = 0;
    }
}
?>

<div class="space-y-4">
    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="flex items-center justify-between border-b border-border px-5 py-4">
            <h4 class="text-sm font-semibold text-foreground"><?= lang('salary_details') ?></h4>
            <?php if (!empty($emp_salary_info)) { ?>
                <div><?php echo btn_pdf('admin/payroll/make_pdf/' . $emp_salary_info->user_id); ?></div>
            <?php } ?>
        </div>

        <div class="p-5">
            <?php if (!empty($emp_salary_info)) { ?>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div class="rounded-xl border border-border bg-card p-4 space-y-3">
                        <h5 class="text-sm font-semibold text-foreground"><?= lang('salary') . ' ' . lang('summary') ?></h5>
                        <div class="space-y-2 text-sm">
                            <div class="flex items-center justify-between">
                                <span class="text-muted-foreground"><?= lang('salary_grade') ?></span>
                                <span class="font-semibold text-foreground"><?php echo((!empty($emp_salary_info->salary_template_id)) ? $emp_salary_info->salary_grade : $emp_salary_info->hourly_grade); ?></span>
                            </div>
                            <div class="flex items-center justify-between">
                                <span class="text-muted-foreground"><?= lang('basic_salary') ?></span>
                                <span class="font-semibold text-foreground"><?php echo display_money($basic_salary, $curency->symbol); ?></span>
                            </div>
                            <?php if (!empty($emp_salary_info->overtime_salary)) { ?>
                                <div class="flex items-center justify-between">
                                    <span class="text-muted-foreground"><?= lang('overtime') ?> (<?= lang('per_hour') ?>)</span>
                                    <span class="font-semibold text-foreground"><?php echo display_money($emp_salary_info->overtime_salary, $curency->symbol); ?></span>
                                </div>
                            <?php } ?>
                        </div>
                    </div>

                    <?php
                    $total_salary = 0;
                    $total_deduction = 0;
                    if (!empty($salary_allowance_info)) {
                        foreach ($salary_allowance_info as $v_allowance_info) {
                            $total_salary += $v_allowance_info->allowance_value;
                        }
                    }
                    if (!empty($salary_deduction_info)) {
                        foreach ($salary_deduction_info as $v_deduction_info) {
                            $total_deduction += $v_deduction_info->deduction_value;
                        }
                    }
                    $gross_total = $total_salary + $basic_salary;
                    $net_salary = $gross_total - $total_deduction;
                    ?>

                    <div class="rounded-xl border border-border bg-card p-4 space-y-3">
                        <h5 class="text-sm font-semibold text-foreground"><?= lang('total_salary_details') ?></h5>
                        <div class="space-y-2 text-sm">
                            <div class="flex items-center justify-between">
                                <span class="text-muted-foreground"><?= lang('gross_salary') ?></span>
                                <span class="font-semibold text-foreground"><?php echo display_money($gross_total, $curency->symbol); ?></span>
                            </div>
                            <div class="flex items-center justify-between">
                                <span class="text-muted-foreground"><?= lang('total_deduction') ?></span>
                                <span class="font-semibold text-foreground"><?php echo display_money($total_deduction, $curency->symbol); ?></span>
                            </div>
                            <div class="flex items-center justify-between">
                                <span class="text-muted-foreground"><?= lang('net_salary') ?></span>
                                <span class="font-semibold text-foreground"><?php echo display_money($net_salary, $curency->symbol); ?></span>
                            </div>
                        </div>
                    </div>
                </div>

                <?php if (!empty($emp_salary_info->salary_template_id)) { ?>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-4">
                        <div class="rounded-xl border border-border bg-card p-4">
                            <h5 class="text-sm font-semibold text-foreground mb-3"><?= lang('allowances') ?></h5>
                            <?php if (!empty($salary_allowance_info)) : ?>
                                <div class="space-y-2 text-sm">
                                    <?php foreach ($salary_allowance_info as $v_allowance_info) : ?>
                                        <div class="flex items-center justify-between">
                                            <span class="text-muted-foreground"><?php echo $v_allowance_info->allowance_label; ?></span>
                                            <span class="font-semibold text-foreground"><?php echo display_money($v_allowance_info->allowance_value, $curency->symbol) ?></span>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php else: ?>
                                <p class="text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></p>
                            <?php endif; ?>
                        </div>

                        <div class="rounded-xl border border-border bg-card p-4">
                            <h5 class="text-sm font-semibold text-foreground mb-3"><?= lang('deductions') ?></h5>
                            <?php if (!empty($salary_deduction_info)) : ?>
                                <div class="space-y-2 text-sm">
                                    <?php foreach ($salary_deduction_info as $v_deduction_info) : ?>
                                        <div class="flex items-center justify-between">
                                            <span class="text-muted-foreground"><?php echo $v_deduction_info->deduction_label; ?></span>
                                            <span class="font-semibold text-foreground"><?php echo display_money($v_deduction_info->deduction_value, $curency->symbol); ?></span>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php else: ?>
                                <p class="text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></p>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php } ?>
            <?php } else { ?>
                <p class="text-sm text-muted-foreground"><?= lang('no_data') ?></p>
            <?php } ?>
        </div>
    </div>
</div>
