var srvID = "";
var userID = "";
var mPage = 1;
var firstLoad = 0;
var cPage = 1;
var cLoad = 0;
var aryau = "";
var ifrm;
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 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 ClickEvent() {
    $('[data-rel=tooltip]').tooltip();
    $('[data-rel=popover]').popover({ html: true });
}
$(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('=');
    if (!ary[2]) {
        $('html').empty();
        return;
    }
    aryau = coksau.split(';')[0].split('=')[2];
    userID = ary[2].split('&')[0];
    srvID = ary[3];
    var s = window.location.href.split('=');
    if (s.length == 4) {
        ifrm = s[3];
    }
    resize_r();
    loadP($("#PSize").val(), 1);
    $('#CheckModal').on('shown.bs.modal', function () {
        $('#CheckModal .modal-body').css({ 'max-height': $(window.parent.document).find('iframe[id=' + ifrm + ']').height() - 100, 'overflow': 'auto' });
    });
    var myDate = new Date();
    var m = myDate.getMonth() + 1;
    if (m < 10) {
        m = "0" + m;
    }
    var d = myDate.getDate();
    if (d < 10) {
        d = "0" + d;
    }
    var strattime = myDate.getFullYear() + "-" + m + "-01";
    var stime = myDate.getFullYear() + "-" + m + "-" + d;
    $("#TStart").val(strattime);
    $("#TEnd").val(stime);
}
function loadP(pagesize, page) {
    var l = layer.load('正在加载数据......');
    $(".ace:checkbox").attr("checked", false);
    $.ajax({
        url: "../Handlers/system/Daily.aspx?Method=LoadMyCopy&srvid=" + srvID + "&userid=" + userID + "&page=" + page + "&rows=" + pagesize + "",
        type: "get",
        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("<a style='color:#000;cursor:pointer;' onclick='DailyCheck(this)'>" + data.rows[i].Adder + "</a>");
        var _td7 = $('<td class="center"></td>');
        if (data.rows[i].Type == "0") {
            _td7.append("<a style='color:#000;cursor:pointer;' onclick='DailyCheck(this)'>日报</a>");
        }
        else if (data.rows[i].Type == "1") {
            _td7.append("<a style='color:#000;cursor:pointer;' onclick='DailyCheck(this)'>周报</a>");
        }
        else if (data.rows[i].Type == "2") {
            _td7.append("<a style='color:#000;cursor:pointer;' onclick='DailyCheck(this)'>月报</a>");
        }
        else if (data.rows[i].Type == "3") {
            _td7.append("<a style='color:#000;cursor:pointer;' onclick='DailyCheck(this)'>例会</a>");
        }
        if (data.rows[i].Annex != "") {
            _td7.append('<i class="ace-icon fa fa-paperclip bigger-110"></i>');
        }
        var _td2 = $('<td class="center"></td>');
        if (data.rows[i].Responder == "已批注") {
            _td2.append("<label class='label label-green' onclick='DailyCheck(this)' style='cursor:pointer;'>已批注</label>");
        } else {
            _td2.append("<label class='label label-danger' onclick='DailyCheck(this)' style='cursor:pointer;'>未批注</label>");
        }
        var _td3 = $('<td class="center"></td>');

        if (data.rows[i].WhetherSee == "未查看") {
            _td3.append("<label class='label label-danger' onclick='DailyCheck(this)' style='cursor:pointer;'>未查看</label>");
        }
        else {
            _td3.append("<label class='label label-green' onclick='DailyCheck(this)' style='cursor:pointer;'>已查看</label>");
        }
        var _td4 = $('<td class="center"></td>');
        _td4.append('<a style="color:#000;cursor:pointer;" onclick=\'DailyCheck(this)\'>' + data.rows[i].TimeStr.split(" ")[0] + '</a>');
        var _td6 = $('<td style="display: none;"></td>');
        _td6.text(data.rows[i].ID);
        var _td8 = $('<td style="display: none;"></td>');
        _td8.text(data.rows[i].Adder);
        var _td9 = $('<td style="display: none;"></td>');
        _td9.text(data.rows[i].Respond);
        var _td10 = $('<td style="display: none;"></td>');
        _td10.text(data.rows[i].Time);
        var _td11 = $('<td style="display: none;"></td>');
        _td11.text(data.rows[i].Annex);
        var _td12 = $('<td style="display: none;"></td>');
        _td12.text(data.rows[i].Attach);
        var _td13 = $('<td style="display: none;"></td>');
        _td13.text(data.rows[i].WhoRead);
        _tr1.append(_td1);
        _tr1.append(_td7);
        _tr1.append(_td2);
        _tr1.append(_td3);
        _tr1.append(_td4);
        _tr1.append(_td6);
        _tr1.append(_td8);
        _tr1.append(_td9);
        _tr1.append(_td10);
        _tr1.append(_td11);
        _tr1.append(_td12);
        _tr1.append(_td13);
        $('#Management tbody').append(_tr1);
    }
    ClickEvent();
    SelRole();
}
var isopen = false;
function DailyCheck(obj) {
    $("#contentstr").html("");
    var tds = $(obj).parent().parent().find("td");
    var ID = tds.eq(5).text();
    var userXm = tds.eq(6).text();
    var Time = tds.eq(8).text();
    var fj = tds.eq(9).text();
    var csrname = tds.eq(10).text();
    var pz = tds.eq(7).text();
    var yd = tds.eq(11).text();
    var l = layer.load("正在加载...");
    $.ajax({
        type: "get",
        url: "../Handlers/system/Daily.aspx?Method=getCon&update=yes&ID=" + ID + "&user=" + userID,
        cache: false,
        dataType: "text",
        success: function (data) {
            layer.close(l);
            if (data != null) {
                $("#contentstr").append(data);
                $("#userid").val(userID);
                $("#EditHid").val(ID);
                $("#AddName").text(userXm);
                $("#AddTime").text(Time);
                var td3 = tds.eq(3);
                $(td3).children(0).removeClass("label-danger").addClass("label-green").text("已查看");
                $('#fList1').empty();
                if (fj != "") {
                    var fjList = fj.split('╯');
                    for (var i = 0; i < fjList.length; i++) {
                        if (fjList[i].length > 0) {
                            var mc = fjList[i].substring(fjList[i].lastIndexOf('/') + 1);
                            mc = mc.substring(0, mc.lastIndexOf('.'));
                            var eachItem = $('<li style="float:left;"><img ></img><a style="float:left;"></a></li>');
                            if (fjList[i].indexOf('http://') >= 0 || fjList[i].indexOf('www.') >= 0) {
                                eachItem.find('a').attr('href', fjList[i]);
                            }
                            else {
                                eachItem.find('a').attr('href', "../" + fjList[i]);
                            }
                            //eachItem.find('a').attr('href', "../" + fjList[i]);
                            eachItem.find('a').attr('target', "_blank");
                            eachItem.find('a').css({ 'display': 'block' });
                            eachItem.find('img').attr('src', "../../Scripts/BootStrap/img/fj.png");
                            eachItem.find('img').css({ 'width': '15px', 'height': '15px', 'float': 'left', 'margin-left': '5px' });
                            eachItem.find('a').text(mc);
                            $('#fList1').append(eachItem);
                        }
                    }
                    //                    $("#fj_s").text(fj.split("/")[6]);
                    //                    $("#fj_s").removeAttr("href");
                    //                    $("#fj_s").attr("href", fj);
                } else {
                    var eachItem = $('<li style="float:left;">暂无附件！</li>');
                    $('#fList1').append(eachItem);
                    //                    $("#fj_s").removeAttr("href");
                    //                    $("#fj_s").text("暂无附件！");
                }
                $("#csr_s").text(csrname);
                var pz_s = pz.split(";");
                var tb = document.getElementById("pz");
                for (var i = tb.rows.length - 1; i >= 0; i--) {
                    tb.deleteRow(i);
                }
                for (var i = 0; i < pz_s.length; i++) {
                    var _tr1 = $('<tr></tr>');
                    var _td0 = $('<td></td>');
                    _td0.text(pz_s[i]);
                    _tr1.append(_td0);
                    $('#pz').append(_tr1);
                }
                var yd_s = yd.split(";");
                var tb_y = document.getElementById("yd");
                for (var i = tb_y.rows.length - 1; i >= 0; i--) {
                    tb_y.deleteRow(i);
                }
                for (var i = 0; i < yd_s.length; i++) {
                    var _tr = $('<tr></tr>');
                    var _td1 = $('<td></td>');
                    _td1.text(yd_s[i]);
                    _tr.append(_td1);
                    $('#yd').append(_tr);
                }
                $("#CheckModal").modal("show");
                //                $("img").bind("click", function () {
                //                    $.ajax({
                //                        url: "../Handlers/system/Daily.aspx?Method=Encryp&img=" + $(this).attr("src"),
                //                        type: "post",
                //                        cache: false,
                //                        success: function (msg) {
                //                            layer.close(l);
                //                            if (data != "error") {
                //                                window.open("ReadImg.htm?img=" + msg);
                //                            }
                //                            else {
                //                                layer.msg("读取失败！", 2, 3);
                //                            }
                //                        },
                //                        error: function () {
                //                            layer.close(l);
                //                            layer.msg('网络请求错误!', 2, 5);
                //                        }
                //                    });
                //                });
            }
        },
        error: function () {
            layer.close(l);
            layer.msg("网络错误！", 2, 3);
        }
    });
}
function Done(id) {
    $("#userid").val(userID);
    $("#EditHid").val(id);
    $("#CheckModal").modal("show");
}
function Add() {
    // $('#fmGonggao').validation();
    var alm = $(".popover");
    if (alm.length > 0) {
        return;
    }
    $(".popover ").remove();
    if ($('#txtContent').val().length < 1) {
        $('#txtContent').focus();
        //layer.msg("批注内容不能为空！", 2, 3);
        return;
    }
//    alert($('#fmGonggao').serialize());
//    alert(decodeURIComponent($('#fmGonggao').serialize()));
//    alert(encodeJson_News(decodeURIComponent($('#fmGonggao').serialize())));
   // return;
    var l = layer.load("正在提交...");
    $.ajax({
        url: "../Handlers/system/Daily.aspx?Method=pizhu",
        type: "post",
        cache: false,
        data: encodeJson_News(decodeURIComponent($('#fmGonggao').serialize())),
        success: function (data) {
            layer.close(l);
            if (data == "success") {
                layer.msg("操作成功！", 1, 1);
                $("#CheckModal").modal("hide");
                loadP($("#PSize").val(), 1);
            }
            else {
                layer.msg("操作失败！", 2, 3);
            }
        },
        error: function () {
            layer.close(l);
            layer.msg('网络请求错误!', 2, 5);
        }
    });
}
function check() {
    cLoad = 0;
    Btn_check($('#PSize').val(), 1)
}
function Btn_check(pagesize, page) {
    var l = layer.load('正在加载数据......');
    var pagesize = $("#PSize").val();
    $.ajax({
        url: "../Handlers/system/Daily.aspx?Method=LoadMyCopy&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 (cLoad == 0) {
                $("#pagination").jBootstrapPage({
                    pageSize: pagesize,
                    total: data.total,
                    maxPageButton: 5,
                    onPageClicked: function (obj, pageIndex) {
                        cPage = pageIndex + 1;
                        Btn_check($("#PSize").val(), cPage);
                    }
                });
                cLoad = 1;
            }
            if (pagesize < data.total) {
                if (mPage * pagesize >= data.total) {
                    $("#pFooter").html("当前显示：" + ((cPage - 1) * pagesize + 1).toString() + "---" + data.total + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
                }
                else {
                    $("#pFooter").html("当前显示：" + ((cPage - 1) * pagesize + 1).toString() + "---" + mPage * pagesize + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
                }
            }
            else {
                $("#pFooter").html("当前显示：" + ((cPage - 1) * pagesize + 1).toString() + "---" + data.total + "条记录；&nbsp;&nbsp;共：" + data.total + "条记录");
            }
            layer.close(l);
            resize_r();
        },
        error: function () {
            layer.msg('网络请求错误!', 2, 5);
        }
    });
}
function ShuaXin() {
    firstLoad = 0;
    loadP($("#PSize").val(), 1);
}
$(function () {
    $(".datepicker").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1
    });
});