var app = getApp();
var $ = require('../../../js/util.js');
var QQMapWX = require('../../../js/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
    id: 0,
    param: {
      types: 1,
      car_name: '',
      mileage: 0,
      status: 0
    },
    //省市县三级联动
    curtitle: '当前选择',
    area: {},
    province: {},
    city: {},
    district: {},
    province_infos: {},
    city_infos: {},
    district_infos: {},
    multiIndex: [0, 0, 0],
    //省市县三级联动end
    //地址搜索
    address: '请选择',
    modalhidden: true,
    citykeyword: '',
    address_index: 0,
    addresslist: {},
    lat: 0,
    lng: 0
    //地址搜索end
  },
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });
    qqmapsdk = new QQMapWX({
      key: app.getQQmapkey()
    });
  },
  onReady: function () {
    //初始化数据
    var self = this;
    app.getUserInfo(function () {
      self.getIndex();
    });
  },
  getIndex: function () {
    var self = this;
    var action = 'c=Member&a=address';
    var param = $.ObjectToStr(self.data.param);
    var id = self.data.id;
    if (id > 0) { param = param + '&id=' + id; }
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'GET', param, function (ret) {
      if (ret && ret.status == 1) {
        if (ret.code && ret.code == 'success') {
          app.HCtoast('操作成功！');
          setTimeout(function () {
            app.HCchangeData(1, '', function () {
              app.HCnavigateBack();
            })
          }, 2000);
        } else {
          if (ret.data && ret.data.init_list) {
            self.setData({
              address_info: ret.data.init_list,
              curtitle: ret.data.init_list.region,
              address: ret.data.init_list.address,
            });
            self.data.param.province = ret.data.init_list.province;
            self.data.param.city = ret.data.init_list.city;
            self.data.param.district = ret.data.init_list.district;
            self.data.param.address = ret.data.init_list.address;
          }
          if (ret.data && ret.data.region) {
            self.setData({
              province_infos: ret.data.region,
              area: ret.data.area,

            });
            self.init_area();
          }
        }
      } else {
        app.HCerror('提示', ret.msg, false); return;
      }
    }, true);
  },
  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 (value.house_no == '') {
      app.HCerror('提示', '门牌号不能为空', false); return;
    }
    var param = this.data.param;
    if (this.data.id <= 0) {
      if (!province_info || !city_info || !district_info) {
        app.HCerror('提示', '地址不能为空', false); return;
      }
    }
    if (province_info && city_info) {
      param.province = province_info;
      param.city = city_info;
      param.district = district_info;
      param.address = this.data.address;
    }

    param.types = 4;
    param.consignee = value.consignee;
    param.mobile = value.mobile;
    param.house_no = value.house_no;


    param.ypoint = this.data.lat;//纬度
    param.xpoint = this.data.lng;//经度
    this.getIndex();
  },
  checkboxChange: function (e) {
    var status = e.detail.value;
    var param = this.data.param;
    param.status = status;
    this.setData({
      param: param
    });
  },
  init_area: function () {
    var self = this;
    var area = self.data.area;
    var province = self.data.province_infos;
    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);
    }
    if (area[city[multiIndex[1]]['i']]) {
      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 = value[2] ? region[2][value[2]] : 0;
    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,
          address_index: 0,
          address: res.data[0].address
        })
      },
      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
    })
  },
})