const app = getApp();
import ActionSheet, { ActionSheetTheme } from '../../../miniprogram_npm/tdesign-miniprogram/action-sheet/index';
let handler = null;
Component({
    properties: {
        billId: {
            type: String,
            value: -1
        },
        /* transfer & repayment */
        changeType: {
            type: String,
            value: 'TRANSFER'
        },
    },
    data: {
        transferGrid1: [],
        transferGrid2: [],
        repaymentGrid1: [],
        repaymentGrid2: [],
        priceError: false,
        grid: '',
        typeText: '',
        accountText: '',
        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,
        fromAccount: null,
        account: null,
        moneyType: null,
        remark: null,
    },
    attached(){
        app.$api.getSettings().then(setting => {
            if (setting){
                let cash = setting.data.CASH_SETTING;
                let owe = setting.data.OWE_SETTING;
                let transferGrid1 = []
                let transferGrid2 = []
                let repaymentGrid1 = []
                let repaymentGrid2 = []
                cash.forEach(x=>{
                    transferGrid1 = transferGrid1.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                    transferGrid2 = transferGrid2.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                    repaymentGrid1 = repaymentGrid1.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });

                })
                owe.forEach(x=>{
                    repaymentGrid2 = repaymentGrid2.concat({
                        id: x.id,
                        label: x.name,
                        icon: x.icon,
                    });
                })
                this.setData({transferGrid1,transferGrid2,repaymentGrid1,repaymentGrid2})
            }
        })
        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 fromAccount = res.data.fromAccount;
                    let account = res.data.account;
                    let outText = res.data.fromAccountName;
                    let inText = res.data.accountName;
                    let moneyType = res.data.moneyType;
                    let remark = res.data.remark;
                    let dateText = res.data.date;
                    this.setData({
                        money,billType,fromAccount,account,outText,inText,moneyType,remark,dateText
                    })
                }
            })
        }else {
            let billType = 'TRANSFER'
            let moneyType = -1
            if ('REPAYMENT' == this.properties.changeType){
                billType = 'REPAYMENT'
                moneyType = -2
            }
            this.setData({billType,moneyType})
        }
    },
    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 == 'out') {
                handler = ActionSheet.show({
                    theme: ActionSheetTheme.Grid,
                    selector: '#t-action-sheet',
                    context: this,
                    items: this.properties.changeType == 'REPAYMENT' ? this.data.repaymentGrid1 : this.data.transferGrid1
                });
            }else {
                handler = ActionSheet.show({
                    theme: ActionSheetTheme.Grid,
                    selector: '#t-action-sheet',
                    context: this,
                    items: this.properties.changeType == 'REPAYMENT' ? this.data.repaymentGrid2 : this.data.transferGrid2
                });
            }
        },
        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 == 'out'){
                this.setData({fromAccount:e.detail.selected.id})
            }
            if (grid == 'in'){
                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,
                fromAccount:this.data.fromAccount,
                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();
                }
            })
        }
    }
});
