const app = getApp();
Page({
    data: {
        sid: -1,
        showSetMoney: false,
        currentMoneyText: 0,
        expendBalance: 0,
        incomeBalance: 0,
        priceError: false,
        visible : false,
        dateRangeText : null,
        dateRange : null,
        minDate: new Date().getTime() - 365 * 24 * 60 * 60 * 1000,
        maxDate: new Date().getTime(),
        incomeList : [],
        payList : []
    },
    onLoad: function (options) {
        let sid = options.sid
        let currentMoneyText = options.balance
        this.setData({sid,currentMoneyText})
        let dateRange = [new Date().getTime()- 24 * 60 * 60 * 1000,new Date().getTime()]
        let dateRangeText = app.$utils.formatDate(new Date(dateRange[0])) + '至' +
            (dateRange[1] ? app.$utils.formatDate(new Date(dateRange[1])) : app.$utils.formatDate(new Date()));
        this.setData({dateRange,dateRangeText})

    },
    onShow() {
        app.$api.listBillByAccount({
            id:this.data.sid,
            dateRangeText:this.data.dateRangeText,
        }).then(res => {
            if (res.data){
                let incomeList = res.data.incomeList;
                let payList = res.data.incomeList;
                let expendBalance = res.data.expendBalance;
                let incomeBalance = res.data.incomeBalance;
                this.setData({
                    incomeList,payList,expendBalance,incomeBalance
                })
            }
        })
    },
    handleCalendar() {
        this.setData({ visible: true });
    },
    handleConfirm(e) {
        const { value } = e.detail;
        console.log(value);
        let valueText = app.$utils.formatDate(new Date(value[0])) + '至' +
            (value[1] ? app.$utils.formatDate(new Date(value[1])) : app.$utils.formatDate(new Date()));
        this.setData({
            visible: false,
            dateRange: value,
            dateRangeText: valueText
        });
    },

    showDialog() {
        this.setData({
            showSetMoney: true,
            currentMoneyText: this.data.currentMoneyText
        })
    },
    onPriceInput(e) {
        this.setData({
            currentMoneyText: e.detail.value
        })
    },
    onConfirm () {
        const isNumber = /^\d+(\.\d+)?$/.test(this.data.currentMoneyText);
        if (!isNumber) {
            this.setData({
                priceError: true
            })
            return;
        }
        app.$api.editAccount({});
        this.setData({
            currentMoneyText: this.data.currentMoneyText,
            priceError: false
        })
        this.closeDialog()
    },
    closeDialog() {
        this.setData({
            showSetMoney: false
        })
    }
});
