import ActionSheet, { ActionSheetTheme } from '../../../miniprogram_npm/tdesign-miniprogram/action-sheet/index';
let handler = null;
const app = getApp();
Page({
    data: {
        isAccount: 0,
        isIncome: 0,
        isAdd: 0,
        showDeleteConfirm: false,
        id : -1,
        icon : null,
        settingType: null,
        name: null,
    },
    onLoad: function (options) {
        this.setData({
            isAccount : options.isAccount,
            isIncome : options.isIncome,
            isAdd : options.isAdd
        })
        if (options.isAdd == 0){
            this.setData({
                id : options.id
            })
            app.$api.getSetting(options.id).then(res => {
                if (res.data){
                    let name = res.data.name
                    let icon = res.data.icon
                    let settingType = res.data.settingType
                    this.setData({name,icon,settingType})
                }
            })
        }
        if (options.isAdd == 1){
            let settingType = 'CASH_SETTING';
            if (options.isAccount == 0){
                settingType = 'EXPEND_SETTING';
                if (options.isIncome == 1){
                    settingType = 'INCOME_SETTING'
                }
            }
            this.setData({settingType})
        }
    },
    handleMultiAction(e) {
        if (this.data.isAccount == 1) {
            handler = ActionSheet.show({
                theme: ActionSheetTheme.Grid,
                selector: '#t-action-sheet',
                context: this,
                items: app.globalData.accountIcon
            });
        }else {
            handler = ActionSheet.show({
                theme: ActionSheetTheme.Grid,
                selector: '#t-action-sheet',
                context: this,
                items: app.globalData.billIcon
            });
        }
    },
    onCancel(){
        handler.close();
    },
    handleSelected(e) {
        let {icon} = e.detail.selected
        this.setData({icon})
    },
    changeItem(e) {
        this.setData({ settingType : e.detail.value})
    },
    onNameInput(e) {
        this.setData({ name : e.detail.value})
    },
    saveSetting () {
        let param = {
            id : this.data.id,
            name : this.data.name,
            icon : this.data.icon,
            settingType : this.data.settingType
        }
        app.$api.editSetting(param).then(res => {
            if (res){
                wx.showToast({
                    title: '保存成功！',
                    icon: 'none',
                    duration: 2000
                })
                wx.navigateBack();
            }
        })
    },
    deleteSetting() {
        this.setData({
            showDeleteConfirm: true
        })
    },
    onConfirm (e) {
        app.$api.deleteSetting(this.data.id).then(res => {
            if (res){
                wx.navigateBack();
                this.closeDialog();
            }
        });
    },
    closeDialog() {
        this.setData({
            showDeleteConfirm: false
        })
    }
});
