<template>
    <div class="layout-container-main" id="jcsb">
        <div class="item" id="box1">
            <div class="layout-container-main-top">
                <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 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>
            <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 props = defineProps({
    // 表格数据
    tableData: {
        type: Array,
        default: []
    },
    // 折线图数据
    lineListData: {
        type: Object,
        default: {}
    },
    // 汇总数据
    totalProfit: {
        type: Object,
        default: {}
    },
    // 类型 'earn'- 收益 | 'ecpm'
    type: {
        type: String,
        default: 'earn'
    },
    title: {
        type: String,
        default: ''
    }
});

const xAxisList = [
    '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00',
    '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00',
    '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'
];

const options = ref({
    /* title: {
        text: "单位：个",
        textStyle: {
            color: "#808080",
            fontSize: 12
        },
        left: "20",
        top: "0"
    }, */
    grid: {
        top: 20,
        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) {
                // 为每个系列添加数据行
                result += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:'
                    + item.color + '"></span>'
                    + item.seriesName + ': ' + item.value + '<br>';
            });
            return result;
        }
    },
    legend: {
        data: ['穿山甲', '优量汇', '快手', 'Sigmob', '百度'],
        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: []
});


// 监听 props.lineListData 变化，动态更新 series
watch(() => props.lineListData, (newVal) => {
    if (Object.keys(newVal).length === 0) return; // 空数据不更新

    options.value.series = [
        {
            type: 'line',
            name: '穿山甲',
            symbolSize: 6,
            z: 1,
            label: { show: false },
            itemStyle: { color: '#add7f6' },
            lineStyle: { color: '#add7f6', width: 2 },
            data: props.type === 'earn' ?
                newVal['穿山甲']?.estimatedRevenueList ?? [] :
                newVal['穿山甲']?.estimatedRevenueEcpmList ?? []
        },
        {
            type: 'line',
            name: '优量汇',
            symbolSize: 6,
            z: 1,
            label: { show: false },
            itemStyle: { color: '#f7a8b8' },
            lineStyle: { color: '#f7a8b8', width: 2 },
            data: props.type === 'earn' ?
                newVal['优量汇']?.estimatedRevenueList ?? [] :
                newVal['优量汇']?.estimatedRevenueEcpmList ?? []
        },
        {
            type: 'line',
            name: '快手',
            symbolSize: 6,
            z: 1,
            label: { show: false },
            itemStyle: { color: '#ffcc5c' },
            lineStyle: { color: '#ffcc5c', width: 2 },
            data: props.type === 'earn' ?
                newVal['快手']?.estimatedRevenueList ?? [] :
                newVal['快手']?.estimatedRevenueEcpmList ?? []
        },
        {
            type: 'line',
            name: 'Sigmob',
            symbolSize: 6,
            z: 1,
            label: { show: false },
            itemStyle: { color: '#88d8b0' },
            lineStyle: { color: '#88d8b0', width: 2 },
            data: props.type === 'earn' ?
                newVal['Sigmob']?.estimatedRevenueList ?? [] :
                newVal['Sigmob']?.estimatedRevenueEcpmList ?? []
        },
        {
            type: 'line',
            name: '百度',
            symbolSize: 6,
            z: 1,
            label: { show: false },
            itemStyle: { color: '#9966cc' },
            lineStyle: { color: '#9966cc', width: 2 },
            data: props.type === 'earn' ?
                newVal['百度']?.estimatedRevenueList ?? [] :
                newVal['百度']?.estimatedRevenueEcpmList ?? []
        }
    ]
}, { immediate: 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: #fff;
        box-shadow: 0 0 0.13rem #00000029;

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

            .top-left {
                display: flex;

                .line {
                    width: 0.25rem;
                    height: 1.56rem;
                    border-radius: 0.13rem;
                    background: #409eff;
                }

                span {
                    margin: 0 1rem;
                    font-family: "PingFang SC SNaNremibold";
                    font-weight: 600;
                    font-size: 1.13rem;

                    color: #333;
                }

                .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__header th) {
    background-color: #f5f7fa;
    font-weight: bold;
}
</style>