<template>
  <view class="rateMain">
    <image v-if="preferPath" style="width: 144rpx;height: 144rpx" :src="preferPath"></image>
    <canvas style="width: 144rpx;height: 144rpx;position:fixed;left: 100%" :canvas-id="name" id="rate" />
  </view>
</template>

<script>
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
  name: "rateCharts",
  data() {
    return {
      preferPath: '',
    }
  },
  props: {
    success: {
      type: Array,
      default: []
    },
    colorList: {
      type: Array,
      default: []
    },
    name: {
      type: String,
      default: ''
    }
  },
  watch: {
    success: {
      handler(value) {
        // 数据无效时不绘制，等待 API 返回有效数据后再触发
        if (!value || value.length < 3 || value[0] === undefined || value[1] === undefined || value[2] === undefined) {
          return;
        }
        this.preferPath = ''
        let sum = value[0] + value[1] + value[2]
        if (sum === 0) {
          let res = {
            series: [{
              data: [{ name: '便利店', value: 0 }, { name: '餐饮客房', value: 0 }, { name: '商铺租赁', value: 0 }]
            }]
          }
          this.drawCharts(this.name, res)
        } else {
          let firstRate = Number(((value[0] / sum) * 100).toFixed(2))
          let secondRate = Number(((value[1] / sum) * 100).toFixed(2))
          let thirdRate = 100 - Number(firstRate) - Number(secondRate)
          let res = {
            series: [{
              data: [{ name: '便利店', value: firstRate }, { name: '餐饮客房', value: secondRate }, { name: '商铺租赁', value: thirdRate }]
            }]
          }
          this.drawCharts(this.name, res)
        }
      },
      immediate: true
    }
  },
  methods: {
    drawCharts(id, data) {
      const ctx = uni.createCanvasContext(id, this);
      let _this = this
      uChartsInstance[id] = new uCharts({
        type: "ring",
        context: ctx,
        width: 72,
        height: 72,
        series: data.series,
        animation: false,
        rotate: false,
        rotateLock: false,
        background: "#FFFFFF",
        color: _this.colorList,
        padding: [5, 5, 5, 5],
        dataLabel: false,
        enableScroll: false,
        legend: {
          show: false,
          position: "right",
          lineHeight: 25
        },
        title: {
          name: '模式占比',
          fontSize: 10,
          color: "#A69E9F"
        },
        extra: {
          ring: {
            ringWidth: 8,
            activeOpacity: 0.5,
            activeRadius: 10,
            offsetAngle: -90,
            labelWidth: 15,
            border: false,
            borderWidth: 3,
            customRadius: 34,
            borderColor: "#FFFFFF"
          }
        }
      });
      setTimeout(() => {
        _this.canvasToTempImage(_this.name)
      }, 500)
    },
    canvasToTempImage(id) {
      uni.canvasToTempFilePath({
        canvasId: id,
        complete: (res) => {
          if (res.tempFilePath) {
            uni.getFileSystemManager().readFile({
              filePath: res.tempFilePath,
              encoding: 'base64',
              success: res => {
                let base64 = 'data:image/png;base64,' + res.data;
                this.preferPath = base64
              }
            })
          }
        }
      }, this)
    },
  }
}
</script>

<style lang="scss" scoped>
.rateMain {
  width: 144rpx;
  height: 144rpx;

  .month {
    width: 136rpx;
    height: 136rpx;
    position: fixed;
    left: 100%;
  }
}
</style>
