<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>chart</title>
    <script src="/js/echarts.min.js"></script>
</head>
<body>
<div  id="progress-overview-chart" style="width: 600px; height: 400px;"></div>
<script th:inline="javascript">
    // Thymeleaf 데이터 삽입
    const chartData = /*[[${chartData}]]*/ [];

    document.addEventListener("DOMContentLoaded", function () {
        const chartDom = document.getElementById('progress-overview-chart');
        const myChart = echarts.init(chartDom);

        // 데이터 포맷팅
        const formattedData = [
            ['product', 'TEST1', 'TEST2', 'TEST3'],
            ...chartData.map(item => [
                item.product,
                item.actualProgress,
                item.expectedProgress,
                item.requirementCount
            ])
        ];

        console.log(formattedData);
        // ECharts 옵션 설정
        const option = {
            legend: { textStyle: { color: '#000' } },
            tooltip: {},
            dataset: { source: formattedData },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                axisLabel: { color: '#000' }
            },
            yAxis: {
                axisLabel: { color: '#000' }
            },
            series: [{ type: 'bar' }, { type: 'bar' }, { type: 'bar' }]
        };

        myChart.setOption(option);

        myChart.on('finished', function () {
            // 이미지 생성
            const chartImage = myChart.getDataURL({
                type: 'png', // PNG 형식의 이미지
                pixelRatio: 2, // 해상도 배율
                backgroundColor: '#fff'
            });

            // 서버로 전송
            fetch('/html/mail/save-chart', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ image: chartImage })
            })
            .then(response => response.json())
            .then(data => {
                console.log("Server Response:", data);
            })
            .catch(error => {
                console.error("Error sending chart image:", error);
            });
        });

    });
</script>
</body>
</html>