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 type = $("#type").val();
    var special = $("#special").val();
    var enterpriseTypeName = "医院";
    var idCardName = "登记号";
    var username = $("#username").val();
    var name = $("#name").val();
    var idCard = $("#idCard").val();
    var legal = $("#legal").val();
    var ephone = $("#ephone").val();
    var medicalCommunityId = $("#medicalCommunityId").val();
    var isGeneral = $("#isGeneral").val();
    var province = $("#province").val();
    var city = $("#city").val();
    var county = $("#county").val();
    var address = $("#address").val();
    var bankCard = $("#bankCard").val();
    var bank = $("#bank").val();
    var bankNetwork = $("#bankNetwork").val();
    var agentName = $("#agentName").val();
    var agentPhone = $("#agentPhone").val();
    var agentEmail = $("#agentEmail").val();
    if (username == null || username == '') {
        Feng.info("请填写账号!");
        return;
    }
    if (username.length > 50) {
        Feng.info("账号最多50个字符!");
        return;
    }
    if (name == null || name == '') {
        Feng.info("请填写" + enterpriseTypeName + "名称!");
        return;
    }
    if (name.length > 100) {
        Feng.info(enterpriseTypeName + "名称最多100个字符!");
        return;
    }
    if (idCard == null || idCard == '') {
        Feng.info("请填写" + idCardName + "!");
        return;
    }
    if (idCard.length > 40) {
        Feng.info(idCardName + "最多40个字符!");
        return;
    }
    if (legal == null || legal == '') {
        Feng.info("请填写法人代表!");
        return;
    }
    if (ephone == null || ephone == '') {
        Feng.info("请填写" + enterpriseTypeName + "电话");
        return;
    }
    if (medicalCommunityId == null || medicalCommunityId == '') {
        Feng.info("请选择医共体");
        return;
    }
    if (isGeneral == null || isGeneral == '') {
        Feng.info("请选择是否总院");
        return;
    }
    if (province == null || province == '') {
        Feng.info("请选择" + enterpriseTypeName + "所在省份!");
        return;
    }
    if (city == null || city == '') {
        Feng.info("请选择" + enterpriseTypeName + "所在市!");
        return;
    }
    if ((county == null || county == '') && $("#county option").length > 1 && !$("#county").val()) {
        Feng.info("请选择" + enterpriseTypeName + "所在县\区!");
        return;
    }
    if (address == null || address == '') {
        Feng.info("请填写" + enterpriseTypeName + "地址!");
        return;
    }
    if (bankCard == null || bankCard == '') {
        Feng.info("请填写银行账号!");
        return;
    }
    if (bank == null || bank == '') {
        Feng.info("请填写开户银行!");
        return;
    }
    if (bankNetwork == null || bankNetwork == '') {
        Feng.info("请填写开户银行网点!");
        return;
    }
    if (agentName == null || agentName == '') {
        Feng.info("请填写人才联络员!");
        return;
    }
    if (agentName.length > 30) {
        Feng.info("人才联络员最多30个字符!");
        return;
    }
    if (agentPhone == null || agentPhone == '') {
        Feng.info("请填写人才联络员电话!");
        return;
    }

    if (agentEmail == null || agentEmail == '') {
        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(enterpriseTypeName + "电话格式不合法!");
        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.epCallBack = function (data) {
    $("#agentPhone").prop("disabled", true);
    Feng.info(data.msg);
    if (data.code == 200) {
        setInterval(function () {
            window.location.href = Feng.ctxPath + "/";
        }, 3000);
    }
};
$("#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;
        }
    }
}

Register.afterSelectProvince = function () {
    var province = $("#province").val();
    $("#city").empty();
    $("#county").empty();
    if (province == null || province == '') {
        return;
    }
    Feng.addAjaxSelect({
        "id": "city",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/tool/findCityByProvinceSelect/code/" + province
    });
}
/**
 * 加载县
 */
Register.afterSelectCity = function () {
    var city = $("#city").val();
    $("#county").empty();
    if (city == null || city == '') {
        return;
    }
    Feng.addAjaxSelect({
        "id": "county",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/tool/findCountyByCitySelect/code/" + city
    });
}

$(function () {
    //加载省份
    Feng.addAjaxSelect({
        "id": "province",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/tool/getProvinceSelect"
    });

    $("select").each(function () {
        $(this).val($(this).attr("value"));
        $(this).trigger("change");
    });
    Register.setFieldDisabled();
});