{#
/**
 * Copyright (C) 2020 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
#}

<script type="text/javascript">

    // Runs after form opens
    function datasetview_form_edit_open() {

        if($(this).find('form').data('formStep') === 1) {
            // If data set exists, set the drop-down to that value
            var dataSetId = $(this).find('form').data().elementOptions.dataSetId;
            
            if(dataSetId !== undefined) {
                $(this).find('#dataSetId').val(dataSetId);
            }
        } else if($(this).find('form').data('formStep') === 2) {
            formHelpers.setupCheckboxInputFields($(this).find('form'), '#useDuration', '.duration-fields');

            // Set override template field, using the helper ()
            formHelpers.setupCheckboxInputFields($(this), '#overrideTemplate', '.template-override-controls', '.template-selector-control');

            // Set clock type input field, using the helper ()
            formHelpers.setupObjectValueInputFields($(this), '#templateId', ['.template-selector-default'], ['empty']);

            // Set order and filtering clauses fields, using the helper ()
            formHelpers.setupCheckboxInputFields($(this).find('form'), '#useOrderingClause', '.order-clause-field', '#orderClause');
            formHelpers.setupCheckboxInputFields($(this).find('form'), '#useFilteringClause', '.filter-clause-field', '#filterClause');

            // Initialise CKEditor for the noDataMessage
            formHelpers.setupDualTypeTextArea(this, 'noDataMessage');

            // Setup template override
            formHelpers.setupTemplateOverriding(this, '#overrideTemplate', '#templateId', 
            {
                '#styleSheet': 'css'
            });

            dataSetFormSetup(this);
        }
    }

    // Runs before form submit
    function datasetview_form_edit_submit() {
        
        if($(this).find('form').data('formStep') === 2) {
            var form = $("#dataSetViewEditForm2");

            // Clean the previous generated hidden dataSetColumns from the form
            form.find('input[name="dataSetColumnId[]"]').remove();

            // Add all the selected columns to the form as hidden input fields
            $($("#columnsIn").sortable('toArray')).each(function() {
                form.append('<input type="hidden" name="dataSetColumnId[]" value="' + this + '" />');
            });

            form.data("apply", true);
            
            formHelpers.updateCKEditor();
        }
    }

    function dataSetFormSetup(dialog) {

        // Setup lists drag and sort ( with double click )
        $("#columnsIn, #columnsOut").sortable({
            connectWith: '.connectedSortable',
            dropOnEmpty: true
        }).disableSelection();
        $(".li-sortable", dialog).dblclick(switchLists);

        // Configure the query for sorting and filtering
        formHelpers.configureQueryBuilder(dialog, datasetQueryBuilderTranslations);

        // Populate the font list with options
        var $fontFamily = $(dialog).find("#fontFamily");
        $.ajax({
            method: "GET",
            url: $fontFamily.data('searchUrl'),
            success: function(res) {
                if (res.success) {
                    $.each(res.data, function(index, element) {
                        if ($fontFamily.data('value') === element.familyName) {
                            $fontFamily.append($('<option value="' + element.familyName + '" selected>' + element.displayName + '</option>'));
                        } else {
                            $fontFamily.append($('<option value="' + element.familyName + '">' + element.displayName + '</option>'));
                        }
                    });
                }
            }
        });

        if($(dialog).hasClass('modal')) {
            $(dialog).on("hide.bs.modal", function(e) {
                if(e.namespace === 'bs.modal') {
                    // Remove colour pickers
                    destroyColorPicker($(dialog).find(".XiboColorPicker"));
                }
            });
        }
    }
</script>