<script setup lang="ts">
import SmallTitle from '../smallTitle/smallTitle.vue';
import './AnnualAccountsReceivable.less'
import * as echarts from 'echarts/core';
import { BarChart } from 'echarts/charts';
import {
    GridComponent,
    TitleComponent,
    TooltipComponent,
    LegendComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import { onBeforeUnmount, onMounted } from 'vue';
import { handleGetProjectYearlyArrearageList } from '../../service';


// 注册组件
echarts.use([
    BarChart,
    GridComponent,
    TitleComponent,
    TooltipComponent,
    LegendComponent,
    CanvasRenderer
]);

let myChart: echarts.ECharts;


onMounted(async () => {

    const res = await handleGetData()

    const chartDom = document.getElementById('AnnualAccountsReceivableCharts');
    if (!chartDom) return;

    myChart = echarts.init(chartDom);

    const option = {
        yAxis: {  // 改为 yAxis 显示分类数据
            type: 'category',
            data: res.category,
            axisLabel: {
                width: 80,
                overflow: 'truncate',
                ellipsis: '...',
                interval: 0
            }
        },
        xAxis: {  // 改为 xAxis 显示数值
            type: 'value',
            name: "万元",
            splitLine: { show: false }
        },
        series: [
            {
                data: res.seriesData,
                realData: res.seriesData,
                type: 'bar',
                barWidth: '6',  // 现在是柱子的高度
                showBackground: true,
                backgroundStyle: {
                    borderRadius: [0, 4, 4, 0],  // 调整圆角方向
                    color: 'rgba(23, 42, 70, 1)'
                },
                itemStyle: {
                    borderRadius: [0, 4, 4, 0],  // 调整圆角方向
                    color: function (params: any) {
                        const index = params.dataIndex;
                        if (index > 6) {
                            return new echarts.graphic.LinearGradient(
                                0, 0, 1, 0,  // 渐变方向改为水平
                                [
                                    { offset: 0, color: '#FF4F00' },
                                    { offset: 1, color: '#D3B70E' }
                                ]
                            );
                        }
                        else {

                            return new echarts.graphic.LinearGradient(
                                0, 0, 1, 0,  // 渐变方向改为水平
                                [
                                    { offset: 0, color: '#018FFF' },
                                    { offset: 1, color: '#00F4FF' }
                                ]
                            );
                        }
                    }
                }
            },
        ],
        grid: {
            left: '0',  // 左侧留更多空间给y轴标签
            right: '10%',
            bottom: '5%',
            top: '0',
            containLabel: true
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: { type: 'shadow' },
            formatter: function (params: any) {
                const data: any = params[0]
                return `
                <div style="font-weight:bold">${data?.name || '-'}年</div>
                <div>已付金额${data?.value || '-'}万元 </div>
            `;
            }
            //  + '笔'
        }
    };

    myChart.setOption(option);
    window.addEventListener('resize', resizeChart);
})

// 获取数据的方法
const handleGetData = async () => {
    let category: any = []
    let seriesData: any = []

    const req: any = {

    }
    const data = await handleGetProjectYearlyArrearageList(req)
    console.log('handleGetProjectYearlyArrearageListhandleGetProjectYearlyArrearageListhandleGetProjectYearlyArrearageList', data);

    let list: any = data.ProjectMonthlyCompleteList.slice(0, 5)
    list.reverse()
    if (list && list.length > 0) {
        list.forEach((item: any) => {
            category.push(item.Business_Year)
            seriesData.push(item.Payment_Amount)
        })
    }

    return {
        category: category,
        seriesData: seriesData,
    }
}

const resizeChart = () => {
    myChart?.resize();
};

onBeforeUnmount(() => {
    window.removeEventListener('resize', resizeChart);
    myChart?.dispose();
});
</script>


<template>
    <div class="AnnualAccountsReceivableBox">
        <SmallTitle title="年度应收总额完成度" />

        <div class="AnnualAccountsReceivableContent">
            <div class="AnnualAccountsReceivableCharts" id="AnnualAccountsReceivableCharts"></div>
        </div>
    </div>
</template>