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

<script>
import uCharts from '@/components/u-charts.js';

var uChartsInstance = {};
export default {
  name: "ProportionCharts",
  data() {
    return {
      preferPath:'',
      showImg:false,
      hide:true
    }
  },
  props: {
    dataList:{
      type: Array,
      default:[]
    },
    colorList:{
      type: Array,
      default:[]
    },
    name:{
      type: String,
      default: ''
    }
  },
  watch: {
    dataList: {
      handler(value) {
        this.hide = true
        this.preferPath = ''
        if (value && value.length===2){
          let first = value[0]
          let second = value[1]
          let res = {
            series:[{
              data:[{name:'自营收入',value:first},{name:'其他收入',value:second}]
            }]
          }
          this.drawCharts(this.name,res)
        }

      },
      immediate:true
    },
    preferPath:{
      handler(value){
        if (value){
          this.showImg = true
        }else{
          this.showImg = false
        }
      },
      immediate:true
    }
  },
  methods: {
    drawCharts(id, data) {
      if (this.name){
        const ctx = uni.createCanvasContext(id, this);
        let _this = this
        uChartsInstance[id] = new uCharts({
          type: "ring",
          context: ctx,
          width: 34,
          height: 34,
          series: data.series,
          animation: false,
          rotate: false,
          rotateLock: false,
          background: "#FFFFFF",
          color: _this.colorList,
          padding: [0,0,0,0],
          dataLabel: false,
          enableScroll: false,
          legend: {
            show: false,
            position: "right",
            lineHeight: 25
          },
          extra: {
            ring: {
              ringWidth: 4,
              activeOpacity: 0.5,
              activeRadius: 10,
              offsetAngle: -90,
              labelWidth: 0,
              border: false,
              borderWidth: 3,
              customRadius: 17,
              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.hide = false
              }
            })
          }
        }
      },this)
    },
  }
}
</script>

<style lang="scss" scoped>
.main{
  width: 68rpx;
  height: 68rpx;
  .month{
    width: 68rpx;
    height: 68rpx;
  }
}
</style>
