var m_Report = [];//报表信息缓存
var m_StartDate = "";//查询起始时间
var m_EndDate = "";//查询结束时间
var m_UpdateId = "";
var m_Customname = "";//储存当前选中的自定义报表名称
//保存好友档案列表的列是否可见及排序
var m_SelectColCacheFansOld = [
   { isshow: true, id: "isopenid", name: "好友ID", order: 0 },
   { isshow: true, id: "state", name: "好友状态", order: 1 },
   { isshow: true, id: "iremark", name: "备注名", order: 2 },
   { isshow: true, id: "isnickname", name: "昵称", order: 3 },
   { isshow: true, id: "isname", name: "姓名", order: 4 },
   { isshow: true, id: "isphone", name: "联系方式", order: 5 },
   { isshow: true, id: "isshxscode", name: "所属微信", order: 6 },
   { isshow: true, id: "issex", name: "性别", order: 7 },
   { isshow: true, id: "ispostion", name: "位置", order: 8 },
   { isshow: true, id: "islastvisittime", name: "最后通话", order: 9 },
   { isshow: true, id: "isexcode", name: "微信号", order: 10 },
   { isshow: true, id: "isage", name: "年龄", order: 11 },
   { isshow: true, id: "isgroupname", name: "所属分组", order: 12 },
   { isshow: true, id: "createtime", name: "添加时间", order: 13 }
];
var m_SelectColCacheFans = m_SelectColCacheFansOld;
//初始化绑定
$(function () {
    //绑定报表列表
    BindReportList();
})

//绑定报表列表
function BindReportList() {
    //绑定自定义报表
    var strParaJson = $.toJSON({ type: "1" });
    var reportJson = $.parseJSON(homeObj.getmyreportresult(strParaJson));
    m_Report = reportJson.list;
    $("#selectReport").empty();//清空
    $("#selectReport").append("<option value=''>自定义查询</option>");
    for (var i = 0; i < m_Report.length; i++) {
        $("#selectReport").append("<option value='" + m_Report[i].id + "'>" + m_Report[i].name + "</option>");
    }
    $("#selectReport").append("<option disabled=\"disabled\">----------------</option>");
    $("#selectReport").append("<option value='0'>设置自定义报表</option>");
}

//获取报表信息
function GetReprot() {
    var strContent = "";
    strContent += "<table class=\"table table-striped table-hover tabel-border table-fix\"><thead><tr>";
    strContent += GetTableHeadNoOrderStr("序号");
    strContent += GetTableHeadNoOrderStr("自定义查询名称");
    strContent += GetTableHeadNoOrderStr("查询条件");
    strContent += GetTableHeadNoOrderStr("显示数据");
    strContent += GetTableHeadNoOrderStr("操作");

    strContent += "</tr></thead>";
    strContent += "<tbody>";
    for (var i = 0; i < m_Report.length; i++) {
        var item = m_Report[i];
        var strTrClass = i % 2 == 0 ? "even" : "odd";
        strContent += "<tr class=\"" + strTrClass + "\">";
        strContent += "<td class=\"td-number\" >" + (i + 1) + "</td>";
        strContent += "<td>" + item.name + "</td>";
        var StrCustomers = "";
        var StrPostion = "";

        (item.para.cusname == "") ? StrCustomers = "不限" : StrCustomers = item.para.cusname;
        (item.para.wechatname == "") ? StrPostion = "不限" : StrPostion = item.para.wechatname;

        strContent += "<td>接待客服：" + StrCustomers + "，客服微信：" + StrPostion + "...</td>";
        //显示列
        var StrSelectedContent = "";
        var StrLongSelectedContent = "";
        for (var j = 0; j < item.col.length; j++) {
            StrSelectedContent += (item.col[j].isshow ? item.col[j].name + "|" : "");
        }

        (StrSelectedContent.length > 15) ? StrLongSelectedContent = StrSelectedContent.substring(0, 15) + "..." : StrLongSelectedContent = StrSelectedContent;

        strContent += "<td>" + StrLongSelectedContent + "</td>";
        strContent += "<td><button class=\"btn  btn-default btn-add edit-m\" onclick=\"CreatCode('" + i + "','" + item.name + "');\"><span data-toggle=\"tooltip\" data-placement=\"top\" title=\"编辑\" class=\"glyphicon glyphicon-pencil aaa\"></span></button>";

        strContent += "<button class=\"btn btn-danger edit-m\" onclick=\"ShowDelete('" + item.id + "','" + item.name + "');\"><span data-toggle=\"tooltip\" data-placement=\"top\" title=\"删除\" class=\"glyphicon glyphicon-trash\"></span></button></td>";
        strContent += "</tr>";
    }
    strContent += "</tbody>";
    strContent += "</table>";

    if (m_Report.length == 0) {
        strContent = "<center>暂无数据</center>";
    }
    $("#divReportContent").html(strContent);
    $("#popGetReportModel").modal({ backdrop: 'static', keyboard: false });
}

//获取报表信息条件
function GetReportPara(id) {
    for (var i = 0; i < m_Report.length; i++) {
        if (m_Report[i].id == id) {
            return m_Report[i].para;
        }
    }
    return null;
}

//获取报表信息列表列是否显示
function GetReportCol(id) {
	var strreportcolum = "";
	if (id.length > 0) {
		for (var i = 0; i < m_Report.length; i++) {
			if (m_Report[i].id == id) {
				strreportcolum = m_Report[i].col;
			}
		}
	}else {
	    strreportcolum = m_SelectColCacheFansOld;
	}
   
	return strreportcolum;
}



//自定义时间隐藏/显示 粉丝档案
function SelectTimeTypeChangeFans() {
    if ($("#selectTimeTypeFans").val() == "9") {
        $("#divTimeDefaultFans").show();
    }
    else {
        $("#divTimeDefaultFans").hide();
    }
}

function SelectTimeTypeChangeFans1() {
    if ($("#selectTimeTypeFans1").val() == "9") {
        $("#divTimeDefaultFans1").show();
    }
    else {
        $("#divTimeDefaultFans1").hide();
    }
}

function SelectTimeTypeChangeFans2() {
    if ($("#selectTimeTypeFans2").val() == "9") {
        $("#divTimeDefaultFans2").show();
    }
    else {
        $("#divTimeDefaultFans2").hide();
    }
}



//修改绑定历史对话记录 ，粉丝档案
function CreatCode(i, name) {
	IsFirst = 1;//是否是第一次加载
	m_Customname = name;
    m_SelectColCacheFans = [
       { isshow: true, id: "isopenid", name: "好友ID", order: 0 },
       { isshow: true, id: "state", name: "好友状态", order: 1 },
       { isshow: true, id: "iremark", name: "备注名", order: 2 },
       { isshow: true, id: "isnickname", name: "昵称", order: 3 },
       { isshow: true, id: "isname", name: "姓名", order: 4 },
       { isshow: true, id: "isphone", name: "联系方式", order: 5 },
       { isshow: true, id: "isshxscode", name: "所属微信", order: 6 },
       { isshow: true, id: "issex", name: "性别", order: 7 },
       { isshow: true, id: "ispostion", name: "位置", order: 8 },
       { isshow: true, id: "islastvisittime", name: "最后通话", order: 9 },
       { isshow: true, id: "isexcode", name: "微信号", order: 10 },
       { isshow: true, id: "isage", name: "年龄", order: 11 },
       { isshow: true, id: "isgroupname", name: "所属分组", order: 12 },
       { isshow: true, id: "createtime", name: "添加时间", order: 13 }
    ];
    $("#popGetReportModel").modal("hide");
    ResetSelectControlFans();
    m_UpdateId = i;
    var item = m_Report[i];

    ClearPageFans();
     $("#selectPersonDept1").html("好友分组");
     if (m_UpdateId != null) {
        $("#txtReportName").val(item.name);
        $("#selDept1").val(item.para.dpt);
        // 绑定客服
        CustomerDefault = item.para.cususerid;
        BindCompanyUsers(1);
        // 绑定微信
        WechatDefault = item.para.uin;
        $("#buddystatustiao").val(item.para.state);//好友状态绑值
        $("#selectQuerySex1").val(item.para.sex);
        $("#txtReportTimeFrom").val(item.para.timefrom);//添加好友开始时间绑定
        $("#txtReportTimeEnd").val(item.para.timeend);//添加结束开始时间绑定
        $("#talkReportTimeFrom").val(item.para.talktimefrom);//对话开始时间绑定
        $("#talkReportTimeEnd").val(item.para.talktimeto);//对话结束时间绑定
        var groupname = item.para.group.length > 0 ? item.para.group : "好友分组";
        $("#selectPersonDept1").html(groupname);
        //
        var strBindGroupNameColor = "";
        var groupindex;
        for (i in m_GroupItem) {
            if (m_GroupItem[i].key == "friendgroup") {
                var groupnameitem = $.parseJSON(m_GroupItem[i].valus);
                for (var j = 0; j < groupnameitem.length; j++) {
                    if (groupnameitem[j] == item.para.group) {
                        groupindex = j;
                    }
                }
            }
        }

        for (var n = 0; n < m_GroupItem.length; n++) {
            if (m_GroupItem[n].key == "friendgroupcolor") {
                var groupnamecoloritem = $.parseJSON(m_GroupItem[n].color);
                    strBindGroupNameColor = groupnamecoloritem[groupindex];
            }
        }
        $("#selectPersonDept1").css("border-left", "5px solid " + strBindGroupNameColor + "");

        //文本框值
        $("#txtQueryFriendNick1").val(item.para.nickname);
        $("#txtQueryFriendId1").val(item.para.wxcode);
        $("#txtQueryNumber1").val(item.para.pid);
        $("#txtQueryPostion1").val(item.para.point);

        //  显示列是否选中
        m_SelectColCacheFans = item.col;
        ResetSelectControlFans();
     }
     if (i == undefined) {
         $("#txtReportTimeFrom").val("");
         $("#txtReportTimeEnd").val("");
         $("#talkReportTimeFrom").val("");
         $("#talkReportTimeEnd").val("");
     }
    $("#SetReport").modal("show");
}
//删除模版对话框
function ShowDelete(id, name) {
    $("#hidQrId").val(id);
    $("#spDeleteName").html(name);
    $("#DeleteModal").modal("show");
}
//调用删除
function CommitDelete() {
    var strId = $("#hidQrId").val();
    var strParaJson = $.toJSON({
        id: strId
    });
    var JsonDeleteResult = $.parseJSON(homeObj.deletemyreportresult(strParaJson));
    if (JsonDeleteResult.success == true) {
        ShowMsg("myPageAlert", "操作成功");
        BindReportList();
        GetReprot();//报表列表重新加载 
        $("#DeleteModal").modal("hide");
    }
    else {
        ShowError("myPageAlert", "操作失败：" + error);
        $("#DeleteModal").modal("hide");
    }
}

//粉丝档案 修改，删除
function EditFans() {
    //下拉列表所选值
    var StrDept = $("#selDept1 option:selected").val();//部门
    var StrCus = $("#selectQueryCustomer1 option:selected").val();//客服
    var StrCusName = $("#selectQueryCustomer1 option:selected").html();//微信昵称
    var StrWechat = $("#selectQueryWechat1 option:selected").val();//微信
    var StrWechatName = $("#selectQueryWechat1 option:selected").html();//微信昵称
    var StrSex = $("#selectQuerySex1 option:selected").val();//性别

    var groupname = $("#selectPersonDept1").html() == "好友分组" ? "" : $("#selectPersonDept1").html();
    var StrPersonDept = groupname;//好友分组

    //文本框值
    var StrFriendNick = $("#txtQueryFriendNick1").val();//好友昵称
    var StrFriendId = $("#txtQueryFriendId1").val();//好友微信
    var StrNumber = $("#txtQueryNumber1").val();//好友ID
    var StrPostion = $("#txtQueryPostion1").val();//位置

    var txtReportName = $("#txtReportName");//报表名称
    var buddystatus = $("#buddystatustiao").val();//好友状态
	//好友时间查询时间限制
    var time1 = Date.parse(new Date("20" + $("#txtReportTimeFrom").val()));//添加好友起始时间
    var time2 = Date.parse(new Date("20" + $("#txtReportTimeEnd").val()));//添加好友结束时间
    var diff = parseInt((time2 - time1) / 1000 / 3600 / 24);
    if (diff < 0) {
        ShowError("myPageAlert", "起始时间不能大于终止时间");
        return;
    }

	//对话时间查询时间限制
    var txtReportTimeFrom = $("#txtReportTimeFrom").val();
    var txtReportTimeEnd = $("#txtReportTimeEnd").val();
    var strTalkStartTime = Date.parse(new Date("20" + $("#talkReportTimeFrom").val()));//添加对话起始时间
    var strTalkEndTime = Date.parse(new Date("20" + $("#talkReportTimeEnd").val()));//添加对话结束时间
    var strTalkdiff = parseInt((strTalkEndTime - strTalkStartTime) / 1000 / 3600 / 24);
    if (strTalkdiff < 0) {
    	ShowError("myPageAlert", "起始时间不能大于终止时间");
    	return;
    }
    var talkReportTimeFrom = $("#talkReportTimeFrom").val();
    var talkReportTimeEnd = $("#talkReportTimeEnd").val();
    //验证
    if ($.trim(txtReportName.val()) == "") {
        ShowError("myPageAlert", "请输入报表名称");
        $("#txtReportName").val("");
        txtReportName.focus();
        return;
    }
	//判断自定义报表名称是否重复
    for (var i = 0; i < m_Report.length; i++) {
    	if (m_Report[i].name == $.trim(txtReportName.val()) && $.trim(txtReportName.val()) != m_Customname) {
    		ShowError("myPageAlert", "该报表名称已存在！");
    		txtReportName.focus();
    		return;
    	}
    }

    //报表名称
    var json = {};
    json.type = 1;
    json.name = txtReportName.val();
    //查询条件
    var jsonPara = {};
    jsonPara.dpt = StrDept;
    jsonPara.cususerid = StrCus;
    jsonPara.cusname = StrCusName;
    jsonPara.uin = StrWechat;
    jsonPara.wechatname = StrWechatName;
    jsonPara.sex = StrSex;
    jsonPara.group = StrPersonDept;

    jsonPara.nickname = StrFriendNick;
    jsonPara.wxcode = StrFriendId;
    jsonPara.pid = StrNumber;
    jsonPara.point = StrPostion;
    jsonPara.timefrom = txtReportTimeFrom;//添加好友开始时间
    jsonPara.timeend = txtReportTimeEnd;//添加好友开始时间
    jsonPara.talktimefrom = talkReportTimeFrom;//添加好友开始时间
    jsonPara.talktimeto = talkReportTimeEnd;//添加好友开始时间
    jsonPara.state = buddystatus;
    json.para = jsonPara;

    //显示列
    //for (var i in m_SelectColCacheFans) {
    //    var a = m_SelectColCacheFans[i];
    //    var b = typeof (m_SelectColCacheFans[i])
    //}
    json.col = m_SelectColCacheFans;

    if (typeof (m_UpdateId) != "undefined" || m_UpdateId != null) {
        var item = m_Report[m_UpdateId];
        json.id = item.id;
    }
    var BisResult = "";
    if (typeof (m_UpdateId) != "undefined") {
        BisResult = $.parseJSON(homeObj.updatemyreportresult(JSON.stringify(json)));
    }
    else {
        BisResult = $.parseJSON(homeObj.addmyreportresult(JSON.stringify(json)));
    }
    if (BisResult.success == true) {
        ShowMsg("myPageAlert", "操作成功");
        ClearPageFans();//清空页面
        BindReportList();
        GetReprot();
        $("#SetReport").modal("hide");
    }
    else {
        ShowError("myPageAlert", "操作失败：" + error);
    }
}
//清空页面数据 粉丝档案
function ClearPageFans() {
    $("#txtReportName").val("");
    WechatDefault = "";// 初始化绑定微信用
    $("#selDept1 option[value='']").attr("selected", "selected");
    $("#selectQueryCustomer1 option[value='']").attr("selected", "selected");
    $("#selectQueryWechat1 option[value='']").attr("selected", "selected");
    $("#selectQuerySex1 option[value='']").attr("selected", "selected");
    $("#selectPersonDept1").html("");;

    $("#txtQueryFriendNick1").val("");
    $("#txtQueryFriendId1").val("");
    $("#txtQueryNumber1").val("");
    $("#txtQueryPostion1").val("");
}
//粉丝档案显示列
function GetSelectColHtmlFans(isselect, id, name, index) {
    var strContent = "";
    var strStyle1 = isselect ? "li-list-box checked-box li-yreport-check" : "li-list-box li-yreport-check";
    var strStyle2 = isselect ? "myreport-checkbox checked-person" : "myreport-checkbox";

    strContent += "<li class=\"" + strStyle1 + "\" onclick=\"ChangeColStateFans(" + !isselect + ",'" + id + "');\">";
    strContent += "<ul class=\"list-unstyled\">";
    strContent += "<li class=\"" + strStyle2 + "\"> </li>";
    if (index == 0) {
        strContent += "<li  style=\"width;'200px'\"  class=\"customer-service-name\">" + name + "\</li><li onclick=\"SetRightServiceState(" + index + ");\"  class=\"angel-right-l\"><i class=\"fa fa-angle-right\" aria-hidden=\"true\"></i></li>";
    } else if (index == 12) {
        strContent += "<li onclick=\"SetLeftServiceState(" + index + ");\" class=\"angel-left-l\"><i class=\"fa fa-angle-left\" aria-hidden=\"true\"></i></li><li  style=\"width;'200px'\"  class=\"customer-service-name\">" + name + "\</li>";
    }
    else {
        strContent += "<li onclick=\"SetLeftServiceState(" + index + ");\"  class=\"angel-left-l\"><i class=\"fa fa-angle-left\" aria-hidden=\"true\"></i></li><li style=\"display:block;width;'200px'\"  class=\"customer-service-name\">" + name + "\</li><li onclick=\"SetRightServiceState(" + index + ");\" class=\"angel-right-l\"><i class=\"fa fa-angle-right\" aria-hidden=\"true\"></i></li>";
    }
    strContent += "</ul>";
    strContent += "</li>";

    return strContent;
}

//粉丝档案显示列 遍历显示
function ResetSelectControlFans() {
    var strContent = "";

    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[0].isshow, m_SelectColCacheFans[0].id, m_SelectColCacheFans[0].name, m_SelectColCacheFans[0].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[1].isshow, m_SelectColCacheFans[1].id, m_SelectColCacheFans[1].name, m_SelectColCacheFans[1].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[2].isshow, m_SelectColCacheFans[2].id, m_SelectColCacheFans[2].name, m_SelectColCacheFans[2].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[3].isshow, m_SelectColCacheFans[3].id, m_SelectColCacheFans[3].name, m_SelectColCacheFans[3].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[4].isshow, m_SelectColCacheFans[4].id, m_SelectColCacheFans[4].name, m_SelectColCacheFans[4].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[5].isshow, m_SelectColCacheFans[5].id, m_SelectColCacheFans[5].name, m_SelectColCacheFans[5].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[6].isshow, m_SelectColCacheFans[6].id, m_SelectColCacheFans[6].name, m_SelectColCacheFans[6].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[7].isshow, m_SelectColCacheFans[7].id, m_SelectColCacheFans[7].name, m_SelectColCacheFans[7].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[8].isshow, m_SelectColCacheFans[8].id, m_SelectColCacheFans[8].name, m_SelectColCacheFans[8].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[9].isshow, m_SelectColCacheFans[9].id, m_SelectColCacheFans[9].name, m_SelectColCacheFans[9].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[10].isshow, m_SelectColCacheFans[10].id, m_SelectColCacheFans[10].name, m_SelectColCacheFans[10].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[11].isshow, m_SelectColCacheFans[11].id, m_SelectColCacheFans[11].name, m_SelectColCacheFans[11].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[12].isshow, m_SelectColCacheFans[12].id, m_SelectColCacheFans[12].name, m_SelectColCacheFans[12].order);
    strContent += GetSelectColHtmlFans(m_SelectColCacheFans[13].isshow, m_SelectColCacheFans[13].id, m_SelectColCacheFans[13].name, m_SelectColCacheFans[13].order);
    $("#ulColListFans").html(strContent);
}
//粉丝档案选中未选中事件
function ChangeColStateFans(isselect, id) {
    for (var i = 0; i < m_SelectColCacheFans.length; i++) {
        if (m_SelectColCacheFans[i].id == id) {
            m_SelectColCacheFans[i].isshow = isselect;
        }
    }

    ResetSelectControlFans();
}

//右移状态
function SetRightServiceState(index) {
    event.cancelBubble = true;//禁止出发父级div事件
    var ItemServiceState = m_SelectColCacheFans[index];
    m_SelectColCacheFans[index].order = index + 1;
    m_SelectColCacheFans[index] = m_SelectColCacheFans[index + 1];
    m_SelectColCacheFans[index + 1].order = index;
    m_SelectColCacheFans[index + 1] = ItemServiceState;
    ResetSelectControlFans();
}
//左移状态
function SetLeftServiceState(index) {
    event.cancelBubble = true;//禁止出发父级div事件
    var ItemServiceState = m_SelectColCacheFans[index];
    m_SelectColCacheFans[index].order = index - 1;
    m_SelectColCacheFans[index] = m_SelectColCacheFans[index - 1];
    m_SelectColCacheFans[index - 1].order = index;
    m_SelectColCacheFans[index - 1] = ItemServiceState;
    ResetSelectControlFans();
}