var app = getApp();
var $ = require('../../js/util.js');
var QQMapWX = require('../../js/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
    id: 0,
    param: {
     
    },
    //地址搜索
    address: '请选择',
    modalhidden: true,
    citykeyword: '',
    address_index: 0,
    addresslist: {},
    pointlist:{},
    lat: 0,
    lng: 0,
    curaddress:''
    //地址搜索end
  },
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'JFNBZ-ZGIKU-ESEVG-4RQGG-U5UUK-42B6B'
    });
  },
  onReady: function () {
    //初始化数据
    var self = this;
    app.getUserInfo(function (ret) {
      self.getIndex();
      self.getLocation();
    });
  },
  changeData:function(){
    this.getIndex();
  },
  getLocation:function(){
    var self = this;
    app.HCgetlocation(function (ret) {
      self.setData({
        lng: ret.longitude,
        lat: ret.latitude
      });
      self.getData(ret.longitude, ret.latitude);
    });
  },
  gotoaddress:function(){
    app.HCnavigateTo('../address/address');
  },
  getData: function (lng,lat) {
    var self = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lng
      },
      coord_type: 1,
      get_poi: 1,
      success: function (res) {
        if (res.message =='query ok'){
          self.setData({
            curaddress: res.result.formatted_addresses.recommend,
            pointlist: res.result.pois
        })
        }
        // console.log(res);
      },
      fail: function (res) {
        // console.log(res);
      }
    });
  },
  changeaddress:function(event){
    console.log(event);
    var data = event.currentTarget.dataset.id;
    var param = new Object;
    param.xpoint = data.xpoint;
    param.ypoint = data.ypoint;
    param.address = data.address;
    this.doneshopid(param);
  },
  changepoint: function (event) {
    var data = event.currentTarget.dataset.id;
    var param = new Object;
    param.xpoint = data.location.lng;
    param.ypoint = data.location.lat;
    param.address = data.title;
    this.doneshopid(param);
  },
  doneshopid:function(data){
    app.showLoading('加载中...');
    var self=this;
    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;
      }
      self.getData(data.xpoint, data.ypoint);
      app.HCtoast('操作成功！');
      setTimeout(function () {
        app.HCchangeData(1, data, function (ret) {
          app.HCnavigateBack();
        })
      }, 2000);
    }, true);
  },
  getIndex: function () {
    var self = this;
    var action = 'c=Member&a=address_list';
    app.showLoading('加载中...');
    var param = new Object;
    app.ajaxRequest(action, 'POST', param, function (ret) {
      console.log(ret);
      if(ret.status==1){
        self.setData({
          addresslist: ret.data
        });
      }
    }, true);
  },
  creataddress: function (event) {
    var id = event.currentTarget.dataset.id;
    if (!id) {id = 0;}
    app.HCnavigateTo('../userinfo/add_address/add_address?id='+id);
  },
  carnameInput: function (event) {
    var param = this.data.param;
    param.car_name = event.detail.value;
    this.setData({
      param: param
    });
  },
  formSubmit: function (event) {
    var province_info = 0;
    var city_info = 0;
    var district_info = 0
    if (this.data.province) {
      province_info = this.data.province.i
    }
    if (this.data.city) {
      city_info = this.data.city.i
    }
    if (this.data.district) {
      district_info = this.data.district.i
    }

    var value = event.detail.value;
    if (value.consignee == '') {
      app.HCerror('提示', '收货人不能为空', false); return;
    }
    if (value.mobile == '') {
      app.HCerror('提示', '手机号不能为空', false); return;
    }
    if (!province_info || !city_info || !district_info) {
      app.HCerror('提示', '地址不能为空', false); return;
    }

    var param = this.data.param;
    param.types = 4;
    param.consignee = value.consignee;
    param.mobile = value.mobile;
    param.house_no = value.house_no;
    param.province = province_info;
    param.city = city_info;
    param.district = district_info;
    param.address = this.data.address;
    param.lat = this.data.lat;
    param.lng = this.data.lng;
    this.getIndex();
  },
  checkboxChange: function (e) {
    var status = e.detail.value;
    var param = this.data.param;
    param.status = status;
  },
  init_area: function () {
    var self = this;
    var area = self.data.area;
    var province = self.data.province;
    var region = new Array;
    region.push(province);//构造省级列表
    var multiIndex = self.data.multiIndex;
    var cityobj = area[province[multiIndex[0]]['i']];
    if (cityobj) {//构造市级列表
      var city = new Array;
      for (var i in cityobj) {
        city.push(cityobj[i]);
      }
      region.push(city);
    }
    var districtobj = area[city[multiIndex[1]]['i']];
    if (districtobj) {//构造区县级列表
      var district = new Array;
      for (var i in districtobj) {
        district.push(districtobj[i]);
      }
      region.push(district);
    }
    self.setData({
      region: region,
    });
  },
  bindMultiPickerChange: function (event) {
    var self = this;
    var value = event.detail.value;
    var region = self.data.region;
    //赋值
    var province = region[0][value[0]];
    var city = region[1][value[1]];
    var district = region[2][value[2]];
    self.setData({
      province: province,
      city: city,
      district: district,
      curtitle: ''
    });
  },
  bindMultiPickerColumnChange: function (event) {
    var self = this;
    var column = event.detail.column;
    var value = event.detail.value;
    var multiIndex = self.data.multiIndex;
    multiIndex[column] = value;
    self.setData({
      multiIndex: multiIndex,
    });
    self.init_area();
  },
  searchaddress: function () {
    var self = this;
    if ($.isEmptyObject(self.data.city)) {
      app.HCerror('提示', '请先选择地址', false); return;
    } else if (self.data.citykeyword == '') {
      app.HCerror('提示', '请输入关键词', false); return;
    }
    qqmapsdk.getSuggestion({
      keyword: self.data.citykeyword,
      region: self.data.city.n,
      success: function (res) {
        // console.log(res);
        self.setData({
          addresslist: res.data,
        })
      },
      fail: function (res) {
         console.log(res);
      },
    });
  },
  bindaddressChange: function (event) {
    var addresslist = this.data.addresslist
    var address = addresslist[event.detail.value].address;
    this.setData({
      address_index: event.detail.value,
      address: address
    })
  },
  bindkeywordval: function (e) {
    this.setData({
      citykeyword: e.detail.value
    })
  },
  showsearch: function () {
    this.setData({
      modalhidden: false
    })
  },
  addresscancel: function () {
    this.setData({
      modalhidden: true
    })
  },
  addressconfirm: function (event) {
    if (Object.keys(this.data.addresslist).length != 0) {
      var location = this.data.addresslist[this.data.address_index].location;
      this.setData({
        lat: location.lat,
        lng: location.lng,
      })
    }
    this.setData({
      modalhidden: true
    })
  },
  changeData:function(data){
    var self=this;
    var param = this.data.param;
    self.setData({
      curaddress: data.address,
    });
    setTimeout(function () {
      app.HCchangeData(1, data, function (ret) {
        self.get_data(data.xpoint,data.ypoint);
      })
    }, 2000);
  },
  get_data: function (lng,lat) {
    var self = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lng
      },
      coord_type: 1,
      get_poi: 1,
      success: function (res) {
        if (res.message =='query ok'){
          self.setData({
            pointlist: res.result.pois
        })
        }
        // console.log(res);
      },
      fail: function (res) {
        // console.log(res);
      }
    });
  },
})