<?php
echo message_box('success');
echo message_box('error');
$created = can_action('128', 'created');
$edited = can_action('128', 'edited');
$deleted = can_action('128', 'deleted');
$edit_id = $this->uri->segment(5);
$lead_source_value = '';
$action_url = base_url() . 'admin/settings/lead_source/update_lead_source';
$submit_label = lang('add');
if (!empty($edit_id) && !empty($lead_source_info)) {
    $lead_source_value = $lead_source_info->lead_source;
    $action_url = base_url() . 'admin/settings/lead_source/update_lead_source/' . $lead_source_info->lead_source_id;
    $submit_label = lang('update');
}
?>

<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_source') ?></h2>
                    <p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('lead_source') ?></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="radio-tower" class="h-4 w-4"></i>
                                <span><?= $submit_label === lang('update') ? lang('update') : lang('new') ?> <?= lang('lead_source') ?></span>
                            </div>
                            <form method="post" action="<?= $action_url ?>" class="space-y-4">
                                <?= form_input_group('lead_source', lang('lead_source'), $lead_source_value, 'text', true, array('placeholder' => lang('enter') . ' ' . lang('lead_source'))); ?>
                                <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_source/') ?>" 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
                        if (!empty($all_lead_source)) {
                            foreach ($all_lead_source as $lead_source) {
                                $total_lead_source = count(array($this->db->where('lead_source_id', $lead_source->lead_source_id)->get('tbl_leads')->result()));
                                ?>
                                <div id="lead_source_<?= $lead_source->lead_source_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_source->lead_source ?></h4>
                                            <p class="mt-2 text-xs text-muted-foreground"><?= lang('total') . ' ' . lang('leads') ?>:
                                                <span class="text-foreground"><?= $total_lead_source ?></span>
                                            </p>
                                        </div>
                                        <div class="flex items-center gap-2">
                                            <?php if (!empty($edited)) { ?>
                                                <?= btn_edit('admin/settings/lead_source/edit_lead_source/' . $lead_source->lead_source_id) ?>
                                            <?php }
                                            if (!empty($deleted)) { ?>
                                                <?php echo ajax_anchor(base_url("admin/settings/delete_lead_source/" . $lead_source->lead_source_id), lang('delete'), array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#lead_source_" . $lead_source->lead_source_id)); ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            <?php }
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
