$(function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
    var sortList;
    var sort_list = [];
    var checkbox_html;
    sort_list[1] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
    sort_list[2] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
    sort_list[3] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
    sort_list[4] = '';
    sort_list[5] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
    sort_list[6] = '<div class="f_formTextarea"><div class="f_textareaContent"><textarea class="f_textarea"></textarea></div></div>';
    sort_list[7] = '<div class="f_formSelect"><div class="f_selectContent"><select><option>请选择</option></select></div></div>';
    sort_list[8] = '<div class="f_formText"><input type="file" disabled class="f_input"/></div>';
    sort_list[9] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
    sort_list[10] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
    sort_list[11] = '\n' +
        '            <div class="f_formSelect">\n' +
        '                <div class="f_selectContent">\n' +
        '                    <select>\n' +
        '                        <option>请选择</option>\n' +
        '                    </select>\n' +
        '                </div>\n' +
        '                <div class="f_selectContent">\n' +
        '                    <select>\n' +
        '                        <option>请选择</option>\n' +
        '                    </select>\n' +
        '                </div>\n' +
        '                <div class="f_selectContent">\n' +
        '                    <select>\n' +
        '                        <option>请选择</option>\n' +
        '                    </select>\n' +
        '                </div>\n' +
        '             </div>';
    sort_list[12] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
    // 添加表单元素
    $('.f_item').click(function(){
        var id = $(this).data('id');
        var sort = $(this).data('type');
        var name = $(this).data('name');
        var html = forms(id, sort, name);
        if($(this).hasClass('active')) {
            $(this).removeClass('active');
            $('#item'+id).remove();
            $('#check'+id).parent('.checkbox').remove();
            if($('.f_content').html() == '') {
                $('.f_submit').hide();
            }
        } else {
            $(this).addClass('active');
            $('.f_content').append(html);
            $('.checkbox_content').append(checkbox_html);
            $('.f_submit').show();
        }
    });
    // 切换选中
    $('.f_body').on('click','.f_group',function(){
        var id = $(this).data('id');
        $('.checkbox').hide();
        $('#check'+id).parent('.checkbox').show();
        $('.f_group').siblings().find('.f_component').removeClass('f_clicked');
        $(this).find('.f_component').addClass('f_clicked');
    });
    // 添加渲染内容
    function forms(id, sort, name, status = 0){
        sortList = sort_list[sort];
        if(sort == 4) {
            var data = {
                id: id,
                sort: sort
            };
            getRadioData(data);
        }
        var html = `<div class="f_group" id="item${id}" data-id="${id}" data-type="${sort}" data-status="${status}">
                        <div class="f_component">
                            <div class="f_mask"></div>
                            <div class="f_contentText">
                                <p class="f_title">
                                    <span>${name}</span>
                                </p>
                            </div>
                            <div class="f_contentBody">
                                ${sortList}
                            </div>
                        </div>
                        <div class="f_remove"></div>
                        <div class="f_clear"></div>
                    </div>`;
        checkbox_html = `<div class="checkbox" style="display: none;">
                        <label id="check${id}"><input type="checkbox">是否为必填项</label>
                    </div>`;
        return html;
    }
    // 获取单选内容数据
    function getRadioData(data) {
        $.ajax({
            async: false,
            url: radio_url,
            type: "POST",
            data: data,
            dataType: "JSON",
            success: function(res){
                if(res.code == 1) {
                    html = '';
                    $(res.data).each(function(index,element){
                        html += '<div class="f_radioContent">' +
                                '    <span class="">'+element+'</span>\n' +
                                '</div>';
                    });
                    sortList = '<div class="f_formRadio">\n'+
                                html + '</div>';
                }
            }
        })
    }
    // 移除元素
    $('.f_body').on('click','.f_remove',function(){
        $(this).parent().remove();
    });
    // 提交选择的表单数据
    $('.btn-primary').click(function(){
        var data = [];
        var btn = $(this);
        var text = $(this).html();
        $('.f_group').each(function(index,element){
            var id = $(this).data('id');
            var type = $(this).data('type');
            var status = $(this).data('status');
            var checked = 0;
            if($('#check'+id).find('input').prop("checked")) {
                checked = 1;
            }
            param = {
                id: $(this).data('id'),
                type: $(this).data('type'),
                status: $(this).data('status'),
                checked: checked
            };
            data.push(param);
        });
        console.log({
            name: $('input[name=name]').val(),
            icon: $('input[name=icon]').val(),
            param: data
        });
        $.ajax({
            url: url,
            type: "POST",
            data: {
                name: $('input[name=name]').val(),
                icon: $('input[name=icon]').val(),
                param: data
            },
            dataType: "JSON",
            beforeSend: function(){
                btn.attr('disabled',true);
                btn.text(text+'中...');
            },
            success: function(data){
                new Noty({
                    text: data.msg,
                    type: 'success',
                    layout: 'topCenter',
                    modal: true,
                    animation: {
                        open: 'animated bounceInDown', // Animate.css class names
                        close: 'animated bounceOutUp', // Animate.css class names
                    },
                    timeout: 1,
                    callbacks: {
                        afterClose: function () {
                            if (data.url) {
                                //返回带跳转地址
                                window.location.href = data.url;
                            } else {
                                if (data.code == 1) {
                                    //刷新当前页
                                    reloadPage(window);
                                }
                            }
                        }
                    }
                }).show();
            },
            complete: function(res){
                btn.attr('disabled',false);
                btn.text(text);
            }
        });
    });
});