
$(document).ready(function () {
    //信息来源
    $("#xxly").empty();
    $("#xxly").append('<option selected="selected" value="">请选择</option>');
    $.getJSON(huayi.config.callcenter_url + "Dictionary/GetDicValueListById", {
        "token": $.cookie("token"),
        id: 6
    }, function (data) {
        if (data.state.toLowerCase() == "success") {
            var content = data.data;
            $(content).each(function (i, n) {
                $("<option value='" + n.F_ValueId + "'>" + n.F_Value + "</option>").appendTo($("#xxly"));
            })
        }
    });
    //信息分类
    $("#xxlb").empty();
    $("#xxlb").append('<option selected="selected" value="">请选择</option>');
    $.getJSON(huayi.config.callcenter_url + "Dictionary/GetDicValueListById", {
        "token": $.cookie("token"),
        id: 2
    }, function (data) {
        if (data.state.toLowerCase() == "success") {
            var content = data.data;
            $(content).each(function (i, n) {
                $("<option value='" + n.F_ValueId + "'>" + n.F_Value + "</option>").appendTo($("#xxlb"));
            })
        }
        $("#xxlb").change(function () {
            $("#tsxl").empty();
            $("#tsxl").append('<option selected="selected" value="">请选择</option>');
            loadDic($("#tsdl"), $("#xxlb").val());
        });

        $("#tsdl").change(function () {
            loadDic($("#tsxl"), $("#tsdl").val());
        });
    })
    loadArea($("#FArea"));
    loadArea($("#JArea"));
    $("#FArea").change(function () {
        $("#Twon").empty();
        $("#Twon").append('<option selected="selected" value="">请选择</option>');
        var aid = $("#FArea").val();
        if (!aid) { aid = -1; }
        $.getJSON(huayi.config.callcenter_url + "Department/GetTownList", { "token": $.cookie("token"), aid: aid }, function (data) {
            if (data.state.toLowerCase() == "success") {
                var content = data.data;
                $(content).each(function (i, n) {
                    $("<option value='" + n.F_TownId + "'>" + n.F_TownName + "</option>").appendTo($("#Twon"));
                })
            }
        })
    })
    $("#JArea").change(function () {
        $("#Jtype").empty();
        $("#Jtype").append('<option selected="selected" value="">请选择</option>');
        $("#Department").empty();
        $("#Department").append('<option selected="selected" value="">请选择</option>');
        var aid = $("#JArea").val();
        if (!aid) { aid = -1; }
        $.getJSON(huayi.config.callcenter_url + "Department/GetDeptTypeList", { "token": $.cookie("token"), aid: aid }, function (data) {
            if (data.state.toLowerCase() == "success") {
                var content = data.data;
                $(content).each(function (i, n) {
                    $("<option value='" + n.F_DeptTypeId + "'>" + n.F_DeptTypeName + "</option>").appendTo($("#Jtype"));
                })
            }
        })
    })
    $("#Jtype").change(function () {
        $("#Department").empty();
        $("#Department").append('<option selected="selected" value="">请选择</option>');
        var tid = $("#Jtype").val();
        if (!tid) { tid = -1; }
        $.getJSON(huayi.config.callcenter_url + "Department/GetDeptList", { "token": $.cookie("token"), tid: tid }, function (data) {
            if (data.state.toLowerCase() == "success") {
                var content = data.data;
                $(content).each(function (i, n) {
                    $("<option value='" + n.F_DeptId + "'>" + n.F_DeptName + "</option>").appendTo($("#Department"));
                })
            }
        })
    })
    $("#FArea").change(function () {
        $("#sfdz").val($("#FArea").find("option:selected").text());
    })

    $(".min_select").each(function () {
        $(this).empty();
        for (var i = 1; i <= 10; i++) {
            if (i == 5) {
                $("<option selected='selected' value='5'>5</option>").appendTo($(this));
            }
            else {
                $("<option value='" + i + "'>" + i + "</option>").appendTo($(this));
            }
        }
    })
    //上传附件
    $("#scwj").click(function () {
        $("#upFile").trigger("click");
    })
    $("#upFile").change(function () {
        upload();
    })
    $("#scfj").click(function () {
        $(".fjnr").text("");
        $("#scfj").hide();
    })

    //创建工单
    $(".addts").click(function () {
        var source = $("#xxly").val();
        var type = $("#xxlb").val();
        var bigtype = $("#tsdl").val();
        var smalltype = $("#tsxl").val();
        var sourcearea = $("#FArea").val();
        var sourceaddress = $("#sfdz").val();
        var cusname = $("#tskh").val();
        var cusphone = $("#tsdh").val();
        var cusaddress = $("#tsdz").val();
        var email = $("#email").val();
        var zipcode = $("#postcode").val();
        var conname = $("#lxr").val();
        var conphone = $("#lxdh").val();
        var content = $("#content").val();
        var cussex = $("#sex").val();
        var isprotect = $("input[name='baomi']:checked").val();
        var isfile = "false";
        var filename = $(".fjnr").text();
        if (filename) { isfile = "true"; }
        var isresult = "false";
        var flag = 0;
        var hf = $("input[name='huifu']:checked").val();
        if (hf == '1') {
            isresult = "true";
        }
        else if (hf == '2') {
            flag = 1;
        }
        var areaid = $("#JArea").val();
        var depttype = $("#Jtype").val();
        var deptid = $("#Department").val();
        var obj = $("input[name='jiaoban']:checked");
        var opitionid = obj.val();
        var day = obj.parent().find("select").val();
        var assignedopinion = obj.parent().find(".head").text() + day + obj.parent().find(".end").text();
        var remark = $("#remark").val();
        var callid = $(".hidCallID").val();

        //if (!cusname) {
        //    layer.confirm('请输入来电人！', {
        //        btn: ['确定']
        //    });
        //    return;
        //}
        if (!type) {
            layer.confirm('请选择信息类型！', {
                btn: ['确定']
            });
            return;
        }
        if (!content) {
            layer.confirm('请输入情况摘要！', {
                btn: ['确定']
            });
            return;
        }
        if (flag == 1) {
            if (!deptid) {
                layer.confirm('请选择部门！', {
                    btn: ['确定']
                });
                return;
            }
        }
        $.post(huayi.config.callcenter_url + 'WorkOrder/AddWorkOrder', {
            source: source, type: type, bigtype: bigtype, smalltype: smalltype, sourcearea: sourcearea, cusname: cusname, cusphone: cusphone,
            cusaddress: cusaddress, email: email, zipcode: zipcode, conname: conname, conphone: conphone, content: content, isprotect: isprotect,
            isfile:isfile,filename:filename,
            isresult: isresult, cussex: cussex, isfile: "false", sourceaddress: sourceaddress, flag: flag, areaid: areaid, depttype: depttype,
            deptid: deptid, day: day, opitionid: opitionid, assignedopinion: assignedopinion, remark: remark, callid: callid, "token": $.cookie("token")
        }, function (result) {
            result = $.parseJSON(result);
            if (result.state.toLowerCase() == "success") {
                layer.msg("创建工单成功");
                Clean();
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭  
                parent.initTable();
            }
        })
    });
});
//清空
function Clean() {
    $("#xxly").val('');
    $("#xxlb").val('');
    $("#tsdl").val('');
    $("#tsxl").val('');
    $("#FArea").val('');
    $("#sfdz").val('');
    $("#tskh").val('');
    $("#tsdh").val('');
    $("#email").val('');
    $("#postcode").val('');
    $("#lxr").val('');
    $("#lxdh").val('');
    $("#content").val('');
    $("#sex").val('');
    $("#JArea").val('');
    $("#Jtype").val('');
    $("#Department").val('');
    $("#remark").val('');
    $(".fjnr").text("");
    $("#scfj").hide();
}
//加载字典
function loadDic(obj, pid) {
    obj.empty();
    obj.append('<option selected="selected" value="">请选择</option>');
    $.getJSON(huayi.config.callcenter_url + "Dictionary/GetDicValueListByParentId", {
        "token": $.cookie("token"),
        pid: pid
    }, function (data) {
        if (data.state.toLowerCase() == "success") {
            var content = data.data;
            $(content).each(function (i, n) {
                $("<option value='" + n.F_ValueId + "'>" + n.F_Value + "</option>").appendTo(obj);
            })
        }
    })
}
//加载区域
function loadArea(obj) {
    obj.empty();
    obj.append('<option selected="selected" value="">请选择</option>');
    $.getJSON(huayi.config.callcenter_url + "Department/GetAreaList", {
        "token": $.cookie("token")
    }, function (data) {
        if (data.state.toLowerCase() == "success") {
            var content = data.data;
            $(content).each(function (i, n) {
                $("<option value='" + n.F_AreaId + "'>" + n.F_AreaName + "</option>").appendTo(obj);
            })
        }
    })
}
//上传文件
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"));
        var typeName=Files[0].name.split('.')[Files[0].name.split('.').length - 1];
        if (typeName=="png"||typeName=='jpg'||typeName=='mp4'||typeName=='mp3'||typeName=='pdf'||typeName=='doc'||typeName=='docx'||typeName=='xls'||typeName=='xlsx') {
	        $.ajax({
	            url: huayi.config.callcenter_url + "/WorkOrder/UploadFile",
	            type: "POST",
	            data: formData,
	            /**
	            *必须false才会自动加上正确的Content-Type
	            */
	            contentType: false,
	            /**
	            * 必须false才会避开jQuery对 formdata 的默认处理
	            * XMLHttpRequest会对 formdata 进行正确的处理
	            */
	            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") {
	                    $(".fjnr").text(r.data);
	                    $("#scfj").show();
	                }
	            }
	        });
	    }else{
    		layer.msg("格式不正确");
    	}
    }
    else {
        layer.confirm('请上传文件！', {
            btn: ['确定']
        });
    }
}