// commpents/form/index.js
const app = getApp()
Component({
    options: {
        multipleSlots: true // 在组件定义时的选项中启用多 slot 支持
    },
    /**
     * 组件的属性列表
     */
    properties: {
        fields: { type: Array },
    },

    /**
     * 组件的初始数据
     */
    data: {
        form: {
            icon: []
        },
        isSubmit: false,
        fileList: [],
    },

    /**
     * 组件的方法列表
     */
    methods: {
        toSubmit() {
            const form = this.data.form;
            this.triggerEvent('submit', form)
        },
        toSelected(event) {
            const data = event?.detail;
            const form = this.data.form;
            const { value, model } = data;
            form[model] = value;
            this.setData({ form })

        },
        onChange(event) {
            // event.detail 为当前输入的值
            const data = this.data.form;
            const model = event.target?.dataset?.model;
            const value = event?.detail;
            if (!model) return;
            data[model] = value;
            this.setData({ form: data })
        },
        onOChange(event) {
            const data = this.data.form;
            const model = event.target?.dataset?.model;
            const value = event?.detail?.value;
            if (!model) return;
            data[model] = value;
            this.setData({ form: data })

        },
        uploadIcon(event) {
            const data = event?.detail;
            if (!data) return;
            const form = this.data.form;
            if (!form?.icon) form.icon = []
            form.icon.push(data);
            this.setData({ form })
        },
        deleteImg(event) {
            const i = event?.detail?.index;
            if (i <= -1) return;
            const form = this.data.form;
            form.icon.splice(i, 1)
            this.setData({ form })
        }
    }
})
