// packageE/title/title.js
var location = require("../../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    U_homedata: {
      type: null,
    },
    index: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
    clientWidth: "375",
    countData: {}, //统计数据
    region_name: '', //省/市/区   名称
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });
    this._getLocation((mapdata, e) => {
      this.setData({
        region_name: mapdata.address_component[this.data.datas.city_type]
      });
      this.getData();
      console.log(mapdata);
      this.recordLocation({
        point: mapdata.location,
        addressComponent: mapdata.address_component
      });
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    //获取数据
    getData() {
      let urlStr = app.getNetAddresss(
        "plugin.store-cashier.frontend.store.store.get-store-count"
      );
      app._postNetWork({
        url: urlStr,
        data: {
          city_type: this.data.datas.city_type,
          region_name: this.data.region_name
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            let countData = {
              all_stores_count: this.transform(parseInt(res.data.sotreCount + this.data.datas.all_stores_count)),
              all_fans_count: this.transform(parseInt(res.data.memberCount + this.data.datas.all_fans_count)),
              local_stores_count: this.transform(parseInt(res.data.sotreCityCount + this.data.datas.local_stores_count)),
              local_fans_count: this.transform(parseInt(res.data.memberCityCount + this.data.datas.local_fans_count))
            };
            this.setData({
              countData: countData
            });
            console.log(this.data.countData);
          } else {
            let countData = {
              all_stores_count: 0,
              all_fans_count: 0,
              local_stores_count: 0,
              local_fans_count: 0
            };
            this.setData({
              countData: countData
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
    // 后台记录会员的定位
    recordLocation(obj = {}) {
      let urlStr = app.getNetAddresss(
        "member.member.save-member-location"
      );
      console.log(obj);
      let point = app.BMapTransqqMap(
        parseFloat(obj.point.lng),
        parseFloat(obj.point.lat)
      );
      app._postNetWork({
        url: urlStr,
        data: {
          province_name: obj.addressComponent.province,
          city_name: obj.addressComponent.city,
          district_name: obj.addressComponent.district,
          longitude: point.lng,
          latitude: point.lat,
        },
        success: (resdata) => {},
        fail: function (res) {
          console.log(res);
        },
      });
    },
    transform(value) {
      let newValue = ["", "", ""];
      let fr = 1000;
      // const ad = 1;
      let num = 3;
      let fm = 1;
      while (value / fr >= 1) {
        fr *= 10;
        num += 1;
      }
      if (num <= 4) { // 千
        newValue[1] = "千";
        newValue[0] = parseFloat(value / 1000) + "";
      } else if (num <= 8) { // 万
        const text1 = parseInt(num - 4) / 3 > 1 ? "千万" : "万";
        fm = "万" === text1 ? 10000 : 10000000;
        newValue[1] = text1;
        newValue[0] = (value / fm) + "";
      } else if (num <= 16) { // 亿
        let text1 = (num - 8) / 3 > 1 ? "千亿" : "亿";
        text1 = (num - 8) / 4 > 1 ? "万亿" : text1;
        text1 = (num - 8) / 7 > 1 ? "千万亿" : text1;
        fm = 1;
        if ("亿" === text1) {
          fm = 100000000;
        } else if ("千亿" === text1) {
          fm = 100000000000;
        } else if ("万亿" === text1) {
          fm = 1000000000000;
        } else if ("千万亿" === text1) {
          fm = 1000000000000000;
        }
        newValue[1] = text1;
        newValue[0] = parseFloat(value / fm) + "";
      }
      if (value < 1000) {
        newValue[1] = "";
        newValue[0] = value + "";
      }
      return newValue.join("");
    },
  },
});