var select_pin = "";
var f_cur = 0; //当前选中的一级标记
var s_cur = 0;  //当前选中的二级标记
// 添加一级菜单
function addFirstMenu() {
    //统计view_footer_right的数量,最多只能新增3个
    var length = $(".view_footer_right .view_menu").length;
    if (4 > length) {
        var cur = 1;
        if (1 < length) {
            var number = $(".view_footer_right .view_menu:last").prev().data('number');
            cur = number + 1;
        }
        $(".subbutton-actived").removeClass('subbutton-actived');
        var html = "<div class=\"subbutton-actived view_menu view_menu_"+cur+"\" data-number='"+cur+"'>\n" +
            "<div id='title_"+cur+"' class=\"text-ellipsis\" onclick='changeSubbuttonActived(this,"+cur+",0);'>新建菜单</div>\n" +
            "<ul class=\"view_menu-sub\">\n" +
            "<li class='add-second'><div class=\"text-ellipsis\" onclick='addSecondMenu(this);' data-parentnumber='"+cur+"'><i class=\"glyphicon glyphicon-plus text-info\"></i></div></li>\n" +
            "</ul>" +
            "</div>";
        $(".view_footer_right .view_menu:last").before(html);
        add_first_menu_form(cur);
        if (3 == length) {
            $(".view_footer_right .view_menu:last").remove();
        }
        select_pin = "_"+cur;
        f_cur = cur;
    }
}
// 添加二级菜单
function addSecondMenu(obj) {
    var parentnumber = $(obj).data('parentnumber');
    //最多新增5个
    var last_e = $(".view_menu_"+parentnumber+" ul li:last");
    var length = $(".view_menu_"+parentnumber+" ul li").length;
    if (6 > length) {
        var cur = 1;
        if (1 < length) {
            var number = last_e.prev().data('number');
            cur = number + 1;
            $(".subbutton-actived").removeClass('subbutton-actived');
            var html = "<li class='subbutton-actived' onclick='changeSubbuttonActived(this,"+parentnumber+","+cur+");' id=\"sub_"+parentnumber+"_"+cur+"\" data-number='"+cur+"'><div id='title_"+parentnumber+"_"+cur+"' class=\"text-ellipsis\">新建子菜单</div></li>\n";
            last_e.before(html);
            add_second_menu_form(parentnumber,cur);
            if (5 == length) {
                last_e.remove();
            }
            select_pin = "_"+parentnumber+"_"+cur;
            f_cur = parentnumber;
            s_cur = cur;
        }else if (1 == length) {
            layer.confirm('添加子菜单后，一级菜单的内容将被清除。确定添加子菜单？', {
                shade: layer_shade,
                area: ['480px', '190px'],
                move: false,
                title: '提示',
                btnAlign:'r',
                closeBtn: 3,
                btn: ['确定', '取消'] ,//按钮
                success: function () {
                    $(".layui-layer-content").css('text-align', 'left');
                }
            }, function(){
                $(".subbutton-actived").removeClass('subbutton-actived');
                var html = "<li class='subbutton-actived' onclick='changeSubbuttonActived(this,"+parentnumber+","+cur+");' id=\"sub_"+parentnumber+"_"+cur+"\" data-number='"+cur+"'><div id='title_"+parentnumber+"_"+cur+"' class=\"text-ellipsis\">新建子菜单</div></li>\n";
                last_e.before(html);
                add_second_menu_form(parentnumber,cur);
                if (5 == length) {
                    last_e.remove();
                }
                select_pin = "_"+parentnumber+"_"+cur;
                f_cur = parentnumber;
                s_cur = cur;
                layer.closeAll();
            }, function(index){
                layer.closeAll();
                return false;
            });
        }

    }
}

//添加一级菜单栏右侧内容
function add_first_menu_form(cur) {
    $(".cm-edit-after").addClass('none');
    $(".cm-edit-before").addClass('none');
    var content_html = "<div class=\"cm-edit-after\" id=\"right_form_"+cur+"\" data-label='_"+cur+"'>\n" +
        "<div class=\"cm-edit-right-header\">\n" +
        "<span id=\"cm-tit\"></span>\n" +
        "<a class=\"pull-right\" href=\"javascript:void(0);\" onclick='delMenu(this,1);'>删除菜单("+cur+")</a></div>\n" +
        "<div class=\"form-horizontal wrapper-md\">\n" +
        "<div class=\"form-group\">\n" +
        "<label class=\"col-sm-2 control-label\">菜单名称:</label>\n" +
        "<div class=\"col-sm-5\">\n" +
        "<input name=\"data["+cur+"][value][title]\" type=\"text\" class=\"form-control\" oninput='changeInput(this);' maxlength=\"20\">\n" +
        "</div>\n" +
        "<div class=\"col-sm-5 help-block\">\n" +
        "<div>字数不超过4个汉字或8个字符</div>\n" +
        // "<div class=\"font_sml\" style=\"display: block;\">若无二级菜单，可输入20个汉字或60个字符</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "<div class=\"form-group\" id=\"radioGroup\">\n" +
        "<label class=\"col-sm-2 control-label\">菜单内容:</label>\n" +
        "<div class=\"col-sm-10 LebelRadio\">\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"sendmsg\" checked> 发送消息</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"view\"> 跳转网页</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"click\"> 触发关键字</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"scancode_push\"> 扫码（推事件）</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"scancode_waitmsg\"> 扫码（等待信息）</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"pic_sysphoto\"> 系统拍照发图</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"pic_photo_or_album\"> 拍照或者相册发图</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"pic_weixin\"> 微信相册发图</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+cur+"][value][menu]\" value=\"location_select\"> 地理位置</label>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +

        "<div class=\"cm-edit-content-con\" id=\"editMsg\">\n" +
        "<div class=\"editTab\">\n" +
        "<div class=\"editTab-heading\">\n" +
        "<ul id=\"clickUl\" class=\"msg-panel-tab\">\n" +
        "<li class=\"msg-tab_item on\" onclick=\"changeType(this,'pic')\" >\n" +
        "<span class=\"msg-icon msg-icon-tuwen\"></span>\n" +
        "图文消息\n" +
        "</li>\n" +
        "<li class=\"msg-tab_item\" onclick=\"changeType(this,'text')\">\n" +
        "<span class=\"msg-icon msg-icon-word\"></span>\n" +
        "文本\n" +
        "</li>\n" +
        "</ul>\n" +
        "</div>\n" +
        "<div class=\"editTab-body\">\n" +
        "<div class=\"msg-panel-context\">\n" +
        "<div class=\"msg-context-item\" data-href='"+pic_url+"' onclick=\"openFullframe(this, '选择素材', '80%', '80%');\">\n" +
        "<div class=\"msg-panel-center msg-panel-center\" data-toggle=\"modal\" data-target=\"#selectModal\">\n" +
        "<span class=\"message-plus\">+</span><br>从素材库中选择</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +

        "<div class=\"cm-edit-content-con none\" id=\"editPage\">\n" +
        "<div class=\"cm-edit-page\">\n" +
        "<div class=\"row\">\n" +
        "<label class=\"col-sm-6 control-label\" style=\"text-align: left;\">粉丝点击该菜单会跳转到以下链接:\n" +
        "</label>\n" +
        "</div>\n" +
        "<div class=\"row\">\n" +
        "<label class=\"col-sm-2 control-label\" style=\"text-align: left;\">页面地址:\n" +
        "</label>\n" +
        "<div class=\"col-sm-5\">\n" +
        "<input type=\"text\" name=\"data["+cur+"][value][url]\" class=\"form-control\" placeholder=\"认证号才可手动输入地址\">\n" +
        "<span class=\"help-block\">必填,必须是正确的URL格式</span>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>";
    $(".inner-right").append(content_html);
}

//添加二级菜单栏右侧内容
function add_second_menu_form(first_cur,cur) {
    $(".cm-edit-after").addClass('none');
    $(".cm-edit-before").addClass('none');
    var content_html = "<div class=\"cm-edit-after\" id=\"right_form_"+first_cur+"_"+cur+"\" data-label='_"+first_cur+"_"+cur+"'>\n" +
        "<div class=\"cm-edit-right-header\">\n" +
        "<span id=\"cm-tit\"></span>\n" +
        "<a class=\"pull-right\" href=\"javascript:void(0);\" onclick='delMenu(this,2);'>删除菜单("+first_cur+"_"+cur+")</a></div>\n" +
        "<div class=\"form-horizontal wrapper-md\">\n" +
        "<div class=\"form-group\">\n" +
        "<label class=\"col-sm-2 control-label\">菜单名称:</label>\n" +
        "<div class=\"col-sm-5\">\n" +
        "<input name=\"data["+first_cur+"][child]["+cur+"][title]\" type=\"text\" oninput='changeInput(this);' class=\"form-control\" maxlength=\"5\">\n" +
        "</div>\n" +
        "<div class=\"col-sm-5 help-block\">\n" +
        "<div>字数不超过5个汉字或16个字符</div>\n" +
        "<div class=\"font_sml\" style=\"display: block;\">若无二级菜单，可输入20个汉字或60个字符</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "<div class=\"form-group\" id=\"radioGroup\">\n" +
        "<label class=\"col-sm-2 control-label\">菜单内容:</label>\n" +
        "<div class=\"col-sm-10 LebelRadio\">\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"sendmsg\" checked> 发送消息</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"link\"> 跳转网页</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"click\"> 触发关键字</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"scancode_push\"> 扫码（推事件）</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"scancode_waitmsg\"> 扫码（等待信息）</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"pic_sysphoto\"> 系统拍照发图</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"pic_photo_or_album\"> 拍照或者相册发图</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"pic_weixin\"> 微信相册发图</label>\n" +
        "<label class=\"checkbox-inline\"> <input type=\"radio\" onclick=\"changeRadio(this);\" name=\"data["+first_cur+"][child]["+cur+"][menu]\" value=\"location_select\"> 地理位置</label>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "<div class=\"cm-edit-content-con\" id=\"editMsg\">\n" +
        "<div class=\"editTab\">\n" +
        "<div class=\"editTab-heading\">\n" +
        "<ul id=\"clickUl\" class=\"msg-panel-tab\">\n" +
        "<li class=\"msg-tab_item on\" onclick=\"changeType(this,'pic')\">\n" +
        "<span class=\"msg-icon msg-icon-tuwen\"></span>\n" +
        "图文消息\n" +
        "</li>\n" +
        "<li class=\"msg-tab_item\" onclick=\"changeType(this,'text');\">\n" +
        "<span class=\"msg-icon msg-icon-word\"></span>\n" +
        "文本\n" +
        "</li>\n" +
        "</ul>\n" +
        "</div>\n" +

        "<div class=\"editTab-body\">\n" +
        "<div class=\"msg-panel-context\">\n" +
        "<div class=\"msg-context-item\" data-href='"+pic_url+"' onclick=\"openFullframe(this, '选择素材', '80%', '80%');\">\n" +
        "<div class=\"msg-panel-center msg-panel-center\" data-toggle=\"modal\" data-target=\"#selectModal\">\n" +
        "<span class=\"message-plus\">+</span><br>从素材库中选择</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +

        "</div>\n" +
        "</div>\n" +

        "<div class=\"cm-edit-content-con none\" id=\"editPage\">\n" +
        "<div class=\"cm-edit-page\">\n" +
        "<div class=\"row\">\n" +
        "<label class=\"col-sm-6 control-label\" style=\"text-align: left;\">粉丝点击该菜单会跳转到以下链接:\n" +
        "</label>\n" +
        "</div>\n" +
        "<div class=\"row\">\n" +
        "<label class=\"col-sm-2 control-label\" style=\"text-align: left;\">页面地址:\n" +
        "</label>\n" +
        "<div class=\"col-sm-5\">\n" +
        "<input type=\"text\" name=\"data["+first_cur+"][child]["+cur+"][url]\" class=\"form-control\" placeholder=\"认证号才可手动输入地址\">\n" +
        "<span class=\"help-block\">必填,必须是正确的URL格式</span>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>\n" +
        "</div>";

    $(".inner-right").append(content_html);
}

//切换菜单编辑
function changeSubbuttonActived(obj,first_cur,second_cur) {
    if (0 < second_cur) {
        select_pin = "_"+first_cur+"_"+second_cur;
        f_cur = first_cur;
        s_cur = second_cur;
    }else {
        select_pin = "_"+first_cur;
        f_cur = first_cur;
        s_cur = 0;
    }

    $(".subbutton-actived").removeClass('subbutton-actived');//移除左侧选中样式
    $(obj).addClass('subbutton-actived');//重新赋值选中
    $(".cm-edit-after").addClass('none');
    $(".cm-edit-before").addClass('none');
    $("#right_form"+select_pin).removeClass('none');
}

//菜单删除
function delMenu(obj,type) {
    // type = 1(一级菜单)/type = 2(二级菜单)
    $("#right_form"+select_pin).remove();
    if (1 == type){
        $(".view_menu"+select_pin).remove();
        var length = $(".view_footer_right .add-first-menu").length;
        if (0 == length) {
            var html = "<div class=\"view_menu add-first-menu\" id=\"view_menu_add\">\n" +
                "                                        <div class=\"text-ellipsis\" onclick=\"addFirstMenu();\"><i class=\"glyphicon glyphicon-plus text-info iBtn\"></i></div>\n" +
                "                                    </div>";
            $(".view_footer_right").append(html);
        }
    }else{
        $("#sub"+select_pin).remove();
        var length = $(".view_menu_"+f_cur+" .add-second").length;
        if (0 == length) {
            var html = "<li class='add-second'><div class=\"text-ellipsis\" onclick='addSecondMenu(this);' data-parentnumber='"+f_cur+"'><i class=\"glyphicon glyphicon-plus text-info\"></i></div></li>";
            $(".view_menu_"+f_cur+" ul").append(html);
        }
    }
    $(".cm-edit-before").removeClass('none');
}

//清空菜单
function emptyMenu() {
    var  html = "<div class=\"view_menu add-first-menu\" id=\"view_menu_add\">\n" +
        "        <div class=\"text-ellipsis\" onclick=\"addFirstMenu();\"><i class=\"glyphicon glyphicon-plus text-info iBtn\"></i></div>\n" +
        "    </div>";
    $(".view_footer_right").html(html);
    $(".cm-edit-after").remove();
}

function changeType(obj,type) {
    $("#right_form"+select_pin+" .msg-tab_item").removeClass('on');
    $(obj).addClass('on');

    if ('pic' == type){
        var html = "<div class=\"msg-context-item\" data-href='"+pic_url+"' onclick=\"openFullframe(this, '选择素材', '80%', '80%');\">\n" +
            "<div class=\"msg-panel-center msg-panel-center\" data-toggle=\"modal\" data-target=\"#selectModal\">\n" +
            "<span class=\"message-plus\">+</span><br>从素材库中选择</div>\n" +
            "</div>";
    } else{
        var html = "<div class=\"msg-context-item\" data-href='"+text_url+"' onclick=\"openFullframe(this, '选择素材', '80%', '80%');\">\n" +
            "<div class=\"msg-panel-center msg-panel-center\" data-toggle=\"modal\" data-target=\"#selectModal\">\n" +
            "<span class=\"message-plus\">+</span><br>从素材库中选择</div>\n" +
            "</div>";
    }
    $("#right_form"+select_pin+" .msg-panel-context").html(html);

}

//切换菜单内容
function changeRadio(obj) {
    var val = $(obj).val();
    if ('sendmsg' == val){
        $("#right_form"+select_pin).find("#editMsg").eq(0).show();
        $("#right_form"+select_pin).find("#editPage").eq(0).hide();
    } else {
        $("#right_form"+select_pin).find("#editMsg").eq(0).hide();
        $("#right_form"+select_pin).find("#editPage").eq(0).show();
    }
}

function selected(obj,type) {
    var id = $(obj).data('id');
    var title = $(obj).data('intro');

    var html = "<div class=\"msg-template\" >\n";
    if ('PIC' == type) {
        var litpic = $(obj).data('litpic');
        title = $(obj).data('title');
        if(0 < s_cur){
        //二级
            html += "   <input type='hidden' name=\"data["+f_cur+"][child]["+s_cur+"][material_id]\" value='"+id+"'>\n";
        }else{
        //一级
            html += "   <input type='hidden' name=\"data["+f_cur+"][value][material_id]\" value='"+id+"'>\n";
        }
        html += "   <img src='"+litpic+"' alt=\"\" >\n" +
            "   <div >"+title+"</div>\n" +
            "</div>";
    }else{
        if(0 < s_cur){
            //二级
            html += "   <input type='hidden' name=\"data["+f_cur+"][child]["+s_cur+"][material_id]\" value='"+id+"'>\n";
        }else{
            //一级
            html += "   <input type='hidden' name=\"data["+f_cur+"][value][material_id]\" value='"+id+"'>\n";
        }

        html += title +"</div>";
    }

    $("#right_form"+select_pin+" .msg-panel-context").html(html);

    layer.closeAll();
}

function changeInput(obj) {
    console.log($(obj).val());
    $("#title"+select_pin).html($(obj).val());

}

// 判断输入框是否为空
function check_submit(obj){
    // if($.trim($('input[name=title]').val()) == ''){
    //     $($('.tab-base').find('.tab')[0]).trigger('click');
    //     showErrorMsg('标题不能为空！');
    //     $('input[name=title]').focus();
    //     return false;
    // }
var post_url = $(obj).data('url');
    // layer_loading('正在处理');
    // post_form
    $.ajax({
        type: 'POST',
        url: post_url,
        data: $("#post_form").serialize(),
        dataType: 'json',
        success: function(res) {
            if (res.code == 1) {
                layer.msg(res.msg, {icon: 1, time:1000}, function(){
                    location.href = "{:url('Gzh/material_list_pictext')}"
                });
            } else {
                //切换菜单编辑
                if (!res.data.second_cur){
                    var obj = "#title_"+res.data.first_cur
                } else{
                    var obj = "#sub_"+res.data.first_cur+"_"+res.data.second_cur;
                }
                changeSubbuttonActived(obj,res.data.first_cur,res.data.second_cur);

                layer.msg(res.msg, {icon: 5});
            }
        },
        error:function (e) {
            layer.alert(e.responseText);
        }
    });
}