<template>
    <simple-form-modal
        :title="title"
        :items="formItems"
        ref="editRef"
        label-width="110px"
        :submit-handler="handleSubmit"
        @success="handleSuccess"
    >
        <template #isbn="{ model }">
            <el-input
                v-model="model.isbn"
                placeholder="请输入"
                @blur="() => checkIsbn(model)"
            />
        </template>
    </simple-form-modal>
</template>

<script setup>
    import { ref, computed, nextTick } from 'vue';
    import { ElMessage } from 'element-plus';
    import SimpleFormModal from '@/components/CommonPage/SimpleFormModal.vue';
    import request from '@/utils/request';
    import { useDictData } from '@/utils/use-dict-data';

    const emit = defineEmits(['success']);
    const editRef = ref(null);
    const title = ref('添加图书');
    const currentId = ref(null);
    const [complaint_reason] = useDictData(['complaint_reason']);

    const formItems = computed(() => [
        {
            type: 'isbn', // Custom slot
            label: 'ISBN：',
            prop: 'isbn',
            required: true
        },
        {
            type: 'checkbox',
            label: '易投诉平台：',
            prop: 'platformList',
            required: true,
            options: [
                { label: '拼多多', value: '拼多多' },
                { label: '淘宝', value: '淘宝' },
                { label: '小程序', value: '小程序' },
                { label: '孔网', value: '孔网' }
            ]
        },
        {
            type: 'checkbox',
            label: '易投诉原因：',
            prop: 'reasonList',
            required: true,
            options: complaint_reason.value.map((item) => ({
                label: item.dictLabel,
                value: item.dictValue
            }))
        }
    ]);

    const checkIsbn = (model) => {
        // Mock check removed or implement real check if needed
    };

    const handleOpen = (row) => {
        if (row) {
            title.value = '编辑图书';
            currentId.value = row.id;
            request.get(`/shop/complaintBook/getInfo/${row.id}`).then((res) => {
                const data = res.data.data || res.data;
                nextTick(() => {
                    editRef.value?.handleOpen({
                        id: data.id,
                        isbn: data.isbn,
                        platformList: data.platformList || [],
                        reasonList: data.reasonList || []
                    });
                });
            });
        } else {
            title.value = '添加图书';
            currentId.value = null;
            nextTick(() => {
                editRef.value?.handleOpen({
                    isbn: '',
                    platformList: [],
                    reasonList: []
                });
            });
        }
    };

    const handleSubmit = (model) => {
        return request.post('/shop/complaintBook/setComplaintBook', {
            id: currentId.value,
            isbn: model.isbn,
            platformList: model.platformList,
            reasonList: model.reasonList
        });
    };

    const handleSuccess = () => {
        emit('success');
    };

    defineExpose({
        handleOpen
    });
</script>
