var srvID = "";
var userID = "";
var mPage = 1;
var firstLoad = 0;
var aryau = "";
var ifrm;
var username = "";
jQuery(function ($) {
    $('[data-rel=tooltip]').tooltip();
    $('[data-rel=popover]').popover({ html: true });
});
function resize_r() {
    var s = 0;
    var ss = $(window.parent.document).find('iframe[id=' + ifrm + ']');
    var l = $(window.parent.document).height() - ss.offset().top;
    var t = $(document).height();
    if (l > t) {
        s = l;
    }
    else {
        s = $(document).height();
    }
    $($(window.parent.document).find('iframe[id=' + ifrm + ']')).height(s);
    window.parent.sidebar_r();
}
function ClickEvent() {
    $('[data-rel=tooltip]').tooltip();
    $('[data-rel=popover]').popover({ html: true });
}
function existRole(s, p) {
    var e = p.indexOf(s);
    if (e == -1) {
        return false;
    }
    else {
        return true;
    }
}
function SelRole() {
    $('[tag]').each(function (i, e) {
        var tag = $(e).attr('tag');
        if (existRole(tag, aryau)) {
            $(e).show();
        }
        else {
            $(e).hide();
        }
    });
}
$(function () {
    $("#PSize").change(function () {
        firstLoad = 0;
        mPage = 1;
        loadP($("#PSize").val(), 1);
    });
    $("[tag]").hide();
});
function createPage(pageSize, buttons, total) {
    $("#pagination").jBootstrapPage({
        pageSize: pageSize,
        total: total,
        maxPageButton: buttons,
        onPageClicked: function (obj, pageIndex) {
            mPage = pageIndex + 1;
            loadP($("#PSize").val(), mPage);
        }
    });
}
function LoadData() {
    var coks = document.cookie.substring(document.cookie.indexOf('userinf_tcwl'));
    var coksau = document.cookie.substring(document.cookie.indexOf('TCWL_All'));
    var ary = coks.split(';')[0].split('=');
    aryau = coksau.split(';')[0].split('=')[2];
    if (!ary[2]) {
        $('html').empty();
        return;
    }
    userID = ary[2].split('&')[0];
    srvID = ary[3];
    var s = window.location.href.split('=');
    if (s.length == 4) {
        ifrm = s[3];
    }
    resize_r();
    Done(userID);
    loadP($("#PSize").val(), 1);
}
function loadP(pagesize, page) {
    var l = layer.load('正在加载数据......');
    var href = window.location.href.split("=");
    var mid = href[3];
    var url = "../Handlers/system/Mission.aspx?Method=LoadReceive&srvid=" + srvID + "&userid=" + userID + "&page=" + page + "&rows=" + pagesize + "";
    if (window.location.href.indexOf("check") != -1) {
        mid = mid.substring(1, mid.length);
        url += "&check=" + mid + "";
    }
    $.ajax({
        url: url,
        type: "post",
        cache: false,
        dataType: "json",
        success: function (data) {
            CreateTable(data);
            if (firstLoad == 0) {
                createPage(pagesize, 5, data.total);
                firstLoad = 1;
            }
            if (pagesize < data.total) {
                if (mPage * pagesize >= data.total) {
                    $("#pFooter").html("当前显示：" + ((mPage - 1) * pagesize + 1).toString() + "---" + data.total + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
                }
                else {
                    $("#pFooter").html("当前显示：" + ((mPage - 1) * pagesize + 1).toString() + "---" + mPage * pagesize + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
                }
            }
            else {
                $("#pFooter").html("当前显示：" + ((mPage - 1) * pagesize + 1).toString() + "---" + data.total + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
            }
            layer.close(l);
            resize_r();
        },
        error: function () {
            layer.msg('网络请求错误!', 2, 5);
        }
    });
}
function CreateTable(data) {
    var tb = document.getElementById("Management");
    for (var i = tb.rows.length - 1; i > 0; i--) {
        tb.deleteRow(i);
    }
    for (var i = 0; i < data.rows.length; i++) {
        var _tr1 = $('<tr></tr>');
        var _td1 = $('<td></td>');
        _td1.append(data.rows[i].sendername);
        var _td2 = $('<td></td>');
        _td2.append('<a href="javascript:void(0)" onclick="Check(this)" title="' + data.rows[i].ContentStr + '" >' + data.rows[i].ContentStr.substring(0, 60) + '</span>');
        var _td3 = $('<td class="center"></td>');
        _td3.append(data.rows[i].State);
        var _td4 = $('<td class="center"></td>');
        _td4.text(data.rows[i].TimeStr);
        var _td7 = $('<td class="center"></td>');
        _td7.text(data.rows[i].EndTime);
        var _td6 = $('<td style="display: none;"></td>');
        _td6.text(data.rows[i].ID);
        _tr1.append(_td1);
        _tr1.append(_td2);
        _tr1.append(_td3);
        _tr1.append(_td4);
        _tr1.append(_td7);
        _tr1.append(_td6);
        $('#Management tbody').append(_tr1);
    }
    ClickEvent();
    SelRole();
}
function Done(id) {
    $.ajax({
        type: "get",
        url: "../Handlers/system/Mission.aspx?Method=GetName&ID=" + userID,
        cache: false,
        success: function (data) {
            $("#hiddenname").val(data);
        },
        error: function () {
            layer.msg("网络错误！", 2, 3);
        }
    });
}
function Add() {
    $('#fmGonggao').validation();
    var alm = $(".popover");
    if (alm.length > 0) {
        return;
    }
    $(".popover ").remove();
    $.ajax({
        url: "../Handlers/system/Mission.aspx?Method=done&user=" + userID,
        type: "post",
        cache: false,
        data: encodeJson_News(decodeURIComponent($('#fmGonggao').serialize())),
        success: function (data) {
            if (data == "success") {
                layer.msg("操作成功！", 1, 1);
                $("#m_check").modal("hide");
                loadP($("#PSize").val(), 1);
            }
            else {
                layer.msg("操作失败！", 2, 3);
            }
        },
        error: function () {
            layer.msg('网络请求错误!', 2, 5);
        }
    });
}
function Btn_check(pagesize, page) {
    var l = layer.load('正在加载数据......');
    var pagesize = $("#PSize").val();
    var page = 1
    $.ajax({
        url: "../Handlers/system/Mission.aspx?Method=LoadReceive&srvid=" + srvID + "&userid=" + userID + "&page=" + page + "&rows=" + pagesize + "&where=where",
        type: "post",
        cache: false,
        data: $('#checkform').serialize(),
        dataType: "json",
        success: function (data) {
            CreateTable(data);
            if (firstLoad == 0) {
                createPage(pagesize, 5, data.total);
                firstLoad = 1;
            }
            if (pagesize < data.total) {
                if (mPage * pagesize >= data.total) {
                    $("#pFooter").html("当前显示：" + ((mPage - 1) * pagesize + 1).toString() + "---" + data.total + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
                }
                else {
                    $("#pFooter").html("当前显示：" + ((mPage - 1) * pagesize + 1).toString() + "---" + mPage * pagesize + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
                }
            }
            else {
                $("#pFooter").html("当前显示：" + ((mPage - 1) * pagesize + 1).toString() + "---" + data.total + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
            }
            layer.close(l);
            resize_r();
        },
        error: function () {
            layer.msg('网络请求错误!', 2, 5);
        }
    });
}
function ShuaXin() {
    loadP($("#PSize").val(), 1);
}
function Check(_this) {
    var tds = $(_this).parent().parent().find("td");
    var id = $(tds).eq(5).text();
    var receivenames = $(tds).eq(0).text();
    var contentstr = $(tds).eq(1).children(0).attr("title");
    var state = $(tds).eq(2).text();
    var mtime = $(tds).eq(3).text();
    var endtime = $(tds).eq(4).text();
    var username = $("#hiddenname").val();
    $("#hidname").val(username);
    $.ajax({
        type: "get",
        url: "../Handlers/system/Mission.aspx?Method=receive&ID=" + id + "&userid=" + userID + "&name=" + username + "",
        cache: false,
        success: function (data) {
            if (data == "success") {
                loadP($("#PSize").val(), 1);
                var now = new Date();
                endtime = endtime + " 23:59:59";
                var etime = new Date(endtime.replace(/-/, "/"));
                if (now > etime) {
                    $("#btnres").hide();
                }
                else {
                    if (state == "已处理") {
                        $("#btnres").hide();
                    } else {
                        $("#btnres").show();
                    }
                }
                $("#hidid").val(id);
                $("#lbreceive").text(receivenames);
                $("#lbcontent").text(contentstr);
                $("#lbtime").text(mtime);
                $("#lbendtime").text(endtime);
                $("#recon").val("");
                $("#m_check").modal("show");
            }
        },
        error: function () {
            layer.msg("网络错误！", 2, 3);
        }
    });
}