/*
职业选择
Date：2016-09-27
Author:Mr.Luo

settings 参数说明
-----
data:填充下拉框的数据；
nameId:所选择值附加到的文本框或其它容器Id
maxCount:允许最大选择数量
------------------------------ */
(function ($) {
    $.fn.positionSelect = function (settings) {
		if (this.length < 1) {
			return;
		}
		// 默认值
        settings = $.extend({
            containerId: null,
            className: null,
            nameId: null,
            required: true,
            maxCount: 1,
            onConfirm: null
        }, settings);

        var box_obj = $(this);
        var container = $("#" + settings.containerId);
        var num = settings.containerId;
        var title = "";
        if (settings.maxCount == "1") {
            title = "<div class=\"title1\"><b>选择行业</b><span class=\"tip\"></span> &nbsp&nbsp;<span class='tip' style='font-weight:bold;' id='tip_" + num + "'></span><a style=\"width:80px;height:30px;line-height:30px;\"></a></div>";
        }
        else {
            title = "<div class=\"title1\"><b>选择经营范围</b><span class=\"tip\"></span> &nbsp&nbsp;<span class='tip' style='font-weight:bold;' id='tip_" + num + "'></span><a style=\"width:80px;height:30px;line-height:30px;\"></a></div>";
        }
        var selectedResult = "<div class=\"sele-tag\"><dl><dt>已选择：</dt><dd id='ddResult_" + num + "'><a id='btnClose_" + num + "' href='javascript:void(0)' class='btn'>关闭</a><a id='btnSure_" + num + "' href='javascript:void(0)' class='btn'>确定</a></dd></dl></div>";
		//
        function setValue(value) {
            var type = $("#" + settings.nameId);
		//alert(settings.nameId);
            if (type.attr("type") == "text") {
                type.val(value);
            }
            else {
		type.html("");
		//alert("aaaa");
                type.html(value);
            }
            
            if (settings.maxCount == "1") {
                if ($("#qhzh").val() == "0") {
                    $("#hymcdiv").show();
                    $("#hymc").val("");
                    $("#zzxxdiv").show();
                    $("#zzxx").val("");
                }
                //GetXx(encodeURI(value));
                //2017-11-13新加功能，根据小项获取行业名称
                //$.ajax({
                //    url: "GetHymc?pid=" + encodeURI(value),
                //    type: "get",
                //    dataType: 'json',
                //    data: "{}",
                //    success: function (data) {
                //        if (data != "" && data != null) {
                //            $("#hymcdiv").show();
                //            $("#hymc").val("");
                //            $("#hymclist").empty();
                //            //$("#hymc").append("<option value=\"\">请选择</option>");
                //            var html = "";
                //            $.each(data.Table, function (i, item) {
                //                //$("#hymclist").append("<option value=\"" + data.Table[i].DataDictionaryDetailId + "\">" + data.Table[i].FullName + "</option>");
                //                //$("#hymclist").append("<li><a href=\"javascript:GetName('"+data.Table[i].FullName+"')\">" + data.Table[i].FullName + "</a></li>")
                //                html += "<li><a href=\"javascript:GetName('" + data.Table[i].FullName + "')\">" + data.Table[i].FullName + "</a></li>";
                //            })
                //            //alert("aaaa");
                //            $("#hymclist").html(html);
                //            GetXx(encodeURI(value));
                //            //$("#zzxxdiv").show();
                //        }
                //    },
                //    error: function (XMLHttpRequest, textStatus, errorThrown) {
                //        alert("数据加载失败")
                //    }
                //});
            }
            //$("#zzxxdiv").show();
        }

        function init() {
            if ($("body #" + num + "_bg").length <= 0) {
                $("body").append("<div id='" + num + "_bg' class='mask-Bg'></div>");
            }
            if (settings.className != null) {
                container.addClass(settings.className);
            }
            var data = eval("(" + positiondata + ")");
            if ($.trim(container.html()) == "") {
                container.append(title);
                container.append(selectedResult);
                container.append("<div class=\"position-menu\" id='" + num + "_datalist'></div>");
                var datalist = $("#" + num + "_datalist");
                datalist.append("<div class='menu' id='firstMenu'></div>");
                var firstMenu = datalist.find("#firstMenu");
                firstMenu.append("<ul></ul>");
                $.each(data, function (i, obj) {   // 循环第一级
                    $(firstMenu).find("ul").append("<li id='dl_" + i + "' name='" + i + "'>" + obj.fullName + "</li>");
                });

                if (datalist.find("div[class='sub-menu']").length <= 0) {
                    datalist.append("<div class='sub-menu' id='secondMenu'></div>");
                }
                var secondMenu = datalist.find("#secondMenu");

                // 第一级菜单鼠标悬浮事件，弹出二级菜单和三级菜单项
                $("#" + num + "_datalist #firstMenu ul li").bind("click", function () {
                    secondMenu.html("");
                    $("#" + num + "_datalist #firstMenu ul li").removeClass("sele");
                    $(this).addClass("sele");
                    var index = $(this).attr("name");
                    if(data[index].children[0].children!=undefined) {
                        $.each(data[index].children, function (j, item) {
                            secondMenu.append("<dl id='dl_" + j + "'></dl>");
                            var dtItem = "<dt id='dt_" + j + "'>" + item.fullName + "</dt>";
                            secondMenu.find("dl[id='dl_" + j + "']").append(dtItem);
                            secondMenu.find("dl[id='dl_" + j + "']").append("<dd id='dd_" + j + "'></dd>");
                            $.each(data[index].children[j].children, function (m, dist) {
                                var threeMenu = "<a href='javascript:void(0)' id='item_" + index + "_" + j + "_" + m + "'>" + dist.fullName + "</a>";
                                secondMenu.find("dl[id='dl_" + j + "'] dd[id='dd_" + j + "']").append(threeMenu);
                            });

                        });
                    }else {
                        secondMenu.append("<dl id='dl_" + 0 + "'></dl>");
                        var dtItem = "<dt id='dt_" + 0 + "'>经营范围</dt>";
                        secondMenu.find("dl[id='dl_" + 0 + "']").append(dtItem);
                        secondMenu.find("dl[id='dl_" + 0 + "']").append("<dd id='dd_" + 0 + "'></dd>");
                        $.each(data[index].children, function (m, dist) {
                            var threeMenu = "<a href='javascript:void(0)' id='item_" + index + "_" + 0 + "_" + m + "'>" + dist + "</a>";
                            secondMenu.find("dl[id='dl_" + 0 + "'] dd[id='dd_" + 0 + "']").append(threeMenu);
                        });
                    }
                    
                    //根据已选择的项，将相同的列表展示项添加样式
                    var resultItems = container.find("#ddResult_" + num + " a");
                    $.each(resultItems, function (n, ritem) {
                        var rid = $(ritem).attr("id").substr(2, $(ritem).attr("id").length);
                        secondMenu.find("a[id='" + rid + "']").addClass("sele");
                    });
                    
                    // 列表项点击事件，选中则在结果容器中显示添加选中样式
                    secondMenu.find("a").bind("click", function () {
                        var selectedCount = container.find("#ddResult_" + num + " a").length-1;
                        //alert(selectedCount);
                        if ($(this).hasClass("sele")) {
                            $(this).removeClass("sele");
                            container.find("#ddResult_" + num).find("a[id='c_" + $(this).attr("id") + "']").remove();
                        }
                        else {
                            if (selectedCount >= settings.maxCount) {
                                //获取已选择的类别，删除，并把容器中的选中样式也删除
                                var rrid = $("#ddResult_" + num).children().eq(0).attr("id");
                                //alert(rrid);
                                if (rrid != "btnClose_" + num) {
                                    $("#" + rrid).remove();
                                    var ssid = rrid.substr(2, rrid.length);
                                    secondMenu.find("a[id='" + ssid + "']").removeClass("sele");
                                }
                                //
                                $(this).addClass("sele");
                                container.find("#ddResult_" + num + " #btnClose_" + num).before("<a href='#' id='c_" + $(this).attr("id") + "'>" + $.trim($(this).html()) + "</a>");
                            }
                            else {
                                $(this).addClass("sele");
                                container.find("#ddResult_" + num + " #btnClose_" + num).before("<a href='#' id='c_" + $(this).attr("id") + "'>" + $.trim($(this).html()) + "</a>");
                            }
                        }
                        // 结构容器中选中项点击事件，移除并将列表中的选中样式取消
                        container.find("#ddResult_" + num + " a").bind("click", function () {
                            var rid = $(this).attr("id");
                            if (rid != "btnSure_" + num) {
                                var sid = rid.substr(2, rid.length);
                                $(this).remove();
                                secondMenu.find("a[id='" + sid + "']").removeClass("sele");
                            }
                        });
                    });

                });

                // 默认显示第一级
                $(firstMenu).find("ul li:first").addClass("sele").trigger("click");

                // 关闭选择框
                container.find("#btnClose_"+num).bind("click", function () {
                    container.hide();
                    $("#" + num + "_bg").hide();
                });

                // 确定
                container.find("#ddResult_" + num + " a[id='btnSure_" + num + "']").bind("click", function () {
                    var selectedItems = container.find("#ddResult_" + num + " a[id!='btnSure_" + num + "']");
                    var results = "";
                    $.each(selectedItems, function (i, item) {
                        if ($.trim($(item).html()) != "关闭") {
                            results += $.trim($(item).html()) + ",";
                        }
                    });
                    if (results.length > 0) {
                        results = results.substr(0, results.length - 1);
                    }
                    //alert($("li.sele").html().length);
                    if (results != "") {
                        if (settings.maxCount == "1") {
                            results = $("li.sele").html() + "-" + results;//获取大项、拼接字符串,返回选择
                        }
			else {
                            if ($("#jyfw").val() != "") {
                                results = $("#jyfw").val() +","+ results + ",";
                            }
                            else {
                                results += ",";
                            }
                        }
                    }
                    if (results != "") {
                        setValue(results);
                        container.hide();
                    }
                    else {
                        if (num == "positionDiv") {
                            jqueryAlert({
                                'icon': '../../Content/images/warning.png',
                                'content': '请选择行业子项',
                                'closeTime': 4000,
                            })

                        }
                        else {
                            container.hide();
                        }
                    }
                    $("#" + num + "_bg").hide();
                });
            }
            container.show();
            $("#" + num + "_bg").show();
        }
        init();
    };
})(jQuery);