<div class="indexPanel">
  <p class="indexTitle">平台已检测到您的如下信息，完善即可进入比赛</p>
  <div class="indexInfos">

    <%= form_tag('', method: :post, id: 'oauth_form',  class: 'form-inline search-form flex-1', remote: true) do %>
      <%= hidden_field_tag 'callback_url', params[:callback_url] %>
      <div class="indexInfo">
        <span>登录名：</span>
        <%= text_field_tag :login, params[:login], placeholder: '请输入登录名', readonly: true, id: 'login' %>
        <p class="checkInfo loginCheck"><span></span></p>
      </div>
      <div class="indexInfo">
        <span>用户名称：</span>
        <%= text_field_tag :lastname, params[:lastname], placeholder: '登录平台后显示的用户名称', maxlength: 40, id: 'lastname' %>
        <p class="checkInfo lastnameCheck"><span></span></p>
      </div>
      <div class="indexInfo">
        <span>邮箱：</span>
        <%= text_field_tag :mail, params[:mail], placeholder: '请输入绑定邮箱', maxlength: 40, id: 'email' %>
        <p class="checkInfo emailCheck"><span></span></p>
      </div>
      <div class="indexInfo">
        <span>密码：</span>
        <%= password_field_tag :password, '', placeholder: '输入EduCoder账号密码或新的密码',maxlength: 15, id: 'password'  %>
        <p class="checkInfo passwordCheck"><span></span></p>
      </div>
      <div class="indexBtn">
        <button type="button" class="indexSubmit" id="indexSubmit"  onclick="submitdata();">确定</button>
      </div>
    <% end %>
  </div>
</div>
<script type="text/javascript">
  function submitdata(){
    var login = $("#login").val();
    var email = $("#email").val();
    var lastname = $("#lastname").val();
    var password = $("#password").val();

    if(!login){
      $(".loginCheck span").html("请输入账号");
      return;
    }else{
      $(".loginCheck span").html("");
    }
    if(!lastname){
      $(".lastnameCheck span").html("请输入用户名称");
      return;
    }else{
      $(".lastnameCheck span").html("");
    }
    if(!email){
      $(".emailCheck span").html("请输入绑定的邮箱");
      return;
    }else{
      $(".emailCheck span").html("");
    }
    if(!password){
      $(".passwordCheck span").html("请输入账号密码");
      return;
    }else if(password.length < 8){
      $(".passwordCheck span").html("密码最少为8位数");
      return;
    }else{
      $(".passwordCheck span").html("");
    }
    $.ajax({
      url: "<%=  oauth_auto_register_path %>",
      data: $("#oauth_form").serialize(),
      type: 'post',
      dataType: "json",
      success: function(data) {
        console.log(data)
        if (data) {
          if(data.email_exist){
            $(".emailCheck span").html("该邮箱已存在.");
            return;
          }
          // data.redirect contains the string URL to redirect to
          // window.location.href = "<%#= params[:callback_url] %>";
           window.location.href = data.callback_url;
        }
      },
    	error: function (data) {
    		console.log('ajax error handling',data);
    	}

    });
  }
</script>
