<!-- 价格编辑弹窗 -->
<template>
    <ele-modal
        form
        :width="500"
        v-model="visible"
        :title="modalTitle"
        @closed="handleClosed"
    >
        <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="ISBN" prop="isbn">
                <el-input
                    v-model="form.isbn"
                    placeholder="请输入ISBN"
                    disabled
                />
            </el-form-item>

            <el-form-item label="书名" prop="bookName">
                <el-input v-model="form.bookName" placeholder="书名" disabled />
            </el-form-item>

            <!-- 零售价编辑 -->
            <el-form-item
                v-if="priceType === 'retail'"
                label="零售价"
                prop="retailPrice"
            >
                <el-input-number
                    v-model="form.retailPrice"
                    :min="0"
                    :precision="2"
                    :step="0.01"
                    placeholder="请输入零售价"
                    style="width: 100%"
                />
            </el-form-item>

            <!-- 批发价编辑 -->
            <el-form-item
                v-if="priceType === 'wholesale'"
                label="批发价"
                prop="wholesalePrice"
            >
                <el-input-number
                    v-model="form.wholesalePrice"
                    :min="0"
                    :precision="2"
                    :step="0.01"
                    placeholder="请输入批发价"
                    style="width: 100%"
                />
            </el-form-item>
        </el-form>

        <template #footer>
            <el-button @click="handleCancel">取消</el-button>
            <el-button type="primary" :loading="loading" @click="handleSubmit">
                保存
            </el-button>
        </template>
    </ele-modal>
</template>

<script setup>
    import { ref, reactive, computed } from 'vue';
    import { EleMessage } from 'ele-admin-plus/es';
    import request from '@/utils/request';

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

    /** 表单引用 */
    const formRef = ref(null);

    /** 加载状态 */
    const loading = ref(false);

    /** 价格类型 */
    const priceType = ref('retail'); // 'retail' 或 'wholesale'

    /** 弹窗标题 */
    const modalTitle = computed(() => {
        return priceType.value === 'retail' ? '零售价编辑' : '批发价编辑';
    });

    /** 表单数据 */
    const form = reactive({
        isbn: '',
        bookName: '',
        retailPrice: null,
        wholesalePrice: null
    });

    /** 表单验证规则 */
    const rules = computed(() => {
        if (priceType.value === 'retail') {
            return {
                retailPrice: [
                    {
                        required: true,
                        message: '请输入零售价',
                        trigger: 'blur'
                    },
                    {
                        type: 'number',
                        min: 0,
                        message: '零售价不能小于0',
                        trigger: 'blur'
                    }
                ]
            };
        } else {
            return {
                wholesalePrice: [
                    {
                        required: true,
                        message: '请输入批发价',
                        trigger: 'blur'
                    },
                    {
                        type: 'number',
                        min: 0,
                        message: '批发价不能小于0',
                        trigger: 'blur'
                    }
                ]
            };
        }
    });

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

    /** 弹窗关闭事件 */
    const handleClosed = () => {
        formRef.value?.resetFields();
        Object.assign(form, {
            isbn: '',
            bookName: '',
            retailPrice: null,
            wholesalePrice: null
        });
        loading.value = false;
    };

    /** 提交表单 */
    const handleSubmit = async () => {
        try {
            await formRef.value?.validate();
            loading.value = true;

            if (priceType.value === 'retail') {
                // 更新零售价
                await request.post(
                    '/activation/bookActivationInfo/updateRetailPrice',
                    {
                        isbn: form.isbn,
                        price: form.retailPrice
                    }
                );
                EleMessage.success('零售价更新成功');
            } else {
                // 更新批发价
                await request.post(
                    '/activation/bookActivationInfo/updateWholesalePrice',
                    {
                        isbn: form.isbn,
                        price: form.wholesalePrice
                    }
                );
                EleMessage.success('批发价更新成功');
            }

            visible.value = false;

            // 触发刷新事件
            emit('success');
        } catch (error) {
            console.error('价格更新失败:', error);
            EleMessage.error(error.message || '价格更新失败');
        } finally {
            loading.value = false;
        }
    };

    /** 弹窗打开事件 */
    const handleOpen = (data, type = 'retail') => {
        if (data) {
            priceType.value = type;
            Object.assign(form, {
                isbn: data.isbn || '',
                bookName: data.bookName || '',
                retailPrice: data.retailPrice || 0,
                wholesalePrice: data.wholesalePrice || 0
            });
            visible.value = true;
        }
    };

    /** 定义事件 */
    const emit = defineEmits(['success']);

    defineExpose({
        handleOpen
    });
</script>

<style scoped>
    :deep(.el-input-number) {
        width: 100%;
    }

    :deep(.el-input-number .el-input__inner) {
        text-align: left;
    }
</style>
