<?php
$languages = CE_Lib::getEnabledLanguages(true);

/* Build Categories */
$Categories = '';
foreach ($this->arrayCategories as $cat) {
    if ($cat['is_global_series']) {
        $cat['name'] .= " (" . $this->user->lang("Global Series") . ")";
    } elseif ($cat['is_series']) {
        $cat['name'] .= " (" . $this->user->lang("Series") . ")";
    }

    $prename = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $cat['blanks']);
    if ($cat['blanks'] > 0) {
        $prename .= "|-";
    }

    $Categories .= '<option value="'.$cat['id'].'"'
        .($this->categoryid == $cat['id'] ? ' selected' : '')
        .'>'.$prename.$cat['name'].'</option>';
}
?>

<script data-cfasync="false">
$(function () {

    // BS5 tooltips
    document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(el => {
        new bootstrap.Tooltip(el);
    });

    // Language tabs
    $('#langTabs button').on('click', function () {
        const lang = $(this).data('lang');
        $('#langTabs button').removeClass('active');
        $(this).addClass('active');
        $('.language').hide();
        $('.language.' + lang).show();
    });

    // Ticket type
    $('#input_tickettype').select2({
        width: '100%',
        dropdownParent: $('.richwindow'),
        minimumResultsForSearch: 10,
        allowClear: false,
        ajax: {
            url: "index.php?fuse=support&action=gettickettypes&controller=tickettype&addAll=1",
            dataType: 'json',
            delay: 400,
            processResults: function (data) {
                const results = [{ id: -1, text: clientexec.lang('All ticket types') }];
                data.groups.forEach(item => {
                    if (item.ticketTypeId !== -1) {
                        results.push({ id: item.ticketTypeId, text: item.ticketTypeName });
                    }
                });
                return { results };
            }
        }
    }).append(
        new Option(
            "<?php echo $this->tickettypename; ?>",
            "<?php echo $this->tickettypeid; ?>",
            true,
            true
        )
    ).trigger('change');

    // Tags
    $('#input_tags').select2({
        width: '100%',
        dropdownParent: $('.richwindow'),
        tags: true,
        minimumInputLength: 2,
        tokenSeparators: [','],
        data: <?php echo json_encode($available); ?>
    });

    // SEO Keywords
    $('#seo_keywords').select2({
        width: '100%',
        dropdownParent: $('.richwindow'),
        tags: true,
        minimumInputLength: 2,
        tokenSeparators: [',']
    });
});
</script>

<?php if (count($languages) > 1) { ?>
<ul class="nav nav-pills mb-3" id="langTabs">
    <?php $active = true; foreach ($languages as $key => $lang) { ?>
        <li class="nav-item">
            <button type="button"
                class="nav-link<?php if ($active) { echo ' active'; $active = false; } ?>"
                data-lang="<?php echo $key; ?>">
                <?php echo $lang; ?>
            </button>
        </li>
    <?php } ?>
</ul>
<?php } ?>

<div class="container-fluid">
    <div class="row g-3">

        <div class="col-md-6">
            <label class="form-label fw-bold"><?php echo $this->user->lang('Title'); ?></label>
            <?php
            if (count($languages) > 1) {
                $active = true;
                foreach ($languages as $key => $lang) { ?>
                    <span class="language <?php echo $key; ?>" <?php if (!$active) echo 'style="display:none"'; ?>>
                        <input class="form-control required"
                            name="title<?php echo $key; ?>"
                            value="<?php echo $this->titleLanguages[$key]; ?>"
                            <?php if ($active) { echo 'id="mainlanguagetitle"'; $active = false; } ?>>
                    </span>
            <?php }} else { ?>
                <input class="form-control required" name="title" id="mainlanguagetitle" value="<?php echo $this->title; ?>">
            <?php } ?>
        </div>

        <div class="col-md-6">
            <label class="form-label fw-bold"><?php echo $this->user->lang('Author'); ?></label>
            <input class="form-control required" name="author" value="<?php echo $this->authorname; ?>">
        </div>

        <div class="col-md-6">
            <label class="form-label fw-bold"><?php echo $this->user->lang('Category'); ?></label>
            <select class="form-select select2" name="category">
                <option value="-1">-- <?php echo $this->user->lang('Select a Category'); ?> --</option>
                <?php echo $Categories; ?>
            </select>
        </div>

        <div class="col-md-6">
            <label class="form-label fw-bold"><?php echo $this->user->lang('Access'); ?></label>
            <select class="form-select select2" name="access">
                <option value="-1"<?php if ($this->access == -1) echo ' selected'; ?>>Draft</option>
                <option value="2"<?php if ($this->access == 2) echo ' selected'; ?>>Public</option>
                <option value="0"<?php if ($this->access == 0) echo ' selected'; ?>>Members Only</option>
                <option value="1"<?php if ($this->access == 1) echo ' selected'; ?>>Staff Only</option>
            </select>
        </div>

        <div class="col-md-6">
            <label class="form-label fw-bold"><?php echo $this->user->lang('Ticket Type'); ?></label>
            <select id="input_tickettype" name="tickettype"></select>
        </div>

        <div class="col-md-6">
            <label class="form-label fw-bold"><?php echo $this->user->lang('Tags'); ?></label>
            <select class="form-control" id="input_tags" name="tags[]" multiple>
                <?php
                    if (!empty($this->tags)) {
                        foreach (explode(',', $this->tags) as $tag) { ?>
                    <option selected><?php echo htmlspecialchars($tag); ?></option>
                <?php
                        }
                    }
                ?>
            </select>
        </div>

        <div class="col-12">
            <div class="form-check">
                <input class="form-check-input" type="checkbox" name="includefaq" id="includefaq"
                    <?php if ($this->includefaq) echo 'checked'; ?>>
                <label class="form-check-label" for="includefaq">
                    <?php echo $this->user->lang('Designate this article as an FAQ entry.'); ?>
                </label>
            </div>
        </div>

        <div class="col-12">
            <label class="form-label fw-bold"><?php echo $this->user->lang('SEO Description'); ?></label>
            <input class="form-control" name="seo_desc" value="<?php echo $this->seoDesc; ?>">
        </div>

        <div class="col-12">
            <label class="form-label fw-bold"><?php echo $this->user->lang('SEO Keywords'); ?></label>
            <select class="form-control" id="seo_keywords" name="seo_keywords[]" multiple>
                <?php
                    if (!empty($this->seoKeywords)) {
                        foreach (explode(',', $this->seoKeywords) as $kw) { ?>
                    <option selected><?php echo htmlspecialchars($kw); ?></option>
                <?php
                        }
                    }
                ?>
            </select>
        </div>

        <div class="col-12">
            <label class="form-label fw-bold"><?php echo $this->user->lang('Content'); ?></label>
            <?php
            if (count($languages) > 1) {
                $active = true;
                foreach ($languages as $key => $lang) { ?>
                    <span class="language <?php echo $key; ?>" <?php if (!$active) echo 'style="display:none"'; ?>>
                        <textarea class="form-control wysihtml5 required"
                            name="content<?php echo $key; ?>"
                            rows="8"
                            style="height:270px"
                            <?php if ($active) { echo 'id="mainlanguagecontent"'; $active = false; } ?>>
<?php echo $this->contentLanguages[$key]; ?>
                        </textarea>
                    </span>
            <?php }} else { ?>
                <textarea class="form-control wysihtml5 required"
                    name="content"
                    id="mainlanguagecontent"
                    rows="8"
                    style="height:270px"><?php echo $this->content; ?></textarea>
            <?php } ?>
        </div>

    </div>
</div>