<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="avatar">
                    <material-picker v-model="formData.avatar" />
                    <!-- <avaterUpload :formData="formData" value="avatar"></avaterUpload> -->
                </el-form-item>
                <el-form-item label="真实姓名" prop="real_name">
                    <el-input v-model="formData.real_name" clearable placeholder="请输入真实姓名" :readonly="false" />
                </el-form-item>
                <el-form-item label="用户昵称" prop="nickname">
                    <el-input v-model="formData.nickname" clearable placeholder="请输入用户昵称" :readonly="false" />
                </el-form-item>
                <el-form-item label="用户账号" prop="account">
                    <el-input v-model="formData.account" clearable placeholder="请输入用户账号" :readonly="false" />
                </el-form-item>
                <el-form-item label="用户密码" prop="password">
                    <el-input v-model="formData.password" clearable placeholder="请输入用户密码" :readonly="false" />
                </el-form-item>
                <el-form-item label="用户电话" prop="mobile">
                    <el-input v-model="formData.mobile" clearable placeholder="请输入用户电话" :readonly="false" />
                </el-form-item>
                <el-form-item label="用户性别" prop="sex">
                    <el-radio-group v-model="formData.sex" placeholder="请选择用户性别" :disabled="false">
                        <el-radio v-for="(item, index) in dictData.sex_type" :key="index" :label="parseInt(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否禁用" prop="is_disable">
                    <el-radio-group v-model="formData.is_disable" placeholder="请选择是否禁用" :disabled="false">
                        <el-radio v-for="(item, index) in dictData.system_disable" :key="index"
                            :label="parseInt(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="用户余额" prop="user_money">
                    <el-input v-model="formData.user_money" clearable placeholder="请输入用户余额" :readonly="false" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="userEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiUserAdd, apiUserEdit, apiUserDetail } from '@/api/user'
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: '',
    avatar: '',
    real_name: '',
    nickname: '',
    account: '',
    password: '',
    mobile: '',
    sex: '',
    is_disable: '',
    user_money: '',
})


// 表单验证
const formRules = reactive<any>({
    real_name: [{
        required: true,
        message: '请输入真实姓名',
        trigger: ['blur']
    }],
    nickname: [{
        required: true,
        message: '请输入用户昵称',
        trigger: ['blur']
    }],
    account: [{
        required: true,
        message: '请输入用户账号',
        trigger: ['blur']
    }],
    password: [{
        required: true,
        message: '请输入用户密码',
        trigger: ['blur']
    }],
    mobile: [{
        required: true,
        message: '请输入用户电话',
        trigger: ['blur']
    }]
})


// 获取详情
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]
        }
    }


}

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


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