<?= message_box('success'); ?>
<?= message_box('error');
$created = can_action('123', 'created');
$edited = can_action('123', 'edited');
$deleted = can_action('123', 'deleted');
$edit_id = $this->uri->segment(5);
$action_url = base_url() . 'admin/settings/income_category/update_income_category';
$submit_label = lang('add');
$income_name = '';
$income_desc = '';
if (!empty($edit_id) && !empty($income_category_info)) {
    $action_url = base_url() . 'admin/settings/income_category/update_income_category/' . $income_category_info->income_category_id;
    $submit_label = lang('update');
    $income_name = $income_category_info->income_category;
    $income_desc = $income_category_info->description;
}
?>

<div class="space-y-6">
    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="p-6">
            <div class="grid grid-cols-1 lg:grid-cols-12 gap-6">
                <?php if (!empty($created) || !empty($edited)) { ?>
                    <div class="lg:col-span-6">
                        <div class="rounded-2xl border border-dashed border-border bg-card p-4 space-y-4">
                            <form method="post" action="<?= $action_url ?>" class="space-y-4">
                                <?= form_input_group('income_category', lang('income_category'), $income_name, 'text', true); ?>
                                <?= form_editor('description', lang('description'), $income_desc, false); ?>
                                <div class="flex items-center gap-2">
                                    <button type="submit" class="btn btn-sm btn-primary inline-flex items-center gap-2">
                                        <i data-lucide="check" class="h-4 w-4"></i>
                                        <?= $submit_label ?>
                                    </button>
                                    <?php if (!empty($edit_id)) { ?>
                                        <a href="<?= base_url('admin/settings/income_category/') ?>" class="btn btn-danger btn-sm"><?= lang('cancel') ?></a>
                                    <?php } ?>
                                </div>
                            </form>
                        </div>
                    </div>
                <?php } ?>

                <div class="lg:col-span-6">
                    <div class="grid grid-cols-1 2xl:grid-cols-2 gap-4">
                        <?php
                        $currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
                        $category_totals = isset($category_totals) ? $category_totals : array();
                        if (!empty($all_income_category)) {
                            foreach ($all_income_category as $income_category) {
                                $total_income = !empty($category_totals[$income_category->income_category_id]) ? $category_totals[$income_category->income_category_id] : 0;
                                ?>
                                <div id="income_category_<?= $income_category->income_category_id ?>" class="rounded-xl border border-border bg-muted/30 p-4">
                                    <div class="flex items-start justify-between gap-2">
                                        <div>
                                            <h4 class="text-sm font-semibold text-foreground"><?= $income_category->income_category ?></h4>
                                            <p class="text-xs text-muted-foreground"><?= $income_category->description ?></p>
                                            <p class="text-xs text-muted-foreground"><?= lang('total') . ' ' . lang('income') . ' : ' . display_money($total_income, $currency->symbol) ?></p>
                                        </div>
                                        <div class="flex items-center gap-2">
                                            <?php if (!empty($edited)) { ?>
                                                <?= btn_edit('admin/settings/income_category/edit_income_category/' . $income_category->income_category_id) ?>
                                            <?php }
                                            if (!empty($deleted)) { ?>
                                                <?php echo ajax_anchor(base_url("admin/settings/delete_income_category/" . $income_category->income_category_id), lang('delete'), array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#income_category_" . $income_category->income_category_id)); ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            <?php }
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
