<!-- 通知公告-资讯来源-编辑 -->
<template>
    <app-drawer :title="t('investor.user.open.edit.title')" width="480" v-model:show="show" :refresh="refresh"
        :loading="loading">
        <el-form ref="formRef" class="el-form--vertical" label-width="100px" :model="formData" :rules="formRules"
            :show-message="false">
            <el-form-item :label="t('notice.newssource.edit.membername')" prop="membername">
                <el-input v-model="formData.membername" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('notice.newssource.edit.membertags')" prop="membertags">
                <el-input v-model="formData.membertags" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('notice.newssource.edit.imageurl')" prop="imageurl">
                <app-upload v-model="uploadFiles" :file-types="['image']" :max-size="uploadConfig.maxFileSize" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.submit') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType, onMounted } from 'vue'
import { ElMessage, FormInstance, FormRules, UploadUserFile } from 'element-plus'
import { parseFilePaths, extractFilePaths } from '@/filters'
import { uploadConfig } from '@/constants/common'
import { useRequest } from '@/hooks/request'
import { dealcolumnsource, columnsourceDetail } from '@/services/api/notice'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppUpload from '@pc/components/base/upload/index.vue'

const props = defineProps({
    record: {
        type: Object as PropType<Notice.ColumnSourceQueryRsp>
    }
})

const { global: { t } } = i18n
const formRef = ref<FormInstance>()
const show = ref(true)
const refresh = ref(false)
const uploadFiles = ref<UploadUserFile[]>([])

const formData = ref<Notice.DealColumnSourceReq>({
    membername: ''
})

const { loading, run } = useRequest(columnsourceDetail, {
    manual: true,
    onSuccess: ((res) => {
        const { memberid, imageurl, membername, membertags } = res.data
        formData.value = {
            memberid,
            imageurl,
            membername,
            membertags
        }
        uploadFiles.value = parseFilePaths(imageurl)
    }),
    onError: (err) => {
        ElMessage.error(err)
    }
})

// 表单验证规则
const formRules: FormRules = {
    membername: [{ required: true }],
}

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    const rawData = { ...formData.value }
    rawData.imageurl = extractFilePaths(uploadFiles.value)

    formRef.value?.validate((valid) => {
        if (valid) {
            loading.value = true
            dealcolumnsource({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}

onMounted(() => {
    const memberid = props.record?.memberid
    if (memberid) {
        run({ memberid })
    }
})
</script>