<% if @wechat_user %>

    <div class="page">
      <div class="weui_msg">
        <div class="weui_icon_area"><i class="weui_icon_success weui_icon_msg"></i></div>
        <div class="weui_text_area">
          <h2 class="weui_msg_title">操作成功</h2>
          <p class="weui_msg_desc">您已经绑定过微信了,用户名: (<%= @wechat_user.user.login %>)</p>
        </div>
        <div class="weui_opr_area">
          <p class="weui_btn_area">
            <a onclick="WeixinJSBridge.call('closeWindow');" class="weui_btn weui_btn_primary">确定</a>
          </p>
        </div>

      </div>
    </div>

<% else %>
<div class="loginIn">
  <div>
    <p class="weui_cells_title wechat-error">
      <%= @wechat_bind_errors %>
    </p>
  </div>


  <%= form_tag(bind_wechat_path,:id=>'main_login_form',:method=>'post') do %>
      <div class="weui_cells weui_cells_form">
        <div class="weui_cell">
          <div class="weui_cell_hd"><label class="weui_label">用户名</label></div>
          <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" autocapitalize="off" type="text" name="username" placeholder="请输入邮箱/登录名"/>          </div>
        </div>

        <div class="weui_cell">
          <div class="weui_cell_hd"><label class="weui_label">密码</label></div>
          <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="password" name="password" placeholder="请输入密码"/>
          </div>
        </div>

        <div class="weui_btn_area">
          <a class="weui_btn weui_btn_primary" id="submitForm">确定</a>
        </div>

      </div>

  <% end %>

</div>



<script type="text/javascript">
  $(function(){
    console.log("started.");
    $("#submitForm").click(function(){

      var data = {};
      $("#main_login_form").serializeArray().map(function(x){data[x.name] = x.value;});
      console.log(data);

      $.ajax({
        type: "POST",
        url: $("#main_login_form").attr("action"),
        data:data,
        dataType: 'json',
        success: function(data){
          console.log(data);
          if(data.status == 0){
            byConfirm(data.msg, function(){
              window.closeWindow();
            });
          } else {
            byAlert(data.msg, "绑定失败");
          }
        }
      });

    })
  });
</script>


<% end %>