<!-- 通知公告-资讯栏目-编辑 -->
<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.newscolumn.edit.columntype')" prop="columntype">
                <el-select v-model="formData.columntype">
                    <el-option v-for="item in columntypeEnum.getEnumOptions()" :key="item.value" :label="item.label"
                        :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('notice.newscolumn.edit.columnname')" prop="columnname">
                <el-input v-model="formData.columnname" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('notice.newscolumn.edit.sort')" prop="sort">
                <el-input-number v-model="formData.sort" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('notice.newscolumn.edit.isshow')" prop="isshow">
                <el-radio-group v-model="formData.isshow">
                    <el-radio v-for="item in flagEnum.getEnumOptions()" :key="item.value" :value="item.value"
                        :label="item.label" />
                </el-radio-group>
            </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 } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { useEnum } from '@/hooks/enum'
import { dealcolumn, columnDetail } from '@/services/api/notice'
import { i18n } from '@/stores'
import AppDrawer from '@pc/components/base/drawer/index.vue'

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

const flagEnum = useEnum('flag')
const columntypeEnum = useEnum('columntype') // 栏目类型

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

const formData = ref<Partial<Notice.DealColumnReq>>({
    isshow: 1,
    sort: 1
})

const { loading, run } = useRequest(columnDetail, {
    manual: true,
    onSuccess: ((res) => {
        const { id, columnname, columntype, isshow, sort } = res.data
        formData.value = {
            id,
            columnname,
            columntype,
            isshow,
            sort
        }
    }),
    onError: (err) => {
        ElMessage.error(err)
    }
})

// 表单验证规则
const formRules: FormRules = {
    columntype: [{ required: true }],
    columnname: [{ required: true }],
    sort: [{ required: true }],
    isshow: [{ required: true }],
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            loading.value = true
            dealcolumn({
                data: formData.value
            }).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>