/**
 * js for event behavior
 *
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */

$(function () {
    hide_del_btn();
});
/** Handle press the Add button */
$('.add').on('click', function () {
    var base = $(this).parent().parent().parent('.tb-item');
    base.find('.del').show();
    // clone input form
    base.clone(true).insertAfter(base);

    // Scroll to added element
    $('html,body').animate({ scrollTop: base.next().offset().top });

    // Input form accordion display
    base.parent().parent('.tb').find('.ac-tb-form').show();
});
/** Handle press the Delete button */
$('.del').on('click', function () {
    var base = $(this).parent().parent().parent('.tb-item');
    var tbwrap = base.parent('.tb-wrap');
    var tb = tbwrap.parent('.tb');

    // Delete area
    base.remove();
    // Hidden delete button
    hide_del_btn();
    // Form area display switch
    tbWrapClear(tbwrap);

    // Hide collapse icon
    if (tb.find('.ac-tb-form').is(':hidden')) {
        tb.find('.fold-area').addClass('hide');
    }
});

/** handle Press the close button */
$('.close-window').on('click', function () {
    window.close();
});

/** Hide delete button when there is only one tablespace form */
function hide_del_btn() {
    $('.tb-wrap').each(function (i, elem) {
        var len = $(elem).find('.tb-item').length;
        // When there is one item in the table
        if (len <= 1) {
            $(elem).find('.del').hide();
            $(this).children('.tb-item').removeClass('visually-hidden');
            // Input form accordion hidden
            $(this).parent().find('.ac-tb-form').hide();
        }
    });
}
/** Handle when deleting while closing the accordion */
function tbWrapClear(tbwrap) {
    // If you delete the form while closing the accordion
    // Hide parent element if all child elements are hidden
    if (tbwrap.children(':not(.visually-hidden)').length == 0) {
        tbwrap.addClass('visually-hidden');
    } else {
        tbwrap.removeClass('visually-hidden');
    }
}

/** input form accordion */
$('.ac-tb-form').on('click', function () {
    var tb = $(this).parent().parent().parent('.tb');
    var elems = tb.find('.tb-wrap').find('.tb-item');
    var tbwrap = tb.find('.tb-wrap');
    if (!$(this).hasClass('hide')) {
        elems.each(function (i) {
            i = i + 1;
            if (i != elems.size()) {
                $(this).addClass('visually-hidden');
            }
        });
        $(this).text('開く');

        // Form area display switch
        tbWrapClear(tbwrap);

        // Collapse icon view
        tb.find('.fold-area').removeClass('hide');
    } else {
        elems.removeClass('visually-hidden');
        $(this).text('閉じる');

        // Form area display switch
        tbWrapClear(tbwrap);

        // Hide collapse icon
        tb.find('.fold-area').addClass('hide');
    }
    // opening and closing
    $(this).toggleClass('hide');
});

/** toggle switch */
$('.toggle').on('click', function () {
    $(this).toggleClass('checked');
    if (!$('input[name="check"]').prop('checked')) {
        $('.toggle input').prop('checked', true);
    } else {
        $('.toggle input').prop('checked', false);
    }
});

/** Menu display */
$('.c-dropdown-trigger').on('click', function () {
    $('[data-target="' + $(this).attr('id') + '"]').toggleClass('show');
    $('.mask').removeClass('hide');
});

/** Close menu */
$('.mask').on('click', function () {
    $('.mask').addClass('hide');
    $('.c-dropdown-menu').removeClass('show');
});

/** Select card list */
$('.select-card-list .card .select-label').on('click', function () {
    $(this).closest('.select-card-list').find('.card').removeClass('selected');
    $(this).closest('.card').addClass('selected');
});

/** List display switching (list display) */
$('.view-menu .view-list-btn').on('click', function () {
    $('.view-block-btn').removeClass('active');
    $('.view-list-btn').addClass('active');
    $('.view-content').removeClass('view-block');
    $('.view-content').addClass('view-list');
});

/** List display switching (grid display) */
$('.view-menu .view-block-btn').on('click', function () {
    $('.view-list-btn').removeClass('active');
    $('.view-block-btn').addClass('active');
    $('.view-content').removeClass('view-list');
    $('.view-content').addClass('view-block');
});