<?php
echo message_box('success');
echo message_box('error');
$created = can_action('129', 'created');
$edited = can_action('129', 'edited');
$deleted = can_action('129', 'deleted');
$edit_id = $this->uri->segment(4);
$action_url = base_url() . 'admin/settings/update_state_reason';
$submit_label = lang('add');
$state_value = '';
$reason_value = '';
if (!empty($edit_id) && !empty($state_info)) {
    $action_url = base_url() . 'admin/settings/update_state_reason/' . $state_info->opportunities_state_reason_id;
    $submit_label = lang('update');
    $state_value = $state_info->opportunities_state;
    $reason_value = $state_info->opportunities_state_reason;
}
$state_options = array(
    'open' => lang('open'),
    'won' => lang('won'),
    'abandoned' => lang('abandoned'),
    'suspended' => lang('suspended'),
    'lost' => lang('lost'),
);
?>

<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_select_group('opportunities_state', lang('opportunities_state'), $state_options, $state_value); ?>
                                <?= form_input_group('opportunities_state_reason', lang('reason'), $reason_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/opportunities_state_reason/') ?>" 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
                        $all_opportunities_state_reason = $this->db->get('tbl_opportunities_state_reason')->result();
                        if (!empty($all_opportunities_state_reason)) {
                            foreach ($all_opportunities_state_reason as $opportunities_state) {
                                ?>
                                <div id="state_reason_<?= $opportunities_state->opportunities_state_reason_id ?>" class="rounded-xl border border-border bg-muted/30 p-4">
                                    <div class="flex items-start flex-wrap justify-between gap-2">
                                        <div>
                                            <h4 class="text-sm font-semibold text-foreground"><?= lang($opportunities_state->opportunities_state) ?></h4>
                                            <p class="text-xs text-muted-foreground"><?= $opportunities_state->opportunities_state_reason ?></p>
                                        </div>
                                        <div class="flex items-center gap-2">
                                            <?php if (!empty($edited)) { ?>
                                                <?= btn_edit('admin/settings/opportunities_state_reason/' . $opportunities_state->opportunities_state_reason_id) ?>
                                            <?php } ?>
                                            <?php if (!empty($deleted)) { ?>
                                                <?php echo ajax_anchor(base_url("admin/settings/delete_state_reason/" . $opportunities_state->opportunities_state_reason_id), lang('delete'), array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#state_reason_" . $opportunities_state->opportunities_state_reason_id)); ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            <?php }
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
