/// <reference path="jquery-1.8.2.min.js" />
/// <reference path="tools/tools.js" />
/// <reference path="jquery-ui-1.8.24.min.js" />

$(function () {
    $("[mycontrol='1'][controltype='1']").click(function () {
        ss(undefined, undefined, true);
    });

    $("[mycontrol=1][controltype='5']").each(function () {
        var val = QueryString(this.id);
        var el = $(this);
        //if (el.is("select") && el.attr("istext") == "1") {
        //    //el.find("option[text='" + val + "']").attr("selected", true);
        //    //$.each(el.find("option"), function () {
        //    //    if (val == $(this).text()) {
        //    //        $(this).attr("selected", true);
        //    //        return false;
        //    //    }
        //    //});
        //}
        //else {
        //$(this).val(val);
        //}

        //lm
        if (el.is("li")) {
            var name = el.attr("name");
            var aval = ',' + el.attr("avalue") + ',';
            var qval = ',' + QueryString(name) + ',';
            //var vals = qval.indexOf(aval);
            if (qval.indexOf(aval) > -1) {
                el.addClass("current");
            }
        } else {
            $(this).val(val);
        }
    });



    $("th.sort").click(function () {
        var span = $(this).find("span");
        var field = $(this).attr("field");
        var sort = "asc";
        if (span.hasClass("asc")) {
            span.removeClass("asc").addClass("desc");
            sort = "desc";
        } else {
            span.removeClass("desc").addClass("asc");
            sort = "asc";
        }
        //pm.Name ASC DESC

        ss(undefined, undefined, true, field + " " + sort);
    });

    var sort = QueryString("sort");
    if (sort != undefined && sort != "") {
        var sortstr = sort.split(" ");
        if (sortstr.length === 2) {
            var sortfield = sortstr[0];
            var sortType = sortstr[1];

            var th = $("th.sort[field='" + sortfield + "']");

            th.find("span").addClass(sortType);
        }
    }
});


function MyDictionary() {
    this.Keys = [];
    this.Values = [];

    this.getelements = function (key) {
        var array = [];
        for (var i = 0, len = this.Values.length; i < len; i++) {
            if (this.Values[i].key === key) {
                array.push(this.Values[i].value);
            }
        }

        return array;
    }

    this.Add = function (key, value) {
        var tflag = this.ContainsKey(key);
        if (!tflag) {
            this.Keys.push(key);
        }

        this.Values.push({ key: key, value: value });
    };
    this.ContainsKey = function (key) {
        var tflag = false;
        for (var i = 0, len = this.Keys.length; i < len; i++) {
            if (this.Keys[i] === key) {
                tflag = true;
                break;
            }
        }

        return tflag;
    }
}

function ss(bol, url, isBtnSearch, sort) {
    var pars = [];

    var dic = new MyDictionary();

    var isValid = true;

    $("[mycontrol=1][controltype='5']").each(function () {
        var el = $(this);

        var id = el.attr("id");
        var val = el.val();
        var rule = el.attr("rule");
        var reqx = el.attr("myRequired");

        if (isBtnSearch && val == "" && reqx === "1") {
            isValid = false;
            //暂时这么写 
            alert(el.parent().parent()[0].innerText + "不能为空！")
            return false;
        }

        //if (el.is("select") && el.attr("istext") == "1") {
        //    val = el.find("option:selected").text();
        //}
        if (el.is("select") && val != "-1") {
            var par = id + "=" + val;
            pars.push(par);
        }
        else if (!el.is("select")) {
            if (rule === "6" && el.is(':checked')) {
                dic.Add(el.attr("name"), el.attr("avalue"));
            }
            //else if (rule === "11") {
            //    var tt = el.find("span");
            //    if (tt.html() === "√") {
            //        dic.Add(el.attr("name"), el.attr("avalue"));
            //    }
            //}
            else if (el.is("li") && el.hasClass("current")) {
                dic.Add(el.attr("name"), el.attr("avalue"));
            }
            else if (id != null && id != "") {
                var par = id + "=" + val;
                pars.push(par);
            }
        }
    });

    if (!isValid) {
        return;
    }

    for (var i = 0, len = dic.Keys.length; i < len; i++) {
        var values = dic.getelements(dic.Keys[i]);
        pars.push(dic.Keys[i] + "=" + values);
    }

    if (isBtnSearch) {
        pars.push("isBtnSearch=true");
    }

    var str = "?";
    var count = pars.length;
    $(pars).each(function (i) {
        str += this + (i == (count - 1) ? "" : "&");
    });

    var page = 0;//QueryString("page");
    if (page > 0)
        str += "&page=" + page;

    if (sort != undefined)
        str += "&sort=" + sort;

    //alert(str);
    if (bol) {
        window.location.href = url + decodeURIComponent(str);
    } else {
        window.location.href = window.location.pathname + decodeURIComponent(str);
    }
}
//lm
//单个查询条件调用的
function ss2(ele, isMultSelect) {
    var pars = [];
    var el = $(ele);
    var parent = el.parent();

    if (isMultSelect === true) {
        if (el.hasClass("current"))
            el.removeClass("current");
        else
            el.addClass("current");
    }
    else {
        $("li.current", parent).removeClass("current");
        el.addClass("current");
    ss(undefined, undefined, true);
    }
}
function del(el) {
    if (confirm("是否继续删除？")) {
        var myid = $(el).attr("myid");
        var deltype = $(el).attr("deltype");
        $.post("/Public/del", { id: myid, type: deltype }, function (data) {
            if (data.state == 1) {
                window.location.reload();
            } else {
                alert(data.mess);
                window.location.reload();
            }
        }, "json");
    }
}

function ProjectState(el) {
    var myid = $(el).attr("myid");
    $.post("/public/ProjectStateUpdate", { id: myid }, function (data) {
        if (data.state == 1) {
            window.location.reload();
        } else {
            alert(data.mess);
            window.location.reload();
        }
    }, "json");
}

function Import(url) {
    ss(true, url);
}