<template>
	<a-table :data-source="tableData" :columns="columns" :pagination="false" style="width: 100%">
		<template #bodyCell="{ column, record }">
			<template v-if="column.key === 'operation'">
				<a
					@click="geta1(record)"
					ref="downloadLink"
					target="_blank"
					:style="{ fontSize: '12px', color: '#409EFF', marginRight: '10px' }"
					:href="$file?.getDownloadFilePath2(record)"
					:download="record.fileName"
				>
					<DownloadOutlined /> 下载
				</a>
				<a-button type="link" danger size="small" @click="handleDelete(record)">删除</a-button>
			</template>
		</template>
	</a-table>
</template>

<script setup>
	import { ref, onMounted, getCurrentInstance } from 'vue'
	import { delSensitiveRecord } from '@/api/myResource/file.js' // 假设路径调整为相对路径
	import { message } from 'ant-design-vue'
	import { DownloadOutlined } from '@ant-design/icons-vue'

	const props = defineProps({
		tableData: {
			type: Array,
			required: true
		}
	})

	const emit = defineEmits(['delRecords'])

	const newname = ref('')
	const downloadLink = ref(null)

	// 获取全局属性，例如 $common 和 $config
	const instance = getCurrentInstance()
	const globalProperties = instance?.appContext.config.globalProperties

	const columns = [
		{ title: '文件名', dataIndex: 'fileName', key: 'fileName' },
		{ title: '敏感词', dataIndex: 'sensitiveWord', key: 'sensitiveWord' },
		{ title: '过滤时间', dataIndex: 'createTime', key: 'createTime' },
		{ title: '文件所属者', dataIndex: 'createUser', key: 'createUser' },
		{ title: '操作', key: 'operation', fixed: 'right', width: 150 }
	]

	const handleDelete = (row) => {
		delSensitiveRecord(row.id).then(
			(res) => {
				if (res.code === 0) {
					message.success('删除过滤记录成功')
					emit('delRecords')
				} else {
					message.error(res.message || '删除失败')
				}
			},
			(err) => {
				message.error(`删除失败: ${JSON.stringify(err)}`)
			}
		)
	}

	const geta1 = (record) => {
		const src = globalProperties?.$file?.getDownloadFilePath2(record)
		console.log('src', src)

		const cookies = globalProperties?.$common?.getCookies(globalProperties?.$config?.tokenKeyName)

		let name1 = ''
		let name2 = ''

		if (downloadLink.value && downloadLink.value.download) {
			const parts = downloadLink.value.download.split('.')
			name1 = parts[0]
			name2 = parts.length > 1 ? parts[parts.length - 1] : ''
		} else {
			// Fallback if ref is not available or download attribute is missing
			name1 = record.fileName.split('.')[0]
			name2 =
				record.fileName.split('.').length > 1 ? record.fileName.split('.')[record.fileName.split('.').length - 1] : ''
		}

		if (name2) {
			newname.value = name1 + '.' + name2
		} else {
			newname.value = name1 + '.' + 'zip'
		}

		console.log('this.$config.baseContext', window.location.host)
		const newname1 = newname.value

		let url = ''
		if (downloadLink.value && downloadLink.value.href) {
			const hrefParts = downloadLink.value.href.split('/api')
			if (hrefParts.length > 1) {
				url = `${
					'http://' + window.location.host + globalProperties?.$config?.baseContext + hrefParts[1] + '&' + cookies
				}`
			} else {
				// Fallback if /api is not in href
				url = `${'http://' + window.location.host + globalProperties?.$config?.baseContext + src + '&' + cookies}`
			}
		} else {
			// Fallback if ref is not available
			url = `${'http://' + window.location.host + globalProperties?.$config?.baseContext + src + '&' + cookies}`
		}

		let isios = navigator.userAgent.indexOf('iPhone') > -1
		let isAndroid = navigator.userAgent.indexOf('Android') > -1

		console.log('url', url)
		console.log('cookies', cookies)
		console.log('newname1', newname1)

		if (isios) {
			window.webkit.messageHandlers.Download.postMessage({
				params: {
					url,
					cookies,
					newname1
				}
			})
		} else if (isAndroid) {
			window.Download.downList(url, cookies, newname1)
		}
	}

	// onMounted(() => {
	//     // 如果需要，可以在这里执行一些初始化逻辑
	// });
</script>

<style lang="less" scoped>
	.btnsy {
		background-color: #29175b;
		border-color: #29175b;
	}
</style>
