$(document).ready(function(){ 
  //专业筛选条件
  $("#detail_form").find("select[name='specialid']").change(function(){
    var specialid = $(this).val();
    showXzclassList(specialid);
  });

  //切换角色条件
  $("#detail_form").find("select[name='roleid']").change(function(){
    var roleid = $(this).val();
    onChangeRoleName(roleid);
  });
});

//根据专业切换班级
function showXzclassList(specialid, xzclassid){
  $.ajax({
    url: '/register/xzclasslist', 
    type: 'POST',
    dataType: "html", 
    data: {specialid:specialid, xzclassid:xzclassid},
    success: function(data){
      $("select[name='xzclassid']").html(data);
      form.render('select');
    },
    error: function(){
      layer.msg("系统错误，请联系管理员", {icon:2});
    }
  });
  return false;
}

//切换角色
function onChangeRoleName(roleid){
  //console.log(roleid);
  if(roleid==3 || roleid==4){
    $("#specialtybox").show();
    $("#xzclassbox").show();
  }else{
    $("#specialtybox").hide();
    $("#xzclassbox").hide();
  }
}

//提交表单
$("#finished_button").on("click", function(){
  var roleid = $("#detail_form").find("select[name='roleid']");
  var username = $("#detail_form").find("input[name='username']");
  var password = $("#detail_form").find("input[name='password']");
  var realname = $("#detail_form").find("input[name='realname']");
  var stdnumber = $("#detail_form").find("input[name='stdnumber']");
  var email = $("#detail_form").find("input[name='email']");
  var male = $("#detail_form").find("input[name='male']");
  if($(roleid).val()==""){
    $.toast("请选择角色");
    return false;
  }
  if($(username).val()==""){
    $.toast("请填写用户名");
    return false;
  }
  var regex = /^\s*[A-Za-z0-9_-]{4,20}\s*$/;
  if(!regex.test($(username).val())){
    $.toast("用户名格式错误");
    return false;
  }
  if($(password).val()==""){
    $.toast("请填写密码");
    return false;
  }
  if($(realname).val()==""){
    $.toast("请填写姓名");
    return false;
  }
  var regex = /^\s*[\u4e00-\u9fa5\(\)a-zA-Z0-9_]{2,20}\s*$/;
  if(!regex.test($(realname).val())){
    $.toast("姓名格式错误");
    return false;
  }
  if($(stdnumber).val()==""){
    $.toast("请填写学号");
    return false;
  }
  var regex = /^\s*[A-Za-z0-9_-]{4,20}\s*$/;
  if(!regex.test($(stdnumber).val())){
    $.toast("学号格式错误");
    return false;
  }
  if($(email).val()!=""){
    var regex = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[-A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    if(!regex.test($(email).val())){
      $.toast("邮箱格式错误");
      return false;
    }
  }
  $.ajax({
    url: '/register/basicok', 
    type: 'POST',
    dataType: "json", 
    data: $("#detail_form").serialize(),
    success: function(data){
      if(data.code=="success"){
        $.toast("注册成功");
        setTimeout(function(){window.location.href = "/api/register/waiting";}, 2000);
      }else{
        $.toast(data.msg);
      }
    },
    error: function(){
      $.toast("系统错误，请联系管理员");
    }
  });
  return false;
});