<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="提现金额" prop="amount">
                    <el-input v-model="formData.amount" clearable placeholder="请输入提现金额" disabled />
                </el-form-item>
                <el-form-item label="提现类型" prop="tx_type">
                    {{ Number(formData.tx_type) == 1 ? '本人提现' : '茶艺师账户提现' }}
                    <!-- <el-input v-model="formData.tx_type" clearable disabled /> -->
                </el-form-item>
                <el-form-item label="提现状态" prop="status">
                    <el-select class="flex-1" v-model="formData.status" clearable placeholder="请选择"
                        @change="handleStatusChange">
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="通过" :value="1"></el-option>
                        <el-option label="拒绝" :value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item v-if="Number(formData.tx_type) === 1" label="打款证明" prop="voucher" required>
                    <material-picker v-model="formData.voucher" :limit="1" />
                </el-form-item>
                <!-- <el-form-item label="1本人提现 2茶艺师账户提现" prop="tx_type"> -->
                <!-- <el-select class="flex-1" v-model="formData.tx_type" clearable placeholder="请选择1本人提现 2茶艺师账户提现">
                        <el-option 
                            v-for="(item, index) in dictData."
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                </el-form-item> -->
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="brokerReflectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBrokerReflectAdd, apiBrokerReflectEdit, apiBrokerReflectDetail } from '@/api/broker_reflect'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value = '提现审核'
})

// 表单数据
const formData = reactive({
    id: '',
    amount: '',
    voucher: '',
    status: '',
    tx_type: '',
})


// 表单验证
const formRules = reactive<any>({
    voucher: [
        {
            validator: (_rule: any, value: string, callback: (error?: Error) => void) => {
                if (Number(formData.tx_type) === 1 && !value) {
                    callback(new Error('请上传打款证明'))
                    return
                }
                callback()
            },
            trigger: ['change', 'blur']
        }
    ]
})

const handleStatusChange = () => {
    if (Number(formData.tx_type) !== 1) {
        formData.voucher = ''
        formRef.value?.clearValidate(['voucher'])
        return
    }
    nextTick(() => {
        formRef.value?.validateField('voucher')
    })
}

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    console.log("🚀 ~ setFormData ~ data:", data)
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }

}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiBrokerReflectDetail({
        dtime: row.dtime
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiBrokerReflectEdit(data)
        : await apiBrokerReflectAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
