$(function () {
    helper.getDropList.getOrderAreas($("#sourceAreas"));
    ZX($("#ZX"));
    LY($("#source"));
    LB($("#type"));
    ZT($("#keyid"));
    yardman($("#yardman"),"ZXLD");
    yardman($("#dbyardman"),"DBZY");
    yardman($("#auditorMultimediaPersonnel"),"ZXLD"); 
    tree();
    treeReactionCategory();
    //所属分类下拉
    $(".inps").focus(function () {
        $(".xlAdd").css("display", "block");
    });
    $(".xl").click(function () {
        if ($(".xlAdd").css("display") == "block") {
            $(".xlAdd").css("display", "none");
        } else {
            $(".xlAdd").css("display", "block");
        }
    });
    $(".addTree").mouseleave(function () {
        $(this).css("display", "none");
    });
    //清除
    $(".Cleans ").click(function () {
        $(".inps").val("");
        $("#PID").val("");
        $("#Dpment").val("");
    });

    //所属分类下拉
    $(".inpsReactionCategory").focus(function () {
        $(".xlAddReactionCategory").css("display", "block");
    });
    $(".xlReactionCategory").click(function () {
        if ($(".xlAddReactionCategory").css("display") == "block") {
            $(".xlAddReactionCategory").css("display", "none");
        } else {
            $(".xlAddReactionCategory").css("display", "block");
        }
    });
    $(".addTreeReactionCategory").mouseleave(function () {
        $(this).css("display", "none");
    });
    //清除
    $(".CleansReactionCategory ").click(function () {
        $(".inpsReactionCategory").val("");
        $("#keyid").val("");
    });

    //键盘登录事件
    $("input").bind("keypress", function (event) {
        if (event.keyCode == "13") {
            $(".Seach").trigger("click");
        }
    });
    ///搜素
    $(".Seach").click(function () {
        load();
    });
    //导出
    // $(".Export").click(function () {
    //     dcexcel(this);
    // });
});

///坐席
function ZX(obj) {
    obj.empty();
    $.getJSON(
        huayi.config.callcenter_url + "UserAccount/GetSeatList",
        {
            token: $.cookie("token"),
        },
        function (data) {
            if (data.state.toLowerCase() == "success") {
                var content = data.data;
                $(content).each(function (i, n) {
                    $(
                        "<option value='" +
                            n.F_UserCode +
                            "'>" +
                            n.F_UserName +
                            "</option>"
                    ).appendTo(obj);
                });
                obj.selectpicker({
                    noneSelectedText: "请选择", //默认显示内容
                });
                obj.selectpicker("refresh");
            }
        }
    );
}

//调度员
function yardman(obj,rolecode) {
    obj.empty();
    $.getJSON(
        huayi.config.callcenter_url + "UserAccount/GetUsersList",
        {
            rolecode: rolecode,
            token: $.cookie("token"),
        },
        function (data) {
            if (data.state.toLowerCase() == "success") {
                var content = data.data;
                $("<option value=''>请选择</option>").appendTo(obj)
                $(content).each(function (i, n) {
                    $(
                        "<option value='" +
                            n.F_UserCode +
                            "'>" +
                            n.F_UserName +
                            "</option>"
                    ).appendTo(obj);
                });
                obj.selectpicker({
                    noneSelectedText: "请选择", //默认显示内容
                });
                obj.selectpicker("refresh");
            }
        }
    );
}
function dbman(obj,rolecode) {
    obj.empty();
    $.getJSON(
        huayi.config.callcenter_url + "UserAccount/GetUsersList",
        {
            rolecode: rolecode,
            token: $.cookie("token"),
        },
        function (data) {
            if (data.state.toLowerCase() == "success") {
                var content = data.data;
                $("<option value=''>请选择</option>").appendTo(obj)
                $(content).each(function (i, n) {
                    $(
                        "<option value='" +
                            n.F_UserCode +
                            "'>" +
                            n.F_UserName +
                            "</option>"
                    ).appendTo(obj);
                });
                obj.selectpicker({
                    noneSelectedText: "请选择", //默认显示内容
                });
                obj.selectpicker("refresh");
            }
        }
    );
}
//审核
function audit(str) {
    layer.confirm(
        "确认通过审核吗？",
        {
            btn: ["是", "否"], //按钮
        },
        function () {
            $.post(
                huayi.config.callcenter_url + "WorkOrder/AuditWorkOrder ",
                {
                    workorderid: str,
                    token: $.cookie("token"),
                },
                function (result) {
                    result = JSON.parse(result);
                    if (result.state.toLowerCase() == "success") {
                        layer.msg("操作成功");
                        load();
                    }
                }
            );
        }
    );
}
//转督办
function zdb(str) {
    layer.confirm(
        "确定督办吗？",
        {
            btn: ["是", "否"], //按钮
        },
        function () {
            $.post(
                huayi.config.callcenter_url + "WorkOrder/RemindWorkOrder",
                {
                    workorderid: str,
                    type: 1,
                    state: 0,
                    token: $.cookie("token"),
                },
                function (result) {
                    result = JSON.parse(result);
                    if (result.state.toLowerCase() == "success") {
                        layer.msg("操作成功");
                        load();
                    }
                }
            );
        }
    );
}

function dcexcel(obj) {
    //坐席
    var usercodeID = "";
    var usercode_ = $("#ZX").val(); //坐席
    if (usercode_ != null) {
        $(usercode_).each(function (i, n) {
            var obj1 = "";
            obj1 = n + ",";
            usercodeID += obj1;
        });
    } else {
        usercodeID = "";
    }
    //	var sponsorID = "";
    //	var sponsor = $("#sponsor").text(); //坐席
    //	if(sponsor != null) {
    //		sponsorID = $("#sponsor").attr();
    //	} else {
    //		sponsorID = ""
    //	}
    //调度员
    var ddusercodeID = "";
    var ddusercode_ = $("#yardman").val(); //坐席
    if (ddusercode_ != null) {
        $(ddusercode_).each(function (i, n) {
            var obj2 = "";
            obj2 = n + ",";
            ddusercodeID += obj2;
        });
    } else {
        ddusercodeID = "";
    }
    var startTime;
    var endTime;
    if ($("#starttime").val()) {
        startTime = $("#starttime").val();
        endTime = $("#endtime").val();
    } else {
        startTime = "";
        endTime = "";
    }
    var ComTitle = $("#workOrderTitle").val().replace(/\s+/g, ""); // 工单标题
    var ComContent = $("#workOrderContent").val().replace(/\s+/g, ""); // 工单内容
    var Result = $("#workOrderProcessingResult").val().replace(/\s+/g, ""); // 办理结果
    var dealstarttime = $("#dealstarttime").val(); // 办理开始时间
    var dealstrendtime = $("#dealstrendtime").val(); // 办理结束时间
    var isopen = $("#isopen").val(); // 是否公开
    var phoneType = $("#phoneType").val() == undefined ? "" : $("#phoneType").val();
    var jianhang = $("#jianhang").val() == undefined ? "" : $("#jianhang").val();
    var dbgd = $("#whetherSupervisor").val(); // 是否督办

    var url = huayi.config.callcenter_url + "WorkOrder/GetList?token=" + $.cookie("token") + "&isdc=1";
    url +=
        "&starttime=" +
        startTime +
        "&endtime=" +
        endTime +
        "&workid=" +
        $("#wids").val() +
        "&state=" +
        $("#state").val() +
        "&type=" +
        $("#type").val() +
        "&tel=" +
        $("#tel").val() +
        "&name=" +
        $("#name").val() +
        "&keyid=" +
        $("#keyid").val() +
        "&source=" +
        $("#source").val() +
        "&usercode=" +
        usercodeID +
        "&ddusercode=" +
        ddusercodeID +
        "&deptid=" +
        $("#Dpment").val() +
        "&type=" +
        $("#type").val() +
        "&dealtype=" +
        $("#dealtype").val() +
        "&sourcearea=" +
        $("#sourceAreas").val() +
        "&ComTitle=" +
        ComTitle +
        "&ComContent=" +
        ComContent +
        "&Result=" +
        Result +
        "&dealstarttime=" +
        dealstarttime +
        "&dealstrendtime=" +
        dealstrendtime +
        "&isopen=" + 
        isopen + 
        "&dbgd=" + 
        dbgd + 
        "&phoneType=" + 
        phoneType +
        "&jianhang=" + 
        jianhang
    window.location.href = url;
    //obj.href = url;
}

function treeReactionCategory() {
    $.get(
        huayi.config.callcenter_url + "Dictionary/GetZTreeListNew",
        {
            token: $.cookie("token"),
            pid: 38,
        },
        function (result) {
            result = $.parseJSON(result);
            $.fn.zTree.init(
                $("#addTreeDemoReactionCategory"),
                settingReactionCategory,
                result.data
            ); //实例化树形图
        }
    );
}

var settingReactionCategory = {
    data: {
        key: {
            name: "text",
        },
        simpleData: {
            enable: true,
            idKey: "id",
            rootPId: 0,
        },
    },
    callback: {
        onClick: zTreeOnClicksettingReactionCategory,
    },
};

function zTreeOnClicksettingReactionCategory(event, treeId, treeNode) {
    console.log(treeNode.children);
    // if(!treeNode.children) {
    $(".inpsReactionCategory").val(treeNode.text);
    $("#keyid").val(treeNode.id);
    $(".CleansReactionCategory").show();
    // }else{
    // 	layer.msg("请选择最后一级");
    // }
}