var Register = {};

var enterpriseCountDown = 0;
var enterpriseSmsBtn = $("#enterprise_sms_btn");
var persionCountDown = 0;
var psSmsBtn = $("#ps_sms_btn");


Register.enterpriseUserRegister = function () {
    $("#agentPhone").removeAttr("disabled");
    var username = $("#username").val();
    var name = $("#name").val();
    var idCard = $("#idCard").val();
    var agentName = $("#agentName").val();
    var agentPhone = $("#agentPhone").val();
    var legal = $("#legal").val();
    var street = $("#street").val();
    var address = $("#address").val();
    var type = $("#type").val();
    var agencyType = $("#agencyType").val();
    var special = $("#special").val();
    var agentEmail = $("#agentEmail").val();
    var ephone = $("#ephone").val();
    var bankCard = $("#bankCard").val();
    var bank = $("#bank").val();
    var bankNetwork = $("#bankNetwork").val();
    if (username == null || username == '') {
        Feng.info("请填写账号!");
        return;
    }
    if (username.length > 50) {
        Feng.info("账号最多50个字符!");
        return;
    }
    if (name == null || name == '') {
        Feng.info("请填写单位名称!");
        return;
    }
    if (name.length > 100) {
        Feng.info("单位名称最多100个字符!");
        return;
    }
    if (idCard == null || idCard == '') {
        Feng.info("请填写统一社会信用代码!");
        return;
    }
    if (type == null || type == '') {
        Feng.info("请选择申报类型!");
        return;
    }
    if (idCard.length > 40) {
        Feng.info("统一社会信用代码最多40个字符!");
        return;
    }
    if (agentName == null || agentName == '') {
        Feng.info("请填写人才联络员!");
        return;
    }
    if (agentName.length > 30) {
        Feng.info("人才联络员最多30个字符!");
        return;
    }
    if (agentPhone == null || agentPhone == '') {
        Feng.info("请填写人才联络员电话!");
        return;
    }
    if (ephone == null || ephone == '') {
        Feng.info("请填写单位电话");
        return;
    }
    if (legal == null || legal == '') {
        Feng.info("请填写法人代表!");
        return;
    }
    if (street == null || street == '') {
        Feng.info("请选择所属街道!");
        return;
    }
    if (address == null || address == '') {
        Feng.info("请填写单位地址!");
        return;
    }
    if (bankCard == null || bankCard == '') {
        Feng.info("请填写企业银行账号!");
        return;
    }
    if (bank == null || bank == '') {
        Feng.info("请填写企业开户银行!");
        return;
    }
    if (bankNetwork == null || bankNetwork == '') {
        Feng.info("请填写企业开户银行网点!");
        return;
    }

    if (type == null || type == '') {
        Feng.info("请选择企业类别");
        return;
    }
    if (agentEmail == null || agentEmail == '') {
        Feng.info("请填写电子邮箱!");
        return;
    }
    if (special == 0 && type == 1) {
        if (agencyType == null || agencyType == '') {
            Feng.info("请选择机构类型");
            return;
        }
        var industryFieldNew = $("#industryFieldNew").val();
        var industryFieldOld = $("#industryFieldOld").val();
        var enterpriseTag = $("#enterpriseTag").val();
        var enterpriseType = $("#enterpriseType").val();

        switch (agencyType) {
            case "1":
                if (industryFieldNew == null || industryFieldNew == '') {
                    Feng.info("请选择产业领域");
                    return;
                }
                if (industryFieldOld == null || industryFieldOld == '') {
                    Feng.info("请选择行业领域");
                    return;
                }
                break;
            case "2":
                break;
        }
        if (enterpriseTag == null || enterpriseTag == '') {
            Feng.info("请选择企业标签!");
            return;
        }
        if (enterpriseType == null || enterpriseType == '') {
            Feng.info("请选择企业类型!");
            return;
        }
    }
    if (!/(^(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})))$/.test(ephone)) {
        Feng.info("单位电话格式不合法!");
        return;
    }
    if (!/(^(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})))$/.test(agentPhone)) {
        Feng.info("人才联络员电话格式不合法!");
        return;
    }
    if (!/[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/.test(agentEmail)) {
        Feng.info("电子邮箱格式不合法!");
        return;
    }
    $("#ep_form")[0].submit();
};

Register.changeEnterpriseType = function () {
    var enterpriseType = $("#enterpriseType").val();
    switch (enterpriseType) {
        case "guishang":
        case "gaoxinjishu":
        case "zhuanjingtexin":
            $("td.typeUploader").css("display", "table-cell");
            break;
        default:
            $("td.typeUploader").css("display", "none");
            break;
    }
};

/**
 * 企业用户注册回调
 */
Register.epCallBack = function (data) {
    $("#agentPhone").prop("disabled", true);
    Feng.info(data.msg);
    if (data.code == 200) {
        setInterval(function () {
            window.location.href = Feng.ctxPath + "/";
        }, 3000);
    }
};
Register.typeChange = function () {
    var type = $("#type").val();
    if (type == 1) {
        $(".agencyType").attr("style", "display:block");
    } else {
        $(".agencyType").attr("style", "display:none");
        $("#typeGroup").attr("style", "display:none");
        $("#enterpriseTag,#industryFieldNew,#industryFieldOld,#enterpriseType").val("");
    }
}
Register.agencyTypeChange = function () {
    var agencyType = $("#agencyType").val();
    switch (agencyType) {
        case "1":
            $("#typeGroup").attr("style", "display:block");
            $(".agencyType1").attr("style", "");
            $(".agencyType2").attr("style", "");
            break;
        case "2":
            $("#typeGroup").attr("style", "display:block");
            $(".agencyType1").attr("style", "display:none");
            $(".agencyType2").attr("style", "");
            $("#industryFieldNew").val("");
            $("#industryFieldOld").val("");
            break;
        default:
            $("#typeGroup").attr("style", "display:none");
            break;
    }
}
Register.industryChange = function () {
    var industryNew = $("#industryFieldNew").val();
    var arr = [{"name": "industryFieldOld", "code": industryNew + "_field"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
}
$("#imgurl,#bankImg,#beian,#domainImg,#typeImg").change(function (e) {
    var that = this;
    if (!Feng.chkFileInvalid(that.files[0], 5, 10, ["image/jpeg", "image/gif", "image/png", "image/jpg", "application/pdf"], "只允许上传PDF或图片")) {
        return;
    }
    var file = that.files[0];
    var imgSrc;
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function () {
        if (Feng.isImg(file.type)) {
            imgSrc = this.result;
            $(that).prev("img").attr("src", imgSrc);
        } else {
            $(that).prev("img").attr("src", Feng.ctxPath + "/static/img/Pdf.png");
        }
    };
});
Register.setFieldDisabled = function () {
    //注释注册驳回$("#ep_form :input").not("button").prop("disabled", true);
    var fields = $("#fields").val().split(",");
    for (var i in fields) {
        $("#" + fields[i]).removeAttr("disabled");
    }
    var files = $("#files").val().split(",");
    for (var i in files) {
        switch (files[i]) {
            case "imgurl":
                $("#imgurl").removeAttr("disabled");
                var html = $("#imgurl").parent().find("label").html() + "<br><span style=\"color:red;\">(可修改)</span>";
                $("#imgurl").parent().find("label").html(html);
                break;
            case "bankImg":
                $("#bankImg").removeAttr("disabled");
                var html = $("#bankImg").parent().find("label").html() + "<br><span style=\"color:red;\">(可修改)</span>";
                $("#bankImg").parent().find("label").html(html);
                break;
            case "domainImg":
                $("#domainImg").removeAttr("disabled");
                var html = $("#domainImg").parent().find("label").html() + "<br><span style=\"color:red;\">(可修改)</span>";
                $("#domainImg").parent().find("label").html(html);
                break;
            case "typeImg":
                $("#typeImg").removeAttr("disabled");
                var html = $("#typeImg").parent().find("label").html() + "<br><span style=\"color:red;\">(可修改)</span>";
                $("#typeImg").parent().find("label").html(html);
                break;
            case "beian":
                $("#beian").removeAttr("disabled");
                var html = $("#beian").parent().find("label").html() + "<br><span style=\"color:red;\">(可修改)</span>";
                $("#beian").parent().find("label").html(html);
                break;
        }
    }
}

$(function () {
    var industryNew = $("#industryFieldNew").attr("value");
    var arr = [
        {"name": "street", "code": "street"},
        {"name": "enterpriseTag", "code": "enterprise_tag"},
        {"name": "organizationTag", "code": "organization_tag"},
        {"name": "institutionTag", "code": "institution_tag"},
        {"name": "enterpriseType", "code": "enterprise_type"},
        {"name": "industryFieldNew", "code": "industry_field"},
        {"name": "industryFieldOld", "code": industryNew + "_field"},
        {"name": "agencyType", "code": "agency_type"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
    $("select").each(function () {
        $(this).val($(this).attr("value"));
        $(this).trigger("change");
    });
    Register.setFieldDisabled();
});