<!-- 编辑弹窗 -->
<template>
    <ele-modal
        form
        :width="460"
        v-model="visible"
        title="加入黑名单"
        @open="handleOpen"
    >
        <SimpleForm
            :items="items"
            labelWidth="90px"
            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 SimpleForm from '@/components/CommonPage/SimpleForm.vue';
    import { ElMessage } from 'element-plus';
    import request from '@/utils/request';

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

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

    const form = reactive({
        reason: ''
    });

    const items = reactive([
        {
            label: '拉黑原因',
            prop: 'reason',
            type: 'dictSelect',
            required: true,
            props: {
                code: 'book_black_reason',
                value: 'dictLabel',
                label: 'dictLabel'
            }
        }
    ]);

    const formRef = ref();
    const emit = defineEmits(['refresh']);
    const currentRow = ref();

    const handleOpen = (isbn) => {
        visible.value = true;
        nextTick(() => {
            if (isbn) {
                currentRow.value = isbn;
            }
        });
    };

    const handleSumbit = () => {
        formRef.value?.submitForm().then((data) => {
            request
                .post('/book/bookRecycleInfo/addBlackList', {
                    isbnList: currentRow.value,
                    reason: data.reason
                })
                .then(() => {
                    ElMessage.success('操作成功');
                    visible.value = false;
                    emit('refresh');
                });
        });
    };

    defineExpose({
        handleOpen
    });
</script>
