<template>
	<a-table
		ref="table"
		:columns="columns"
		:data-source="dataSources"
		:row-key="(record) => record.courseId"
		bordered
		:expand-row-by-click="true"
		:pagination="false"
		size="small"
	>
		<template #bodyCell="{ column, text, record }">
			<template v-if="column.dataIndex === 'haveAnswer'">
				<span v-if="text == 0" style="color: blue">否</span>
				<span v-if="text == 1" style="color: red">是</span>
			</template>
			<template v-if="column.dataIndex === 'recordTime'">
				<span >{{tool.formatMillisecondsToTime(text)}}</span>
			</template>

			<template v-if="column.dataIndex === 'action'">
<!--				<a-button size="small" @click="handleDetail(record)" style="margin-right: 5px">详情</a-button>-->
<!--				<a-button v-if="record.haveAnswer == 1" size="small" @click="handleEdit(record)" style="margin-right: 5px">新回复</a-button>-->
				<span   @click="handleEdit(record)" style="margin-right: 5px;cursor: pointer">回复</span>
				<a-popover v-model:visible="popoverVisibles[record.id]" title="确定删除?" trigger="click">
					<template #content>
						<a-button style="margin-right: 10px" type="primary" @click="handleDelete(record)">确定
						</a-button>
						<a-button @click="()=>{	popoverVisibles[record.id] = false}">取消</a-button>
					</template>
					<!--					<a-button size="small" style="margin-right: 5px">选择</a-button>-->
					<span   style="margin-left: 10px;cursor: pointer">删除</span>
				</a-popover>
			</template>
		</template>
	</a-table>
	<div style="display: flex; width: 100%; justify-content: flex-end; margin-top: 10px">
		<a-pagination
			v-model:current="pagination.current"
			v-model:pageSize="pagination.size"
			:total="total"
			show-less-items
			@change="handlerChange"
		/>
	</div>
</template>

<script setup>
import tool from '@/utils/tool'
import {ref, onMounted} from 'vue'
import {EyeOutlined, EditOutlined, SnippetsOutlined, DeleteOutlined} from '@ant-design/icons-vue'
import {list,del} from '@/api/answer'
import {useRouter} from 'vue-router'
import {updateCourseStatus} from '@/api/course/courseDetail'

const router = useRouter()

const emit = defineEmits(['handleEdit'])
//发布按钮状态
const releaseVisible = ref(false)
const loading = ref(false) // 列表loading
const dataSources = ref([])
const popoverVisible = ref({})
const popoverVisibles = ref({})
const formState = ref({
	name: '',
	loacl: ''
}) // 列表loading
const columns = [
	{
		title: '课时名称',
		dataIndex: 'hourIdName',
		// sorter: true,
		width: '5%'
	},
	// {
	// 	title: '内容',
	// 	dataIndex: 'info',
	// 	// sorter: true,
	// 	width: '15%'
	// },
	{
		title: '课程名',
		dataIndex: 'courseIdName',
		// sorter: true,
		width: '10%'
	},
	{
		title: '问答类型',
		dataIndex: 'infoTypeName',
		// sorter: true,
		width: '5%'
	},
	{
		title: '视频时间',
		dataIndex: 'recordTime',
		// sorter: true,
		width: '5%'
	},
	{
		title: '是否回复',
		dataIndex: 'haveAnswer',
		// sorter: true,
		width: '5%'
	},
	{
		title: '操作',
		dataIndex: 'action',
		// sorter: true,
		width: '7%'
	}
]
// tool.formatTimestamp()

const formatTimestamp = (time) => {
	return tool.formatTimestamp(time)
}
const total = ref(0)
const pagination = ref({
	eduIdentity : 1,
	size: 10,
	current: 1,
})
// const onChangeCurrent = (current) => {
// 	router.push({
// 		path: '/' + current
// 	})
// }
const handlerChange = (page, pageSize) => {
	console.log('分页参数', page, pageSize)
	// pagination.value.size = pageSize
	// pagination.value.current = page

	getList()
}
const handleDetail = (record) => {
	console.log('查看详情', record)
	router.push({
		path: '/portal/courseDetails',
		query: {
			id: record.courseId
		}
	})
	// 在这里添加查看详情的逻辑
}

// 编辑按钮点击事件
const handleEdit = (record) => {
	console.log('编辑记录', record)
	// 在这里添加编辑记录的逻辑

	emit('handleEdit', record)
}

// 上架按钮点击事件
const handleShelf = (record,num) => {
	console.log('上架记录', record)
	popoverVisible.value[record.collegeId] = false
	// 在这里添加上架记录的逻辑
	// {
	// 	"courseId": "1948183431150227458",
	// 	"putawayStatus": 1
	// }
	// updateCourseStatus({courseId : record.courseId,putawayStatus : num}).then((res)=>{
	// 	getList()
	// })

}

// 删除按钮点击事件
const handleDelete = (record) => {
	console.log('删除记录', record)
	// 在这里添加删除记录的逻辑
	del([{id : record.id}]).then((res)=>{
		getList()
	})
}
const getList = () => {
	console.log('获取列表 getList')
	let tempData = {
		eduIdentity : 1
	}
	list({...pagination.value}, {...tempData}).then((data) => {
		if (data.code == 200) {
			console.log('获取列表 新数组', data.data.records)
			dataSources.value = []
			dataSources.value = data.data.records
			console.log('获取列表 最新数组', dataSources.value)
			pagination.value.current = data.data.current
			pagination.value.size = data.data.size
			total.value = data.data.total
		}
		// data.records
	})
}
const setList = (search) => {
	console.log('获取列表 setList',search)
	// courseName: '',
	// 	collegeId: '',
	// 	majorId: '',
	// 	courseType: '',
	// 	loacl: []
	formState.value = search
	pagination.value.current = 1
	list({...pagination.value, ...formState.value}).then((data) => {
		if (data.code == 200) {
			dataSources.value = data.data.records
			pagination.value.current = data.data.current
			pagination.value.size = data.data.size
			total.value = data.data.total
		}
		// data.records
	})
}

// 重置按钮点击事件
onMounted(() => {
	// getListData()
	getList()
})


// watch(
// 	() => dataSources.value,
// 	(newVal, oldVal) => {
// 		console.log('数据源变化了 ', ' 新的 ',newVal, '  旧的 ',oldVal)
// 	},
// 	{ deep: true, immediate: true }
// )
defineExpose({
	setList,getList
})
</script>

<style scoped>
.desc p {
	margin-bottom: 1em;
}
</style>
