<template>
    <div class="edit-popup">
        <popup
            title="修改用户余额"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
            ref="popupRef"
        >
            <el-form ref="formRef" :model="formData" label-width="90px">
                <el-form-item label="修改余额" prop="username">
                    <el-radio-group v-model="formData.type">
                        <el-radio value="1" size="large" label="1">增加</el-radio>
                        <el-radio value="-1" size="large" label="-1">减少</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="金额">
                    <el-input-number v-model="formData.now_money" :min="0.01" :max="99999" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="formData.mark" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="balance">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { setnowMoney } from '@/api/user'

const formRef = shallowRef<FormInstance>()
const emit = defineEmits(['success', 'close'])
const popupRef = shallowRef<InstanceType<typeof Popup>>()

// 表单数据
const formData = reactive({
    id: '',
    type: '1',
    now_money: '',
    mark: ''
})
//打开弹窗
const open = (type = 'add') => {
    popupRef.value?.open()
}
// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    await setnowMoney(data)
    // const data = { ...formData }
    // await apiUserEdit(data)

    popupRef.value?.close()
    emit('success')
}
// 获取详情
const setFormData = async (data: Record<any, any>) => {
    formData.id = data.id
}
// 关闭回调
const handleClose = () => {
    emit('close')
}
defineExpose({
    open,
    setFormData
})
</script>
