var id;
var toast = new auiToast();

$(function() {
  id = GetQueryString("id");
  product = GetQueryString('product');
  getData();
});

function getData() {
  _vp.AddressOper(
    {
      type: 5,
      id: id
    },
    function(res) {
      if (res.status == 2001) {
        //需要登录
        top.location.href = "login.html";
        return;
      } else if (res.status == -1) {
        toast.fail({
          title: "系统繁忙",
          duration: 2000
        });
        return;
      }
      $("#name").val(res.data.user_name);
      $("#phone").val(res.data.phone);
      $("#address").val(res.data.address);
      state = res.data.state;
      if(res.data.state==1){
        $("#state").prop("checked",true); 
      }else {
        $("#state").prop("checked",false); 
      }
      
    }
  );
}

var state = 0;//1：默认地址 0：非默认地址

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 {
        _vp.AddressOper(
            {
              type: 3,
              user_name: name,
              phone: phone,
              province: '',
              city: '',
              area: '',
              address: address,
              state: state,
              id:id
            },
            function(res) {
                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(product){
                      window.location.href = "address-manage.html?state=2&id=" + product;
                      return;
                    }
                    window.location.href = document.referrer;
                  }
            })
      }
}
