<template>
	<a-card title="近12小时进出人数统计" style="margin-bottom: 10px">
		<a-row :gutter="20">
			<a-col :span="8">
				<div class="chart_content">
					<chart-liquid :id="1" :list="personData"></chart-liquid>
				</div>
			</a-col>
			<a-col :span="16">
				<div class="chart_content">
					<chart-person v-if="chartList.length > 0" :id="1" :data="{ name: '人员识别', unit: '人', list: chartList }"></chart-person>
					<a-empty style="padding-top: 100px" />
				</div>
			</a-col>
		</a-row>
	</a-card>
	<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="类型" name="personType">
						<a-select></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="结果" name="capResult">
						<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="deleteBatchPerson" />
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'carImgUrl'">
					<div>
						<div v-viewer>
							<img v-if="record.carImgUrl" class="record-img" :src="record.carImgUrl" 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 @click="formRef.onOpen(record, searchFormState)">纠错</a>
					<a-divider type="vertical"/>
					<a-popconfirm title="确定删除这条数据吗？" @confirm="deletePerson(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<person-form ref="formRef" @successful="table.refresh(true)"></person-form>
</template>

<script setup name="pollingPerson">
	import chartLiquid from '../chart/chartLiquid.vue'
	import chartPerson from '../chart/chartPerson.vue'
	import bizAiPollingApi from "@/api/biz/ai/bizAiPollingApi"
	import personForm from '../form/personForm.vue'

	const searchFormRef = ref()
	const searchFormState = ref({})
	const date = ref([])

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

	const formRef = ref()
	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: 'name',
			resizable: true,
			width: 200,
			ellipsis: true
		},
		{
			title: '类型',
			dataIndex: 'personType',
			resizable: true,
			width: 150,
			ellipsis: true
		},
		{
			title: '照片',
			dataIndex: 'carImgUrl',
			resizable: true,
			width: 150,
			ellipsis: true
		},
		{
			title: '尝试进入区域',
			dataIndex: 'devName',
			resizable: true,
			width: 200,
			ellipsis: true
		},
		{
			title: '结果',
			dataIndex: 'capResult',
			resizable: true,
			width: 200,
			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 loadData = async (parameter) => {
		let params = {}
		if (date.value.length > 0) {
			params.startCapTime = date.value[0]
			params.endCapTime = date.value[1]
		}
		return bizAiPollingApi.getPersonCapturePage(Object.assign(parameter, params, searchFormState.value)).then(res => {
			return res
		})
	}

	const personData = ref({
		name: '进出总人数',
		unit: '人',
		formColor: '#3699FD',
		toColor: '#3BA3F9',
		nameColor: '#579CFE',
		value: 0
	})
	const chartList = ref([])
	const init = () => {
		bizAiPollingApi.getPersonCapture({}).then(res => {
			personData.value.value = res.total;
			chartList.value = res.list;
		})
	}

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

	const deleteBatchPerson = (params) => {
		bizAiPollingApi.delPersonCapturePage(params).then(res => {
			table.value.clearRefreshSelected()
		})
	}

	const follow = (item, val) => {
		item.isFollow = val
		let params = {
			eventId: item.id,
			eventType: 1
		}
		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)
			})
		}
	}

	onMounted(() => {
		init()
	})
</script>

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