
$(function() {
    validateRule();
    $('.imgcode').click(function() {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });

    $.ajax({
        type: "POST",
        dataType:"json",
        url: ctx + "system/dept/list",
        data: { parentId: 100 },
        success: function (data) {
            $.each(data, function (index, value) {
                $('#deptId').append("<option value=" + value.deptId + ">" + value.deptName + "</option>");
            });
        }
    })
//    $('#deptId').select2({
//        placeholder: '请选择公司编码',
//        minimumResultsForSearch: -1,
//        ajax: {
//            type: "POST",
//            dataType:"json",
//            url: ctx + "system/dept/list",
//            data: { parentId: 100 },
//            processResults: function (data) {
//                var processedData = [];
//                $.each(data, function (index, value) {
//                    processedData.push({
//                        id: value.deptId,
//                        text: value.deptName
//                    })
//                });
//                return {
//                    results: processedData
//                };
//            },
//            cache: true
//        }
//    });
});

$.validator.setDefaults({
    submitHandler: function() {
    	register();
    }
});

function register() {
    $.modal.loading($("#btnSubmit").data("loading"));
    var username = $.common.trim($("input[name='username']").val());
    var phonenumber = $.common.trim($("input[name='phonenumber']").val());
    var deptId = $.common.trim($('#deptId').val());
    var password = $.common.trim($("input[name='password']").val());
    var validateCode = $("input[name='validateCode']").val();
    $.ajax({
        type: "post",
        url: ctx + "register",
        data: {
            "loginName": username,
            "phonenumber": phonenumber,
            "deptId": deptId,
            "password": password,
            "validateCode": validateCode
        },
        success: function(r) {
            if (r.code == web_status.SUCCESS) {
            	layer.alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", {
            	    icon: 1,
            	    title: "系统提示"
            	},
            	function(index) {
            	    //关闭弹窗
            	    layer.close(index);
            	    location.href = ctx + 'login';
            	});
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$(".code").val("");
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $.validator.addMethod("checkPhone",function(value,element,params){
        var regex = /^1[345678]\d{9}$/;
        return regex.test(value);
    }, "请输入正确的手机号码");
    $("#registerForm").validate({
        rules: {
            username: {
                required: true,
                rangelength: [2,20]
            },
            phonenumber: {
                required: true,
                checkPhone: true
            },
            password: {
                required: true,
                rangelength: [5,20]
            },
            confirmPassword: {
                required: true,
                equalTo: "[name='password']"
            }
        },
        messages: {
            username: {
                required: icon + "请输入用户名",
                rangelength: icon + "账户长度必须在2到20个字符之间"
            },
            phonenumber: {
                required: icon + "请输入手机号码"
            },
            password: {
            	required: icon + "请输入密码",
                rangelength: icon + "密码长度必须在5到20个字符之间"
            },
            confirmPassword: {
                required: icon + "请再次输入密码",
                equalTo: icon + "两次密码输入不一致"
            }
        }
    })
}
