let _Http = getApp().globalData.http,
  {
    formatTime
  } = require("../../../../../utils/getTime"),
  obj = {},
  currency = require("../../../../../utils/currency");
Component({
  lifetimes: {
    attached: function () {
      let date = formatTime().split(" ")[0].split("/");
      obj = {
        year: date[0],
        month: date[1],
        day: date[2],
        quarter: Math.ceil(date[1] / 3)
      };
      this.getDetail()
      _Http.basic({
        id: 20231108160203,
        content: {
          date: Date.now()
        }
      }).then(res => {
        console.log("获取区域", res)
        if (res.msg != '成功') return;
        this.setData({
          areanames: res.data
        })
      })
    },
  },
  data: {
    areanames: [],
    sa_saleareaid: 0
  },
  methods: {
    getDetail() {
      _Http.basic({
        "id": "20231018103203",
        "content": {
          date: Date.now(),
          sa_saleareaid: this.data.sa_saleareaid
        },
      }).then(res => {
        console.log("获取经销商统计", res)
        if (res.data.length == 0) return;
        let data = res.data[0],
          ar = [{
            fulfill: data.m1a + data.m2a + data.m3a,
            target: data.m3l + data.m3l + data.m3l,
          }, {
            fulfill: data.m4a + data.m5a + data.m6a,
            target: data.m4l + data.m5l + data.m6l,
          }, {
            fulfill: data.m7a + data.m8a + data.m9a,
            target: data.m7l + data.m8l + data.m9l,
          }, {
            fulfill: data.m10a + data.m11a + data.m12a,
            target: data.m10l + data.m11l + data.m12l,
          }].map(v => {
            v.achievingRate = (v.fulfill / v.target) * 100
            return v
          });
        console.log("obj", obj)
        this.setData({
          areafullname: data.areafullname,
          statistics: [{
            label: "月度",
            fulfill: data[`m${obj.month -0}a`],
            target: data[`m${obj.month -0}l`],
            distance: Math.abs(data[`m${obj.month -0}a`] - data[`m${obj.month -0}l`]).toFixed(2), //差额
            achievingRate: (data[`m${obj.month -0}a`] / data[`m${obj.month -0}l`]) * 100, //达成
            color: "#FF9900"
          }, {
            label: "季度",
            ...ar[obj.quarter - 1],
            distance: Math.abs(ar[obj.quarter - 1].fulfill - ar[obj.quarter - 1].target).toFixed(2), //差额
            color: "#2AB4A3"
          }, {
            fulfill: data.y1a,
            target: data.y1l,
            achievingRate: (data.y1a / data.y1l) * 100,
            label: "年度",
            distance: Math.abs(data.y1a - data.y1l).toFixed(2), //差额
            color: "#FF001F"
          }].map(v => {
            v.achievingRate = currency(v.achievingRate).value - 0;
            console.log(" v.achievingRate", v.achievingRate)
            return v
          })
        })
      });


    },
    onChange(e) {
      this.data.sa_saleareaid = this.data.areanames[e.detail.value].sa_saleareaid;
      this.getDetail()
    }
  }
})