$('.DicSelect').on('click', function () {
    var rootCode = $(this).find("input").eq(1).attr("diccode");
    var code1 = document.getElementsByName("F_DeviceCount")[0].value;
    var name1 = document.getElementsByName("F_Fax")[0].value;

    var code2 = document.getElementsByName("F_AfterSaleNameID")[0].value;
    var name2 = document.getElementsByName("F_Email")[0].value;

    var code3 = document.getElementsByName("F_WORKORDERLEVELID")[0].value;
    var name3 = document.getElementsByName("F_WORKORDERNAME")[0].value;


    layer.open({
        type: 2,
        scrollbar: false,
        content: '/WXInterFace/GetDicSelect/?diccode=' + rootCode + "&code1=" + code1 + "&name1=" + name1 + "&code2=" + code2 + "&name2=" + name2 + "&code3=" + code3 + "&name3=" + name3,
        area: ['700px', '500px'],
        id: 'goodNameSelect',
        title: '选择'

    })
});

function saveParams(code1, name1, code2, name2, code3, name3) {
    document.getElementsByName("F_DeviceCount")[0].value = code1;
    document.getElementsByName("F_Fax")[0].value = name1;

    document.getElementsByName("F_AfterSaleNameID")[0].value = code2;
    document.getElementsByName("F_Email")[0].value = name2;

    document.getElementsByName("F_WORKORDERLEVELID")[0].value = code3;
    document.getElementsByName("F_WORKORDERNAME")[0].value = name3;
}




layui.use(['form', 'layer', 'jquery', 'laydate'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery,
        laydate = layui.laydate;

    //日期
    laydate.render({
        elem: '#date'
    });
    //日期
    laydate.render({
        elem: '#date1'
    });
    //监听提交
    form.on('submit(SubmitData)', function (data) {
        //所有表单数据
        var dataArr = new Array();
        dataArr = data.field;

        //转办
        if (dataArr["F_WORKORDERTYPEID"] == 1) {
            var zbbm = $("#F_DEPTID").find("option:selected").val();
            var zbry = $("#F_CREATEBY").find("option:selected").val();
            if (zbbm == null || zbbm == "") {
                alert("转办部门不能为空!");
                //layui.layer.alert("转办部门不能为空!", {
                //    icon: 2
                //});
                return false;
            }
            if (zbry == null || zbry == "") {
                alert("转办人员不能为空!");
                //layui.layer.alert("转办人员不能为空!", {
                //    icon: 2
                //});
                return false;
            }

            dataArr["F_WORKORDERSTATEID"] = 1;
            dataArr["F_REPAIRREQUEST"] = "处理中";
        }
        else {
            dataArr["F_WORKORDERSTATEID"] = 2;
            dataArr["F_REPAIRREQUEST"] = "已办结";
        }

        //业务类型名称
        dataArr["F_ADSLACCOUNT"] = $("#F_ADSLACCOUNT").find("option:selected").text();

        //业务类别
        dataArr["F_FILEFLAG"] = $("#F_ADSLACCOUNT").find("option:selected").attr("parentid");
        //业务类别名称
        dataArr["F_HOUSING"] = $("#F_ADSLACCOUNT").find("option:selected").attr("parentname");

        //投诉类回复时间不能为空
        if (dataArr["F_FILEFLAG"] != null && dataArr["F_FILEFLAG"] != "" && dataArr["F_FILEFLAG"] == 3) {
            if (dataArr["F_RETURNVISITTIME"] == null || dataArr["F_RETURNVISITTIME"] == "") {
                layui.layer.alert("计划回访时间不能为空!", {
                    icon: 2
                    ,offset: '40%'
                });
                return false;
            }
        }



        //转办部门
        dataArr["F_DEPTCODE"] = $("#F_DEPTID").find("option:selected").text();
        //转办人员
        dataArr["F_USERNAME"] = $("#F_CREATEBY").find("option:selected").text();

        //是否回访
        if (dataArr["F_RETURNVISITFLAG"] == "on") {
            dataArr["F_RETURNVISITFLAG"] = 1;
        }
        else {
            dataArr["F_RETURNVISITFLAG"] = 0;
        }
        //来电号码
        dataArr["F_CUSTOMERTELEPHONE"] = $("#F_Mobile").val();

        //客户信息
        var costomInfoModle = {
            F_CustomerId: $("#F_CustomerId").val(),
            F_CustomerName: $("#F_CustomerName").val(),
            F_CustomerPym: $('.sexParam input[name="F_CustomerPym"]:checked ').val(),
            F_CustomerCode: $("#F_CustomerCode").val(),
            F_Telephone: $("#F_Telephone").html().trim(),
            F_CustomerEName: $("#F_CustomerEName").html(),
            F_PostCode: $("#F_PostCode").html(),
            F_Mobile: $("#F_Mobile").val().trim(),
            F_DeviceCount: $("#F_DeviceCount").val(),
            F_Fax: $("#F_Fax").val(),
            F_AfterSaleNameID: $("#F_AfterSaleNameID").val(),
            F_Email: $("#F_Email").val()
        }

        dataArr["CustomerBaseModel"] = costomInfoModle;



        $.ajax({
            url: "/WXInterFace/workOrderAddData",
            type: "post",
            async: false,//同步请求
            contentType: "application/json",
            dataType: "text",
            cache: false,
            data: JSON.stringify(dataArr),
            // data: dataArr,
            success: function (thisResult, result) {

                if (thisResult == "True") {
                    alert("提交成功");
                    window.open('', '_top');
                    window.top.close();
                    //layer.open({
                    //    content: '提交成功'
                    //                        , btn: ['确定']
                    //                        , yes: function (index, layero) {
                    //                            window.open('', '_top');
                    //                            window.top.close();
                                            

                    //                        }
                    //                           , cancel: function () {
                    //                               window.open('', '_top');
                    //                               window.top.close();

                    //                           }
                    //});
                    return false;
                } else {
                    alert("提交失败");
                    //layui.layer.alert("提交失败!", {
                    //    icon: 2
                    //});

                    return false;
                }

            },
            error: function (error) {
                return false;
            }
        });
        return false;
    });
    //监听保存
    form.on('submit(SubmitSaveData)', function (data) {
        //所有表单数据
        var dataArr = new Array();
        dataArr = data.field;
        var zbbm = $("#F_DEPTID").find("option:selected").val();
        var zbry = $("#F_CREATEBY").find("option:selected").val();
        dataArr["F_WORKORDERSTATEID"] = 0;
        dataArr["F_REPAIRREQUEST"] = "待处理";

        //业务类型名称
        dataArr["F_ADSLACCOUNT"] = $("#F_ADSLACCOUNT").find("option:selected").text();

        //业务类别
        dataArr["F_FILEFLAG"] = $("#F_ADSLACCOUNT").find("option:selected").attr("parentid");
        //业务类别名称
        dataArr["F_HOUSING"] = $("#F_ADSLACCOUNT").find("option:selected").attr("parentname");


        //转办部门
        dataArr["F_DEPTCODE"] = $("#F_DEPTID").find("option:selected").text();
        //转办人员
        dataArr["F_USERNAME"] = $("#F_CREATEBY").find("option:selected").text();

        //是否回访
        if (dataArr["F_RETURNVISITFLAG"] == "on") {
            dataArr["F_RETURNVISITFLAG"] = 1;
        }
        else {
            dataArr["F_RETURNVISITFLAG"] = 0;
        }
        //来电号码
        dataArr["F_CUSTOMERTELEPHONE"] = $("#F_Mobile").val();

        //客户信息
        var costomInfoModle = {
            F_CustomerId: $("#F_CustomerId").val(),
            F_CustomerName: $("#F_CustomerName").val(),
            F_CustomerPym: $('.sexParam input[name="F_CustomerPym"]:checked ').val(),
            F_CustomerCode: $("#F_CustomerCode").val(),
            F_Telephone: $("#F_Telephone").html().trim(),
            F_CustomerEName: $("#F_CustomerEName").html(),
            F_PostCode: $("#F_PostCode").html(),
            F_Mobile: $("#F_Mobile").val().trim(),
            F_DeviceCount: $("#F_DeviceCount").val(),
            F_Fax: $("#F_Fax").val(),
            F_AfterSaleNameID: $("#F_AfterSaleNameID").val(),
            F_Email: $("#F_Email").val()
        }



        dataArr["CustomerBaseModel"] = costomInfoModle;
        $.ajax({
            url: "/WXInterFace/workOrderAddData",
            type: "post",
            async: false,//同步请求
            contentType: "application/json",
            dataType: "text",
            cache: false,
            data: JSON.stringify(dataArr),
            // data: dataArr,
            success: function (thisResult, result) {

                if (thisResult == "True") {
                    alert("保存成功");
                    window.open('', '_top');
                    window.top.close();
                    //layer.open({
                    //    content: '保存成功'
                    //    , btn: ['确定']
                    //    , yes: function (index, layero) {

                    //        window.open('', '_top');
                    //        window.top.close();

                    //    }
                    //       , cancel: function () {
                    //           window.open('', '_top');
                    //           window.top.close();

                    //       }
                    //});
                    return false;
                } else {
                    alert("保存失败");
                    //layui.layer.alert("保存失败!", {
                    //    icon: 2
                    //});

                    return false;
                }

            },
            error: function (error) {
                return false;
            }
        });
        return false;
    });




    //.监听当前 select，动态改变子项
    form.on('select(busstype)', function (d) {
        $.ajax({
            url: "/WXInterFace/GetTypeContenet",
            type: "get",
            contentType: "application/json",
            dataType: "text",
            cache: false,
            data: { "bussnessType": d.value, "creatDateTime": $("#creatDateTime").val() },
            success: function (result, status) {
                if (status) {

                    $("#bussContent").text(result);

                }


            }
        });
    });

    //.监听当前 select，动态改变子项
    form.on('select(fcoptype)', function (d) {
        $.ajax({
            url: "/WXInterFace/GetDeptUsers",
            type: "get",
            contentType: "application/json",
            dataType: "text",
            cache: false,
            data: { "deptId": d.value },
            success: function (result, status) {
                if (status) {
                    $("#F_CREATEBY").empty();
                    $("#F_CREATEBY").append("<option value='' ></option>");
                    var list = eval("(" + result + ")");
                    if (list.length > 0) {
                        for (var i = 0; i < list.length; i++) {


                            $("#F_CREATEBY").append("<option  value='" + list[i].F_UserId + "'>" + list[i].F_UserName + "</option>");

                        }
                    }

                    form.render('select');
                }
                else {
                    layui.layer.alert("获取该部门下用户信息失败!", {
                        icon: 2
                        ,offset: '40%'
                    });
                }

            }
        });
    });

})


//关闭当前页
$('.closeThisPage').on('click', function () {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
});
