<template>
	<a-form ref="formRefAdd" :model="formDataAdd" :rules="formRules" layout="vertical">
		<a-row :gutter="16">
			<a-col :span="24">
				<a-form-item name="noteContent" label="笔记">
					<a-textarea v-model:value="formDataAdd.noteContent" placeholder="请输入笔记" :rows="4" />
				</a-form-item>
			</a-col>
		</a-row>
	</a-form>
	<div class="frc">
		<a-button type="primary" :loading="submitLoading" @click="submitForm">新增</a-button>
	</div>
	<a-divider />
	<a-list
		class="demo-loadmore-list"
		:loading="initLoading"
		item-layout="horizontal"
		:data-source="listData"
		:pagination="pagination"
	>
		<template #renderItem="{ item }">
			<a-list-item>
				<a-skeleton avatar :title="false" :loading="!!item.loading" active>
					<div style="width: 100%">
						<a-list-item-meta>
							<template #title>{{ item.courseName }}</template>
							<template #avatar v-if="item.avatar">
								<a-avatar :src="item.avatar" />
							</template>
						</a-list-item-meta>
						{{ item.noteContent }}
						<div class="flc">
							<div @click="editNote(item)">
								<a-tooltip title="编辑" :getPopupContainer="(trigger) => trigger.parentElement">
									<edit-outlined />
								</a-tooltip>
							</div>
							<div class="ml-2" @click="delNote(item)">
								<a-tooltip title="删除" :getPopupContainer="(trigger) => trigger.parentElement">
									<delete-outlined />
								</a-tooltip>
							</div>
						</div>
					</div>
				</a-skeleton>
			</a-list-item>
		</template>
	</a-list>
	<a-modal v-model:visible="visible" title="编辑" @ok="handleOk" append-to-body>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="24">
					<a-form-item name="noteContent" label="问题">
						<a-textarea v-model:value="formData.noteContent" placeholder="请输入问题" :rows="4" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</a-modal>
</template>

<script setup>
	import classCentre from '@/api/student/classCentre'
	import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
	import { createVNode } from 'vue'
	import { Modal } from 'ant-design-vue'
	import { required } from '@/utils/formRules'
	// 表单数据，也就是默认给一些数据
	const visible = ref(false)
	const formData = ref({})
	const formRef = ref()
	const formDataAdd = ref({})
	const formRefAdd = ref()
	const submitLoading = ref(false)
	// 默认要校验的
	const formRules = {
		noteContent: [required('请输入笔记')]
	}
	const props = defineProps({
		idsObj: {
			type: [Array, Object],
			required: () => {}
		}
	})
	const count = 3
	const emit = defineEmits({ edit: null })
	const initLoading = ref(true)
	const loading = ref(false)
	const data = ref([])
	const listData = ref([])
	const itemNote = ref({})
	onMounted(() => {
		loading.value = false
		initLoading.value = false
		getList()
	})
	const pagination = ref({
		current: 1,
		onChange: (page) => {
			pagination.value.current = page
			getList()
		},
		pageSize: 10
	})
	const getList = () => {
		delete props.idsObj.parent
		classCentre
			.notesList(
				{
					current: pagination.value.current,
					size: pagination.value.pageSize,
					...props.idsObj
				},
				itemNote.value.noteId
			)
			.then((data) => {
				data.records = data.records.map((r) => {
					return {
						...r,
						loading: false
					}
				})
				listData.value = data.records
				pagination.value.total = data.total
				loading.value = false
				initLoading.value = false
			})
	}
	const editNote = (e) => {
		formData.value.noteId = e.noteId
		formData.value.noteContent = e.noteContent
		itemNote.value.noteId = e.noteId
		visible.value = true
	}
	const delNote = (e) => {
		Modal.confirm({
			title: '确定要删除笔记',
			icon: createVNode(ExclamationCircleOutlined),
			onOk() {
				classCentre.notesEdit([{ noteId: e.noteId }]).then((data) => {
					getList()
				})
			},
			onCancel() {
				console.log('Cancel')
			}
		})
	}
	// 修改
	const handleOk = (e) => {
		formRef.value.validate().then(() => {
			classCentre
				.notesSubmitForm(
					{
						...formData.value
					},
					formData.value.noteId
				)
				.then(() => {
					visible.value = false
					formData.value.noteId = ''
					formRef.value.resetFields()
					getList()
				})
		})
	}
	//添加
	const submitForm = (e) => {
		formRefAdd.value
			.validate()
			.then(() => {
				submitLoading.value = true
				classCentre
					.notesSubmitForm({
						...props.idsObj,
						...formDataAdd.value
					})
					.then(() => {
						formRefAdd.value.resetFields()
						getList()
					})
			})
			.finally(() => {
				submitLoading.value = false
			})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		getList
	})
</script>
<style scoped lang="less">
	.flc {
		display: flex;
		align-items: center;
	}
	.fcbc {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.frc {
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}
</style>
