<template>
    <widget-card :bodyStyle="bodyStyle" ref="container">
        <template #header>拉新统计</template>
        <div class="top" ref="top">
            <div class="top-content">
                <div class="top-item">
                    <div class="text1">今日新增(人)</div>
                    <div class="text2">{{ today }}</div>
                </div>
            </div>
            <div class="top-content">
                <div class="top-item">
                    <div class="text1">累计注册(人)</div>
                    <div class="text2">{{ info.userNum }}</div>
                </div>
            </div>
        </div>
        <div class="chart-box">
            <canvas ref="chart" class="chart"></canvas>
        </div>
    </widget-card>
</template>
<script>
import WidgetCard from './WidgetCard';
import VueCharts from 'vue-chartjs';
import { format } from 'date-fns';
import addDays from 'date-fns/esm/addDays';

export default {
    props: {
        info: {
            type: Object,
            default: () => {
                return {};
            }
        }
    },
    data() {
        return {
            myChart: null,
            bodyStyle: {
                display: 'flex',
                flexDirection: 'column',
                alignItems: 'center'
            },
            today: 0
        };
    },
    mounted() {
        setTimeout(() => {
            this.$nextTick(() => {
                this.$refs.chart.width = this.$refs.container.$el.offsetWidth - 20;
                this.$refs.chart.height = this.$refs.container.$el.offsetHeight - this.$refs.top.offsetHeight - 100;
                this.init();
            });
        }, 500);
    },
    methods: {
        init() {
            this.$http.get('/statistic/userTrend', { day: 7 }).then(res => {
                let start = addDays(new Date(), -6);
                let days = [];
                let datas = [];
                for (let i = 0; i < 7; i++) {
                    days.push(format(addDays(start, i), 'MM-dd'));
                    datas.push(res[format(addDays(start, i), 'yyyy-MM-dd')] || 0);
                }

                this.today = res[format(new Date(), 'yyyy-MM-dd')] || 0;

                this.myChart = new Chart(this.$refs.chart.getContext('2d'), {
                    type: 'line',
                    data: {
                        labels: days,
                        datasets: [
                            {
                                label: '新增人数',
                                borderColor: '#409EFF',
                                backgroundColor: '#409EFF50',
                                data: datas.map(item => {
                                    return Number(item);
                                })
                            }
                        ]
                    },
                    options: {
                        title: { text: '7日新增用户趋势', display: true },
                        legend: { display: false },
                        responsive: true,
                        maintainAspectRatio: false
                    }
                });
            });
        }
    },
    components: {
        WidgetCard
    }
};
</script>
<style lang="less" scoped>
.top {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-around;
    align-self: stretch;
}
.top-content {
    width: 50%;
    display: flex;
    justify-content: center;
    &:nth-child(2) {
        .top-item {
            .text2 {
                color: #4dcc6f;
            }
        }
    }
}
.top-item {
    width: 153px;
    height: 125px;
    background: #f5f7fa;
    border-radius: 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .text1 {
        font-size: 14px;
        font-weight: bold;
        color: #666666;
        line-height: 20px;
    }

    .text2 {
        font-size: 22px;
        font-weight: bold;
        color: #409eff;
        line-height: 29px;
        margin-top: 2px;
    }
}
</style>
