<?php echo message_box('success'); ?>
<?php echo message_box('error');
$edited = can_action('92', 'edited');
$deleted = can_action('92', 'deleted');
$department_options = array('' => lang('select_department'));
if (!empty($all_department_info)) {
    foreach ($all_department_info as $v_department_info) {
        if (!empty($v_department_info->deptname)) {
            $deptname = $v_department_info->deptname;
        } else {
            $deptname = lang('undefined_department');
        }
        $department_options[$v_department_info->departments_id] = $deptname;
    }
}
$staff_department_name = lang('undefined_department');
if (!empty($staff_details) && is_object($staff_details) && !empty($staff_details->deptname)) {
    $staff_department_name = $staff_details->deptname;
} elseif (!empty($employee_info) && is_object($employee_info) && !empty($employee_info->deptname)) {
    $staff_department_name = $employee_info->deptname;
}
?>

<div class="space-y-4">
    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-5 py-4">
            <div class="flex items-center gap-2 text-sm font-semibold text-foreground">
                <i data-lucide="wallet" class="h-5 w-5"></i>
                <span><?= lang('make_payment') ?></span>
            </div>
        </div>
        <div class="p-5">
            <form id="form" role="form" enctype="multipart/form-data"
                action="<?php echo base_url() ?>admin/payroll/make_payment" method="post"
                class="space-y-4">
                <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
                    <?= form_select_group('departments_id', lang('select_department'), $department_options, !empty($departments_id) ? $departments_id : '', true, array('class' => 'select_box')); ?>
                    <?= form_datepicker('payment_month', lang('select') . ' ' . lang('month'), !empty($payment_month) ? $payment_month : '', true, array('class' => 'monthyear', 'data-type' => 'month', 'data-format' => 'Y-m', 'data-auto-close' => 'true')); ?>
					
                </div>


                <div class="flex justify-end">
                    <button id="submit" type="submit" name="flag" value="1"
                        class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-4 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500">
                        <i data-lucide="arrow-right" class="h-4 w-4"></i>
                        <?= lang('go') ?>
                    </button>
                </div>
            </form>
        </div>
    </div>

    <?php if (!empty($flag)) : ?>
        <div class="rounded-2xl border border-border bg-card shadow-sm">
            <div class="border-b border-border px-5 py-4">
                <div class="flex flex-wrap items-center justify-between gap-2 text-sm font-semibold text-foreground">
                    <span><?= lang('payment_info_for') ?>
                        <?php
                        if (!empty($payment_month)) {
                            echo ' <span class="text-muted-foreground">' . date('F Y', safe_strtotime($payment_month)) . '</span>';
                        }
                        ?>
                    </span>
                </div>
            </div>
            <div class="p-5">
                <div class="overflow-x-auto rounded-xl border border-border">
                <table class="min-w-full text-sm" id="datatable_action" cellspacing="0" width="100%">
                    <thead class="bg-muted/40 text-xs uppercase tracking-wide text-muted-foreground">
                    <tr>
                        <th class="px-4 py-3 text-left"><?= lang('emp_id') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('name') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('salary_type') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('basic_salary') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('net_salary') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('details') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('status') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('action') ?></th>
                    </tr>
                    </thead>
                    <tbody class="divide-y divide-border bg-background [&>tr>td]:px-4 [&>tr>td]:py-3">
                    <?php
                    $employee_rows = array();
                    if (!empty($employee_info)) {
                        if (is_object($employee_info)) {
                            $employee_rows[] = array($employee_info);
                        } else {
                            $employee_rows = $employee_info;
                        }
                    }
                    $akey = 0;
                    if (!empty($employee_rows)) : foreach ($employee_rows as $v_emp_info) :
                        ?>
                        <?php if (!empty($v_emp_info)) :
                        $akey += count(array($v_emp_info));
                        $key = $akey - 1;
                        foreach ($v_emp_info as $v_employee) : ?>
                            <tr>
                                <td><?php echo $v_employee->employment_id; ?></td>
                                <td>
                                    <?php if (!empty($salary_info) && $salary_info->user_id == $v_employee->user_id) { ?>
                                        <?= btn_modal(
                                            'admin/payroll/salary_payment_details/' . $salary_info->salary_payment_id,
                                            html_escape($v_employee->fullname),
                                            lang('view'),
                                            'lg',
                                            false,
                                            array('class' => 'text-primary hover:underline mb-0')
                                        ) ?>
                                    <?php } else { ?>
                                        <?= btn_modal(
                                            'admin/payroll/view_payment_details/' . $v_employee->user_id . '/' . $payment_month,
                                            html_escape($v_employee->fullname),
                                            lang('view'),
                                            'lg',
                                            false,
                                            array('class' => 'text-primary hover:underline mb-0')
                                        ) ?>
                                    <?php } ?>
                                
                                </td>
                                <td><?php
                                    $set_salary = false;
                                    if (!empty($v_employee->salary_grade)) {
                                        echo $v_employee->salary_grade . ' <small>(' . lang('monthly') . ')</small>';
                                    } else if (!empty($v_employee->hourly_grade)) {
                                        echo $v_employee->hourly_grade . ' <small>(' . lang('hourly') . ')</small>';
                                    } else {
                                        echo '<span class="text-danger">' . lang('did_not_set_salary_yet') . '</span>';
                                        $set_salary = true;
                                    }
                                    ?></td>
                                <td><?php
                                    if (!empty($v_employee->basic_salary)) {
                                        echo $v_employee->basic_salary;
                                    } else if (!empty($v_employee->hourly_grade)) {
                                        echo $v_employee->hourly_rate . ' <small>(' . lang('per_hour') . ')</small>';
                                    } else {
                                        echo '-';
                                    }
                                    ?></td>
                                <td><?php
                                    if (!empty($total_hours)) {
                                        foreach ($total_hours as $index => $v_total_hours) {
                                            if ($index == $v_employee->user_id) {
                                                if (!empty($v_total_hours)) {
                                                    $total_hour = $v_total_hours['total_hours'];
                                                    $total_minutes = $v_total_hours['total_minutes'];
                                                    if ($total_hour > 0) {
                                                        $hours_ammount = $total_hour * $v_employee->hourly_rate;
                                                    } else {
                                                        $hours_ammount = 0;
                                                    }
                                                    if ($total_minutes > 0) {
                                                        $amount = round($v_employee->hourly_rate / 60, 2);
                                                        $minutes_ammount = $total_minutes * $amount;
                                                    } else {
                                                        $minutes_ammount = 0;
                                                    }
                                                    if (!empty($advance_salary[$index])) {
                                                        $advance_amount = $advance_salary[$index]['advance_amount'];
                                                    } else {
                                                        $advance_amount = 0;
                                                    }
                                                    if (!empty($award_info[$index])) {
                                                        $total_award = $award_info[$index]['award_amount'];
                                                    } else {
                                                        $total_award = 0;
                                                    }
                                                    $total_amount = $hours_ammount + $minutes_ammount + $total_award - $advance_amount;
                                                    echo round($total_amount, 2);
                                                }
                                            }
                                        }
                                    }
                                    if (!empty($v_employee->basic_salary)) {
                                        if (!empty($allowance_info)) {
                                            foreach ($allowance_info as $al_index => $v_allowance) {
                                                if ($al_index == $v_employee->user_id) {
                                                    $total_allowance = $v_allowance;
                                                }
                                            }
                                        }
                                        if (!empty($deduction_info)) {
                                            foreach ($deduction_info as $dd_index => $v_deduction) {
                                                if ($dd_index == $v_employee->user_id) {
                                                    $total_deduction = $v_deduction;
                                                }
                                            }
                                        }
                                        if (!empty($advance_salary)) {
                                            foreach ($advance_salary as $add_index => $v_advance) {
                                                if ($add_index == $v_employee->user_id) {
                                                    $total_advance = $v_advance['advance_amount'];
                                                }
                                            }
                                        }
                                        if (!empty($award_info)) {
                                            foreach ($award_info as $aw_index => $v_award_info) {
                                                if ($aw_index == $v_employee->user_id) {
                                                    $total_award = $v_award_info['award_amount'];
                                                }
                                            }
                                        }
                                        
                                        if (!empty($overtime_info) && !empty($v_employee->overtime_salary)) {
                                            foreach ($overtime_info as $over_index => $v_overtime) {
                                                if ($over_index == $v_employee->user_id) {
                                                    $total_hour = $v_overtime['overtime_hours'];
                                                    $total_minutes = $v_overtime['overtime_minutes'];
                                                    if ($total_hour > 0) {
                                                        $hours_ammount = $total_hour * $v_employee->overtime_salary;
                                                    } else {
                                                        $hours_ammount = 0;
                                                    }
                                                    if ($total_minutes > 0) {
                                                        $amount = round($v_employee->overtime_salary / 60, 2);
                                                        $minutes_ammount = $total_minutes * $amount;
                                                    } else {
                                                        $minutes_ammount = 0;
                                                    }
                                                    $total_amount = $hours_ammount + $minutes_ammount;
                                                }
                                            }
                                        }
                                        
                                        if (empty($total_advance)) {
                                            $total_advance = 0;
                                        }
                                        if (empty($total_deduction)) {
                                            $total_deduction = 0;
                                        }
                                        if (empty($total_award)) {
                                            $total_award = 0;
                                        }
                                        if (empty($total_allowance)) {
                                            $total_allowance = 0;
                                        }
                                        if (empty($total_amount)) {
                                            $total_amount = 0;
                                        }
                                        if (empty($v_employee->basic_salary)) {
                                            $basic_salary = 0;
                                        } else {
                                            $basic_salary = $v_employee->basic_salary;
                                        }
                                        $new_salary = $basic_salary + $total_allowance + $total_amount + $total_award - $total_advance - $total_deduction;
                                        // get per day salary by total_working_days and sum total_present
                                        $per_day_salary = $new_salary / $total_working_days;
                                        $total_salary = $per_day_salary * $total_present[$v_employee->user_id];
                                        
                                        echo display_money($total_salary);
                                        // check existing payment by employee id and payment month
                                    }
                                    $salary_info = $this->payroll_model->check_by(array('user_id' => $v_employee->user_id, 'payment_month' => $payment_month), 'tbl_salary_payment');
                                    ?></td>
                                <td><?php if (!empty($salary_info) && $salary_info->user_id == $v_employee->user_id) { ?>
                                        <?= btn_modal(
                                            'admin/payroll/salary_payment_details/' . $salary_info->salary_payment_id,
                                            '',
                                            lang('view'),
                                            'lg',
                                            'eye',
                                            array('class' => 'inline-flex items-center justify-center rounded-lg border border-slate-200 bg-slate-50 p-2 text-slate-600 hover:bg-slate-100 h-7 mb-0')
                                        ) ?>
                                    <?php } else { ?>
                                        <?= btn_modal(
                                            'admin/payroll/view_payment_details/' . $v_employee->user_id . '/' . $payment_month,
                                            '',
                                            lang('view'),
                                            'lg',
                                            'eye',
                                            array('class' => 'inline-flex items-center justify-center rounded-lg border border-slate-200 bg-slate-50 p-2 text-slate-600 hover:bg-slate-100 h-7 mb-0')
                                        ) ?>
                                    <?php } ?>
                                </td>
                                <td>
                                    <?php if (!empty($salary_info) && $salary_info->user_id == $v_employee->user_id) { ?>
                                        <span class="inline-flex items-center rounded-full bg-emerald-500/15 px-2 py-0.5 text-xs font-semibold text-emerald-600 dark:text-emerald-300"><?= lang('paid') ?></span>
                                        <?php
                                    } else {
                                        if (empty($set_salary)) {
                                            ?>
                                            <span class="inline-flex items-center rounded-full bg-rose-500/15 px-2 py-0.5 text-xs font-semibold text-rose-600 dark:text-rose-300"><?= lang('unpaid') ?></span>
                                        <?php }
                                    } ?>
                                </td>
                                <td>
                                    <?php if (!empty($salary_info) && $salary_info->user_id == $v_employee->user_id) { ?>
                                        <a class="text-success" target="_blank"
                                           href="<?php echo base_url() ?>admin/payroll/receive_generated/<?php echo $salary_info->salary_payment_id; ?>"><?= lang('generate_payslip') ?></a>
                                    <?php } else {
                                        if (!empty($set_salary) && !empty($edited)) {
                                            ?>
                                            <a class="text-warning text-bold" target="_blank"
                                               href="<?php echo base_url() ?>admin/payroll/manage_salary_details/<?php echo $v_employee->departments_id; ?>"><?= lang('set_slary') ?></a>
                                        <?php } else {
                                            ?>
                                            <a class="text-danger"
                                               href="<?php echo base_url() ?>admin/payroll/make_payment/<?php echo $v_employee->user_id . '/' . $v_employee->departments_id . '/' . $payment_month; ?>"><?= lang('make_payment') ?></a>
                                        <?php }
                                    } ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                    <?php endforeach; ?>
                    <?php endif; ?>
                    </tbody>
                </table>
                </div>
            </div>
        </div>
    <?php endif; ?>
        <?php
        if (!empty($payment_flag)) :
        if (!empty($advance_salary)) {
            $advance_amount = $advance_salary['advance_amount'];
        } else {
            $advance_amount = 0;
        }
        if (!empty($total_hours)) {
            $total_hour = $total_hours['total_hours'];
            $total_minutes = $total_hours['total_minutes'];
            if ($total_hour > 0) {
                $hours_ammount = $total_hour * $employee_info->hourly_rate;
            } else {
                $hours_ammount = 0;
            }
            if ($total_minutes > 0) {
                $amount = round($employee_info->hourly_rate / 60, 2);
                $minutes_ammount = $total_minutes * $amount;
            } else {
                $minutes_ammount = 0;
            }
            $total_hours_amount = $hours_ammount + $minutes_ammount;
        }
        if (!empty($employee_info->basic_salary)) {
            if (empty($deduction_info)) {
                $deduction_info = 0;
            } else {
                $deduction_info = $deduction_info;
            }
            if (empty($allowance_info)) {
                $allowance_info = 0;
            } else {
                $allowance_info = $allowance_info;
            }
            if (!empty($overtime_info)) {
                $total_hour = $overtime_info['overtime_hours'];
                $total_minutes = $overtime_info['overtime_minutes'];
                if ($total_hour > 0) {
                    $hours_ammount = $total_hour * $employee_info->overtime_salary;
                } else {
                    $hours_ammount = 0;
                }
                if ($total_minutes > 0) {
                    $amount = round($employee_info->overtime_salary / 60, 2);
                    $minutes_ammount = $total_minutes * $amount;
                } else {
                    $minutes_ammount = 0;
                }
                $total_amount = $hours_ammount + $minutes_ammount + $allowance_info;
            }
        }
        if (empty($total_advance)) {
            $total_advance = 0;
        }
        if (empty($deduction_info)) {
            $deduction_info = 0;
        }
        if (empty($total_award)) {
            $total_award = 0;
        }
        if (empty($total_allowance)) {
            $total_allowance = 0;
        }
        if (empty($total_amount)) {
            $total_amount = 0;
        }
        if (empty($v_employee->basic_salary)) {
            $basic_salary = 0;
        } else {
            $basic_salary = $v_employee->basic_salary;
        }
        
        
        ?>
        <form role="form" data-parsley-validate="" novalidate="" enctype="multipart/form-data"
              action="<?php echo base_url(); ?>admin/payroll/get_payment/<?php
              if (!empty($check_salary_payment->salary_payment_id)) {
                  echo $check_salary_payment->salary_payment_id;
              }
              ?>" method="post"
              class="space-y-4">
            <div class="grid grid-cols-1 gap-4 lg:grid-cols-12 items-start" data-spy="scroll" data-offset="0">
                <div class="lg:col-span-4 min-w-0">
                    <div>
                    
                    <div class="fees_payment rounded-2xl border border-border bg-card shadow-sm">
                        <!-- Default panel contents -->
                        <div class="border-b border-border px-5 py-4">
                            <div>
                                <strong><?= lang('payment_for') ?><?php
                                    echo ' <span class="text-danger">' . date('F Y', safe_strtotime($payment_month)) . '</span>';
                                    ?></strong>
                            </div>
                        </div>
                        <div class="space-y-4 p-5">
                            <div class="space-y-1">
                                <label class="text-sm font-medium text-foreground"><?= lang('gross_salary') ?> </label>
                                <input type="text" name="house_rent_allowance" disabled value="<?php
                                if (!empty($total_hours_amount)) {
                                    echo $gross = round($total_hours_amount, 2);
                                    $deduction_info = 0;
                                    $allowance_info = 0;
                                } else {
                                    echo $gross = $employee_info->basic_salary + $total_amount;
                                    $main = $employee_info->basic_salary + $allowance_info;
                                }
                                ?>"
                                       class="salary w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-primary/30">
                            </div>
                            <?php
                            if (empty($main)) {
                                $main = 0;
                            }
                            $main_salary = $main - $deduction_info;
                            $per_day_salary = $main_salary / $total_working_days;
                            $total_absent_salary = round($per_day_salary * $total_absent, 2);
                            ?>
                            <div class="space-y-1">
                                <label class="text-sm font-medium text-foreground"><?= lang('total_deduction') ?> </label>
                                <input type="text" name="" disabled value="<?php
                                echo $deduction = $deduction_info + $advance_amount + $total_absent_salary;
                                ?>" class="salary w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-primary/30">
                            </div>
                            <input type="hidden" name="absent_deduction"
                                   value="<?= $total_absent_salary ?>"/>
                            <div class="space-y-1">
                                <label class="text-sm font-medium text-foreground"><?= lang('net_salary') ?> </label>
                                <input type="text" id="net_salary" name="other_allowance" disabled value="<?php
                                echo $net_salary = $gross - $deduction;
                                ?>"
                                       class="salary w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-primary/30">
                            </div>
                            <?php
                            $total_award = 0;
                            if (!empty($award_info)) : foreach ($award_info as $v_award_info) :
                                ?>
                                <?php if (!empty($v_award_info->award_amount)) : ?>
                                <div class="space-y-1">
                                    <label class="text-sm font-medium text-foreground"><?= lang('award') ?>
                                        <small>( <?php echo $v_award_info->award_name; ?> )</small>
                                    </label>
                                    <input type="text" name="other_allowance" disabled id="award"
                                           value="<?php echo $v_award_info->award_amount; ?>"
                                           class="award w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-primary/30">
                                    <input type="hidden" name="award_name[]" id="award"
                                           value="<?php echo $total_award += $v_award_info->award_amount; ?>"
                                           class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground">
                                </div>
                            <?php endif; ?>
                            <?php endforeach; ?>
                            <?php endif; ?>
                            <input type="hidden" name="total_award" id="total_award" value="" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground">
                            <div class="space-y-1">
                                <label class="text-sm font-medium text-foreground"><?= lang('fine_deduction') ?> </label>
                                <input type="text" data-parsley-type="number" name="fine_deduction" id="fine_deduction"
                                       value="<?php
                                       if (!empty($check_salary_payment->fine_deduction)) {
                                           echo $check_salary_payment->fine_deduction;
                                       }
                                       ?>"
                                       class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-primary/30">
                            </div>
                            <div class="space-y-1">
                                <label class="text-sm font-medium text-foreground"><strong><?= lang('payment_amount') ?> </strong></label>
                                <input type="text" disabled="" value="<?php echo $net_salary + $total_award; ?>"
                                       class="payment_amount w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground">
                            </div>
                            <input type="hidden" name="payment_amount" value="<?php echo $net_salary + $total_award; ?>"
                                   class="payment_amount w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground">
                            <!-- Hidden Employee Id -->
                            <input type="hidden" id="user_id" name="user_id"
                                   value="<?php echo $employee_info->user_id; ?>" class="salary w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground">
                            <input type="hidden" name="payment_month" value="<?php
                            if (!empty($payment_month)) {
                                echo $payment_month;
                            }
                            ?>" class="salary w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground">
                            <div class="space-y-1">
                                <!-- Payment Type -->
                                <label class="text-sm font-medium text-foreground"><?= lang('payment_method') ?> <span class="required">
                                        *</span></label>
                                <select name="payment_type" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground select_box"
                                        onchange="get_payment_value(this.value)">
                                    <option value=""><?= lang('select') . ' ' . lang('payment_method') ?></option>
                                    <?php
                                    $all_payment_method = $this->db->get('tbl_payment_methods')->result();
                                    if (!empty($all_payment_method)) {
                                        foreach ($all_payment_method as $v_payment_method) {
                                            ?>
                                            <option<?php
                                            if (!empty($check_salary_payment->payment_type)) {
                                                echo $check_salary_payment->payment_type == $v_payment_method->payment_methods_id ? 'selected' : '';
                                            }
                                            ?> value="<?= $v_payment_method->payment_methods_id; ?>">
                                                <?= $v_payment_method->method_name; ?></option>
                                        <?php }
                                    } ?>
                                </select>
                            </div><!-- Payment Type -->
                            <div class="space-y-1">
                                <label class="text-sm font-medium text-foreground"><?= lang('comments') ?> </label>
                                <input type="text" name="comments" value="<?php
                                if (!empty($check_salary_payment->comments)) {
                                    echo $check_salary_payment->comments;
                                }
                                ?>" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-primary/30">
                            </div>
                            <div class="space-y-2">
                                <label class="inline-flex items-center gap-1 text-sm font-medium text-foreground"><?= lang('deduct_from_default_account') ?>
                                    <i data-lucide="help-circle" class="h-4 w-4 inline-block" data-toggle="tooltip" data-placement="top"
                                       title="<?= lang('will_be_deduct_into_account') ?>"></i>
                                </label>
                                <div>
                                    <label class="inline-flex cursor-pointer items-center gap-2 text-sm text-foreground">
                                        <input type="checkbox" class="custom-checkbox h-4 w-4 rounded border-border bg-input text-primary focus:ring-primary/30" checked id="use_postmark"
                                               name="deduct_from_account">
                                        <span><?= lang('yes') ?></span>
                                    </label>
                                </div>
                            </div>
                            <div class="space-y-2" id="postmark_config"
                                <?php echo (empty($check_salary_payment->account_id)) ? 'style="display:block"' : '' ?>>
                                <label class="text-sm font-medium text-foreground"><?= lang('select') . ' ' . lang('account') ?></label>
                                <?php
                                $selected_account_id = !empty($check_salary_payment->account_id) ? $check_salary_payment->account_id : config_item('default_account');
                                $selected_account_label = '';
                                if (!empty($selected_account_id)) {
                                    $selected_account_label = get_row('tbl_accounts', array('account_id' => $selected_account_id), 'account_name');
                                }
                                echo form_lookup_group(
                                    'account_id',
                                    '',
                                    $selected_account_id,
                                    $selected_account_label,
                                    false,
                                    array(),
                                    '',
                                    array('type' => 'account')
                                );
                                ?>
                                <?= btn_modal(
                                    'admin/account/new_account',
                                    lang('add_new'),
                                    lang('add_new'),
                                    'md',
                                    'plus',
                                    array('class' => 'inline-flex items-center gap-1 text-primary hover:underline mb-0')
                                ) ?>
                            </div>
                            <div class="pt-2">
                                <div>
                                    <button type="submit" name="sbtn" value="1"
                                            class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-4 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500">
                                        <i data-lucide="save" class="h-4 w-4"></i>
                                        <?= lang('update') ?>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    </div>
                </div>
            </form>
            <!--************ Fees payment End ***********-->

            <!--************ Payment History Start ***********-->
            <!---************** Employee Info show When Print ***********************--->
            <div class="lg:col-span-8 min-w-0" id="payment_history">
            <div class="show_print border-b dark:border-gray-600 pb-2 mb-7" style="width: 100%;">
                <table style="width: 100%; vertical-align: middle;">
                    <tr>
                        <td style="width: 50px; border: 0px;">
                            <img style="width: 50px;height: 50px;margin-bottom: 5px;"
                                 src="<?= base_url() . config_item('company_logo') ?>" alt="" class="rounded-full"/>
                        </td>
                        
                        <td style="border: 0px;">
                            <p style="margin-left: 10px; font: 14px lighter;"><?= config_item('company_name') ?></p>
                        </td>
                    </tr>
                </table>
            </div><!-- show when print start-->
            
            <div class="show_print" style="padding: 5px 0; width: 100%;margin-top: 20px;margin-bottom: 20px;">
                <div>
                    <table style="width: 100%; border-radius: 3px;">
                        <tr>
                            <td style="width: 150px;">
                                <table style="border: 1px solid grey;">
                                    <tr>
                                        <td style="background-color: lightgray; border-radius: 2px;">
                                            <?php if (!empty($staff_details->avatar)) : ?>
                                                <img src="<?php echo base_url() . $staff_details->avatar; ?>"
                                                     style="width: 132px; height: 138px; border-radius: 3px;">
                                            <?php else : ?>
                                                <img alt="Employee_Image">
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td>
                                <table style="width: 300px; margin-left: 10px; margin-bottom: 10px; font-size: 13px;">
                                    <tr>
                                        <td colspan="2">
                                            <h2><?php echo "$staff_details->fullname "; ?></h2>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="width: 100px"><strong><?= lang('emp_id') ?> :</strong> :</td>
                                        <td>&nbsp; <?php echo "$staff_details->employment_id"; ?></td>
                                    </tr>
                                    <tr>
                                        <td style="width: 100px"><strong><?= lang('departments') ?> : </strong></td>
                                        <td>&nbsp; <?php echo html_escape($staff_department_name); ?></td>
                                    </tr>
                                    <tr>
                                        <td style="width: 100px"><strong><?= lang('designation') ?> :</strong></td>
                                        <td>&nbsp; <?php echo "$staff_details->designations"; ?></td>
                                    </tr>
                                    <tr>
                                        <td style="width: 100px"><strong><?= lang('joining_date') ?> :</strong></td>
                                        <td><?= display_date($staff_details->joining_date) ?>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <!--  **************** show when print End ********************* -->
            <div class="print_width w-full">
                
            <div class="rounded-2xl border border-border bg-card shadow-sm">
                    <!-- Default panel contents -->
                    <div class="border-b border-border px-5 py-4">
                        <div class="flex flex-wrap items-center justify-between gap-2 text-sm font-semibold text-foreground">
                            <span><?= lang('payment_history') ?></span>
                            <div class="hidden-print">
                                <button class="inline-flex items-center gap-2 rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-xs font-semibold text-red-600 hover:bg-red-100"
                                        data-toggle="tooltip" data-placement="top" title="<?= lang('print') ?>" type="button"
                                        onclick="payment_history('payment_history')">
                                    <i data-lucide="printer" class="h-4 w-4"></i>
                                    <?= lang('print') ?>
                                </button>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Table -->
                    <div class="overflow-x-auto rounded-xl border border-border">
                    <table class="min-w-full text-sm" id="DataTables" cellspacing="0" width="100%">
                        <thead class="bg-muted/40 text-xs uppercase tracking-wide text-muted-foreground">
                        <tr>
                            <th class="px-4 py-3 text-left"><?= lang('month') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('date') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('gross_salary') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('total_deduction') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('net_salary') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('fine_deduction') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('amount') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('details') ?></th>
                        </tr>
                        </thead>
                        <tbody class="divide-y divide-border bg-background">
                        <script type="text/javascript">
                            $(document).ready(function () {
                                list = base_url +
                                    "admin/payroll/payment_historyList/<?= $staff_details->user_id ?>";
                            });
                        </script>
                        </tbody>
                    </table>
                    </div>
                </div>
                <!--************ Payment History End***********-->
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

</div>

<script type="text/javascript">
    function payment_history(payment_history) {
        var printContents = document.getElementById(payment_history).innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        window.print();
        document.body.innerHTML = originalContents;
    }
</script>
<script type="text/javascript">
    $(document).ready(function () {
        var award = 0;
        $(".award").each(function () {
            award += parseFloat(this.value);
        });
        $("#total_award").val(award);
    });
    $(document).on("change", function () {
        var fine = 0;
        fine = $("#fine_deduction").val();
        var total_award = $("#total_award").val();
        var net_salary = $("#net_salary").val();
        var sub_tatal = parseFloat(net_salary) + parseFloat(total_award);
        var total = sub_tatal - fine;
        $(".payment_amount").val(total);
    });
</script>
