<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$this->load->helper('string');

$is_edit = !empty($bug_info);
$bug_id = $is_edit ? $bug_info->bug_id : null;
$issue_no = $is_edit ? $bug_info->issue_no : strtoupper(random_string('alnum', 7));
$bug_title = $is_edit ? $bug_info->bug_title : '';
$bug_description = $is_edit ? $bug_info->bug_description : '';

$project_opts = array('' => lang('select') . ' ' . lang('project'));
$selected_project = $is_edit ? $bug_info->project_id : '';
$client_id = $this->session->userdata('client_id');
$all_project = $this->db->where('client_id', $client_id)->get('tbl_project')->result();
if (!empty($all_project)) {
    foreach ($all_project as $v_project) {
        $project_opts[$v_project->project_id] = $v_project->project_name;
    }
}

$priority_opts = array();
$priorities = $this->db->get('tbl_priority')->result();
if (!empty($priorities)) {
    foreach ($priorities as $v_priorities) {
        $priority_opts[$v_priorities->priority] = $v_priorities->priority;
    }
}
$selected_priority = $is_edit ? $bug_info->priority : config_item('default_priority');
?>

<div class="space-y-4">
    <div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
        <div>
            <h2 class="text-xl font-semibold text-foreground"><?= $is_edit ? lang('update_bug') : lang('new_bugs'); ?></h2>
            <p class="text-sm text-muted-foreground"><?= lang('bug_management'); ?></p>
        </div>
        <div class="flex flex-wrap gap-2">
            <a href="<?= base_url('client/bugs'); ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-sm font-medium text-foreground hover:bg-muted">
                <?= lang('all_bugs'); ?>
            </a>
        </div>
    </div>

    <div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
        <form method="post" data-parsley-validate="" novalidate=""
            action="<?php echo base_url(); ?>client/bugs/save_bug/<?php if (!empty($bug_id)) echo $bug_id; ?>" class="space-y-6">
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
                <div class="lg:col-span-2 space-y-5">
                    <div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700">
                        <h4 class="text-sm font-semibold text-foreground flex items-center gap-2 text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
                            <i data-lucide="info" class="h-5 w-5"></i>
                            <?= lang('bug') . ' ' . lang('details'); ?>
                        </h4>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-5 mb-4">
                            <?= form_input_group('issue_no', lang('issue_#'), $issue_no, 'text', true); ?>
                            <?= form_input_group('bug_title', lang('bug_title'), $bug_title, 'text', true, array('placeholder' => lang('bug_title'))); ?>
                            <?= form_select_group('project_id', lang('project'), $project_opts, $selected_project, true, array('class' => 'select_box', 'data-search' => true)); ?>
                            <?= form_select_group('priority', lang('priority'), $priority_opts, $selected_priority, true); ?>
                        </div>
                        <?= form_editor('bug_description', lang('description'), $bug_description, false); ?>
                    </div>
                </div>

                <div class="space-y-5">
                    <div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700">
                        <h4 class="text-sm font-semibold text-foreground flex items-center gap-2 text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
                            <i data-lucide="list-plus" class="h-5 w-5"></i>
                            <?= lang('custom_fields'); ?>
                        </h4>
                        <?= custom_form_Fields(6, $bug_id); ?>
                    </div>
                </div>
            </div>

            <div class="flex items-center gap-2">
                <button type="submit" class="inline-flex items-center rounded-lg bg-primary px-4 py-2 text-sm font-semibold text-white shadow hover:bg-primary/90 transition">
                    <?= $is_edit ? lang('updates') : lang('save'); ?>
                </button>
                <a href="<?= base_url('client/bugs'); ?>" class="inline-flex items-center rounded-lg border border-border bg-muted/40 px-4 py-2 text-sm font-semibold text-foreground hover:bg-muted">
                    <?= lang('cancel'); ?>
                </a>
            </div>
        </form>
    </div>
</div>
