<div class="space-y-6">
    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-5 py-4">
            <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_form') ?></h2>
            <p class="text-sm text-muted-foreground"><?= lang('form_builder') ?></p>
        </div>
        <div class="p-5 space-y-6">
            <?php if (!isset($form)) { ?>
                <div class="rounded-lg border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-700">
                    <?= lang('form_builder_create_form_first') ?>
                </div>
            <?php } ?>
            <?php echo form_open('admin/leads/save_form_data/row', array('id' => 'form_info', 'class' => 'space-y-6')); ?>
            <?php if (isset($form)) {
                echo form_hidden('form_id', $form->lead_form_id);
            } ?>
            <?php if (isset($form)) { ?>
                <ul class="nav nav-tabs flex border-b border-border text-sm font-semibold" role="tablist">
                    <li role="presentation" class="active">
                        <a href="#tab_form_build" aria-controls="tab_form_build" role="tab" data-toggle="tab"
                           class="inline-flex items-center gap-2 px-4 py-2 text-foreground hover:text-indigo-600">
                            <?php echo lang('form_builder'); ?>
                        </a>
                    </li>
                    <li role="presentation">
                        <a href="#tab_form_information" aria-controls="tab_form_information" role="tab" data-toggle="tab"
                           class="inline-flex items-center gap-2 px-4 py-2 text-foreground hover:text-indigo-600">
                            <?php echo lang('form_information'); ?>
                        </a>
                    </li>
                </ul>
            <?php } ?>
            <div class="tab-content">
                <?php if (isset($form)) { ?>
                    <div role="tabpanel" class="tab-pane active" id="tab_form_build">
                        <div id="build-wrap"></div>
                    </div>
                <?php } ?>
                <div role="tabpanel" class="tab-pane <?php if (!isset($form)) { echo ' active'; } ?>" id="tab_form_information">
                    <?php if (isset($form)) { ?>
                        <div class="rounded-xl border border-border bg-muted/30 p-4 space-y-3">
                            <div class="text-sm font-semibold text-foreground"><?php echo lang('form_integration_code_help'); ?></div>
                            <div class="flex flex-wrap items-center gap-3">
                                <a class="inline-flex items-center rounded-md border border-border px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted"
                                   href="<?php echo site_url('forms/leads/' . $form->form_key); ?>" target="_blank">
                                    <?php echo site_url('forms/leads/' . $form->form_key); ?>
                                </a>
                            </div>
                            <textarea class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm" id="foo"
                                      rows="2"><iframe width="600" height="850" src="<?php echo site_url('forms/leads/' . $form->form_key); ?>" frameborder="0" allowfullscreen></iframe></textarea>
                        </div>
                        <script type="text/javascript">
                            var textBox = document.getElementById("foo");
                            textBox.onfocus = function () {
                                textBox.select();
                                textBox.onmouseup = function () {
                                    textBox.onmouseup = null;
                                    return false;
                                };
                            };
                        </script>
                    <?php } ?>

                    <?php
                        $language_options = array();
                        if (!empty($languages)) {
                            foreach ($languages as $lang) {
                                $language_options[$lang->name] = ucfirst($lang->name);
                            }
                        }
                    ?>
                    <div class="grid gap-4 md:grid-cols-2">
                        <?= form_input_group('form_name', lang('form') . ' ' . lang('name'), !empty($form) ? $form->form_name : '', 'text', true); ?>
                        <div class="space-y-2">
                            <label class="text-sm font-medium text-foreground"><?= lang('lead_status') ?> <span class="text-red-500">*</span></label>
                            <div class="flex gap-2">
                                <select name="lead_status_id" class="form-control select_box w-full" style="width: 100%" required>
                                    <?php
                                    if (!empty($status_info)) {
                                        foreach ($status_info as $type => $v_leads_status) {
                                            if (!empty($v_leads_status)) { ?>
                                                <optgroup label="<?= lang($type) ?>">
                                                    <?php foreach ($v_leads_status as $v_l_status) { ?>
                                                        <option value="<?= $v_l_status->lead_status_id ?>" <?php
                                                            if (!empty($form->lead_status_id)) {
                                                                echo $v_l_status->lead_status_id == $form->lead_status_id ? 'selected' : '';
                                                            }
                                                        ?>><?= $v_l_status->lead_status ?></option>
                                                    <?php } ?>
                                                </optgroup>
                                            <?php }
                                        }
                                    }
                                    $created = can_action('127', 'created');
                                    ?>
                                </select>
                                <?php if (!empty($created)) { ?>
                                    <a data-toggle="modal" data-target="#myModal"
                                       class="btn_modal inline-flex items-center rounded-lg border border-border px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted"
                                       href="<?= base_url() ?>admin/leads/lead_status">
                                        <i data-lucide="plus" class="h-4 w-4"></i>
                                    </a>
                                <?php } ?>
                            </div>
                        </div>

                        <?= form_select_group('language', lang('language'), $language_options,
                            !empty($form->language) ? $form->language : $this->config->item('language'),
                            false, array('class' => 'select_box', 'style' => 'width:100%')); ?>

                        <div class="space-y-2">
                            <label class="text-sm font-medium text-foreground"><?= lang('lead_source') ?> <span class="text-red-500">*</span></label>
                            <div class="flex gap-2">
                                <select name="lead_source_id" class="form-control select_box w-full" style="width: 100%" required>
                                    <?php
                                    $lead_source_info = $this->db->order_by('lead_source_id', 'DESC')->get('tbl_lead_source')->result();
                                    if (!empty($lead_source_info)) {
                                        foreach ($lead_source_info as $v_lead_source) { ?>
                                            <option value="<?= $v_lead_source->lead_source_id ?>" <?= (!empty($form) && $form->lead_source_id == $v_lead_source->lead_source_id ? 'selected' : '') ?>><?= $v_lead_source->lead_source ?></option>
                                        <?php }
                                    }
                                    $_created = can_action('128', 'created');
                                    ?>
                                </select>
                                <?php if (!empty($_created)) { ?>
                                    <a data-toggle="modal" data-target="#myModal"
                                       class="btn_modal inline-flex items-center rounded-lg border border-border px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted"
                                       href="<?= base_url() ?>admin/leads/lead_source">
                                        <i data-lucide="plus" class="h-4 w-4"></i>
                                    </a>
                                <?php } ?>
                            </div>
                        </div>
                    </div>

                    <?php if (config_item('recaptcha_secret_key') != '' && config_item('recaptcha_site_key') != '') { ?>
                        <div class="rounded-xl border border-border bg-muted/30 p-4">
                            <label class="text-sm font-medium text-foreground block mb-2"><?= lang('form') . ' ' . lang('recaptcha'); ?></label>
                            <input data-toggle="toggle" name="form_recaptcha"
                                   value="1" <?php if (!empty($form) && $form->form_recaptcha == 1) { echo 'checked'; } ?>
                                   data-on="<?= lang('yes') ?>" data-off="<?= lang('no') ?>"
                                   data-onstyle="success" data-offstyle="danger" type="checkbox">
                        </div>
                    <?php } ?>

                    <div class="grid gap-4 md:grid-cols-2">
                        <?= form_input_group('submit_btn_text', lang('submit_btn_text'), !empty($form) ? $form->submit_btn_text : '', 'text', true); ?>
                        <?= form_textarea_group('submit_btn_msg', lang('submit_btn_msg'), !empty($form) ? $form->submit_btn_msg : '', 3, false); ?>
                    </div>

                    <div class="grid gap-4 md:grid-cols-2">
                        <div class="rounded-xl border border-border bg-card p-4">
                            <label class="inline-flex items-center gap-2 text-sm text-foreground">
                                <input name="allow_duplicate" id="showhide" type="checkbox" value="1"
                                    class="h-4 w-4 rounded border-border text-indigo-600 focus:ring-indigo-500"
                                    <?php
                                    if (!empty($form) && $form->allow_duplicate == 1) {
                                        echo 'checked';
                                    } elseif (empty($form)) {
                                        echo 'checked';
                                    }
                                    ?>>
                                <span class="font-medium"><?= lang('allow_duplicate_leads') ?></span>
                            </label>
                        </div>
                        <div id="shresult" class="rounded-xl border border-border bg-card p-4" style="display:
                        <?php
                        if (!empty($form) && $form->allow_duplicate != 1) {
                            echo 'block';
                        } else {
                            echo 'none';
                        } ?>">
                            <label class="text-sm font-medium text-foreground mb-2 block"><?= lang('track_duplicate_field') ?></label>
                            <select name="track_duplicate_field" class="form-control select_box w-full" style="width: 100%">
                                <option value=""></option>
                                <?php foreach ($db_fields as $field) { ?>
                                    <option value="<?php echo $field->name; ?>"<?php if (isset($form) && $form->track_duplicate_field == $field->name) { echo ' selected'; } ?>>
                                        <?php echo $field->label; ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </div>
                        <div class="rounded-xl border border-border bg-card p-4">
                            <label class="inline-flex items-center gap-2 text-sm text-foreground">
                                <input name="notify_lead_imported" type="checkbox" value="1"
                                       class="h-4 w-4 rounded border-border text-indigo-600 focus:ring-indigo-500"
                                    <?php
                                    if (!empty($form) && $form->notify_lead_imported == 1) {
                                        echo 'checked';
                                    } elseif (empty($form)) {
                                        echo 'checked';
                                    }
                                    ?>>
                                <span class="font-medium"><?= lang('notify_lead_imported') ?></span>
                            </label>
                        </div>
                    </div>

                    <?php
                    $permissionL = null;
                    if (!empty($form->permission)) {
                        $permissionL = $form->permission;
                    }
                    ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <?= get_permission(2, 4, $assign_user, $permissionL, lang('assigned_to')); ?>
                    </div>

                    <div class="flex items-center justify-end gap-2">
                        <button type="submit" class="btn btn-primary"><?php echo lang('submit'); ?></button>
                    </div>
                </div>
            </div>
            <?php echo form_close(); ?>
        </div>
    </div>
</div>
<script src="<?= base_url() ?>assets/plugins/jquery-ui/jquery-u.js"></script>
<script src="<?php echo base_url('plugins/formbuilder/form-builder.min.js'); ?>"></script>

<script>
    var buildWrap = document.getElementById('build-wrap');
    var formData = <?php echo json_encode($formData); ?>;

    if (formData != null && formData.length < 1) {
        // If user paste with styling eq from some editor word and the Codeigniter XSS feature remove and apply xss=remove, may break the json.
        formData = formData.replace(/=\\/gm, "=''");
    }
</script>
<?php $this->load->view('admin/leads/_form_js_formatter'); ?>
<script>
    $(function () {
        $('body').on('copy paste', 'input', function (e) {
            e.preventDefault();
        });

        $('body').on('blur', '.form-field.editing', function () {
            $.Shortcuts.start();
        });

        $('body').on('focus', '.form-field.editing', function () {
            $.Shortcuts.stop();
        });

        var formBuilder = $(buildWrap).formBuilder(fbOptions);


        setTimeout(function () {
            $(".form-builder-save").wrap("<div class='btn-bottom-toolbar text-right'></div>");
            $btnToolbar = $('body').find('#tab_form_build .btn-bottom-toolbar');
            $btnToolbar = $('#tab_form_build').append($btnToolbar);
            $btnToolbar.find('.btn').addClass('btn-info');
        }, 100);

        $('body').on('click', '.save_form_data', function () {
            $.post(base_url + 'admin/leads/save_form_data', {
                formData: formBuilder.formData,
                form_id: $('input[name="form_id"]').val()
            }).done(function (response) {
                response = JSON.parse(response);
                if (response.success == true) {
                    toastr['success'](response.message);
                }
            });
        });


    });

</script>
