var dialog = new auiDialog({});
var toast = new auiToast();

var addressList;
var state; //1表示从个人中心进入，2表示从确认夺宝页面
var product; //商品ID

function DeleteQrder() {
  dialog.alert(
    {
      msg: "是否删除订单",
      buttons: ["取消", "确定"]
    },
    function(ret) {
      console.log(ret);
    }
  );
}

/**
 * 获取收货地址
 */
function getAddress() {
  toast.loading({
    title: "加载中",
    duration: 2000
  });
  _vp.AddressOper(
    {
      type: 2
    },
    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;
      }
      // if(res.data.length==0){
      //   $("#add").show();
      // }else {
      //   $("#add").hide();
      // }
      var ele = $("#con");
      addressList = res.data;
      var result = res.data;
      var con = "";
      if (state == 2) {
        con = "&product=" + product;
      }
      ele.html("");
      for (var i = 0; i < result.length; i++) {
        var check = "";
        if (result[i].state == 1) {
          check = "checked";
        }
        ele.append(
          '<section class="aui-content aui-margin-b-10" onclick="goBack(' +
            result[i].id +
            ')">' +
            '<ul class="aui-list">' +
            '<li class="aui-list-item">' +
            '<div class="aui-list-item-inner">' +
            '<div class="aui-list-item-title">' +
            result[i].user_name +
            "</div>" +
            '<div class="aui-list-item-right">' +
            result[i].phone +
            "</div>" +
            "</div>" +
            "</li>" +
            '<li class="aui-list-item">' +
            '<div class="aui-list-item-inner">' +
            '<div class="aui-list-item-title school-name" style="color: #757575;">' +
            result[i].address +
            "</div>" +
            "</div>" +
            "</li>" +
            '<li class="aui-list-item">' +
            '<div class="aui-list-item-inner">' +
            '<div class="aui-list-item-title default" >' +
            '<input type="checkbox" ' +
            check +
            ' onclick="doEditAddress(' +
            i +
            ')">设为默认</div>' +
            '<div class="aui-list-item-right">' +
            '<span class=" aui-edit aui-font-size-12 aui-margin-r-5" onclick="test(&quot;update-address.html?id=' +
            result[i].id +
            con +
            '&quot;)">编辑</span>' +
            '<span class=" aui-delete aui-font-size-12" onclick="doDeleteAddress(' +
            result[i].id +
            ')" >删除</span>' +
            "</div>" +
            "</div>" +
            "</li>" +
            "</ul>" +
            "</section>"
        );
      }
    }
  );
}

/**
 * 删除收货地址
 */
function doDeleteAddress(id) {
  event.stopPropagation();

  dialog.alert(
    {
      msg: "是否删除该地址",
      buttons: ["取消", "确定"]
    },
    function(ret) {
      console.log(ret);
      if (ret.buttonIndex == 2) {
        toast.loading({
          title: "加载中",
          duration: 2000
        });
        _vp.AddressOper(
          {
            type: 4,
            id: id
          },
          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){
                toast.fail({
                    title: "删除成功",
                    duration: 1500
                });
            }
            window.location.reload();
          }
        );
      }
    }
  );
}

/**
 * 设为默认收货地址
 */
function doEditAddress(i) {
  event.stopPropagation();
  toast.loading({
    title: "加载中",
    duration: 2000
  });
  _vp.AddressOper(
    {
      type: 3,
      id: addressList[i].id,
      user_name: addressList[i].user_name,
      phone: addressList[i].phone,
      province: "",
      city: "",
      area: "",
      address: addressList[i].address,
      state: 1
    },
    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) {
        window.location.reload();
      }
    }
  );
}

/**
 * 选择地址后返回确认订单页面
 */
function goBack(id) {
  if (state == 2) {
    window.location.href =
      "confirm.html?state=2&id=" + product + "&addressId=" + id;
  }
}

/**
 * 添加新地址
 */
function addAddress() {
  if (state == 1) {
    window.location.href = "add-address.html";
  } else {
    window.location.href = "add-address.html?state=2&id=" + product;
  }
}

/**
 * 返回按钮
 */
function back() {
  if (state == 1||!state) {
    window.location.href = "personal.html";
  } else {
    window.location.href = "confirm.html?state=1&id=" + product;
  }
}

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