const _Http = getApp().globalData.http,
  currency = require("../../utils/currency"),
  CNY = (value, symbol = "", precision = 2) => currency(value, {
    symbol,
    precision
  }).format();
Page({
  data: {
    dropdownItem: 0,
    filtratelist: [],
    showFiltrate: false,
    active: {
      name: wx.getStorageSync('userMsg').name,
      userid: wx.getStorageSync('userMsg').userid,
      isleave: 1
    }
  },
  onLoad(options) {
    getApp().globalData.Language.getLanguagePackage(this, '作业看板');
    this.setData({
      dropdown: [{
          text: getApp().globalData.Language.getMapText('作业看板'),
          value: 0
        },
        {
          text: getApp().globalData.Language.getMapText('线索'),
          value: 1
        },
        {
          text: getApp().globalData.Language.getMapText('客户'),
          value: 2
        },
        {
          text: getApp().globalData.Language.getMapText('项目商机'),
          value: 3
        },
        {
          text: getApp().globalData.Language.getMapText('作业单据'),
          value: 4
        }
      ],
      active: {
        name: wx.getStorageSync('userMsg').name,
        userid: wx.getStorageSync('userMsg').userid,
        isleave: 1
      }
    })
    console.log("active", this.data.active)
    this.refreshData();
  },
  openFiltrate() {
    this.setData({
      showFiltrate: true
    })
  },
  handleFilter({
    detail
  }) {
    if (detail.name == 'reset') {
      this.selectComponent("#organization").setData({
        isleave: 1
      })
      this.selectComponent("#organization").initDepAndUser().then(active => {
        console.log("active", active)
        active.isleave = 1;
        this.setData({
          active
        })
        this.refreshData();
        this.refreshModel();
      });
    } else {
      this.setData({
        active: this.selectComponent("#organization").data.result
      })
      this.refreshData();
      this.refreshModel();
    }
  },
  refreshData() {
    const conversion = (n) => {
      const integer = (n + '').split(".")[0] + '',
        length = integer.length,
        regexp = /(?:\.0*|(\.\d+?)0+)$/
      if (length <= 4) {
        const index = 4 - length;
        return { //元
          show: CNY(n, "", index).replace(regexp, '$1'),
          value: CNY(n, '￥')
        }
      } else if (length <= 8) {
        return { //万-千万
          show: CNY(currency(n).divide(wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000)).replace(regexp, '$1') + getApp().globalData.Language.getMapText('万'),
          value: CNY(n)
        }
      } else {
        return { //亿
          show: CNY(currency(n).divide(100000000)).replace(regexp, '$1') + getApp().globalData.Language.getMapText('亿'),
          value: CNY(n)
        }
      }
    };
    const sortOut = (obj, data, isConversion = false) => {
      for (const key in obj) {
        if (key == 'tab') continue;
        for (const k in obj[key]) {
          obj[key][k] = isConversion ? conversion(data[key + k]) : data[key + k]
        }
      }
      return obj
    };
    const getData = (dataType) => {
      let active = this.data.active;
      let type = active.userid ? 0 : 1,
        dataid = type == 0 ? active.userid : active.sa_saleareaid,
        where = {
          isleave: active.isleave
        };
      return _Http.basic({
        "id": 20230616131404,
        "content": {
          nocache: true,
          dataType, // 1 作业数据 2 销售数据 3业绩数据
          type,
          dataid,
          where
        }
      }).then(res => {
        console.log("res", res)
        let data = {};
        switch (dataType) {
          case 1:
            data = {
              tab: {
                active: "by",
                list: [{
                  name: getApp().globalData.Language.getMapText('上月'),
                  id: "sy"
                }, {
                  name: getApp().globalData.Language.getMapText('本周'),
                  id: "bz"
                }, {
                  name: getApp().globalData.Language.getMapText('本月'),
                  id: "by"
                }, {
                  name: getApp().globalData.Language.getMapText('本年'),
                  id: "bn"
                }]
              },
              sy: {
                lxxz: 0,
                lxgj: 0,
                khgj: 0,
                khxz: 0,
                xmgj: 0,
                xmxz: 0,
                xsxz: 0,
                xsgj: 0,
              },
              bz: {
                lxxz: 0,
                lxgj: 0,
                khgj: 0,
                khxz: 0,
                xmgj: 0,
                xmxz: 0,
                xsxz: 0,
                xsgj: 0,
              },
              by: {
                lxxz: 0,
                lxgj: 0,
                khgj: 0,
                khxz: 0,
                xmgj: 0,
                xmxz: 0,
                xsxz: 0,
                xsgj: 0,
              },
              bn: {
                lxxz: 0,
                lxgj: 0,
                khgj: 0,
                khxz: 0,
                xmgj: 0,
                xmxz: 0,
                xsxz: 0,
                xsgj: 0,
              }
            }
            break;
          case 2:
            data = {
              tab: {
                active: "by",
                list: [{
                  name: getApp().globalData.Language.getMapText('上月'),
                  id: "sy"
                }, {
                  name: getApp().globalData.Language.getMapText('本周'),
                  id: "bz"
                }, {
                  name: getApp().globalData.Language.getMapText('本月'),
                  id: "by"
                }, {
                  name: getApp().globalData.Language.getMapText('本年'),
                  id: "bn"
                }]
              },
              sy: {
                chje: 0,
                kpje: 0,
                skje: 0,
                xsje: 0
              },
              bz: {
                chje: 0,
                kpje: 0,
                skje: 0,
                xsje: 0
              },
              by: {
                chje: 0,
                kpje: 0,
                skje: 0,
                xsje: 0
              },
              bn: {
                chje: 0,
                kpje: 0,
                skje: 0,
                xsje: 0
              }
            }
            break;
          default:
            data = res.data
            data.tab = {
              active: "by",
              list: [{
                name: getApp().globalData.Language.getMapText('上月'),
                id: "sy"
              }, {
                name: getApp().globalData.Language.getMapText('本月'),
                id: "by"
              }, {
                name: getApp().globalData.Language.getMapText('本季'),
                id: "bj"
              }, {
                name: getApp().globalData.Language.getMapText('本年'),
                id: "bn"
              }]
            }
            break;
        }
        return dataType == 3 ? data : sortOut(data, res.data, dataType == 2)
      })
    };
    wx.showLoading({
      title: getApp().globalData.Language.getMapText('加载中...'),
      mask: true
    })
    let languagecode = wx.getStorageSync('languagecode');
    Promise.all([getData(1), getData(2), getData(3)]).then(res => {
      wx.hideLoading()
      for (const key in res[2]) {
        if (key == 'tab') continue;
        res[2][key].target_l = conversion(res[2][key].target_l);
        //实际
        res[2][key].amount = conversion(res[2][key].amount);
        res[2][key].outamount = conversion(res[2][key].outamount);
        res[2][key].invoiceamount = conversion(res[2][key].invoiceamount);
        //差额
        res[2][key].unamount = CNY(res[2][key].unamount / (languagecode == 'ZH' ? 10000 : 1000), '');
        res[2][key].unamountcolor = res[2][key].unamount >= 0 ? '#5AB73F' : '#EB4B5C';
        res[2][key].unoutamount = CNY(res[2][key].unoutamount / (languagecode == 'ZH' ? 10000 : 1000), '');
        res[2][key].unoutamountcolor = res[2][key].unoutamount >= 0 ? '#5AB73F' : '#EB4B5C';
        res[2][key].uninvoiceamount = CNY(res[2][key].uninvoiceamount / (languagecode == 'ZH' ? 10000 : 1000), '');
        res[2][key].uninvoiceamountcolor = res[2][key].uninvoiceamount >= 0 ? '#5AB73F' : '#EB4B5C';
        //达成率
        res[2][key].wcamount = (res[2][key].wcamount * 100).toFixed(2) + '%';
        res[2][key].wcoutamount = (res[2][key].wcoutamount * 100).toFixed(2) + '%';
        res[2][key].wcinvoiceamount = (res[2][key].wcinvoiceamount * 100).toFixed(2) + '%';
        console.log("res[2][key].wcamount", res[2][key].wcamount)
      }
      this.setData({
        zysj: res[0],
        xssj: res[1],
        yjsj: res[2],
      })
    })
  },
  dropdownItemChange({
    detail
  }) {
    this.setData({
      dropdownItem: detail
    })
    this.refreshModel();
  },
  changeId(e) {
    const {
      id,
      name
    } = e.currentTarget.dataset;
    if (this.data[name].tab.active == id) return;
    this.setData({
      [`${name}.tab.active`]: id
    })
  },
  refreshModel() {
    const dropdownItem = this.data.dropdownItem;
    if (dropdownItem == 0) return;
    const model = this.selectComponent("#model" + dropdownItem);
    if (!model) return;
    const active = this.data.active;
    model.init(active.sa_saleareaid ? '1' : '0', active.sa_saleareaid || active.userid);
  },
  onReady() {
    this.selectComponent("#ListBox").setHeight(".head", this);
    this.selectComponent("#organization").setData({
      isleave: 1
    })
    this.selectComponent("#organization").initDepAndUser().then(active => {
      this.refreshModel();
    });
  }
})