var app = getApp();
Page({
  data: {
    list: [],
    param:{
      code:''
    },
    is_maintain:0,
    //公共提示
    dataNull: true,//未获取到数据提示
  },
  onLoad: function (options) {
    if (options.maintain && options.maintain==1){
      this.setData({
        is_maintain: 1,
      });
    }
    if (options.type == 1) {
      this.setData({
        order_type: options.type,
      });
    }
  },
  onReady: function () {
    //初始化数据
    var self = this;
    app.getUserInfo(function(){
      self.getIndex();
    });
  },
  getIndex: function (code) {
    var self = this;
    var action = 'c=Member&a=address_list';
    var param =self.data.param;
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'POST', param, function (ret) {
      if (ret.status == 1) {
        if(ret.data){
          self.setData({
            list: ret.data,
          });
        }else{
          self.setData({
            dataNull: false,
          });
        }
       
        if (code == 1 && self.data.is_maintain==1){
          app.HCchangeData(1, 'changeaddress', function () {
            app.HCnavigateBack();
          })
        }
      }else{
        self.setData({
          list: [],
        });
      }
    }, true);
  },
  del:function(event){
    var id = event.currentTarget.dataset.id;
    var self = this;
    app.HCerror('温馨提示', '确定删除此记录？', true, function (ret) {
      if (ret.confirm) {
        var param = self.data.param;
        param.code = 'del';
        param.id = id;
        self.setData({
          param: param,
        });
        self.getIndex();
      }
    })
  },
  edit: function (event){
    var id = event.currentTarget.dataset.id;
    app.HCnavigateTo('../add_address/add_address?id='+id);
  },
  creat:function(){
    app.HCnavigateTo('../add_address/add_address');
  },
  change: function (event) {
    var id = event.currentTarget.dataset.id;
    var param = this.data.param;
    param.code = 'change';
    param.id = id;
    this.setData({
      param: param,
    });
    this.getIndex(1);
  },
  changeData:function(){
    var param = this.data.param;
    param.code = '';
    param.id = 0;
    this.setData({
      param: param,
    });
    this.getIndex();
  },
  goToDetaile: function (event) {
    var self = this;
    if (self.data.order_type == 1) {
      var address_data = event.currentTarget.dataset.item;
      var action = 'c=Member&a=distance';
      var data=new Object;
      data.address_id =address_data.id;
      app.showLoading('加载中...');
      app.ajaxRequest(action, 'POST', data, function (ret) {
        if(ret.status==0){
          if (ret.siteinfo) {
            var param = new Object;
            param.xpoint = ret.siteinfo.temp_lng;
            param.ypoint = ret.siteinfo.temp_lat;
            param.address = ret.siteinfo.temp_name;
            self.doneshopid(param);
          }
         
          app.HCerror('提示', "无法配送到此地址，用此下单需回购物车", true,function(ret){
            if (ret.confirm) {
              app.HCswitchTab('../../buycart/index/index');
            }
          });
        }else{
          app.HCchangeData(1, event.currentTarget.dataset.item, function (ret) {
            app.HCnavigateBack();
          })
        }
      }, true);
    }
  },
  doneshopid: function (data) {
    app.showLoading('加载中...');
    var action = 'c=Login&a=setshopid';
    app.ajaxRequest(action, 'POST', data, function (ret) {
      if (ret.bind_shop_id) {
        app.globalData.location_n = data.address;
        app.globalData.HCshopid = ret.bind_shop_id;
        app.globalData.send = ret.send;
      } else {
        app.globalData.HCshopid = 0;
      }
      // app.HCtoast('操作成功！');
      // setTimeout(function () {
      //   app.HCnavigateBack();
      // }, 2000);
    }, true);
  },
})