<template>
	<a-card title="镜头脏污记录">
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
			<a-row :gutter="24" align="middle">
				<a-col :span="4">
					<a-form-item label="类型">
						<a-select></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="结果">
						<a-select></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="日期">
						<a-range-picker v-model:value="date" value-format="YYYY-MM-DD" format="YYYY-MM-DD" />
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item>
						<a-space>
							<a-button type="primary" @click="table.refresh(true)">查询</a-button>
							<a-button @click="reset">重置</a-button>
						</a-space>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:expand-row-by-click="true"
			:alert="options.alert.show"
			bordered
			:tool-config="toolConfig"
			:row-key="(record) => record.id"
			:row-selection="options.rowSelection"
			@resizeColumn="(w, col) => {
				col.width = w
			}"
		>
			<template #operator class="table-operator">
				<a-space>
					<xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchDirty" />
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'imgUrl'">
					<div>
						<div v-viewer>
							<img v-if="record.imgUrl" class="record-img" :src="record.imgUrl" alt="/">
							<span v-else>暂无图片</span>
						</div>
					</div>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<template v-if="record.isFollow === 0">
						<a-button class="a_div" type="link" size="small" @click="follow(record, 1)">关注</a-button>
					</template>
					<template v-else-if="record.isFollow === 1">
						<a-button class="a_div" type="link" danger size="small" @click="follow(record, 0)">取消关注</a-button>
					</template>
					<a-divider type="vertical"/>
					<a-popconfirm title="确定删除这条数据吗？" @confirm="deleteDirty(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
</template>

<script setup name="pollingDirty">
	import bizAiPollingApi from "@/api/biz/ai/bizAiPollingApi"
	const searchFormRef = ref()
	const searchFormState = ref({})
	const date = ref([])
	const chartList = ref([
		{ time: '00:00', value: 16 },
		{ time: '00:00', value: 16 },
		{ time: '00:00', value: 16 },
		{ time: '00:00', value: 16 },
		{ time: '00:00', value: 16 },
		{ time: '00:00', value: 16 }
	])

	const table = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const show = ref(true)
	const columns = [
		{
			title: '时间',
			dataIndex: 'capTime',
			width: 200,
			resizable: true,
			ellipsis: true,
			fixed: true
		},
		{
			title: '照片',
			dataIndex: 'imgUrl',
			resizable: true,
			width: 150,
			ellipsis: true
		},
		{
			title: '设备名称',
			dataIndex: 'devName',
			resizable: true,
			width: 150,
			ellipsis: true
		},
		{
			title: '通知人',
			dataIndex: 'notifier',
			resizable: true,
			width: 200,
			ellipsis: true
		},
		{
			title: '操作',
			dataIndex: 'action',
			width: 200,
			fixed: 'right'
		}
	]

	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	const reset = () => {
		searchFormRef.value.resetFields()
		date.value = []
		table.value.refresh(true)
	}

	const loadData = async (parameter) => {
		let params = {}
		if (date.value.length > 0) {
			params.startCapTime = date.value[0]
			params.endCapTime = date.value[1]
		}
		return bizAiPollingApi.getDirtyCapturePage(Object.assign(parameter, params, searchFormState.value)).then(res => {
			return res
		})
	}

	const deleteDirty = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizAiPollingApi.delDirtyCapturePage(params).then(() => {
			table.value.refresh(true)
		})
	}

	const deleteBatchDirty = (params) => {
		bizAiPollingApi.delDirtyCapturePage(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	const follow = (item, val) => {
		item.isFollow = val
		let params = {
			eventId: item.id,
			eventType: 4
		}
		if (val === 0) {
			bizAiPollingApi.cancelFollow(params).then((res) => {
				message.success(res.msg)
			})
		} else if (val === 1) {
			bizAiPollingApi.addFollow(params).then((res) => {
				message.success(res.msg)
			})
		}
	}
</script>

<style scoped>
.record-img {
	width: 40px;
	height: 40px;
}
.a_div {
	width: 70px;
}
</style>
