<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="用户id" prop="user_id">
                    <el-input v-model="formData.user_id" clearable placeholder="请输入用户id" />
                </el-form-item>
                <el-form-item label="到期时间" prop="expiration_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.expiration_time" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择到期时间">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="创建时间" prop="dtime">
                    <el-date-picker class="flex-1 !flex" v-model="formData.dtime" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择创建时间">
                    </el-date-picker>
                </el-form-item>
                <!-- <el-form-item label="" prop="status">
                    <el-select class="flex-1" v-model="formData.status" clearable placeholder="请选择">
                        <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="userMemberEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiUserMemberAdd, apiUserMemberEdit, apiUserMemberDetail } from '@/api/user_member'
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 == 'edit' ? '编辑会员表' : '新增会员表'
})

// 表单数据
const formData = reactive({
    id: '',
    user_id: '',
    expiration_time: '',
    dtime: '',
    update_dtime: '',
    status: '',
})


// 表单验证
const formRules = reactive<any>({

})


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

    //@ts-ignore
    formData.expiration_time = timeFormat(formData.expiration_time, 'yyyy-mm-dd hh:MM:ss')
    //@ts-ignore
    formData.dtime = timeFormat(formData.dtime, 'yyyy-mm-dd hh:MM:ss')
}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiUserMemberEdit(data)
        : await apiUserMemberAdd(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>
