<template>
    <div class="layout-container-main" id="jcsb">
        <div class="item" id="box1">
            <div class="layout-container-main-top">
                <div class="left_title">
                    <div class="top-left">
                        <div class="line"></div>
                        <span>{{ title }}</span>
                    </div>
                    <div class="main" v-if="totalProfit">
                        <el-row v-if="type === 'earn'">
                            <el-col :span="8">
                                <el-statistic class="statistic" title="今日收益" :value="totalProfit.today" :precision="2" />
                            </el-col>
                            <el-col :span="8">
                                <el-statistic title="昨日收益" :value="totalProfit.yesterday" :precision="2" />
                            </el-col>
                            <el-col :span="8">
                                <el-statistic title="本月收益" :value="totalProfit.month" :precision="2" />
                            </el-col>
                        </el-row>
                    <!--  <el-row v-else>
                            <el-col :span="8">
                                <el-statistic title="今日ECPM" :value="totalProfit.ecpmToday" :precision="2" />
                            </el-col>
                            <el-col :span="8">
                                <el-statistic title="昨日ECPM" :value="totalProfit.ecpmYesterday" :precision="2" />
                            </el-col>
                            <el-col :span="8">
                                <el-statistic title="本月ECPM" :value="totalProfit.ecpmMonth" :precision="2" />
                            </el-col>
                        </el-row> -->
                    </div>
                </div>

                <el-select v-if="appList.length > 0" v-model="appId" @change="getChangeAppId">
                    <el-option v-for="item in appList" :key="item.appId" :value="item.appId" :label="item.appName" />
                </el-select>
            </div>
            <div class="main-echart" ref="chartRef">
                <el-table class="table" :data="tableData" border height="100%" :fit="true" :scroll-x="false"
                    :style="{ width: '100%' }">
                    <el-table-column prop="platform" label="统计" align="center" />
                    <el-table-column :prop="type === 'earn' ? 'today' : 'ecpmToday'" label="今日" align="center" />
                    <el-table-column :prop="type === 'earn' ? 'yesterday' : 'ecpmYesterday'" label="昨日"
                        align="center" />
                    <el-table-column :prop="type === 'earn' ? 'month' : 'ecpmMonth'" label="本月" align="center" />
                </el-table>

                <Chart class="chart" :option="options" />
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import Chart from '@/components/charts/index.vue'

const emit = defineEmits(['update'])

const props = defineProps({
    // 表格数据
    tableData: {
        type: Array,
        default: []
    },
    // 折线图数据
    lineListData: {
        type: Array,
        default: []
    },
    // legend名称
    legendData: {
        type: Array,
        default: []
    },
    // 汇总数据
    totalProfit: {
        type: Object,
        default: {}
    },
    // 类型 'earn'- 收益 | 'ecpm'
    type: {
        type: String,
        default: 'earn'
    },
    title: {
        type: String,
        default: ''
    },
    // 应用数据
    appList: {
        type: Array,
        default: []
    }
});

const xAxisList = Array.from({ length: 24 }, (_, i) => {
    const hour = i.toString().padStart(2, '0')
    return `${hour}:00`
})

const options = ref({
    /* title: {
        text: "单位：个",
        textStyle: {
            color: "#808080",
            fontSize: 12
        },
        left: "20",
        top: "0"
    }, */
    grid: {
        top: 30,
        right: 20,
        bottom: 0,  // 可根据需要设为 0~40
        left: 40,
        containLabel: true
    },
    tooltip: {
        trigger: 'axis', // 触发类型为坐标轴
        axisPointer: {
            type: 'cross' // 指示器类型为十字准星
        },
        formatter: function (params) {
            // 自定义提示框内容
            let result = params[0].axisValue + '<br>'; // 显示时间点
            params.forEach(function (item) {
                // 为每个系列添加数据行
                let value = Number(item.value);
                let fixedValue = isNaN(value) ? '-' : value.toFixed(2);
                result += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:'
                    + item.color + '"></span>'
                    + item.seriesName + ': ' + fixedValue + '<br>';
            });
            return result;
        }
    },
    legend: {
        data: [],
        top: 'top',
        textStyle: {
            color: '#808080'
        }
    },
    xAxis: {
        type: 'category',
        data: xAxisList,
        axisLabel: {
            color: "#808080",
            fontSize: 12
        },
        axisLine: {
            show: false,
        },
        axisTick: {
            show: false
        },
        splitLine: {
            show: false
        },
    },
    yAxis: {
        min: 0,
        nameTextStyle: {
            color: '#808080',
            fontSize: 12,
        },
        axisLabel: {
            color: "#808080",
            fontSize: 12
        },
        axisLine: {
            show: false
        },
        axisTick: {
            show: false
        },
        splitLine: {
            show: true,
            lineStyle: {
                color: 'rgba(0,0,0,0.15)',
            }
        }
    },
    series: []
});

const appId = ref('')
const getChangeAppId = (appId) => {
    emit('update',appId)
}

watch(() => props.appList, (newVal) => {
    // 默认显示第一个应用
    appId.value = props.appList[0].appId
})

// 监听 props.lineListData 变化，动态更新 series
watch(() => props.lineListData, (newVal) => {
    if (Object.keys(newVal).length === 0) return; // 空数据不更新
    // 更新 ECharts 配置
    options.value.legend = {
        data: props.legendData,
        top: 'top',
        textStyle: {
            color: '#808080'
        }
    }

    options.value.series = props.lineListData
}, { immediate: true, deep: true })

</script>

<style lang="scss" scoped>
.layout-container-main {
    // margin-top: 10px;
    display: flex;
    /* margin: 0 1.25rem; */
    flex-wrap: wrap;
    justify-content: space-between;

    .item {
        width: calc(100vw - 1.13rem - 1.31rem);
        display: flex;
        flex-direction: column;
        padding: 1.13rem 1.31rem;
        margin-bottom: 1.25rem;
        /* height: 26.94rem; */
        border-radius: 0.5rem;
        background-color: var(--system-page-background);
        box-shadow: 0 0 0.13rem #00000029;


        .layout-container-main-top {
            height: 6vh;
            display: flex;
            justify-content: space-between;

            .left_title {
                display: flex;
            }

            .top-left {
                display: flex;

                .line {
                    width: 0.25rem;
                    height: 1.56rem;
                    border-radius: 0.13rem;
                    background-color: var(--system-primary-color);
                }

                span {
                    margin: 0 1rem;
                    font-family: "PingFang SC SNaNremibold";
                    font-weight: 600;
                    font-size: 1.13rem;
                    color: var(--system-page-color);
                }

                .text {
                    display: inline-block;
                    /* // margin: 0 0.63rem; */
                    width: 3.63rem;
                    font-weight: 400;
                    height: 1.88rem;
                    line-height: 1.88rem;
                    border-radius: 0.25rem;
                    background: transparent;
                    border: 0.03rem solid #999;
                    font-size: 0.88rem;
                    text-align: center;
                    color: #999;
                    cursor: pointer;
                }

                .active {
                    border-radius: 0.25rem;
                    background: #fdb818;
                    color: #fff;
                    border: 0.03rem solid #fff;
                }
            }

            .main {
                // flex: 1;
                min-width: 300px;
            }
        }

        .main-echart {
            width: 100%;
            display: flex;
            height: 55vh;

            .table {
                flex: 1;
                min-width: 250px;
                /* 防止过窄导致表格挤压 */
                overflow-x: auto;
            }

            .table :deep(.el-table__body-wrapper tbody tr) {
                // height: calc(100% / 7);
                height: calc(61vh / 6);
            }

            .chart {
                flex: 3;
                min-width: 300px;
            }

            @media screen and (max-width: 1024px) {
                flex-direction: column;

                .table,
                .chart {
                    width: 100%;
                    min-width: unset;
                }

                .table {
                    flex: 2;
                    overflow-x: auto;
                    margin-bottom: 10px;
                }

                .table :deep(.el-table__body-wrapper tbody tr) {
                    height: calc(100% / 6);
                }

                .chart {
                    flex: 3;
                }
            }
        }
    }
}


.stats-table {
    margin: 20px;
    width: 100vw;
}

:deep() {
    .el-table {
        color: var(--system-page-color);
        --el-table-tr-bg-color: var(--system-page-background);
        --el-table-bg-color: var(--system-page-background);
        --el-table-header-bg-color: var(--system-page-background);
        --el-table-border-color: var(--system-page-border-color);
        --el-table-row-hover-bg-color: var(--system-container-background);
    }

    .el-statistic {
        --el-statistic-title-color: var(--system-page-tip-color);
        --el-statistic-content-color: var(--system-page-color);
    }
}
</style>