$(function () {
  // $(".header_root").load("./comm/header.html");
  $(".footer_root").load("./comm/footer.html");
  // nav传值显示下划线
  // $(".header_root").data("nav_info", 4);
  $(".err_account").hide();
  $(".err_phone").hide();
  $(".err_mailbox").hide();
  //姓名
  $("#account").blur(() => {
    let name = $("#account").val();
    if (!name) {
      $(".err_account").show();
    }
  });
  $("#account").focus(() => {
    $(".err_account").hide();
  });
  //联系电话
  $("#phone").blur(() => {
    let phone = $("#phone").val();
    if (!phone) {
      $(".err_phone").show();
    }
  });
  $("#phone").focus(() => {
    $(".err_phone").hide();
  });
  var mailboxf = true;
  //邮箱
  $("#mailbox").blur(() => {
    let mailbox = $("#mailbox").val();
    mailboxf = false;
    var filter =
      /^[\w\-\.]+@[a-z0-9]+(\-[a-z0-9]+)?(\.[a-z0-9]+(\-[a-z0-9]+)?)*\.[a-z]{2,4}$/i;
    let blnTest = filter.test(mailbox);
    if (mailbox && !blnTest) {
      $(".err_mailbox").show();
      mailboxf = true;
    }
  });
  $("#mailbox").focus(() => {
    $(".err_mailbox").hide();
  });
  $("#mailbox").bind("input propertychange change", function (event) {
    let mailbox = $("#mailbox").val();
    mailboxf = false;
    var filter =
      /^[\w\-\.]+@[a-z0-9]+(\-[a-z0-9]+)?(\.[a-z0-9]+(\-[a-z0-9]+)?)*\.[a-z]{2,4}$/i;
    let blnTest = filter.test(mailbox);
    if (mailbox && !blnTest) {
      $(".err_mailbox").show();
      mailboxf = true;
    } else {
      $(".err_mailbox").hide();
    }
  });
  //提交
  var Loading = false;

  $(".btn").click(() => {
    if (Loading) {
      return;
    }
    let name = $("#account").val();
    if (!name) {
      $(".err_account").show();
      return;
    }
    let phone = $("#phone").val();
    if (!phone) {
      $(".err_phone").show();
      return;
    }
    if (mailboxf) {
      //邮箱不合法
      return;
    }
    var data = {
      account: $("#account").val(),
      phone: $("#phone").val(),
      mailbox: $("#mailbox").val(),
      comm: $("#comm").val()
    };
    Loading = true;
    //7.1.2 申请试用--添加
    $.ajax({
      type: "POST",
      url: $.baseUrl + "fc/add",
      dataType: "json",
      async: false,
      data: JSON.stringify(data),
      contentType: "application/json; charset=utf-8",
      success: function (data) {
        Loading = false;
        if (data.code === 0) {
          alert("申请成功");
          $("#account").val("");
          $("#phone").val("");
          $("#mailbox").val("");
          $("#comm").val("");
          $(".err_account").hide();
          $(".err_phone").hide();
          $(".err_mailbox").hide();
        } else {
          alert(data.msg);
        }
      }
    });
  });
});
