<template>
	<!-- 评论弹窗 -->
	<el-dialog v-model="reviewVisibleDialog" class="le-dialog" :title="currentTip + '内容'" width="700" destroy-on-close>
		<el-form ref="formRef" v-loading="uploadLoading" label-position="top" element-loading-text="图片上传中..." :model="form" label-width="80px">
			<el-form-item :label="currentTip" prop="content" :rules="[{ required: true, message: '内容不能为空' }]">
				<el-input v-model="form.content" type="textarea" placeholder="请输入内容" maxlength="64" show-word-limit> </el-input>
			</el-form-item>
			<el-form-item v-if="false" prop="attachment" label="附件" class="example-img-box">
				<!--'.docx', '.doc', '.pptx', '.ppt', '.xlsx', '.xls', '.zip', '.csv', '.pdf', '.png', '.jpg'  因前端不支持图片以外格式,所以注释 -->
				<FileUpload
					v-model="form.attachment"
					source="project"
					return="array"
					:limit="5"
					:file-size="10"
					:accept="['.png', '.jpg']"
					@success="clearValidate"
				/>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button :loading="btnDisabled" @click="closeDialog">取 消</el-button>
				<el-button type="primary" :loading="btnDisabled" :disabled="uploadLoading" @click="submitForm">确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup>
import { computed, reactive, ref } from 'vue'
import { processReviewTaskApi, processRevokeTaskApi } from '@/api/flow/processTask'
import FileUpload from '@/components/FileUpload.vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
	modelValue: {
		type: Boolean,
		default: false
	},
	taskId: {
		type: String,
		default: undefined
	},
	instanceId: {
		type: String,
		default: undefined
	},
	// 审核类型 评论(review) or 撤销(revoke)
	currentType: {
		type: String,
		default: 'review'
	}
})
const btnDisabled = ref(false)
const form = reactive({
	content: ''
	// attachment: []
})
const formRef = ref(null)
const uploadLoading = ref(false)

const $myEmit = defineEmits(['update:modelValue', 'successCb'])

// 提交
const submitForm = () => {
	btnDisabled.value = true
	const formData = { ...form, taskId: props.taskId, instanceId: props.instanceId }
	formRef.value
		.validate()
		.then(async valid => {
			if (valid) {
				const flag = props.currentType === 'review'
				if (flag) {
					await processReviewTaskApi(formData)
				} else {
					await processRevokeTaskApi(formData)
				}
				$myEmit('successCb')
				closeDialog()
				btnDisabled.value = false
			}
		})
		.catch(err => {
			btnDisabled.value = false
			ElMessage({
				message: '操作失败',
				type: 'error'
			})
		})
}

const closeDialog = () => {
	$myEmit('update:modelValue', false)
}

const reviewVisibleDialog = computed({
	get() {
		return props.modelValue
	},
	set(val) {
		$myEmit('update:modelValue', val)
	}
})

const currentTip = computed(() => {
	return props.currentType === 'review' ? '评论' : '撤销'
})

const clearValidate = () => {}
</script>

<style scoped>
.example-img-box {
	width: 100% !important;
}

.example-img-box .el-form-item__content {
	display: block;
}
</style>
