$(document).ready(function () {
    getReactionCategory();

    // 反映类别搜索
    $("#reflectCategory").bind("input propertychange", function () {
        $(".layui-dropdown").hide();
        $(".CleansReactionCategory").show();
        if ($("#reflectCategory").val() == "") {
            $(".reflectCategoryList-wrapper").hide();
            return;
        }
        $(".reflectCategoryList-wrapper").show();
        var debounceGetSearchReactionCategory = debounce(
            getSearchReactionCategory,
            500
        );
        debounceGetSearchReactionCategory($("#reflectCategory").val());
    });

    // 承办单位搜索
    $(".inps").bind("input propertychange", function () {
        $(".xlAdd").css("display", "none");
        $(".selDpart1").css("display", "block");
        if ($(".inps").val() == "") {
            $(".selDpart1").css("display", "none");
            return;
        }
        var debounceDepart = debounce(depart, 500);
        debounceDepart($(".inps").val());
    });
    $("#reflectCategoryList").on("click", "li", function () {
        $(".reflectCategoryList-wrapper").hide();
        $("#reflectCategory").val($(this).attr("indexName"));
        $("#keyid").val($(this).attr("index"));
    });
});

// 查看工单详情
function View(val, row) {
    var workId = val;
    var workIdCode = workId.slice(0, 4);
    var workIdAddress = workId.slice(4, 10);
    var workIdDate = workId.slice(10, 16);
    var workIdSerialNumber = workId.slice(16);
    return (
        '<div class="imgs" ><a class="" style="font-weight: 700;" index="' +
        row.CreateUser +
        '" onclick= ckxq("' +
        row.F_WorkOrderId +
        '") >' +
        '<span style="color: #000000">' +
        workIdCode +
        "</span>" +
        '<span style="color: #FF0000">' +
        workIdAddress +
        "</span>" +
        '<span style="color: #008000">' +
        workIdDate +
        "</span>" +
        '<span style="color: #800080">' +
        workIdSerialNumber +
        "</span>" +
        "</a></div>"
    );
}

// 查看详情
function ckxq(str) {
    layer.open({
        type: 2,
        content: "../CommonHtml/WorkDatil.html?wid=" + str, //iframe的url，no代表不显示滚动条
        title: "工单详情",
        area: ["100%", "100%"], //宽高
        maxmin: true,
        shade: 0,
    });
}

// 省平台签收
function provincialPlatformSignup(str) {
    layer.confirm(
        "确定签收吗？",
        {
            btn: ["是", "否"], //按钮
        },
        function () {
            $.post(
                huayi.config.callcenter_url + "Affairs/SubmitWorkOrder",
                {
                    ids: str,
                    token: $.cookie("token"),
                },
                function (result) {
                    result = JSON.parse(result);
                    if (result.state.toLowerCase() == "success") {
                        layer.msg("操作成功");
                        load();
                    }
                }
            );
        }
    );
}

// 省平台退回
function provincialPlatformReturn(str) {
    layer.open({
        type: 2,
        content: "../CommonHtml/provincialPlatformReturn.html?wid=" + str, //iframe的url，no代表不显示滚动条
        title: "省平台退回",
        area: ["70%", "70%"], //宽高
        shade: 0,
    });
}

// 省平台申请延时
function provincialPlatformApplicationDelay(str,type) {
    layer.open({
        type: 2,
        content:
            "../CommonHtml/provincialPlatformApplicationDelay.html?wid=" + str+"&type="+type, //iframe的url，no代表不显示滚动条
        title: "省平台申请延时",
        area: ["70%", "70%"], //宽高
        shade: 0,
    });
    
}

// 省平台反馈
function provincialPlatformFeedback(str) {
    layer.open({
        type: 2,
        content: "../CommonHtml/provincialPlatformFeedback.html?wid=" + str, //iframe的url，no代表不显示滚动条
        title: "省平台反馈",
        area: ["70%", "70%"], //宽高
        shade: 0,
    });
}

// 服务工单上报
function reportServiceWorkOrder(str) {
    layer.open({
        type: 2,
        content: "../CommonHtml/provincialPlatformReport.html?wid=" + str, //iframe的url，no代表不显示滚动条
        title: "省平台上报",
        area: ["70%", "70%"], //宽高
        shade: 0,
    });
}

// 获取反映类别数据，多级下拉选项
function getReactionCategory(pid = 38) {
    $.get(
        huayi.config.callcenter_url + "Dictionary/GetZTreeNew",
        {
            token: $.cookie("token"),
            pid: pid,
        },
        function (result) {
            result = $.parseJSON(result);
            var content = JSON.parse(result.data);
            layui.use("dropdown", function () {
                var dropdown = layui.dropdown;
                dropdown.render({
                    elem: "#reflectCategory", //可绑定在任意元素中，此处以上述按钮为例
                    data: content,
                    id: "reflectCategory",
                    isclickparent: true,
                    //菜单被点击的事件
                    click: function (obj) {
                        $("#reflectCategory").val(obj.title);
                        $("#keyid").val(obj.id);
                        $(".CleansReactionCategory").show();
                    },
                });
            });
        }
    );
}

// 搜索获取反映类别数据
function getSearchReactionCategory(key) {
    $("#reflectCategoryList").empty();
    $.get(
        huayi.config.callcenter_url + "Dictionary/GetKeyListNew",
        {
            token: $.cookie("token"),
            key: key,
        },
        function (result) {
            result = $.parseJSON(result);
            if (result.state.toLowerCase() === "success") {
                var content = result.data;
                if (content.length > 0) {
                    content.forEach(function (e, i) {
                        $(
                            "<li index='" +
                                e.id +
                                "' indexName='" +
                                e.name +
                                "'>" +
                                e.names +
                                "</li>"
                        ).appendTo("#reflectCategoryList");
                    });
                } else {
                    $("<li index='' indexName=''>没有相关数据</li>").appendTo(
                        "#reflectCategoryList"
                    );
                }
            }
        }
    );
}

// 防抖函数
function debounce(fun, delay) {
    return function (args) {
        var that = this;
        var _args = args;
        clearTimeout(fun.id);
        fun.id = setTimeout(function () {
            fun.call(that, _args);
        }, delay);
    };
}

$(".CleansReactionCategory").click(function () {
    $("#reflectCategory").val("");
    $("#keyid").val("");
    $(".CleansReactionCategory").hide();
});

$("#sponsor").on("click", "li", function () {
    $(".selDpart1").css("display", "none");
    $(".inps").val($(this).html());
    $("#PID").val($(this).attr("index"));
    $("#Dpment").val($(this).attr("index"));
    $(".Cleans").show();
});

///部门
function depart(dept) {
    $("#sponsor").empty();
    $.getJSON(
        huayi.config.callcenter_url + "Department/GetDeptListByDept",
        {
            token: $.cookie("token"),
            dept: dept,
        },
        function (data) {
            if (data.state.toLowerCase() == "success") {
                var content = data.data;
                $(content).each(function (i, n) {
                    $(
                        "<li index='" +
                            n.F_DeptId +
                            "'>" +
                            n.F_DeptName +
                            "</li>"
                    ).appendTo("#sponsor");
                });
            }
        }
    );
}

// 获取角色
function getCharacter(obj) {
    obj.empty();
    obj.append("<option value=''>请选择</option>");
    $.ajax({
        type: "get",
        async: false,
        url: huayi.config.callcenter_url + "RoleInfo/GetRoleList",
        data: {
            pageindex: 1,
            pagesize: 100,
            token: $.cookie("token"),
            name: "", //角色名称
        },
        dataType: "json",
        success: function (data) {
            var data = data.rows;
            $.each(data, function (index, value) {
                obj.append(
                    "<option value='" +
                        value.F_RoleCode +
                        "'>" +
                        value.F_RoleName +
                        "</option>"
                );
            });
        },
    });
}

// 多媒体角色人员
function getMultimediaPersonnel(obj) {
    obj.empty();
    obj.append("<option value=''>请选择</option>");
    $.getJSON(
        huayi.config.callcenter_url + "UserAccount/GetList",
        {
            token: $.cookie("token"),
            rolecode: "23", // 23多媒体
            page: 1,
            pagesize: 10000,
        },
        function (data) {
            var content = data.rows;
            $(content).each(function (i, n) {
                $(
                    "<option value='" +
                        n.F_UserCode +
                        "'>" +
                        n.F_UserName +
                        "</option>"
                ).appendTo(obj);
            });
            obj.selectpicker({
                noneSelectedText: "请选择", //默认显示内容
            });
            obj.selectpicker("refresh");
        }
    );
}

// 导出列表
function exportFileFun(api, params) {
    var url = huayi.config.callcenter_url + api;
    if(typeof(params) !== "object") {
        return
    }
    // 分页为空
    params.page = "";
    params.pagesize = "";
    Object.keys(params).forEach(function(key, index) {
        if (index === 0) {
            url += `?${key}=${params[key]}`
        } else {
            url += `&${key}=${params[key]}`
        }
    })
    console.log((url))
    window.location.href = url;
}
