<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="1200px" height="500px" @close="close"
			append-to-body v-model="visible">
			<el-form :model="inputForm" ref="inputForm" v-loading="loading" :rules="rules"
				:class="method === 'view' ? 'readonly' : ''"
				:disabled="status === 'audit' || status === 'taskFormDetail' || method === 'view'" label-width="100px"
				@submit.native.prevent>
				<el-row :gutter="26">
					<el-col :span="12">
						<el-form-item label="语种" prop="language">
							<el-select size="default" v-model="inputForm.language" placeholder="请输入语种"
								style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('user_info_language')"
									:key="item.value" :label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="是否母语" prop="isNative">
							<el-radio-group v-model="inputForm.isNative">
								<el-radio label="1">是</el-radio>
								<el-radio label="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="掌握程度" prop="proficiency">
							<el-input v-model="inputForm.proficiency" placeholder="请输入掌握程度"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="书写能力" prop="writingAbility">
							<el-input v-model="inputForm.writingAbility" placeholder="请输入书写能力"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="阅读能力" prop="readingAbility">
							<el-input v-model="inputForm.readingAbility" placeholder="请输入阅读能力"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="口语能力" prop="speakingAbility">
							<el-input v-model="inputForm.speakingAbility" placeholder="请输入口语能力"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="24">
						<el-form-item label="附件" prop="certificateAttachment">
							<UpLoadComponent ref="upLoadComponent"></UpLoadComponent>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
					<el-button v-if="method === 'edit' || method == 'add'" type="primary" icon="el-icon-circle-check"
						@click="doSubmit()">暂存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import UpLoadComponent from '@/views/common/UpLoadComponentCardStyle'
import enrollmentRegistrationService from '@/api/human/enrollment/EnrollmentRegistrationService'
export default {
	props: {
		businessId: {
			type: String,
			default: ''
		},
		formReadOnly: {
			type: Boolean,
			default: false
		},
		status: {
			type: String,
			default: ''
		},
		list: {
			type: Array,
			default: () => []
		}
	},
	data() {
		return {
			disabled: false,
			title: '',
			method: '',
			visible: false,
			loading: false,
			inputForm: {
				id: "",
				language: '',              // 语言
				isNative: "1",           // 是否母语
				proficiency: '',           // 掌握程度
				writingAbility: '',        // 书写能力
				readingAbility: '',        // 阅读能力
				speakingAbility: '',        // 口语能力
				files: [],
				status: "",
			},
			keyWatch: '',
			rules: {
				language: [
					{ required: true, message: '请输入语言', trigger: 'blur' }
				],
			},
			fileType: ['jpg', 'jpeg', 'png', 'webp'],
		}
	},
	created() {

	},
	components: {
		UpLoadComponent
	},
	computed: {
		bus: {
			get() {
				return this.businessId
			},
			set(val) {
				this.businessId = val
			}
		},
		name() {
			return this.$store.state.user.name
		},
		userId() {
			return this.$store.state.user.id
		}
	},
	watch: {
		'keyWatch': {
			handler(newVal) {
				if (this.commonJS.isNotEmpty(this.bus)) {
					this.init('', this.bus)
				} else {
					this.$nextTick(() => {
						this.$refs.inputForm.resetFields()
					})
				}
			}
		},
		'loading': {
			handler(newVal) {
				this.$emit('changeLoading', newVal)
			}
		}
	},
	methods: {
		getKeyWatch(keyWatch) {
			this.keyWatch = keyWatch
		},
		init(method, id, reId) {
			this.method = method
			this.inputForm = {
				id: "",
				language: '',              // 语言
				isNative: "1",           // 是否母语
				proficiency: '',           // 掌握程度
				writingAbility: '',        // 书写能力
				readingAbility: '',        // 阅读能力
				speakingAbility: '',        // 口语能力
				files: [],
				status: "",
			}
			if (method === 'add') {
				this.title = `新增外语语种`
			} else if (method === 'edit') {
				this.title = '修改外语语种'
			} else if (method === 'view') {
				this.title = '查看外语语种'
			}
			this.inputForm.userId = reId
			this.visible = true
			this.loading = true
			this.$nextTick(() => {
				if (id) {
					enrollmentRegistrationService.findLanguageInfoById({
						id: id
					}).then(res => {
						this.inputForm = this.recover(this.inputForm, res)
						this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
						if (!this.inputForm.files) {
							this.inputForm.files = []
						}
						this.$refs.upLoadComponent.newUpload(method, this.inputForm.files, 'language', 50, null, null, null, null, [...this.fileType])
						this.loading = false
					})
				} else {
					this.$refs.upLoadComponent.newUpload(method, this.inputForm.files, 'language', 50, null, null, null, null, [...this.fileType])
					this.loading = false
				}
			})
		},
		generateFallbackId() {
			const timestamp = Date.now(); // 13位
			const randomPart = Math.floor(Math.random() * 1e6); // 6位随机数
			return String(timestamp) + String(randomPart);  // 返回 19 位字符串
		},
		// 表单提交
		doSubmit() {
			if (this.$refs.upLoadComponent.checkProgress()) {
				return
			}
			this.$refs['inputForm'].validate((valid) => {
				if (valid) {

					let index = this.list.findIndex(temp => {
						//type和no
						return temp.id !== this.inputForm.id && temp.language == this.inputForm.language
					})
					if (index > -1) {
						this.$message.error('已存在相同的语种，请勿重复添加')
						return
					}

					this.inputForm.files = this.$refs.upLoadComponent.getDataList()
					// this.$emit("addLanguageInfo", JSON.parse(JSON.stringify(this.inputForm)))
					// this.close()
					if (this.inputForm.id && this.inputForm.status != "1" && this.inputForm.status != "4") {
						enrollmentRegistrationService.updateInfoStatus({
							id: this.inputForm.id,
							status: "6",
							type: "6"
						}).then(result => {
							// 暂存状态
							this.inputForm.id = ""
							this.inputForm.status = 1
							enrollmentRegistrationService.saveLanguageInfo(this.inputForm).then(res => {
								this.$message.success(res)
								this.$emit("addLanguageInfo", this.inputForm.enrollmentRegistrationId)
								this.loading = false
								this.close()
							})
						})
					} else {
						// 暂存状态
						this.inputForm.status = 1
						enrollmentRegistrationService.saveLanguageInfo(this.inputForm).then(res => {
							this.$message.success(res)
							this.$emit("addLanguageInfo", this.inputForm.enrollmentRegistrationId)
							this.loading = false
							this.close()
						})
					}

				}
			})
		},
		close() {
			this.$refs.upLoadComponent.clearUpload()
			this.$refs.inputForm.resetFields()
			this.visible = false
		},
	}
}
</script>


<style lang="less">
.fileUpload {
	.el-form-item__content {
		width: 100%;
	}
}
</style>
