<template>
	<div class="table_item">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:row-key="(record) => record.id"
			:show-pagination="false"
		>
			<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()">
							<template #icon><plus-outlined /></template>新增
						</a-button>
					</div>
					<div class="btn-right">
						<a-button>
							<template #icon><download-outlined /></template>导出
						</a-button>
					</div>
				</div>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'modelName'">
					{{ $TOOL.dictTypeData('COIDCHAIN', record.modelName) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-button type="link" size="small" @click="formRef.onOpen(record)">编辑</a-button>
					<a-popconfirm title="确定要删除吗？" @confirm="deleteData(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
		<Form ref="formRef" @successful="tableRef.refresh(true)" />
	</div>
</template>

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

	const columns = [
		{
			title: '区域名称',
			dataIndex: 'name',
			align: 'center',
			ellipsis: true
		},

		{
			title: '监控对象',
			dataIndex: 'monitorTargetId',
			align: 'center',
			ellipsis: true
		},
		{
			title: '监控设备',
			dataIndex: 'modelName',
			align: 'center',
			ellipsis: true
		},
		{
			title: '传感器编号',
			dataIndex: 'sensorCode',
			align: 'center',
			ellipsis: true
		},
		{
			title: '传感器类型',
			dataIndex: 'sensorType',
			align: 'center',
			ellipsis: true
		},
		{
			title: '传感器路数',
			dataIndex: 'sensorRoute',
			align: 'center',
			ellipsis: true
		},
		{
			title: '报警上限',
			dataIndex: 'limitUp',
			align: 'center',
			ellipsis: true
		},
		{
			title: '报警下限',
			dataIndex: 'limitDown',
			align: 'center',
			ellipsis: true
		},

		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		}
	]

	const loadData = () => {
		return locationApi.locationTree(Object.assign(searchFormState.value)).then((res) => {
			if (res) {
				return res
			}
			return []
		})
	}
	// 重置
	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()
		})
	}
</script>

<style lang="less" scoped>
	.table_item {
		padding: 15px 20px;
		background-color: #ffffff;
		:deep(.ant-table-pagination-right) {
			justify-content: center !important;
		}
	}
</style>
