<template>
    <lay-container :fluid="true" style="padding: 20px">
        <lay-row :space="10">
            <lay-col :md="24">
                <lay-card>
                    <lay-form style="margin-top: 20px">
                        <lay-row>
                            <lay-col :md="5">
                                <lay-form-item label="日期：" label-width="80">
                                    <lay-date-picker v-model="date" placeholder="选择日期" />
                                </lay-form-item>
                            </lay-col>
                            <lay-col :md="4">
                                <lay-form-item label-width="0">
                                    <lay-button type="primary" @click="toSearch">查询</lay-button>
                                    <lay-button @click="toReset" style="margin-left: 10px">重置</lay-button>
                                </lay-form-item>
                            </lay-col>
                        </lay-row>
                    </lay-form>
                </lay-card>
            </lay-col>
            <lay-col :md="24">
                <lay-card>
                    <template #title>
                        <div style="font-size: 20px;font-weight: 600;color: #333;">日常考评填报 - 分数统计</div>
                    </template>
                    <div id="main" ref="mainRef"></div>
                </lay-card>
            </lay-col>
            <lay-col :md="24">
                <lay-card>
                    <template #title>
                        <div style="font-size: 20px;font-weight: 600;color: #333;">日常考评报表 - 已参评人数统计</div>
                    </template>
                    <div id="barChart" ref="barChartRef"></div>
                </lay-card>
            </lay-col>
        </lay-row>
    </lay-container>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'

const mainRef = ref()
const barChartRef = ref()
const date = ref('')

// 查询方法
const toSearch = () => {
    console.log('查询日期:', date.value)
    // TODO: 这里可以根据日期重新获取数据并刷新图表
    initChart()
    initBarChart()
}

// 重置方法
const toReset = () => {
    date.value = ''
    // TODO: 重置后可以重新加载默认数据
    initChart()
    initBarChart()
}

// 初始化图表
const initChart = () => {
    const chartDom = mainRef.value
    // @ts-ignore
    const myChart = echarts.init(chartDom)
    
    const color = [
        '#6236FF',  // 高贵紫
        '#02C2E4',  // 湖蓝
        '#FFB100',  // 明黄
        '#1CC6A1',  // 翡翠绿
        '#7B61FF',  // 淡紫
        '#00B4D8'   // 天蓝
    ]
    
    const echartData = [
        {
            name: '内科',
            value1: 88,
            value2: 85
        },
        {
            name: '外科',
            value1: 87,
            value2: 84
        },
        {
            name: '儿科',
            value1: 89,
            value2: 86
        },
        {
            name: '妇产科',
            value1: 86,
            value2: 83
        },
        {
            name: '急诊科',
            value1: 85,
            value2: 82
        },
        {
            name: '骨科',
            value1: 87,
            value2: 85
        },
        {
            name: '眼科',
            value1: 88,
            value2: 84
        },
        {
            name: '口腔科',
            value1: 86,
            value2: 83
        }
    ]

    const xAxisData = echartData.map((v) => v.name)
    const yAxisData1 = echartData.map((v) => v.value1)
    const yAxisData2 = echartData.map((v) => v.value2)
    
    const hexToRgba = (hex: string, opacity: number) => {
        let rgbaColor = ''
        let reg = /^#[\da-f]{6}$/i
        if (reg.test(hex)) {
            rgbaColor = `rgba(${parseInt('0x' + hex.slice(1, 3))},${parseInt(
                '0x' + hex.slice(3, 5)
            )},${parseInt('0x' + hex.slice(5, 7))},${opacity})`
        }
        return rgbaColor
    }

    const option = {
        color: color,
        legend: {
            right: 10,
            top: 10,
            data: ['合计自评平均分', '合计科室平均分']
        },
        tooltip: {
            trigger: 'axis',
            formatter: function (params: any) {
                return `<div style="font-weight: 600;font-size: 14px;color: #333;margin-bottom: 8px">
                            ${params[0].name}
                        </div>
                        <div style="color: #666;font-size: 13px;line-height: 24px">
                            <span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${color[0]};"></span>
                            合计自评平均分：
                            <span style="color:${color[0]};font-weight:700;font-size: 14px">${params[0].value}</span>
                            分
                        </div>
                        <div style="color: #666;font-size: 13px;line-height: 24px">
                            <span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${color[1]};"></span>
                            合计科室平均分：
                            <span style="color:${color[1]};font-weight:700;font-size: 14px">${params[1].value}</span>
                            分
                        </div>`
            },
            extraCssText: 'background: #fff; border-radius: 4px; padding: 8px 12px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);color: #333;',
            axisPointer: {
                type: 'line',
                lineStyle: {
                    color: '#ccc',
                    width: 1,
                    type: 'dashed'
                }
            }
        },
        grid: {
            x: '50px',
            y: '50px',
            x2: '50px',
            y2: '50px'
        },
        xAxis: [
            {
                type: 'category',
                boundaryGap: false,
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        color: '#333'
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: '#D9D9D9'
                    }
                },
                data: xAxisData
            }
        ],
        yAxis: [
            {
                type: 'value',
                min: 70,
                max: 90,
                axisLabel: {
                    textStyle: {
                        color: '#666'
                    }
                },
                nameTextStyle: {
                    color: '#666',
                    fontSize: 12,
                    lineHeight: 40
                },
                splitLine: {
                    lineStyle: {
                        type: 'dashed',
                        color: '#E9E9E9'
                    }
                },
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                }
            }
        ],
        series: [
            {
                name: '合计自评平均分',
                type: 'line',
                smooth: true,
                symbolSize: 8,
                zlevel: 3,
                lineStyle: {
                    normal: {
                        color: color[0],
                        shadowBlur: 3,
                        shadowColor: hexToRgba(color[0], 0.5),
                        shadowOffsetY: 8
                    }
                },
                areaStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(
                            0,
                            0,
                            0,
                            1,
                            [
                                {
                                    offset: 0,
                                    color: hexToRgba(color[0], 0.3)
                                },
                                {
                                    offset: 1,
                                    color: hexToRgba(color[0], 0.1)
                                }
                            ],
                            false
                        ),
                        shadowColor: hexToRgba(color[0], 0.1),
                        shadowBlur: 10
                    }
                },
                data: yAxisData1
            },
            {
                name: '合计科室平均分',
                type: 'line',
                smooth: true,
                symbolSize: 8,
                zlevel: 3,
                lineStyle: {
                    normal: {
                        color: color[1],
                        shadowBlur: 3,
                        shadowColor: hexToRgba(color[1], 0.5),
                        shadowOffsetY: 8
                    }
                },
                areaStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(
                            0,
                            0,
                            0,
                            1,
                            [
                                {
                                    offset: 0,
                                    color: hexToRgba(color[1], 0.3)
                                },
                                {
                                    offset: 1,
                                    color: hexToRgba(color[1], 0.1)
                                }
                            ],
                            false
                        ),
                        shadowColor: hexToRgba(color[1], 0.1),
                        shadowBlur: 10
                    }
                },
                data: yAxisData2
            }
        ]
    }
    
    option && myChart.setOption(option)
}

// 初始化柱状图
const initBarChart = () => {
    const chartDom = barChartRef.value
    // @ts-ignore
    const myChart = echarts.init(chartDom)
    
    const barData = [
        { name: '内科', value: 25 },
        { name: '外科', value: 22 },
        { name: '儿科', value: 18 },
        { name: '妇产科', value: 20 },
        { name: '急诊科', value: 15 },
        { name: '骨科', value: 12 },
        { name: '眼科', value: 10 },
        { name: '口腔科', value: 13 }
    ]

    const option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            formatter: function(params: any) {
                return `${params[0].name}<br/>已参评人数：${params[0].value}人`
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data: barData.map(item => item.name),
                axisTick: {
                    alignWithLabel: true
                },
                axisLabel: {
                    color: '#333'
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: '人数',
                axisLabel: {
                    color: '#333'
                },
                splitLine: {
                    lineStyle: {
                        type: 'dashed',
                        color: '#E9E9E9'
                    }
                }
            }
        ],
        series: [
            {
                name: '已参评人数',
                type: 'bar',
                barWidth: '60%',
                data: barData.map(item => item.value),
                itemStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: '#6236FF' },    // 顶部渐变色
                        { offset: 0.5, color: '#02C2E4' },  // 中间渐变色
                        { offset: 1, color: '#1CC6A1' }     // 底部渐变色
                    ])
                },
                emphasis: {
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: '#5020E0' },   // 高亮时顶部色
                            { offset: 0.7, color: '#00A3C4' }, // 高亮时中间色
                            { offset: 1, color: '#15A589' }    // 高亮时底部色
                        ])
                    }
                }
            }
        ]
    }
    
    option && myChart.setOption(option)
}

onMounted(() => {
    initChart()
    initBarChart()
})
</script>

<style lang="less" scoped>
#main, #barChart {
    width: 100%;
    height: 400px;
}
</style>
