const _Http = getApp().globalData.http;

Page({
  data: {
    tradefieldIndex: 0,
    pickerIndex: 0,
    targetYear: null,
    hrid: null,
    "year": new Date().getFullYear().toString(),
    "content": {
      "nocache": true,
      "year": new Date().getFullYear().toString(), //年
      "type": 1,
      "where": {
        "condition": ""
      }
    },
    target: null, //目标
    showActions: false,
    actionSheet: "开票金额",
    actions: [{
      name: '开票金额',
      value: "1"
    }, {
      name: '订单金额',
      value: "2"
    }, {
      name: '出货金额',
      value: "3"
    }],
  },
  onLoad(options) {
    // this.getYear(true)
  },
  getYear(init = false) {
    /* 获取年份 */
    _Http.basic({
      "classname": "sysmanage.develop.optiontype.optiontype",
      "method": "optiontypeselect",
      "content": {
        pageSize: 999,
        "typename": 'targetyearofpersonal',
        "parameter": {
          "siteid": wx.getStorageSync('siteP').siteid
        }
      }
    }).then(res => {
      if (res.msg != '成功') return wx.showToast({
        title: res.msg,
        icon: "none"
      })
      this.setData({
        userYearList: res.data.map(v => v.year),
        "content.year": res.data.length ? res.data[res.data.length - 1].year : this.data.content.year,
        year: res.data.length ? res.data[res.data.length - 1].year : this.data.year,
        pickerIndex: res.data.length - 1
      })
      if (init) this.getData();
    })
    /* 获取年份 */
    _Http.basic({
      "classname": "sysmanage.develop.optiontype.optiontype",
      "method": "optiontypeselect",
      "content": {
        pageSize: 999,
        "typename": 'targetyearofproject',
        "parameter": {
          "siteid": wx.getStorageSync('siteP').siteid
        }
      }
    }).then(res => {
      console.log('项目分类', res)
      if (res.msg != '成功') return wx.showToast({
        title: res.msg,
        icon: "none"
      })
      this.setData({
        projectYearList: res.data.map(v => v.year)
      })
    })
  },
  onShow() {
    this.getData();
  },
  getData() {
    let content = this.data.content;
    _Http.basic({
      "id": wx.getStorageSync('userrole') == '业务员' ? 20230111163102 : 20230110151902,
      content
    }).then(res => {
      console.log("业绩目标", res)
      if (res.msg != '成功') return wx.showToast({
        title: res.data,
        icon: "none"
      })
      this.setData({
        list: res.data,
        tradefieldIndex: 0
      })
      this.setShowData();

    })
  },
  bindTradefieldChange(e) {
    this.setData({
      tradefieldIndex: e.detail.value
    })
    this.setShowData()
  },
  setShowData() {
    let data = this.data.list[this.data.tradefieldIndex];
    let lineData = [],
      histogram = [];
    data.month.forEach(v => {
      lineData = lineData.concat([{
          label: v.month + '月',
          value: v.l,
          type: "基本目标金额"
        },
        {
          label: v.month + '月',
          value: v.h,
          type: "挑战目标金额"
        },
        {
          label: v.month + '月',
          value: v.a,
          type: "实际订单金额"
        }
      ])
      histogram = histogram.concat([{
          label: v.month + '月',
          value: v.pl,
          type: "基础目标实际完成率"
        },
        {
          label: v.month + '月',
          value: v.ph,
          type: "挑战目标实际完成率"
        }
      ])
    });
    //绘制线图
    this.selectComponent("#line").render(lineData);
    // this.selectComponent("#histogram").render(histogram);
    this.setData({
      targetYear: {
        yl: data.y1l,
        yh: data.y1h,
        ya: data.y1a,
        jl: (res.data.y1a == 0 && res.data.y1l == 0) ? '0.00%' : res.data.y1l == 0 ? res.data.y1a + '%' : (res.data.y1a / res.data.y1l * 100).toFixed(2) + '%'
        // jl: data.y1a || data.y1l?(data.y1a / data.y1l * 100).toFixed(2) + '%':'0.00%'
      }
    });
    if (this.data.year == this.data.content.year) {
      const m = new Date().getMonth() + 1;
      let s = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
        [10, 11, 12]
      ].findIndex(v => v.some(va => va == m)) + 1;
      this.setData({
        targetSeason: {
          sl: data[`s${s}l`],
          sh: data[`s${s}h`],
          sa: data[`s${s}a`],
          jl: (res.data[`s${s}a`] == 0 && res.data[`s${s}l`] == 0) ? '0.00%' : res.data[`s${s}l`] == 0 ? res.data[`s${s}a`] + '%' : (res.data[`s${s}a`] / res.data[`s${s}l`] * 100).toFixed(2) + '%'
          // jl: data[`s${s}a`] || data[`s${s}l`] ? (data[`s${s}a`] / data[`s${s}l`] * 100).toFixed(2) + '%' : "0.00%"
        },
        targetMonth: {
          ml: data[`m${m}l`],
          mh: data[`m${m}h`],
          ma: data[`m${m}a`],
          jl: (res.data[`m${m}a`] == 0 && res.data[`m${m}l`] == 0) ? '0.00%' : res.data[`m${m}l`] == 0 ? res.data[`m${m}a`] + '%' : (res.data[`m${m}a`] / res.data[`m${m}l`] * 100).toFixed(2) + '%'
          // jl: data[`m${m}a`] || data[`m${m}l`] ? (data[`m${m}a`] / data[`m${m}l`] * 100).toFixed(2) + '%' : "0.00%"
        }
      })
    }
  },
  /* 弹出选择 */
  select({
    detail
  }) {
    if (this.data.actionSheet == detail.name) return;
    this.setData({
      actionSheet: detail.name,
      "content.type": detail.value,
      showActions: false
    });
    this.getData();
  },
  cancelActions() {
    this.setData({
      showActions: false
    })
  },
  openActions() {
    this.setData({
      showActions: true
    })
  },
  /* 选择年份 */
  bindDateChange({
    detail
  }) {
    let index = detail.value;
    let year = this.data.userYearList[index];
    if (year == detail.value) return;
    this.setData({
      "content.year": year,
      pickerIndex: index
    });
    this.getData();
  },
  onShareAppMessage() {}
})