<template>
	<!-- 加签审批弹窗 -->
	<el-dialog v-model="reviewVisibleDialog" title="审批加签" 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="加签方式" prop="type">
				<el-radio-group v-model="form.type">
					<el-radio label="9"
						>前加签
						<el-tooltip effect="dark" content="在当前节点之前增加一个审批节点，当新增的节点同意后，再流转至当前节点。" placement="top">
							<el-button text :icon="QuestionFilled" style="padding: 0"></el-button>
						</el-tooltip>
					</el-radio>
					<el-radio label="11"
						>后加签
						<el-tooltip effect="dark" content="在当前节点之后增加一个审批节点，当前节点会默认同意，并流转至新增的节点。" placement="top">
							<el-button text :icon="QuestionFilled" style="padding: 0"></el-button>
						</el-tooltip>
					</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="给谁加签" prop="userId" :rules="[{ required: true, message: '请选择加签人员' }]">
				<el-select v-model="form.userId" placeholder="请选择加签人员">
					<el-option v-for="item in userOptList" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="审批意见" prop="content">
				<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, onMounted, reactive, ref } from 'vue'
import FileUpload from '@/components/FileUpload.vue'
import { QuestionFilled } from '@element-plus/icons-vue'
import user from '@/api/system/user'
import { processAppendNodeApi } from '@/api/flow/processTask'

const props = defineProps({
	modelValue: {
		type: Boolean,
		default: false
	},
	taskId: {
		type: String,
		default: undefined
	}
})
const btnDisabled = ref(false)
const form = reactive({
	content: '',
	userId: '',
	type: 9
})
const formRef = ref(null)
const uploadLoading = ref(false)
const userOptList = ref([])

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

// 获取用户
const getUserList = async () => {
	var params = {
		data: {
			keyword: null,
			departmentId: null
		},
		page: 1,
		pageSize: 99999
	}
	const res = await user.userPageApi(params)
	userOptList.value = res.records.map(item => {
		return {
			value: item.id,
			label: item.username
		}
	})
}

// 加签
const submitForm = () => {
	btnDisabled.value = true
	const formData = { ...form, taskId: props.taskId }
	formRef.value
		.validate()
		.then(async valid => {
			if (valid) {
				userOptList.value.forEach(item => {
					if (item.value === formData.userId) {
						formData.username = item.label
					}
				})
				console.log(formData, '===')
				await processAppendNodeApi(formData)
				closeDialog()
				btnDisabled.value = false
			}
		})
		.catch(err => {
			btnDisabled.value = false
		})
}

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

onMounted(() => {
	getUserList()
})

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

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

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

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