<!-- 修改订单回收量弹窗 -->
<template>
    <ele-modal form :width="460" v-model="visible" title="修改订单回收量" @open="handleOpen">
        <SimpleForm :items="items" labelWidth="140px" ref="formRef" :initKeys="form"></SimpleForm>
        <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 SimpleForm from '@/components/CommonPage/SimpleForm.vue';
import validators from '@/utils/validators';
import request from '@/utils/request';

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

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

const form = ref({
    isbn: '',
    recycleMaxPerOrder: ''
});

/** 弹窗打开事件 */
const handleOpen = (data) => {
    visible.value = true;
    nextTick(() => {
        if (data?.isbn) {
            form.value.isbn = data.isbn;
            getParams(data.isbn);
        }
    });
};

//根据isbn获取参数
const getParams = (isbn) => {
    request.get(`/book/bookRecycleInfo/getSepSet/${isbn}`).then((res) => {
        let data = res.data.data;
        form.value.recycleMaxPerOrder = data.recycleMaxPerOrder;
        formRef.value?.setData(form.value);
    });
};

const items = reactive([
    {
        label: '单个订单回收数量',
        prop: 'recycleMaxPerOrder',
        type: 'input',
        required: true,
        rules: [validators.nonNegativeInteger]
    }
]);

const formRef = ref();

const emit = defineEmits(['refresh']);
const handleSumbit = () => {
    formRef.value?.submitForm().then((data) => {
        data.isbn = form.value.isbn;
        request.post('/book/bookRecycleInfo/updateRecyclePerMax', data).then(() => {
            visible.value = false;
            ElMessage.success('修改成功');
            emit('refresh');
        });
    });
};

defineExpose({
    handleOpen
});
</script> 