/**
 * 菜单管理
 */

jQuery(function($) {

    //递归收缩目录	
    function recursiveShrink(id) {
        $("tr").each(function(index) {
            if ($(this).data('parentClass') == id) {
                $(this).hide();
                recursiveShrink($(this).data('classId'));
            }
        });

    }
//递归展开目录	
    function recursiveExpand(id) {
        $("tr").each(function(index) {
            if ($(this).data('parentClass') == id) {
                $(this).show();
                if ($(this).find('.expandChild').data('expandStatus') == 1) {
                    recursiveExpand($(this).data('id'));
                }
            }
        });

    }
    var isloading = 0;//加载锁
    //展开子栏目
    $('table').on('click', '.expandChild', function() {
        var obj = $(this);
        var id = obj.data('id');
        var app_name = obj.data('app_name');
        var status = obj.data('expandStatus');//展开状态
        var level = obj.data('level');
        if (status == 0) {
            if (isloading == 1)
                return false;
            else
                isloading = 1;
            var curTr = $(this).parents('tr');
            $.post(getMenuUrl, {app_name: app_name, id: id, level: level, _csrf:csrfToken}, function(json) {
                if (json.status == 1) {
                    curTr.after(json.data);
                    obj.data('expandStatus', 1);
                    obj.removeClass('away_icon').addClass('start_icon');
                }
                isloading = 0;
            }, 'JSON');
        }
        else if (status == 1) {
            //收缩
            recursiveShrink(id);
            obj.data('expandStatus', 2);
            obj.removeClass('start_icon').addClass('away_icon');
        }
        else {
            //展开
            recursiveExpand(id);
            obj.data('expandStatus', 1);
            obj.removeClass('away_icon').addClass('start_icon');
        }
        return false;
    });

/**
 * 异步获取应用菜单
 */
    $("#chooseApp").on('change', function() {
        var appName = $(this).val();
        $.getJSON(appMenuUrl, {app_name: appName, parent_id: parent_id,disabled_id:disabled_id, _csrf:csrfToken}, function(res) {
            $("#Menu_parent_id option:first-child").nextAll("option").remove();
            $("#Menu_parent_id").append(res);
        });
    });
    $("#chooseApp").change();


    //删除提示
    $(document).on('click', '.menu-delete', function() {
        if (!confirm('确认删除?'))
            return false;
        var $this=$(this);
        var href = $this.attr('href');
        $.getJSON(href,{type:'ajaxdelete', _csrf:csrfToken},function(res) {
            if (res.status) {
                $this.parents('tr:first').remove();
            }
            alert(res.info);
        });
        return false;
    });
});

