<template>
    <x-search-bar class="mb-8-2">
        <template #default="{ gutter, colSpan }">
            <a-form ref="formRef" :model="formData" :rules="formRules">
                <a-form-item :label="'标题'" name="title">
                    <a-textarea :placeholder="'请输入标题'" v-model:value="formData.title"></a-textarea>
                </a-form-item>
                <a-form-item :label="'封面'" name="cover">
                    <gx-upload v-model="formData.cover" :fileNumber="1" />
                    <p class="hint">750*500px，图片大小不超过150kb</p>
                </a-form-item>
                <a-form-item :label="'内容'" name="content">
                    <x-editor v-model="formData.content" placeholder="请输入内容"></x-editor>
                </a-form-item>
                <a-button @click="handleSearch" style="margin-top: 20px">保存</a-button>
            </a-form>
        </template>
    </x-search-bar>
</template>
<script setup>
import { message } from 'ant-design-vue'
import { ref, onMounted } from 'vue'
import { useForm, useModal } from '@/hooks'
import apis from '@/apis' // 假设这里包含 saveAffirm 接口
import { spliceUrl } from '@/utils/util'
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
import { config } from '@/config'
import { getCompanies } from '@/apis/modules/companies'
defineOptions({
    name: 'contactUs',
})
formRules.value = {
    title: { required: true, message: '请输入标题' },
    content: { required: true, message: '请输入内容' },
    cover: [{ required: true, message: '请上传图片', trigger: 'change' }],
}
const { showLoading, hideLoading } = useModal()
const reportImage = ref(null)
const socialMedia = ref([{ name: '', image: '', link: '' }])
onMounted(() => {
    getPageList()
})
// 页面加载时获取声明内容

/**
 * 获取声明内容
 */
async function getPageList() {
    try {
        showLoading()
        const { data, success } = await apis.companyInfo.getDataList()
        if (success) {
            formData.value = {
                id: data.id,
                title: data.title,
                cover: data.cover ? [config('http.apiBasic') + data.cover] : [],
                content: data.content
            }
        }
    } catch (e) {
        message.error('获取声明内容失败')
    } finally {
        hideLoading()
    }
}
/**
 * 提交声明内容
 */
async function handleSearch() {
    formRef.value.validateFields().then(async (values) => {
        try {
            showLoading()
            const params = {
                id:formData.value.id,
                title: formData.value.title,
                cover: formData.value.cover ? spliceUrl(formData.value.cover[0]) : '',
                content: formData.value.content
            }
            const { success } = await apis.companyInfo.updateItem(formData.value.id, params)
            if (success) {
                message.success('保存成功')
                getPageList()
            }
        } catch (e) {
            message.error('保存失败，请重试')
        } finally {
            hideLoading()
        }
    })

}


</script>

<style lang="less" scoped>
.hint {
    color: red;
    margin: 10px 0;
}
</style>
