<!-- 通知公告-公告通知管理-审核 -->
<template>
    <DetailsView ref="detailsRef" v-bind="{ record }" :loading="loading">
        <el-button type="primary" @click="showDialog(1)">审核通过</el-button>
        <el-button type="primary" @click="showDialog(0)">审核不通过</el-button>
        <el-dialog v-model="dialogVisible" :title="formData.auditflag === 1 ? '提示' : ''" :width="480" append-to-body
            align-center>
            <span v-if="formData.auditflag === 1">确认提交审核吗？</span>
            <el-form ref="formRef" class="el-form--vertical" label-width="100px" :model="formData" :rules="formRules"
                :show-message="false" v-else>
                <el-form-item label="不通过原因" prop="msg">
                    <el-input type="textarea" v-model="formData.msg" maxlength="200" :rows="5" placeholder="请输入" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="onSubmit">提交</el-button>
            </template>
        </el-dialog>
    </DetailsView>
</template>

<script lang="ts" setup>
import { reactive, shallowRef, PropType } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { auditMsg } from '@/services/api/notice'
import DetailsView from '../details/index.vue'

const props = defineProps({
    record: {
        type: Object as PropType<Notice.QueryRsp>,
        required: true
    }
})

const formRef = shallowRef<FormInstance>()
const detailsRef = shallowRef()
const loading = shallowRef(false)
const dialogVisible = shallowRef(false)

const formData = reactive<Notice.AuditMsgReq>({
    auditid: props.record.autoid,
    auditflag: 0,
})

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

const showDialog = (auditflag: number) => {
    formData.auditflag = auditflag
    dialogVisible.value = true
}

const onSubmit = () => {
    const run = () => {
        loading.value = true
        dialogVisible.value = false
        auditMsg({
            data: formData
        }).then(() => {
            ElMessage.success('提交成功')
            detailsRef.value?.close(true)
        }).catch((err) => {
            ElMessage.error('提交失败：' + err)
            detailsRef.value?.close()
        }).finally(() => {
            loading.value = false
        })
    }
    if (formRef.value) {
        formRef.value.validate((valid) => {
            if (valid) {
                run()
            }
        })
    } else {
        run()
    }
}
</script>