<!-- 编辑弹窗 -->
<template>
    <ele-modal
        form
        :width="560"
        v-model="visible"
        title="修改回收折扣"
        @open="handleOpen"
    >
        <SimpleForm
            :items="items"
            labelWidth="140px"
            ref="formRef"
            :initKeys="form"
        />
        <template #footer>
            <el-button @click="handleCancel">关闭</el-button>
            <el-button type="primary" @click="handleSumbit">确定</el-button>
        </template>
    </ele-modal>
</template>

<script setup>
    import { ref, reactive, nextTick } from 'vue';
    import { Flag, ChatDotSquare } from '@element-plus/icons-vue';
    import orderTimeline from '@/views/recycleOrder/components/order-timeline.vue';
    import SimpleForm from '@/components/CommonPage/SimpleForm.vue';
    import validators from '@/utils/validators';

    /** 弹窗是否打开 */
    const visible = defineModel({ type: Boolean });

    /** 关闭弹窗 */
    const handleCancel = () => {
        visible.value = false;
        nextTick(() => {
            formRef.value?.resetForm();
        });
    };

    const form = reactive({
        maxCount: '',
        discount: '0.01',
        number: '',
        status: '1'
    });
    /** 弹窗打开事件 */
    const handleOpen = () => {
        visible.value = true;
        nextTick(() => {
            formRef.value?.assignFields(form);
        });
    };

    const items = reactive([
        {
            label: '回收折扣',
            prop: 'discount',
            type: 'input',
            required: true,
            itemProps: {
                rules: [validators.decimalRange10]
            }
        },
        {
            label: '最大回收数量',
            prop: 'maxCount',
            type: 'input',
            required: true,
            itemProps: {
                rules: [validators.nonNegativeInteger]
            }
        },
        {
            label: '单个订单回收数量',
            prop: 'number',
            type: 'input',
            required: true,
            itemProps: {
                rules: [validators.nonNegativeInteger]
            }
        },
        {
            label: '状态',
            prop: 'status',
            type: 'dictRadio',
            props: { code: 'discount_status' },
            required: true
        }
    ]);

    const formRef = ref();
    const handleSumbit = () => {
        formRef.value?.submitForm().then((data) => {
            visible.value = false;
            console.log(data);
        });
    };

    defineExpose({
        handleOpen
    });
</script>
