<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>平台用户行为数据统计与趋势图</span>
                </div>
                <div class="main"></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="name" label="统计" align="center" />
                    <el-table-column prop="today" label="今日" align="center" />
                    <el-table-column prop="yesterday" label="昨日" align="center" />
                    <el-table-column prop="month" label="本月" align="center" />
                </el-table>
                <Chart class="chart" :option="options" />
            </div>
        </div>
    </div>
</template>

<script setup>
import { onBeforeMount, ref } from 'vue'
import Chart from '@/components/charts/index.vue'
import { getIndexUserStatistic } from '@/api/dashboard.js'

// 表格数据
const tableData = ref([
    { name: '注册数', today: 0, yesterday: 0, month: 0 },
    { name: '新增数(ZID)', today: 0, yesterday: 0, month: 0 },
    { name: '登录数', today: 0, yesterday: 0, month: 0 },
    { name: '新增封禁', today: 0, yesterday: 0, month: 0 },
    { name: '总封禁数', today: 0, yesterday: 0, month: 0 },
    { name: '新增封号', today: 0, yesterday: 0, month: 0 },
    { name: '总封号', today: 0, yesterday: 0, month: 0 },
    { name: '风控率', today: 0, yesterday: 0, month: 0 },
]);

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({
    grid: {
        top: 30,
        right: 20,
        bottom: 0,  // 可根据需要设为 0~40
        left: 40,
        containLabel: true
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        // data: ['新增数(ZID)', '注册数', '登录数', '新增封禁', '总封禁', '新增封号', '总封号']
        data: ['注册数', '新增封禁', '新增封号'],
        textStyle: {
            color: '#808080'
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: xAxisList
    },
    yAxis: {
        type: 'value'
    },
    series: []
})

const getIndexUserStatisticData = async () => {
    let res = await getIndexUserStatistic()
    res = res.data
    tableData.value = [
        { name: '注册数', today: res.today, yesterday: res.yesterday, month: res.month },
        // { name: '新增数(ZID)', today: 0, yesterday: 0, month: 0 },
        { name: '登录数', today: res.todayLogin, yesterday: res.yesterdayLogin, month: res.monthLogin },
        { name: '新增封禁', today: res.todayLock, yesterday: res.yesterdayLock, month: res.monthLock },
        // { name: '总封禁数', today: 0, yesterday: 0, month: 0 },
        // { name: '新增封号', today: res.todayRisk, yesterday: res.yesterdayRisk, month: res.monthRisk },
        // { name: '总封号', today: 0, yesterday: 0, month: 0 }，
        { name: '风控率', today: getPercent(res.todayLock,res.today), yesterday: getPercent(res.yesterdayLock,res.yesterday), month: getPercent(res.monthLock,res.month) },
    ]

    const { todayUser, todayLockHour, todayRiskHour } = res

    options.value.series = [
        {
            name: '注册数',
            type: 'line',
            data: formatData(todayUser)
        },
        {
            name: '新增封禁',
            type: 'line',
            data: formatData(todayLockHour)
        },
        /* {
            name: '新增封号',
            type: 'line',
            data: formatData(todayRiskHour)
        }, */
    ]
}

function getPercent(a, b) {
  if (a===0 || b === 0) return '0%';
  let percent = (a / b * 100).toFixed(2);
  return percent + '%';
}


function formatData(apiData) {
    const result = []

    for (let i = 0; i < 24; i++) {
        // 找到 apiData 中是否有该小时的数据
        const key = Object.keys(apiData).find(k => {
            const d = new Date(k)
            return d.getHours() === i
        })

        result.push(
            key ? apiData[key] : 0
        )
    }

    return result
}

onBeforeMount(async () => {
    await getIndexUserStatisticData()
})

</script>

<style lang="scss" scoped>
.box {
    margin: 20px auto 0;
    width: calc(100% - 40px);
    /* height: 400px; */
    background: var(--system-page-background);
    padding: 20px 20px 10px;
    overflow: hidden;
}

.layout-container-main {
    display: flex;
    /* margin: 0 1.25rem; */
    flex-wrap: wrap;
    justify-content: space-between;

    .item {
        width: calc(100vw - 1.13rem - 1.31rem);
        /* width: 40vw; */
        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;

            .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-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% / 8);
                height: calc(58.1vh / 8);
                /* 均匀分7行 */
            }

            .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% / 8);
                }

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


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

:deep(.el-table__header th) {
    background-color: var(--system-page-background);
    font-weight: bold;
}

: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);
  }
}
</style>