<!-- 通知公告-资讯管理-编辑 -->
<template>
    <app-drawer :title="t('investor.user.open.edit.title')" width="900" v-model:show="show" :refresh="refresh"
        :loading="loading">
        <el-form ref="formRef" class="el-form--horizontal" label-width="100px" :model="formData" :rules="formRules"
            :show-message="false">
            <el-form-item :label="t('notice.news.edit.title1')" prop="title">
                <el-input v-model="formData.title" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('notice.news.edit.columnid')" prop="columnid">
                <el-select v-model="formData.columnid">
                    <el-option v-for="item in data?.siteColumnconfig" :key="item.id" :label="item.columnname"
                        :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('notice.news.edit.author')" prop="author">
                <el-input v-model="formData.author" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('notice.news.edit.srcid')" prop="srcid">
                <el-select v-model="formData.srcid">
                    <el-option v-for="item in data?.gzMemberinfo" :key="item.memberid" :label="item.membername"
                        :value="item.memberid" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('notice.news.edit.hits')" prop="hits">
                <el-input-number v-model="formData.hits" :placeholder="t('common.pleaseenter')" :min="0" />
            </el-form-item>
            <el-form-item :label="t('notice.news.edit.publishdate')" prop="publishdate">
                <el-date-picker v-model="formData.publishdate" value-format="YYYY-MM-DD"
                    :disabled-date="(date: Date) => date.getTime() < Date.now() - 8.64e7"
                    :placeholder="t('common.pleasechoice')" />
            </el-form-item>
            <el-form-item :label="t('notice.news.edit.istop')" prop="istop">
                <el-switch v-model="formData.istop" :active-value="1" :inactive-value="0" />
            </el-form-item>
            <el-form-item :label="t('notice.news.edit.isshow')" prop="isshow">
                <el-switch v-model="formData.isshow" :active-value="1" :inactive-value="0" />
            </el-form-item>
            <el-form-item :label="t('notice.news.edit.coverimage')" prop="coverimage">
                <app-upload v-model="uploadFiles" :file-types="['image']" :max-size="uploadConfig.maxFileSize" />
            </el-form-item>
            <el-form-item class="el-form-item--row" :label="t('notice.news.edit.brief')" prop="brief">
                <el-input type="textarea" v-model="formData.brief" maxlength="200" :rows="3"
                    :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item class="el-form-item--row" :label="t('notice.news.edit.context')" prop="context">
                <app-editor v-model="formData.context" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.close') }}</el-button>
            <el-button type="primary" @click="onSubmit(10)">{{ t('operation.submit') }}</el-button>
            <el-button type="primary" @click="onSubmit(1)" v-if="!record?.id">{{ t('operation.record') }}</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 { formatDate, parseFilePaths, extractFilePaths } from '@/filters'
import { uploadConfig } from '@/constants/common'
import { useRequest } from '@/hooks/request'
import { dealinform, informManinit, informDetail } from '@/services/api/notice'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppEditor from '@pc/components/base/editor/index.vue'
import AppUpload from '@pc/components/base/upload/index.vue'

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

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

const formData = ref<Partial<Notice.DealInformReq>>({
    istop: 0,
    isshow: 1,
    hits: 0,
    publishdate: formatDate(new Date().toISOString(), 'YYYY-MM-DD')
})

const { data } = useRequest(informManinit)

const { loading, run } = useRequest(informDetail, {
    manual: true,
    onSuccess: ((res) => {
        res.data.publishdate = formatDate(res.data.publishdate, 'YYYY-MM-DD')
        formData.value = res.data
        uploadFiles.value = parseFilePaths(res.data.coverimage)
    }),
    onError: (err) => {
        ElMessage.error(err)
    }
})

// 表单验证规则
const formRules: FormRules = {
    title: [{ required: true }],
    columnid: [{ required: true }],
    publishdate: [{ required: true }],
    context: [{ required: true }],
}

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

const onSubmit = (status: number) => {
    const rawData = { ...formData.value }
    rawData.status = status
    rawData.coverimage = extractFilePaths(uploadFiles.value)

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

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