/*
* CreateContextJs.js
* CreateContextJs - v1.0.0 (2013年11月29日 16:20:43)
* Copyright : NewStart:TKS | Author:jl
* According to the automatic generation of the right click menu TKS navigation  
*/
$(function () {
    /*!
    *  根据导航自动转换生成右键菜单，只针对复选框列表生效
    *  如需引用，请放置页面最底，在页面事件全部已经被浏览器读取完成后才可绑定
    *  需引用jQuery
    */
    function StringBuffer(initStr) {
        /// <summary>
        ///     1: 本对象用于高效率拼接字符串，提供append(String)方法来多次拼接字符串，提供toString()方法来返回完整的字符串，
        ///        【注：该对象值为undefined，请不要尝试访问该对象】
        ///     &#10;    1.1 - StringBuffer()
        ///     &#10;2: 根据提供的原始字符串创建字符串拼接对象
        ///     &#10;    2.1 - StringBuffer(initStr)
        /// </summary>
        /// <param name="initStr" type="String">
        ///     用来拼接的字符串
        /// </param>
        /// <returns type="undefined" />
        this._strings = new Array();
        if (initStr) {
            this._strings[0] = initStr;
        }
        return this;
    }
    StringBuffer.prototype.append = function (str) {
        this._strings.push(str);
    }
    StringBuffer.prototype.toString = function () {
        return this._strings.join('');
    }


    if ($(".btnbarcontetn").length > 0) {
        var jsonTxt = new StringBuffer("[");
        $(".btnbarcontetn a").each(function () {
            if ($(this).attr("title") != undefined) {
                jsonTxt.append("{'text':'" + $(this).attr("title") + "',");
                jsonTxt.append("'click':function (){" + $(this).attr("onclick") + "}},");
            }
        });
        //需要操作标签
        var objTxt;
        //alert(Mywindows);
        jsonTxt.append("{'text':'-----------------','click':function(){return false;}},");
        jsonTxt.append("{'text':'" + lggaojisoso + "','click':function(){Oselect('table1','" + Mywindows + "');}},");
        jsonTxt.append("{'text':'" + lgtubiao + "','click':function(){TableCharts();}},");
        jsonTxt.append("{'text':'" + lgdaochuexl + "','click':function(){TableExport('table1');}},");
        if (globalTagsStr == "" || globalTagsStr) {
            jsonTxt.append("{'text':'" + lgtag + "','click':function(){return false;},children:[{'id':'closeTags','text':'" + lgtagquxiao + "','click':function(){globalDeleteTages(this); }}" + globalTagsStr + ",{'text':'" + lgtagnew + "','click':function(){globalAddTages(this);}}]}]");
            objTxt = jsonTxt.toString();
        } else {
            objTxt = jsonTxt.toString().substring(0, jsonTxt.toString().length - 1) + "]";
        }
//        alert(objTxt);
        $("#table1").lightMenu("ctId", {
            items: eval('(' + objTxt + ')'),
            onShowMenu: function (eventSource) {
                //$(eventSource).find('td').removeClass('selected').find('input[type="checkbox"]').removeAttr('checked');
                if ($(eventSource).find(".tagsContainer>span").length < 1) {//没有标签
                    //隐藏取消标签按钮
                }
            },
            isCache: (globalTagsStr == "" || globalTagsStr) ? false : true
        });
    }
});
var selectcount = 1; //行号
//调用通用查询
function Oselect(obj, windowname) {
    art.dialog({
        id: 'osel',
        width: '480px',
        padding: '0',
        title: lgtongyongcha,
        content: "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"frm\" id=\"shaixuansql\" width=\"100%\"><thead><tr><td align=\"center\">"+lgso1+"</td><td align=\"center\" >"+lgso2+"</td><td align=\"center\" >"+lgso3+"</td><td align=\"center\">"+lgso4+"</td><td align=\"center\">"+lgso5+"</td></tr></thead><tbody></tbody></table>",
        button: [
                            {
                                name: lgqueren,
                                callback: function () {
                                    GoSelect(obj, windowname);
                                    return false;
                                },
                                focus: true
                            },
                            {
                                name: lgqx,
                                callback: function () {
                                    art.dialog.close();
                                    //return false;
                                }

                            }
                        ]
    });
    GetTitleUpdownList(obj);
}
//插入行
function GetTitleUpdownList(obj) {
    if ($("#" + obj).find("thead th").length == 0) {
        titlename += "<tr><th colspan='5' align='center'>" + lgsowizhaodao + "</th></tr>";
        // return false;
    }
    else {
        id = selectcount;
        var titlename = "<tr id='hang" + id + "'><td><select name=\"title" + id + "\"  id=\"title" + id + "\">";
        $("#" + obj).find("thead th").each(function () {
            // alert();
            if ($(this).text() != "" && $(this).index() != 0) {

                titlename += "<option value='" + $(this).text() + "'>" + $(this).text() + "</option>";
            }
        });

        titlename += "</select></td>";

        titlename += "<td><select name=\"tiaojian" + id + "\"  id=\"tiaojian" + id + "\"><option value='like'>" + lgso6 + "</option><option value='='>" + lgso7 + "</option><option value='>'>" + lgso8 + "</option><option value='<'>" + lgso9 + "</option></select></td>";
        titlename += "<td><input id=\"value" + id + "\" name=\"value" + id + "\" type=\"text\" value=\"\" /></td>";

        titlename += "<td><select name=\"guanxi" + id + "\"  id=\"guanxi" + id + "\"><option value='AND'>" + lgso10 + "</option><option value='OR'>" + lgso11 + "</option></select></td>";

        titlename += "<td><a href='#' onclick=\"DelTitle('" + id + "')\">" + lgdels + "</a> | <a href='#' onclick=\"GetTitleUpdownList('" + obj + "')\">" + lgadd + "</a> </td>";
        titlename += "</tr>";
    }
    //alert(titlename);
    $("#shaixuansql").find("tbody").append(titlename);
    selectcount++;
}
//删除行
function DelTitle(id) {
    $("#hang" + id).remove();
}
//确认查询
function GoSelect(obj, windowname) {
    if ($("#shaixuansql").find("tbody tr").length == 0) {
        //showTipsMsg("您没有选择需要查询的条件！", 200, 5);
        art.dialog({
            time: 1,
            content: '<font color="red">"+lgso12+"</font>'
        });
    }
    else {
        var sqlwhere = "";
        $("#shaixuansql").find("tbody tr").each(function () {//得到行
            //$(this).find("td").eq(0); //列名    //$(this).find("td").eq(1); //条件    //$(this).find("td").eq(2); //值 //$(this).find("td").eq(3); //关系
            if ($(this).find("td").eq(1).find("select").val() == "like") {
                sqlwhere += " " + $(this).find("td").eq(3).find("select").val() + " " + $(this).find("td").eq(0).find("select").val() + " " + $(this).find("td").eq(1).find("select").val() + " '%" + $(this).find("td").eq(2).find("input").val() + "%' ";
            } else {
                sqlwhere += " " + $(this).find("td").eq(3).find("select").val() + " " + $(this).find("td").eq(0).find("select").val() + " " + $(this).find("td").eq(1).find("select").val() + " '" + $(this).find("td").eq(2).find("input").val() + "' ";
            }
        });
        //alert($.trim(sqlwhere));
        window.top.$("#loading").show();
        $("#" + obj).find("tbody").html("<center><img src='/Themes/Images/loading1.gif' />" + lgsoloding + "</center>");
        var list = art.dialog.list;
        for (var i in list) {
            list[i].close();
        };
        $.post("/RMBase/SysWindows/WindowsSearch.ashx",
                { windowsname: windowname, suggest: sqlwhere },
                function (result) {
                    $("#tabletop").remove();
                    $(".pagination").remove();
                    //alert(result);
                    $("#" + obj).html(result);
                    window.top.$("#loading").hide();
                    //  divresize(90);
                    //  FixedTableHeader("#table1", $(window).height() - 121);
                });
    }
}
//表格导出
function TableExport(tableid) {
    art.dialog({
        id: 'osExport',
        width: '400px',
        title: lgdaochuexl,
        content: lgdaochubeizhu,
        button: [
                            {
                                name: lgdaochuquan,
                                callback: function () {
                                    this.content(lgdaolaoding);
                                    GoTableExport("ALL", "table1");
                                }
                            },

                            {
                                name: lgdaochudangqian,
                                callback: function () {
                                    this.content(lgdaolaoding);
                                    GoTableExport("ONE", "table1");
                                },
                                focus: true
                            }
                            ,
                            {
                                name: lgqx,
                                callback: function () {
                                    art.dialog.close();
                                }
                            }
                        ]
    });
}
//提交处理
function GoTableExport(ExportType, tableid) {
    window.top.$("#loading").show();
    var ExportHTML = "";
//    $("#" + tableid).find("td").each(function () {
//        $(this).prepend("·");
//    });
    if (ExportType == "ONE") {
        ExportHTML = "<table border=1>" + $("#" + tableid).html() + "</table>";
        // alert(ExportHTML);
    }
    post('/RMBase/SysWindows/WindowsExport.ashx', { ExportType: ExportType, ExportHTML: ExportHTML, TableName: Mywindows });
    var list = art.dialog.list;
    for (var i in list) {
        list[i].close();
    };
}
//提交数据，下载导出文件
function post(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";
    for (var x in PARAMS) {
        var opt = document.createElement("textarea");
        opt.name = x;
        opt.value = PARAMS[x];
        //alert(opt.name);      
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}