<template>
	<a-modal v-model:visible="visible" title="上传" @ok="handleOk">
		<a-form
			:model="formState"
			:rules="rules"
			ref="formRef"
			layout="horizontal"
			:label-col="{ span: 7 }"
			:wrapper-col="{ span: 12 }"
		>
			<a-form-item label="公告标题" name="title">
				<a-input v-model:value="formState.title" placeholder="输入公告标题"/>
			</a-form-item>

			<a-form-item label="公告内容" name="content">
				<a-textarea v-model:value="formState.content" placeholder="输入公告内容"
							:auto-size="{ minRows: 5, maxRows: 8 }"/>
			</a-form-item>

		</a-form>
	</a-modal>
</template>
<script setup>
import {reactive, ref} from 'vue'
import resourceAuditApi from '@/api/resourceAudit.js'

const collegeMajorOptions = ref([]) //院系
const majorIdName = ref([]) //院系回显
const majorOptions = ref([]) //专业
const formRef = ref() //专业
const mode = ref('add') //专业
import {addItem, detail, editItem} from '@/api/notice'

const visible = ref(false)
const emit = defineEmits([ "handleAddItem" ])
const formState = reactive({
	noticeId : undefined,
	title: undefined,
	content: undefined,
})
const rules = {
	title: [{required: true, message: '请输入标题', trigger: 'blur'}],
	content: [{required: true, message: '请输入内容', trigger: 'blur'}],
}

watch(
	() => visible.value,
	(newVal, oldVal) => {
		if (newVal == false && formRef.value) {
			formRef.value.resetFields()
		}
	},
	{deep: true, immediate: true}
)
const open = () => {

	visible.value = true
	mode.value = 'add'
}
const edit = (item) => {
	visible.value = true
	mode.value = 'edit'
	console.log('编辑内容',item)


	detail({noticeId : item.noticeId}).then((res)=>{
		if(res.code ==200){
			formState.noticeId = item.noticeId
			formState.content = res.data.content
			formState.title = res.data.title
		}
	})

}
const handleOk = (e) => {
	formRef.value.validate().then(() => {
		let json = JSON.parse(JSON.stringify(formState))


		if (mode.value == 'add') {
			addItem(json).then((res) => {
					if(res.code == 200){
						emit("handleAddItem")
						visible.value = false
					}
			})
		}
		if (mode.value == 'edit') {
			editItem(json).then((res) => {
				if(res.code == 200){
					emit("handleAddItem")
					visible.value = false
				}
			})
		}
	})
	// console.logckPoint.value = false
}
const getOrgTreeSelector = () => {
	resourceAuditApi
		.orgList()
		.then((res) => {
			console.log(res.data, '获取学院')
			collegeMajorOptions.value = res.data
		})
		.catch((err) => {
			console.log(err)
		})
}
const changeCollegeMajor = (value, selectedOptions) => {
	console.log('Selected:', value, selectedOptions)
	if (!value) {
		formState.collegeTwoId = ''
		// majorIdName.value = ''
		return false
	}
	formState.majorId = undefined
	// majorIdName.value = selectedOptions.map((it) => it.name).join('/')
	// formState.collegeId = value[0] || null
	// formState.collegeTwoId = value[1] || null
	// formState.collegeThreeId = value[2] || null
	// if (selectedOptions.length) {
	// 获取选中的最后一级
	// const lastSelected = selectedOptions[selectedOptions.length - 1]
	// formState.selectedCollegeMajor = {
	// 	id: lastSelected.id,
	// 	name: lastSelected.name,
	// 	fullPath: selectedOptions.map((opt) => opt.name).join(' / ')
	// }
	console.log(formState.collegeTwoId, '最后一级id')
	getCollegeMajor(formState.collegeTwoId)
	// }
}
const getCollegeMajor = (id) => {
	resourceAuditApi
		.zyselect({collegeId: id})
		.then((res) => {
			console.log(res.data, '专业下拉数据')
			majorOptions.value = res.data
		})
		.catch((err) => {
			console.log(err)
		})
}
onMounted(() => {
	// getOrgTreeSelector()
})

defineExpose({open,edit})
</script>
