<template>
    <div class="main">
        <div class="homePlace">
            <canvas v-if="!homePlacePath" class="carNum" canvas-id="homePlace" id="homePlace"/>
            <image v-if="homePlacePath" class="carNum" :src="homePlacePath"></image>
        </div>
        <div class="homeCity">
            <view class="item" v-for="(item,index) in progressList" :key="index">
                <view class="top">
                    <p class="title">{{item.name}}</p>
                    <p class="rate"> {{item.rate}}</p>
                </view>
                <view class="progress">
                    <view class="have" :style="{width:item.rate}"></view>
                </view>
            </view>
        </div>
    </div>
</template>

<script>
import uCharts from '@/components/u-charts.js';
var uChartsInstance = {};
export default {
    name: "homePlace",
    data() {
        return {
            homePlacePath:'',//图片路径
            progressList:[]//进度条数组
        }
    },
    props: {
        homeData: {
            type: Array,
            default: () => []
        },
        homeCity:{
            type:Array,
            default:() => []
        }
    },
    watch: {
        homeData: {
            handler(value) {
                this.homePlacePath = ''
                this.handleCarData(value)
            }
        },
        homeCity:{
            handler(value){
                console.log('handler',value)
                value.forEach(item=>{
                    item.rate = item.rate.toFixed(2) + '%'
                })
                this.progressList = value
            },
            deep:true
        }
    },
    methods: {
        // 处理传入的数据
        handleCarData(value) {
            let res = {
                series:[
                    {
                        data: value
                    }
                ]
            }
            this.drawCharts('homePlace', res)
        },
        drawCharts(id, data) {
            const ctx = uni.createCanvasContext(id, this);
            let phoneInfo = uni.getStorageSync('phoneInfo')
            let width = phoneInfo.screenWidth-32
            uChartsInstance[id] = new uCharts({
                type: "ring",
                context: ctx,
                width: width,
                height: 160,
                series: data.series,
                animation: false,
                rotate: false,
                rotateLock: false,
                canvas2d: true,
                background: "#FFFFFF",
                color: ["#1E80FF", "#456497","#748AAE","#ACB9CD","#CAD0DA"],
                padding: [5, 5, 5, 5],
                dataLabel: false,
                enableScroll: false,
                legend: {
                    show: true,
                    position: "right",
                    lineHeight: 25
                },
                extra: {
                    ring: {
                        ringWidth: 12,
                        activeOpacity: 0.5,
                        activeRadius: 10,
                        offsetAngle: 0,
                        labelWidth: 15,
                        border: false,
                        borderWidth: 3,
                        borderColor: "#FFFFFF"
                    }
                }
            });
            setTimeout(()=>{
                this.canvasToTempImage('homePlace')
            },2000)
        },
        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.homePlacePath = base64
                            }
                        })
                    }
                }
            },this)
        }
    }
}
</script>

<style scoped lang="scss">
.main{
    .homePlace{
        width: 100%;
        .carNum{
            width: 100%;
            height: 160px;
        }
    }
    .homeCity{
        width: 100%;
        .item{
            width: 100%;
            margin-bottom: 12px;
            .top{
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: space-between;
                margin-bottom: 4px;
                .title{
                    font-size: 14px;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #160002;
                    line-height: 20px;
                }
                .rate{
                    font-size: 14px;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #160002;
                    line-height: 20px;
                }
            }
            .progress{
                width: 100%;
                height: 14px;
                background: #E5E8EF;
                border-radius: 2px;
                position: relative;
                .have{
                    height: 100%;
                    background: #1E80FF;
                    border-radius: 2px;
                    position: absolute;
                    left: 0;top: 0;
                }
            }
        }
    }
}

</style>