const app = getApp();
import ActionSheet, { ActionSheetTheme } from '../../../miniprogram_npm/tdesign-miniprogram/action-sheet/index';
let handler = null;
const firstGrid = [
    {
        label: '红包',
        icon: 'star',
    },
    {
        label: '数码',
        icon: 'star',
    },
    {
        label: '虚拟',
        icon: 'star',
    },
    {
        label: '游戏',
        icon: 'star',
    },
    {
        label: '包包',
        icon: 'star',
    },
    {
        label: '手机',
        icon: 'refresh',
    },
    {
        label: '话费',
        icon: 'download',
    },
    {
        label: '技术',
        icon: 'queue',
    },
];
const secondGrid = [
    {
        label: '银行卡',
        icon: 'star',
    },
    {
        label: '支付宝',
        icon: 'star',
    },
    {
        label: '微信',
        icon: 'star',
    },
    {
        label: '信用卡',
        icon: 'star',
    }
];
Component({
    properties: {
        /* income & out */
        changeType: {
            type: String,
            value: 'income'
        },
    },
    data: {
        price: '10.2',
        priceError: false,
        grid: '',
        outText: '',
        inText: '',
        mode: '',
        dateVisible: false,
        date: new Date().getTime(), // 支持时间戳传入
        dateText: '',
        // 指定选择区间起始值
        start: '2000-01-01 00:00:00',
        end: '2030-09-09 12:12:12',
    },
    methods: {
        onPriceInput(e) {
            const { priceError } = this.data;
            const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
            if (priceError === isNumber) {
                this.setData({
                    priceError: !isNumber,
                });
            }
        },
        handleMultiAction(e) {
            const { grid } = e.currentTarget.dataset;
            this.setData({
                grid
            })
            if (grid == 'type') {
                handler = ActionSheet.show({
                    theme: ActionSheetTheme.Grid,
                    selector: '#t-action-sheet',
                    context: this,
                    items: firstGrid.concat(
                        new Array(8).fill({
                            label: '标题文字',
                            icon: 'star',
                        }),
                    ),
                });
            }else {
                handler = ActionSheet.show({
                    theme: ActionSheetTheme.Grid,
                    selector: '#t-action-sheet',
                    context: this,
                    items: secondGrid.concat(
                        new Array(8).fill({
                            label: '标题文字',
                            icon: 'star',
                        }),
                    ),
                });
            }
        },
        onCancel(){
            handler.close();
        },
        handleSelected(e) {
            const {grid} = this.data
            this.setData({
                [`${grid}Text`]: e.detail.selected.label,
            });
        },
        showPicker(e) {
            const { mode } = e.currentTarget.dataset;
            this.setData({
                mode,
                [`${mode}Visible`]: true,
            });
        },
        hidePicker() {
            const { mode } = this.data;
            this.setData({
                [`${mode}Visible`]: false,
            });
        },
        onConfirm(e) {
            const { value } = e.detail;
            const { mode } = this.data;
            this.setData({
                [mode]: value,
                [`${mode}Text`]: value,
            });
            this.hidePicker();
        },
        bYesterday () {
            let preDate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * 2);
            this.setData({
                dateText: app.$utils.formatDate(preDate)
            })
        },
        yesterday() {
            let preDate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
            this.setData({
                dateText: app.$utils.formatDate(preDate)
            })
        },
        today () {
            this.setData({
                dateText: app.$utils.formatDate(new Date())
            })
        },
        saveBill () {
            wx.navigateBack();
        }
    }
});
