/*!
* 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':'Advanced search','click':function(){Oselect('table1','" + Mywindows + "');}},");
        jsonTxt.append("{'text':'Graphic report','click':function(){TableCharts();}},");
        jsonTxt.append("{'text':'Export Excel','click':function(){TableExport('table1');}},");
        if (globalTagsStr == "" || globalTagsStr) {
            jsonTxt.append("{'text':'Tag','click':function(){return false;},children:[{'id':'closeTags','text':'Cancel the Tag','click':function(){globalDeleteTages(this); }}" + globalTagsStr + ",{'text':'New Tag','click':function(){globalAddTages(this);}}]}]");
            objTxt = jsonTxt.toString();
        } else {
            objTxt = jsonTxt.toString().substring(0, jsonTxt.toString().length - 1) + "]";
        }
        //alert(jsonTxt);
        $("#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: '620px',
        padding: '0',
        title: 'The generic query',
        content: '<table border="0" cellpadding="0" cellspacing="0" class="frm" id="shaixuansql" width="100%"><thead><tr><td align="center">The query content</td><td align="center" >Conditions</td><td align="center" >Value</td><td align="center">nexus</td><td align="center">Operation</td></tr></thead><tbody></tbody></table>',
        button: [
                            {
                                name: 'Determine the query',
                                callback: function () {
                                    GoSelect(obj, windowname);
                                    return false;
                                },
                                focus: true
                            },
                            {
                                name: 'Cancel',
                                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'>Not object to the query to find</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'>Contain</option><option value='='>Be equal to</option><option value='>'>Greater than</option><option value='<'>Less than</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'>And</option><option value='OR'>Or</option></select></td>";

        titlename += "<td><a href='#' onclick=\"DelTitle('" + id + "')\">Delete</a> | <a href='#' onclick=\"GetTitleUpdownList('" + obj + "')\">ADD</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">You have not selected need to query conditions!</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' />Is the query please wait</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: 'Export Excel',
        content: '1, export all: is derived page query all data before screening, including multiple paging. <br />2, the current is derived: display current page content. <br / > note: Please note that derived in all cases, most can only export 1000 rows of data!',
        button: [
                            {
                                name: 'Export all',
                                callback: function () {
                                    this.content("Are derived, please try later...");
                                    GoTableExport("ALL", "table1");
                                }
                            },

                            {
                                name: 'The current',
                                callback: function () {
                                    this.content("Are derived, please try later...");
                                    GoTableExport("ONE", "table1");
                                },
                                focus: true
                            }
                            ,
                            {
                                name: 'Cancel',
                                callback: function () {
                                    art.dialog.close();
                                }
                            }
                        ]
    });
}
//提交处理
function GoTableExport(ExportType, tableid) {
    window.top.$("#loading").show();
    var ExportHTML = "";
    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;
}