// components/dashboard/dashboard.js
const app = getApp();
import {
  linearColors,
  ladderOptions
} from './baseData';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    params: {
      type: Object,
      value: {
        waterStages: []
      }
    }
  },
  observers: {
    params(params) {
      let {
        username,
        userNumber,
        useAmount,
        laddertype,
        waterStages = [],
        ladderlevel
      } = params;
      const newAmount = app.$util.numberFormat(useAmount, 1);
      params.intNum = newAmount.split('.')[0] || 0;
      params.floatNum = newAmount.split('.')[1] || 0;
      const arr = [];
      params.style = linearColors[ladderlevel];
      if (userNumber && username) {
        if (laddertype && waterStages.length) {
          waterStages.forEach((v, k) => {
            arr.unshift(Object.assign({}, v, ladderOptions[k]));
          })
          const baseRate = (ladderlevel - 1) * (1 / waterStages.length);
          const {
            minValue,
            maxValue
          } = waterStages[ladderlevel - 1];
          const otherRate = (useAmount - minValue) / (maxValue - minValue) * 0.25;
          const rate = parseFloat(baseRate + otherRate);
          params.percentage = rate > 1 ? 100 : rate < 0 ? 0 : parseFloat(rate * 100 + (rate ? 1 : 3)).toFixed(2);
          this.setData({
            userInfo: params,
            waterStages: arr
          })
        } else {
          const scale = useAmount ? Math.ceil(useAmount/15)*15 : 10;
          for (let i = 3; i > 0; i--) {
            arr.push({
              maxValue: (scale * i),
              minValue: (scale * (i - 1)),
              color: '#0091FF',
              name: '用水量',
              background: 'rgba(0, 145, 255, 0.1)'
            })
          }
          const rate = useAmount ? parseFloat(useAmount / (scale * 3)) : 0;
          params.percentage = parseFloat(rate * 100 + (rate ? 1 : 3)).toFixed(2);
          this.setData({
            userInfo: params,
            waterStages: arr
          })
        }
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    ec: {
      lazyLoad: true
    },
    userInfo: {},
    waterStages: []
  },
  /**
   * 组件的方法列表
   */
  methods: {
    
  }
})