var state = 0; //1：默认地址 0：非默认地址
var constate;//2表示从确认订单进来
var product;//商品ID

var toast = new auiToast();

/**
 * 是否设置为默认收货地址
 */
function setState() {
  if (state == 0) {
    state = 1;
  } else {
    state = 0;
  }
}

/**
 * 提交新增地址
 */
function submit() {
  var name = $("#name").val();
  var phone = $("#phone").val();
  var address = $("#address").val();

  if (!name) {
    toast.fail({
      title: "请输入收货人姓名",
      duration: 2000
    });
    return;
  } else if (!phone) {
    toast.fail({
      title: "请输入手机号",
      duration: 2000
    });
    return;
  } else if (!isPoneAvailable(phone)) {
    toast.fail({
      title: "请输入正确的手机号码",
      duration: 2000
    });
    return;
  } else if (!address) {
    toast.fail({
      title: "请输入收货地址",
      duration: 2000
    });
    return;
  } else {
    toast.loading({
      title:"加载中",
      duration:2000
  })
    _vp.AddressOper(
      {
        type: 1,
        user_name: name,
        phone: phone,
        province: "",
        city: "",
        area: "",
        address: address,
        state: state
      },
      function(res) {
        toast.hide();
        if (res.status == 2001) {
          //需要登录
          top.location.href = "login.html";
          return;
        } else if (res.status == -1) {
          toast.fail({
            title: "系统繁忙",
            duration: 2000
          });
          return;
        } else if (res.status == 1) {
          if(constate==2){
            window.location.href = "address-manage.html?state=2&id=" + product;
          }else {
            window.location.href = document.referrer;
          }  
        }
      }
    );
  }
}

$(function(){
  constate = GetQueryString("state");
  if (constate == 2) {
    product = GetQueryString("id");
  }
})