<?php
echo message_box('success');
echo message_box('error');
$created = can_action('124', 'created');
$edited = can_action('124', 'edited');
$deleted = can_action('124', 'deleted');
$edit_id = $this->uri->segment(5);
$action_url = base_url() . 'admin/settings/expense_category/update_expense_category';
$submit_label = lang('add');
$expense_name = '';
$expense_desc = '';
if (!empty($edit_id) && !empty($expense_category_info)) {
    $action_url = base_url() . 'admin/settings/expense_category/update_expense_category/' . $expense_category_info->expense_category_id;
    $submit_label = lang('update');
    $expense_name = $expense_category_info->expense_category;
    $expense_desc = $expense_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-4">
                        <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('expense_category', lang('expense_category'), $expense_name, 'text', true); ?>
                                <?= form_textarea('description', lang('description'), $expense_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/expense_category/') ?>" class="btn btn-danger btn-sm"><?= lang('cancel') ?></a>
                                    <?php } ?>
                                </div>
                            </form>
                        </div>
                    </div>
                <?php } ?>

                <div class="lg:col-span-8">
                    <div class="grid grid-cols-1 xl:grid-cols-2 2xl:grid-cols-3 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_expense_category)) {
                            foreach ($all_expense_category as $expense_category) {
                                $total_expense = !empty($category_totals[$expense_category->expense_category_id]) ? $category_totals[$expense_category->expense_category_id] : 0;
                                ?>
                                <div id="expense_category_<?= $expense_category->expense_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"><?= $expense_category->expense_category ?></h4>
                                            <p class="text-xs text-muted-foreground"><?= $expense_category->description ?></p>
                                            <p class="text-xs text-muted-foreground"><?= lang('total') . ' ' . lang('expense') . ' : ' . display_money($total_expense, $currency->symbol) ?></p>
                                        </div>
                                        <div class="flex items-center gap-2">
                                            <?php if (!empty($edited)) { ?>
                                                <?= btn_edit('admin/settings/expense_category/edit_expense_category/' . $expense_category->expense_category_id) ?>
                                            <?php }
                                            if (!empty($deleted)) { ?>
                                                <?php echo ajax_anchor(base_url("admin/settings/delete_expense_category/" . $expense_category->expense_category_id), lang('delete'), array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#expense_category_" . $expense_category->expense_category_id)); ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            <?php }
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
