<template>
    <div class="line-chart-wrapper">
        <div class="date-tabs">
            <div :class="active==i ? 'tab active' : 'tab'" v-for="(item,i) in tabs" @click="clickTab(item, i)">{{ item.label }}</div>
        </div>
        <div ref="LineChart" :class="className" :style="{height:height,width:width}" />
    </div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import api from '@/api/home'
export default {
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '320px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            dataQuery: {
                num: 1,
                type: ''
            },
            chart: null,
            active: 0,
            tabs: [
                { value: 1, label: '今天' },
                { value: 3, label: '3天' },
                { value: 7, label: '7天' },
                { value: 15, label: '15天' }
            ]
        }
    },
    watch: {
        // chartData: {
        //     deep: true,
        //     handler(val) {
        //         this.setOptions(val)
        //     }
        // }
    },
    methods: {
        getData() {
            this.chartData.dateData = []
            this.chartData.numData = []
            api.getTrendData(this.dataQuery).then(res => {
                console.log(res)
                const data = res.results
                data.resultList.forEach(item => {
                    this.chartData.dateData.push(item.createTime)
                    this.chartData.numData.push(item.total)
                })
                this.chartData.ymin =  Math.min.apply(null, this.chartData.numData)
                this.chartData.ymax = Math.max.apply(null, this.chartData.numData)
                // console.log(this.chartData.type, this.chartData.ymax, this.chartData.ymin)
                this.initChart()
            }).catch(err => {
                console.log(err)
            })
        },
        clickTab(item, i) {
            this.active = i
            this.dataQuery.num = item.value
            this.getData()
        },
        sidebarResizeHandler(e) {
            if (e.propertyName === 'width') {
                this.__resizeHandler()
            }
        },
        setOptions({ title, desc, dateData, numData, ymin, ymax } = {}) {
            this.chart.setOption({
                title: {
                    text: title + '趋势'
                },
                xAxis: {
                    data: dateData,
                    boundaryGap: false,
                    axisTick: {
                        show: false
                    }
                },
                grid: {
                    left: 30,
                    right: 40,
                    bottom: 20,
                    top: 65,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    },
                    padding: [5, 10]
                },
                yAxis: {
                    name: '数量',
                    type: 'value',
                    axisTick: {
                        show: false
                    },
                    min: ymin,
                    max: ymax
                },
                series: [
                    {
                        name: desc,
                        smooth: false,
                        type: 'line',
                        itemStyle: {
                            normal: {
                                areaStyle: {
                                    color: '#f3f8ff'
                                }
                            }
                        },
                        data: numData,
                        animationDuration: 2800,
                        animationEasing: 'quadraticOut'
                    }
                ]
            })
        },
        initChart() {
            this.chart = echarts.init(this.$refs.LineChart, 'macarons')
            this.setOptions(this.chartData)
        }
    },
    mounted() {
        this.dataQuery.type = this.chartData.type
        this.getData()

        if (this.autoResize) {
            this.__resizeHandler = debounce(() => {
                if (this.chart) {
                    this.chart.resize()
                }
            }, 100)
            window.addEventListener('resize', this.__resizeHandler)
        }

        // 监听侧边栏的变化
        const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
        sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        if (this.autoResize) {
            window.removeEventListener('resize', this.__resizeHandler)
        }

        const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
        sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

        this.chart.dispose()
        this.chart = null
    }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.line-chart-wrapper {
    position: relative;
    background-color: #fff;
    padding: 10px;
}
.date-tabs {
    display: flex;
    position: absolute;
    top: 15px;
    right: 40px;
    z-index: 1;
    .tab {
        font-size: .9rem;
        color: #36a3f7;
        border: 1px solid #36a3f7;
        border-right: none;
        padding: 5px 12px;
        cursor: pointer;
        &:last-child {
            border-right: 1px solid #36a3f7;
        }
    }
    .active {
        color: #fff;
        background: #36a3f7;
    }
}
</style>
