<template>
  <view class="rateMain">
    <image v-if="preferPath" style="width: 144rpx;height: 144rpx" :src="preferPath"></image>
    <canvas v-if="name"  style="width: 144rpx;height: 144rpx;position:fixed;left: 100%" :canvas-id="name" :id="name"/>
  </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){
        this.preferPath = ''
        let sum = value[0] + value[1]
        if (sum===0){
          let res = {
            series:[{
              data:[{name:'驿站营收',value:0},{name:'未完成',value:0}]
            }]
          }
          this.drawCharts(this.name,res)
        }else{
          let firstRate = Number(((value[0] / sum)*100).toFixed(2))
          let secondRate = 100 - Number(firstRate)
          let res = {
            series:[{
              data:[{name:'驿站营收',value:firstRate},{name:'未完成',value:secondRate}]
            }]
          }
          this.drawCharts(this.name,res)
        }
      },
    }
  },
  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: 10,
            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: 100%;
  height: 100%;
  .month{
    width: 136rpx;
    height: 136rpx;
    position: fixed;
    left: 100%;
  }
}
</style>
