<?php
$created = can_action('143', 'created');
$edited = can_action('143', 'edited');
$deleted = can_action('143', 'deleted');
if (!empty($created) || !empty($edited)) {
    $category_options = array('' => lang('select') . ' ' . lang('categories'));
    $kb_category_info = get_result('tbl_kb_category', array('status' => 1));
    if (!empty($kb_category_info)) {
        $kb_category_info = array_reverse($kb_category_info);
        foreach ($kb_category_info as $v_kb_category) {
            $category_options[$v_kb_category->kb_category_id] = $v_kb_category->category;
        }
    }
    $category_selected = !empty($articles_info->kb_category_id) ? $articles_info->kb_category_id : '';
    $status_selected = !empty($articles_info->status) ? $articles_info->status : '1';
    $for_all_selected = !empty($articles_info->for_all) ? $articles_info->for_all : 'No';
    ?>
    <div class="space-y-4">
        <div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">            
            <?php if (!empty($articles_info->kb_id)) { ?>
                <div class="flex flex-wrap items-center gap-2">
                    <a target="_blank" href="<?= base_url() ?>admin/knowledgebase/details/articles/<?= $articles_info->kb_id ?>"
                       class="inline-flex items-center gap-2 rounded-lg border border-slate-200 bg-slate-50 px-3 py-2 text-xs font-semibold text-slate-600 hover:bg-slate-100">
                        <i data-lucide="eye" class="h-4 w-4"></i>
                        <?= lang('view_details') ?>
                    </a>
                    <a href="<?= base_url() ?>admin/knowledgebase/new_articles"
                       class="inline-flex items-center gap-2 rounded-lg border border-indigo-200 bg-indigo-50 px-3 py-2 text-xs font-semibold text-indigo-600 hover:bg-indigo-100">
                        <i data-lucide="plus" class="h-4 w-4"></i>
                        <?= lang('new') . ' ' . lang('articles') ?>
                    </a>
                    <?php if (!empty($deleted)) { ?>
                        <a href="<?= base_url() ?>admin/knowledgebase/delete_articles/<?= $articles_info->kb_id ?>/1"
                           class="inline-flex items-center gap-2 rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-xs font-semibold text-red-600 hover:bg-red-100">
                            <i data-lucide="trash-2" class="h-4 w-4"></i>
                            <?= lang('delete') ?>
                        </a>
                    <?php } ?>
                </div>
            <?php } ?>
        </div>

        <div class="rounded-2xl border border-border bg-card shadow-sm">
            <div class="border-b border-border px-5 py-4">
                <div class="flex items-center gap-2 text-sm font-semibold text-foreground">
                    <i data-lucide="file-text" class="h-5 w-5"></i>
                    <span><?= lang('articles') ?></span>
                </div>
            </div>
            <div class="p-5">
                <form id="form_validation" data-parsley-validate="" novalidate="" enctype="multipart/form-data"
                      action="<?php echo base_url() ?>admin/knowledgebase/saved_articles/<?php
                      if (!empty($articles_info->kb_id)) {
                          echo $articles_info->kb_id;
                      }
                      ?>" method="post" class="space-y-4">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <?= form_input_group('title', lang('title'), !empty($articles_info->title) ? $articles_info->title : '', 'text', true, array('id' => 'title', 'class' => 'gen_slug')); ?>
                        <?= form_input_group('slug', lang('slug'), !empty($articles_info->slug) ? $articles_info->slug : '', 'text', true, array('id' => 'slug')); ?>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <?php
                        $add_link = form_select_add_link('kb_category_id', base_url() . 'admin/knowledgebase/new_categories/inline', lang('new') . ' ' . lang('categories'));
                        echo form_select_group(
                            'kb_category_id',
                            lang('categories'),
                            $category_options,
                            $category_selected,
                            true,
                            array('class' => 'select_box', 'data-live-search' => 'true'),
                            '',
                            $add_link
                        );
                        ?>
                        <?= form_switch_radio('for_all', lang('internal_view'), $for_all_selected, false, array(), 'stacked', array('Yes' => lang('yes'), 'No' => lang('no'))); ?>
                    </div>

                    <?php
                    $status_options = array(
                        '1' => lang('active'),
                        '2' => lang('inactive'),
                    );
                    ?>
                    <?= form_radio_group('status', lang('status'), $status_options, $status_selected); ?>

                    <?= form_editor('description', lang('description'), !empty($articles_info->description) ? $articles_info->description : '', false, array('rows' => 6, 'class' => 'textarea_lg')); ?>

                    <?php
                    $uploaded_file = !empty($articles_info->attachments) ? json_decode($articles_info->attachments) : array();
                    ?>
                    <div class="rounded-xl border border-border p-4 space-y-3">
                        <?= form_attachment_group('files[]', lang('attachments'), 'dropzone', array(
                            'dropzone_id' => 'kb-files-dropzone',
                            'list_id' => 'kb-files-list',
                            'existing_files' => $uploaded_file,
                            'download_url_base' => !empty($articles_info->kb_id) ? base_url('admin/knowledgebase/download/' . $articles_info->kb_id) : '',
                        )); ?>
                    </div>

                    <div class="flex items-center justify-end gap-2 pt-2">
                        <button type="submit" id="file-save-button" class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500">
                            <i data-lucide="save" class="h-4 w-4"></i>
                            <?= lang('save') ?>
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        $(document).ready(function () {
            function getSlug(title) {
                var slug_url = '<?= base_url('admin/knowledgebase/getSlug')?>';
                $.get(slug_url, {title: title}, function (slug) {
                    $('#slug').val(slug).change();
                });
            }

            $('.gen_slug').change(function (e) {
                getSlug($(this).val());
            });

            $(document).on("click", ".existing_image", function () {
                $(this).closest('.rounded-lg').remove();
            });
        })
    </script>
<?php } ?>
