<template>
	<div class="table_item">
		<a-page-header sub-title="监测点配置" @back="goBack" style="padding: 0 0 20px">
			<template #title>
				<span style="font-size: 18px; cursor: pointer" @click="goBack">返回</span>
			</template>
		</a-page-header>

		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:row-key="(record) => record.id"
			bordered
			@resizeColumn="handleResizeColumn"
		>
			<template #operator>
				<!-- 搜索区域 -->
				<div class="table-search">
					<div class="table-search-form">
						<a-row :gutter="10">
							<a-form
								ref="searchFormRef"
								name="advanced_search"
								layout="inline"
								:label-col="{ style: { width: '70px', justifyContent: 'end' } }"
								:model="searchFormState"
								class="ant-advanced-search-form"
							>
								<a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8"
									><a-form-item label="点位名称" name="name">
										<a-input v-model:value="searchFormState.name" placeholder="请输入点位名称" /> </a-form-item
								></a-col>
							</a-form>
						</a-row>
					</div>
					<div class="table-search-buttons">
						<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
						<a-button class="xn-mg08" @click="reset">重置</a-button>
					</div>
				</div>

				<!-- 其他操作区域 -->
				<div class="table-head-btn">
					<div class="btn-left">
						<a-button type="primary" @click="formRef.onOpen('add', { monitorTargetId: queryId })">
							<template #icon><plus-outlined /></template>新增
						</a-button>
					</div>
				</div>
			</template>
			<template #bodyCell="{ column, record, index }">
				<!-- 序号 -->
				<template v-if="column.dataIndex === 'index'">{{ index + 1 }} </template>
				<template v-if="column.dataIndex === 'name'">
					<a-button type="link" @click="leaveFor('/biz/monitor/trendchart', record)">{{ record.name }}</a-button>
				</template>

				<template v-if="column.dataIndex === 'sensorType'">
					{{ $TOOL.dictTypeData('SENSORTYPE', record.sensorType) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-button type="link" size="small" @click="formRef.onOpen('edit', record)">编辑</a-button>
					<a-popconfirm title="确定要删除吗？" @confirm="deleteData(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>

					<a-button type="link" size="small" @click="leaveFor('/biz/monitor/trendchart', record)">详情</a-button>
				</template>
			</template>
		</s-table>
		<Form ref="formRef" @successful="tableRef.refresh(true)" />
	</div>
</template>

<script setup>
	import locationApi from '@/api/basicset/locationApi.js'
	import { useRoute } from 'vue-router'
	import router from '@/router'
	import Form from './form.vue'
	const formRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	const tableRef = ref()
	const Route = useRoute()

	const columns = [
		{
			title: '序号',
			dataIndex: 'index',
			align: 'center',
			width: 50
		},
		{
			title: '点位名称',
			dataIndex: 'name',
			align: 'center',
			ellipsis: true,
			resizable: true
		},

		{
			title: '监测冰箱',
			dataIndex: 'targetName',
			align: 'center',
			ellipsis: true,
			resizable: true
		},
		{
			title: '冷链设备名称',
			dataIndex: 'deviceName',
			align: 'center',
			ellipsis: true,
			resizable: true
		},
		{
			title: '冷链设备编号',
			dataIndex: 'sensorCode',
			align: 'center',
			ellipsis: true,
			resizable: true
		},
		{
			title: '传感器类型',
			dataIndex: 'sensorType',
			align: 'center',
			ellipsis: true,
			resizable: true
		},
		{
			title: '传感器路数',
			dataIndex: 'sensorRoute',
			align: 'center',
			ellipsis: true,
			resizable: true
		},
		{
			title: '离线预警',
			dataIndex: 'deviceOfflineAlarm',
			align: 'center',
			ellipsis: true,
			resizable: true,
			customRender({ value }) {
				return value ? '是' : '否'
			}
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			align: 'center',
			ellipsis: true,
			resizable: true
		},

		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		}
	]
	// 可伸缩列
	const handleResizeColumn = (w, col) => {
		col.width = w
	}

	const queryId = ref() //监控对象冰箱的id
	onBeforeMount(() => {
		if (Route.query.id) {
			queryId.value = Route.query.id
		}
	})

	// 返回上一页
	const goBack = () => {
		router.go(-1)
	}

	const loadData = (parameter) => {
		if (queryId.value) {
			parameter.monitorTargetId = queryId.value
			return locationApi.locationPage(Object.assign(parameter, searchFormState.value)).then((res) => {
				return res
			})
		}
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteData = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		locationApi.locationDelete(params).then(() => {
			tableRef.value.refresh(true)
			reset()
		})
	}

	// 跳转到趋势图界面
	const leaveFor = (url, record) => {
		router.push({
			path: url,
			query: {
				id: record.monitorDeviceId,
				roads: record.sensorRoute
			}
		})
	}
</script>

<style lang="less" scoped>
	.table_item {
		padding: 15px 20px;
		background-color: #ffffff;

		:deep(.ant-table-pagination-right) {
			justify-content: center !important;
		}

		:deep .ant-page-header-back {
			margin-right: 8px;
		}
	}
</style>
