var table = $("#table1");
var token = $.cookie("token");

//	var logtime;
//	laydate.skin('molv');
//	laydate({
//		elem: '#startTime',
//		event: 'focus',
//		format: 'YYYY-MM-DD', // 分隔符可以任意定义，该例子表示只显示年月
//	});
$(".search").click(function () {
    //		logtime=$('.loginTime').val();
    initTable();
});
//==================添加功能开始================================
$(".add").click(function () {
    $(".addModel").css("display", "block");
});
//关闭按钮
$(".addBtr").click(function () {
    $(".addModel").css("display", "none");
});
//保存按钮
$(".addCun").click(function () {
    var khmc = $(".add_name").val();
    var tsdh = $(".add_mobile").val();
    var lxr = $(".add_lxr").val();
    var lxdh = $(".add_tel").val();
    var postcode = $(".add_post").val();
    var email = $(".add_email").val();
    if (!khmc) {
        layer.confirm("请输入客户名称！", {
            btn: ["确定"],
        });
        return;
    }
    $.post(
        huayi.config.callcenter_url + "Customer/AddCustomer",
        {
            name: khmc,
            lxr: lxr,
            mobile: tsdh,
            telphone: lxdh,
            postcode: postcode,
            email: email,
            token: $.cookie("token"),
        },
        function (result) {
            result = $.parseJSON(result);
            if (result.state.toLowerCase() == "success") {
                $(".addModel").css("display", "none");
                $("#khid").val(result.data);
                cleanText();
                layer.msg("添加成功");
                table.bootstrapTable("refresh");
            }
        }
    );
});
//******************添加功能结束********************************
//==================修改功能开始================================
var did;
$(".change").click(function () {
    var ids = $.map(table.bootstrapTable("getSelections"), function (row) {
        return row.F_CustomerId;
    });
    did = ids.toString();
    if (ids.length <= 0) {
        layer.confirm("请选择要修改的行！", {
            btn: ["确定"],
        });
        return;
    } else {
        $(".changeModel").css("display", "block");
        $.get(
            huayi.config.callcenter_url + "Customer/GetCustomer",
            {
                cid: did,
                token: $.cookie("token"),
            },
            function (result) {
                result = $.parseJSON(result);
                if (result.state.toLowerCase() == "success") {
                    var con = result.data;
                    $(".change_name").val(con.F_CustomerName);
                    $(".change_mobile").val(con.F_Mobile);
                    $(".change_lxr").val(con.F_CustomerEName);
                    $(".change_tel").val(con.F_Telephone);
                    $(".change_post").val(con.F_PostCode);
                    $(".change_email").val(con.F_Email);
                }
            }
        );
    }
});

//关闭按钮
$(".changeBtr").click(function () {
    $(".changeModel").css("display", "none");
});
//保存按钮
$(".changeCun").click(function () {
    var khmc = $(".change_name").val();
    var tsdh = $(".change_mobile").val();
    var lxr = $(".change_lxr").val();
    var lxdh = $(".change_tel").val();
    var postcode = $(".change_post").val();
    var email = $(".change_email").val();
    if (!khmc) {
        layer.confirm("请输入客户名称！", {
            btn: ["确定"],
        });
        return;
    }
    $.post(
        huayi.config.callcenter_url + "Customer/AddCustomer",
        {
            cid: did,
            name: khmc,
            lxr: lxr,
            mobile: tsdh,
            telphone: lxdh,
            postcode: postcode,
            email: email,
            token: $.cookie("token"),
        },
        function (result) {
            result = $.parseJSON(result);
            if (result.state.toLowerCase() == "success") {
                $(".changeModel").css("display", "none");
                layer.msg("修改成功");
                table.bootstrapTable("refresh");
            }
        }
    );
});
//******************修改功能结束********************************

//==================删除功能开始================================
//删除按钮
var delid;
$(".del").click(function () {
    var ids = $.map(table.bootstrapTable("getSelections"), function (row) {
        return row.F_CustomerId;
    });
    delid = ids.toString();
    if (ids.length <= 0) {
        layer.confirm("请选择要删除的行！", {
            btn: ["确定"],
        });
        return;
    } else {
        $(".delModel").css("display", "block");
    }
});
//关闭按钮
$(".delBtr").click(function () {
    $(".delModel").css("display", "none");
});
//取消按钮
$(".return").click(function () {
    $(".delModel").css("display", "none");
});
//确认按钮
$(".sure").click(function () {
    $(".delModel").css("display", "none");
    del(delid);
});

function del(delid) {
    $.ajax({
        type: "post",
        url: huayi.config.callcenter_url + "Customer/DelCustomer",
        async: true,
        dataType: "json",
        data: {
            token: token,
            ids: delid,
        },
        success: function (data) {
            if (data.state.toLowerCase() == "success") {
                layer.msg("删除成功！");
                table.bootstrapTable("refresh");
            }
        },
    });
}
//*****************删除功能结束****************************
initTable();
function initTable() {
    //先销毁表格
    table.bootstrapTable("destroy");
    //初始化表格,动态从服务器加载数据
    table.bootstrapTable({
        method: "get", //使用get请求到服务器获取数据
        url: huayi.config.callcenter_url + "Customer/GetList", //获取数据的Servlet地址
        contentType: "application/x-www-form-urlencoded",
        striped: true, //表格显示条纹
        pagination: true, //启动分页
        pageSize: 10, //每页显示的记录数
        pageNumber: 1, //当前第几页
        pageList: [10, 20, 50, 100], //记录数可选列表
        search: false, //是否启用查询
        showColumns: false, //显示下拉框勾选要显示的列
        showRefresh: false, //显示刷新按钮
        sidePagination: "server", //表示服务端请求
        //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
        //设置为limit可以获取limit, offset, search, sort, order
        queryParamsType: "undefined",
        queryParams: function queryParams(params) {
            //设置查询参数
            var param = {
                page: params.pageNumber,
                pagesize: params.pageSize,
                token: $.cookie("token"),
                keyWord: $(".czName").val(), //操作人名称
                //					logindate: logtime //登录时间
            };
            return param;
        },
        onLoadSuccess: function () {
            //加载成功时执行
            //layer.msg("加载成功");
        },
        onLoadError: function () {
            //加载失败时执行
            //layer.msg("加载数据失败", { time: 1500, icon: 2 });
        },
    });
}

function setCode(val, row, index) {
    return index + 1;
}

// 工单数量
function numberWorkOrders(val, row) {
    return (
        '<div class="imgs" ><a class="" onclick="viewList(\'' + row.F_Mobile + '\', \'9\')" >' + val + '</a></div>'
    );
}

// 来电数量
function ldCountFormatter(val, row) {
    return (
        '<div class="imgs" ><a class="" onclick="callRecords(\'' + row.F_Mobile + '\')" >' + val + '</a></div>'
    );
}

// 联系电话
function mobileFormatter(val, row) {
    return (
        '<div class="imgs" ><a class="" onclick="viewName(\'' + row.F_Mobile + '\')" >' + val + '</a></div>'
    );
}

// 查看列表
function viewList(mobile, state) {
    layer.open({
        type: 2,
        content: "./ContactPersonMangerList.html?mobile=" + mobile + "&state=" + state, //iframe的url，no代表不显示滚动条
        title: "市民资料管理",
        area: ["90%", "90%"], //宽高
    });
}

// 通话记录
function callRecords(mobile) {
    layer.open({
        type: 2,
        content: "./ContactPersonCallRecords.html?mobile=" + mobile, //iframe的url，no代表不显示滚动条
        title: "市民资料管理",
        area: ["90%", "90%"], //宽高
    });
}

function viewName(mobile) {
    layer.open({
        type: 2,
        content: "./ContactPersonNameList.html?mobile=" + mobile, //iframe的url，no代表不显示滚动条
        title: "市民资料管理",
        area: ["90%", "90%"], //宽高
    });
}


function cleanText() {
    $(".add_name").val("");
    $(".add_mobile").val("");
    $(".add_lxr").val("");
    $(".add_tel").val("");
    $(".add_post").val("");
    $(".add_email").val("");
}

$(".importFile").click(function () {
    $("#upFile").trigger("click");
});

$("#upFile").change(function () {
    upload();
});

function upload() {
    if (document.getElementById("upFile").files.length > 0) {
        var formData = new FormData();
        formData.append("upFile", document.getElementById("upFile").files[0]);
        formData.append("token", $.cookie("token"));
        $.ajax({
            url: huayi.config.callcenter_url + "Customer/ImportExcel",
            type: "POST",
            data: formData,
            contentType: false,
            processData: false,
            success: function (result) {
                document.getElementById("upFile").outerHTML =
                    document.getElementById("upFile").outerHTML;
                $("#upFile").change(function () {
                    upload();
                });
                var r = $.parseJSON(result);
                if (r.state.toLowerCase() == "success") {
                    layer.msg("导入成功");
                    initTable();
                }
            },
        });
    } else {
        layer.confirm("请上传文件！", {
            btn: ["确定"],
        });
    }
}

$(".exportTemplate").click(function () {
    var a = document.createElement("a");
    a.style.display = "none";
    a.download = "市民资料模板.xlsx";
    a.href = "../static/xls/市民资料模板.xlsx";
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
});

$(".exportFile").click(function () {
    var keyWord = $(".czName").val();

    var url =
        huayi.config.callcenter_url +
        "Customer/GetList?token=" +
        $.cookie("token");
    url += "&keyWord=" + keyWord + "&isdc=1";
    window.location.href = url;
});
