<?php
echo message_box('success');
echo message_box('error');
$created = can_action('127', 'created');
$edited = can_action('127', 'edited');
$deleted = can_action('127', 'deleted');
$edit_id = $this->uri->segment(5);
$lead_status_value = '';
$lead_type_value = '';
$order_no_value = '';
$action_url = base_url() . 'admin/settings/lead_status/update_lead_status';
$submit_label = lang('add');
if (!empty($edit_id) && !empty($lead_status_info)) {
    $lead_status_value = $lead_status_info->lead_status;
    $lead_type_value = $lead_status_info->lead_type;
    $order_no_value = $lead_status_info->order_no;
    $action_url = base_url() . 'admin/settings/lead_status/update_lead_status/' . $lead_status_info->lead_status_id;
    $submit_label = lang('update');
}
$lead_type_options = array(
    '' => lang('none'),
    'close' => lang('close'),
    'open' => lang('open'),
);
?>

<div class="space-y-6">
    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-6 py-5">
            <div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
                <div>
                    <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('leads') ?></p>
                    <h2 class="text-lg font-semibold text-foreground"><?= lang('lead_status') ?></h2>
                    <p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('lead_status') ?></p>
                </div>
            </div>
        </div>
        <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">
                            <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-2">
                                <i data-lucide="layers" class="h-4 w-4"></i>
                                <span><?= $submit_label === lang('update') ? lang('update') : lang('new') ?> <?= lang('lead_status') ?></span>
                            </div>
                            <form method="post" action="<?= $action_url ?>" class="space-y-4">
                                <?= form_input_group('lead_status', lang('lead_status'), $lead_status_value, 'text', true, array('placeholder' => lang('enter') . ' ' . lang('lead_status'))); ?>
                                <?= form_select_group('lead_type', lang('lead_type'), $lead_type_options, $lead_type_value); ?>
                                <?= form_input_group('order_no', lang('order_no'), $order_no_value, 'text', true); ?>
                                <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/lead_status/') ?>" 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 2xl:grid-cols-2 gap-4">
                        <?php
                        if (!empty($all_lead_status)) {
                            foreach ($all_lead_status as $lead_status) {
                                $total_lead_status = count(array($this->db->where('lead_status_id', $lead_status->lead_status_id)->get('tbl_leads')->result()));
                                ?>
                                <div id="lead_status_<?= $lead_status->lead_status_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"><?= $lead_status->lead_status ?></h4>
                                            <div class="mt-2 space-y-1 text-xs text-muted-foreground">
                                                <p><?= lang('lead_type') ?>: <span class="text-foreground"><?= lang($lead_status->lead_type) ?></span></p>
                                                <p><?= lang('order_no') ?>: <span class="text-foreground"><?= $lead_status->order_no ?></span></p>
                                                <p><?= lang('total') . ' ' . lang('leads') ?>: <span class="text-foreground"><?= $total_lead_status ?></span></p>
                                            </div>
                                        </div>
                                        <div class="flex items-center gap-2">
                                            <?php if (!empty($edited)) { ?>
                                                <?= btn_edit('admin/settings/lead_status/edit_lead_status/' . $lead_status->lead_status_id) ?>
                                            <?php }
                                            if (!empty($deleted)) { ?>
                                                <?php echo ajax_anchor(base_url("admin/settings/delete_lead_status/" . $lead_status->lead_status_id), lang('delete'), array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#lead_status_" . $lead_status->lead_status_id)); ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            <?php }
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
