<?php
echo message_box('success');
echo message_box('error');
$created = can_action('125', 'created');
$edited = can_action('125', 'edited');
$deleted = can_action('125', 'deleted');
$edit_id = $this->uri->segment(5);
$action_url = base_url() . 'admin/settings/customer_group/update_customer_group';
$submit_label = lang('add');
$group_name = '';
$group_desc = '';
if (!empty($edit_id) && !empty($customer_group_info)) {
    $action_url = base_url() . 'admin/settings/customer_group/update_customer_group/' . $customer_group_info->customer_group_id;
    $submit_label = lang('update');
    $group_name = $customer_group_info->customer_group;
    $group_desc = $customer_group_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('customer_group', lang('customer_group'), $group_name, 'text', true); ?>
                                <?= form_editor('description', lang('description'), $group_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/customer_group/') ?>" 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
                        if (!empty($all_customer_group)) {
                            foreach ($all_customer_group as $customer_group) {
                                $total_client = count(array($this->db->where('customer_group_id', $customer_group->customer_group_id)->get('tbl_client')->result()));
                                ?>
                                <div id="customer_group_<?= $customer_group->customer_group_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"><?= $customer_group->customer_group ?></h4>
                                            <p class="text-xs text-muted-foreground"><?= $customer_group->description ?></p>
                                            <p class="text-xs text-muted-foreground"><?= lang('total') . ' ' . lang('client') . ' : ' . $total_client ?></p>
                                        </div>
                                        <div class="flex items-center gap-2">
                                            <?php if (!empty($edited)) { ?>
                                                <?= btn_edit('admin/settings/customer_group/edit_customer_group/' . $customer_group->customer_group_id) ?>
                                            <?php }
                                            if (!empty($deleted)) { ?>
                                                <?php echo ajax_anchor(base_url("admin/settings/delete_customer_group/" . $customer_group->customer_group_id), lang('delete'), array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#customer_group_" . $customer_group->customer_group_id)); ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            <?php }
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
