const app = getApp();
import ActionSheet, { ActionSheetTheme } from '../../../miniprogram_npm/tdesign-miniprogram/action-sheet/index';
let handler = null;
Component({
    properties: {
        billId: {
            type: String,
            value: -1
        },
        /* income & expend */
        changeType: {
            type: String,
            value: 'INCOME'
        },
    },
    data: {
        incomeGrid1: [],
        incomeGrid2: [],
        expendGrid1: [],
        expendGrid2: [],

        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',

        money: null,
        billType: null,
        account: null,
        moneyType: null,
        remark: null,
    },
    attached(){
        let incomeGrid1 = [];
        let incomeGrid2 = [];
        let expendGrid1 = [];
        let expendGrid2 = [];
        app.$api.getSettings().then(setting => {
            if (setting){
                let expend = setting.data.EXPEND_SETTING;
                let income = setting.data.INCOME_SETTING;
                let cash = setting.data.CASH_SETTING;
                let owe = setting.data.OWE_SETTING;
                expend.forEach(x=>{
                    expendGrid1 = expendGrid1.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                })
                income.forEach(x=>{
                    incomeGrid1 = incomeGrid1.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                })
                cash.forEach(x=>{
                    incomeGrid2 = incomeGrid2.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                    expendGrid2 = expendGrid2.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                })
                owe.forEach(x=>{
                    incomeGrid2 = incomeGrid2.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                    expendGrid2 = expendGrid2.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                })
                this.setData({expendGrid1,expendGrid2,incomeGrid1,incomeGrid2})
            }
        });
        if (this.properties.billId != -1){
            app.$api.getBill(this.properties.billId).then(res => {
                if (res.data){
                    let money = res.data.money;
                    let billType = res.data.billType;
                    let account = res.data.account;
                    let moneyType = res.data.moneyType;
                    let typeText = res.data.moneyName;
                    let accountText = res.data.accountName;
                    let remark = res.data.remark;
                    let dateText = res.data.date;
                    this.setData({
                        money,billType,account,moneyType,typeText,accountText,remark,dateText
                    })
                }
            })
        }else {
            let billType = this.properties.changeType
            this.setData({billType})
        }
    },
    methods: {
        onPriceInput(e) {
            const { priceError } = this.data;
            const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
            if (priceError === isNumber) {
                this.setData({
                    priceError: !isNumber,
                });
            }
            let money = e.detail.value;
            this.setData({money})

        },
        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: this.properties.changeType == 'EXPEND' ? this.data.expendGrid1 : this.data.incomeGrid1
                });
            }else {
                handler = ActionSheet.show({
                    theme: ActionSheetTheme.Grid,
                    selector: '#t-action-sheet',
                    context: this,
                    items: this.properties.changeType == 'EXPEND' ? this.data.expendGrid2 : this.data.incomeGrid2
                });
            }
        },
        onCancel(){
            handler.close();
        },
        onRemarkInput(e) {
            this.setData({ remark : e.detail.value})
        },
        handleSelected(e) {
            const {grid} = this.data
            this.setData({
                [`${grid}Text`]: e.detail.selected.label,
            });
            if (grid == 'type'){
                this.setData({moneyType:e.detail.selected.id})
            }
            if (grid == 'account'){
                this.setData({account:e.detail.selected.id})
            }
        },
        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 () {
            let param = {
                id : this.properties.billId,
                money:this.data.money,
                billType:this.data.billType,
                account:this.data.account,
                moneyType:this.data.moneyType,
                remark:this.data.remark,
                date:this.data.dateText
            }
            app.$api.editBill(param).then(res => {
                if (res) {
                    wx.navigateBack();
                }
            })
        }
    }
});
