import {
    getSaleData,
    getIncomeList
} from '~/api/sale'
import event from '~/mixins/event'

Page({
    behaviors: [event],
    data: {
        currentIndex: 1,
        categoryList: [{
            id: 1,
            title: '全部'
        }, {
            id: 2,
            title: '7日'
        }, {
            id: 3,
            title: '月'
        }, {
            id: 4,
            title: '季'
        }, {
            id: 5,
            title: '半年'
        }],
        allIncome: {},
        orderList: {},
        orderListKey: []
    },
    async onShow() {
        let allIncome = await getSaleData()
        this.getData()
        this.setData({
            allIncome,
        })
    },
    async getData() {
        let data = await getIncomeList({
            pageSize: 1000,
            type: this.data.currentIndex
        })
        let detailedList = {}
        data.list.forEach(item => {
            if (detailedList.hasOwnProperty(item.dateStr)) {
                detailedList[item.dateStr].push(item)
            } else {
                detailedList[item.dateStr] = [item]
            }
        })
        this.setData({
            orderList: detailedList,
            orderListKey: Object.keys(detailedList)
        })
    },
    setClass({
        currentTarget
    }) {
        this.setData({
            currentIndex: currentTarget.dataset.index,
        });
        this.getData()
    },
})