var srvID = "";
var userID = "";
var auPro = "";
var mPage = 1;
var firstLoad = 0;
var zt = 2;
var ifrm;
if ('ontouchstart' in document.documentElement) document.write("<script src='Scripts/assets/js/jquery.mobile.custom.min.js'>" + "<" + "/script>");
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 });
}
jQuery(function ($) {
    $('[data-rel=tooltip]').tooltip();
    $('[data-rel=popover]').popover({ html: true });

    $("#PSize").change(function () {
        firstLoad = 0;
        mPage = 1;
        LoadData($("#PSize").val(), 1);
    });
});
function createPage(pageSize, buttons, total) {
    $("#z_p").jBootstrapPage({
        pageSize: pageSize,
        total: total,
        maxPageButton: buttons,
        onPageClicked: function (obj, pageIndex) {
            mPage = pageIndex + 1;
            LoadData($("#PSize").val(), mPage);
        }
    });
}
function LoadData(pagesize, page) {
    var l = layer.load('正在加载数据......');
    $.ajax({
        url: "../Handlers/IndividualCenter/PhoneBook.aspx?Method=All&srvid=" + srvID + "&userID=" + userID + "&page=" + page + "&rows=" + pagesize + "&ss=" + zt,
        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 Btn_check(pagesize, page) {
    var l = layer.load('正在加载数据......');
    var xm = encodeJson($("#sxm").val());
    $.ajax({
        url: "../Handlers/IndividualCenter/PhoneBook.aspx?Method=All&srvid=" + srvID + "&userID=" + userID + "&page=" + page + "&rows=" + pagesize + "&where=select&name=" + encodeURI(encodeURI(xm)) + "&ss=" + zt,
        type: "post",
        cache: false,
        dataType: "json",
        success: function (data) {
            CreateTable(data);
            firstLoad = 0;
            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.text(data.rows[i].Name);
        var _td2 = $('<td></td>');
        _td2.text(data.rows[i].Sex);
        var _td3 = $('<td></td>');
        _td3.text(data.rows[i].Phone);
        var _td4 = $('<td></td>');
        if (data.rows[i].Department.length >= 20) {
            _td4.append('<span style="cursor:pointer;" class="tooltip-info" data-placement="right" data-rel="tooltip" data-original-title="' + data.rows[i].Department + '">' + data.rows[i].Department.substring(0, 20) + "..." + '</span>');
        } else {
            _td4.append('<span style="cursor:pointer;" class="tooltip-info" data-placement="right" data-rel="tooltip" data-original-title="' + data.rows[i].Department + '">' + data.rows[i].Department + '</span>');
        }
        var _td5 = $('<td></td>');
        _td5.text(data.rows[i].Email);
        var _td6 = $('<td></td>');
        if (data.rows[i].Remarks.length >= 20) {
            _td6.append('<span style="cursor:pointer;" class="tooltip-info" data-placement="right" data-rel="tooltip" data-original-title="' + data.rows[i].Remarks + '">' + data.rows[i].Remarks.substring(0, 20) + "..." + '</span>');
        } else {
            _td6.append('<span style="cursor:pointer;" class="tooltip-info" data-placement="right" data-rel="tooltip" data-original-title="' + data.rows[i].Remarks + '">' + data.rows[i].Remarks + '</span>');
        }
        var _td7 = $('<td></td>');
        _td7.text(data.rows[i].State);
        var _td8 = $('<td class="center"></td>');
        if (data.rows[i].FromUser == userID) {
            _td8.append('<div class="wjsygn"><a style="cursor:pointer;display:none;" tag="M_133_E" title="修改" onclick="Update_Dh(this);"><span class="tbqbgw">修改</span></a><a style="cursor:pointer;display:none;" tag="M_133_D" title="删除" onclick="Del_CZ(\'' + data.rows[i].ID + '\');" ><span class="tbqbgw">删除</span></a></div>');
        } else {
            _td8.append('<div class="wjsygn"><a style="cursor:pointer;display:none;" tag="M_133_E" title="修改"><span class="tbqbgw" style="color:#808A87;">修改</span></a><a style="cursor:pointer;display:none;" tag="M_133_D" title="删除" ><span class="tbqbgw" style="color:#808A87;">删除</span></a></div>');
        }
        var _td9 = $('<td style="display: none;"></td>');
        _td9.text(data.rows[i].ID);
        var _td10 = $('<td style="display: none;"></td>');
        _td10.text(data.rows[i].suoshu);
        _tr1.append(_td1);
        _tr1.append(_td2);
        _tr1.append(_td3);
        _tr1.append(_td4);
        _tr1.append(_td5);
        _tr1.append(_td6);
        _tr1.append(_td7);
        _tr1.append(_td8);
        _tr1.append(_td9);
        _tr1.append(_td10);
        $('#Management tbody').append(_tr1);
        ClickEvent();
    }
    $("#Management tbody").find('tr').each(function (i, e) {
        var td = $(e).find('td').eq(7);
        $(td).find('div a').each(function (j, e1) {
            var tag1 = $(e1).attr('tag');
            if (tag1) {
                if (existRole(tag1, auPro)) {
                    $(e1).show();
                }
            }
        });
    });
}
function ShuaXin() {
    LoadData($("#PSize").val(), 1);
}
function ADD_Dh() {
    $('#AddDialog').modal('show');
    $("#ss").val("0");
    $("#xm").val("");
    $("#xb").val("男");
    $("#bm").val("");
    $("#dh").val("");
    $("#yx").val("");
    $("#bz").val("");
}
function Add() {
    $('#frmAdd').validation();
    var alm = $(".popover");
    if (alm.length > 0) {
        return;
    }
    var ss = $("#ss").val();
    var xm = $("#xm").val();
    var xb = $("#xb").val();
    var bm = $("#bm").val();
    var dh = $("#dh").val();
    var yx = $("#yx").val();
    var bz = $("#bz").val();
    if (xm.length > 10) {
        layer.msg('姓名过长，请保持在十个字内！', 2, 5);
        return;
    }
    ss = encodeJson_News(ss);
    xm = encodeJson_News(xm);
    xb = encodeJson_News(xb);
    bm = encodeJson_News(bm);
    dh = encodeJson_News(dh);
    yx = encodeJson_News(yx);
    bz = encodeJson_News(bz);
    var l = layer.load("正在提交...");
    $.ajax({
        type: "post",
        url: "../Handlers/IndividualCenter/PhoneBook.aspx?Method=Add&srvid=" + srvID + "&userID=" + userID + "&ss=" + encodeURI(encodeURI(ss)) + "&xm=" + encodeURI(encodeURI(xm)) + "&xb=" + encodeURI(encodeURI(xb)) + "&bm=" + encodeURI(encodeURI(bm)) + "&dh=" + encodeURI(encodeURI(dh)) + "&yx=" + encodeURI(encodeURI(yx)) + "&bz=" + encodeURI(encodeURI(bz)),
        cache: false,
        success: function (msg) {
            layer.close(l);
            if (msg == "sucess") {
                $('#AddDialog').modal('hide');
                layer.msg('申请成功！', 3, 1);
                $("#ss").val("0");
                $("#xm").val("");
                $("#xb").val("男");
                $("#bm").val("");
                $("#dh").val("");
                $("#yx").val("");
                $("#bz").val("");
                firstLoad = 0;
                mPage = 1;
                LoadData($("#PSize").val(), 1);
            }
            else {
                layer.close(l);
                layer.msg('添加失败！', 3, 5);
            }
        },
        error: function () {
            layer.close(l);
            layer.msg('网络请求错误!', 3, 5);
        }
    });
}
function Update_Dh(obj) {
    var tds = $(obj).parent().parent().parent().find('td');
    $('#uxm').val(tds.eq(0).text());
    $('#uxb').val(tds.eq(1).text());
    $('#udh').val(tds.eq(2).text());
    $('#ubm').val(tds.eq(3).text());
    $('#uyx').val(tds.eq(4).text());
    $('#ubz').val(tds.eq(5).text());
    $('#uss').val(tds.eq(9).text());
    $('#uid').val(tds.eq(8).text());
    $('#uAddDialog').modal('show');
}
function Update() {
    $('#ufrmAdd').validation();
    var alm = $(".popover");
    if (alm.length > 0) {
        return;
    }
    var id = $("#uid").val();
    var ss = $("#uss").val();
    var xm = $("#uxm").val();
    var xb = $("#uxb").val();
    var bm = $("#ubm").val();
    var dh = $("#udh").val();
    var yx = $("#uyx").val();
    var bz = $("#ubz").val();
    if (xm.length > 10) {
        layer.msg('姓名过长，请保持在十个字内！', 2, 5);
        return;
    }
    ss = encodeJson_News(ss);
    xm = encodeJson_News(xm);
    xb = encodeJson_News(xb);
    bm = encodeJson_News(bm);
    dh = encodeJson_News(dh);
    yx = encodeJson_News(yx);
    bz = encodeJson_News(bz);
    var l = layer.load("正在提交...");
    $.ajax({
        type: "post",
        url: "../Handlers/IndividualCenter/PhoneBook.aspx?Method=Update&id=" + encodeURI(encodeURI(id)) + "&ss=" + encodeURI(encodeURI(ss)) + "&xm=" + encodeURI(encodeURI(xm)) + "&xb=" + encodeURI(encodeURI(xb)) + "&bm=" + encodeURI(encodeURI(bm)) + "&dh=" + encodeURI(encodeURI(dh)) + "&yx=" + encodeURI(encodeURI(yx)) + "&bz=" + encodeURI(encodeURI(bz)),
        cache: false,
        success: function (msg) {
            layer.close(l);
            if (msg == "sucess") {
                $('#uAddDialog').modal('hide');
                layer.msg('修改成功！', 3, 1);
                $("#uss").val("0");
                $("#uxm").val("");
                $("#uxb").val("男");
                $("#ubm").val("");
                $("#udh").val("");
                $("#uyx").val("");
                $("#ubz").val("");
                firstLoad = 0;
                mPage = 1;
                LoadData($("#PSize").val(), 1);
            }
            else {
                layer.close(l);
                layer.msg('修改失败！', 3, 5);
            }
        },
        error: function () {
            layer.close(l);
            layer.msg('网络请求错误!', 3, 5);
        }
    });
}
function Del_CZ(ID) {
    $("#DelID").val(ID);
    $('#Del').modal('show');
}
function Del_ZD() {
    var ID = document.getElementById("DelID").value;
    $.ajax({
        type: "post",
        url: "../Handlers/IndividualCenter/PhoneBook.aspx?Method=Del&ID=" + ID,
        cache: false,
        success: function (msg) {
            if (msg == "sucess") {
                $('#Del').modal('hide');
                layer.msg('删除成功！', 3, 1);
                firstLoad = 0;
                mPage = 1;
                LoadData($("#PSize").val(), 1);
            }
            else {
                layer.msg('删除失败！', 3, 5);
            }
        },
        error: function () {
            layer.msg('网络请求错误!', 3, 5);
        }
    });
}
function XuanZe(ss) {
    zt = ss;
    firstLoad = 0;
    mPage = 1;
    LoadData($("#PSize").val(), 1);
}
function existRole(s, p) {
    var e = p.indexOf(s);
    if (e == -1) {
        return false;
    }
    else {
        return true;
    }
}
function iniParms() {
    var coks = document.cookie.substring(document.cookie.indexOf('userinf_tcwl'));
    var ary = coks.split(';');
    var ary1 = ary[0].split('=');
    if (!ary1[2]) {
        $('html').empty();
        return;
    }
    userID = ary1[2].split('&')[0];
    srvID = ary1[3];
    $('#Huserid').val(userID);
    $('#Hsrvid').val(srvID);
    var s = window.location.href.split('=');
    if (s.length == 4) {
        ifrm = s[3];
    }
    resize_r();
    var coksau = document.cookie.substring(document.cookie.indexOf('TCWL_All'));
    var aryau = coksau.split(';');
    var aryau1 = aryau[0].split('=');
    auPro = aryau1[2];
    $('#checkform button').each(function (i, e) {
        var tag = $(e).attr('tag');
        if (tag) {
            if (existRole(tag, auPro)) {
                $(e).show();
            }
        }
    });
    LoadData($("#PSize").val(), 1);
    $("#DaoRu").attr('src', 'Import.aspx?srvid=' + srvID + '&userid=' + userID);
}
function Import() {
    $('#DR').modal('show');
    $("#fileurl").text("");
}