<template>
	<div>
		<a-row :gutter="16">
			<a-col :span="6">
				<a-card class="snowy-monitor-card" :bordered="false" title="设备总数">
					<div class="snowy-monitor-card-span">{{info.deviceNum}}</div>
				</a-card>
			</a-col>
			<a-col :span="6">
				<a-card class="snowy-monitor-card" :bordered="false" title="在线设备">
					<div class="snowy-monitor-card-span green">{{info.offDeviceNum}}</div>
				</a-card>
			</a-col>
			<a-col :span="6">
				<a-card class="snowy-monitor-card" :bordered="false" title="离线设备">
					<div class="snowy-monitor-card-span red">{{info.onDeviceNum}}</div>
				</a-card>
			</a-col>
			<a-col :span="6">
				<a-card class="snowy-monitor-card" :bordered="false" title="设备在线率">
					<div class="snowy-monitor-card-span">{{info.onlineRate}}</div>
				</a-card>
			</a-col>
		</a-row>
	</div>
	<br>
	<a-card :bordered="false" style="margin-bottom: 10px">
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item name="deviceType" label="设备类别">
						<a-select v-model:value="searchFormState.deviceType" placeholder="请选择设备类别">
							<a-select-option v-for="item in typeList" :key="item.id" :value="item.id">{{item.configName}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item name="installPosition" label="安装位置">
						<a-tree-select
							v-model:value="searchFormState.installPosition"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择安装位置"
							allow-clear
							tree-default-expand-all
							:tree-data="treeData"
							:field-names="{
								children: 'children',
								label: 'name',
								value: 'id'
							}"
							selectable="false"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-space>
						<a-button type="primary" @click="table.refresh(true)">
							<template #icon><SearchOutlined /></template>
							查询
						</a-button>
						<a-button @click="reset">重置</a-button>
					</a-space>
				</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
			:row-key="(record) => record.id"
			:row-selection="options.rowSelection"
			@resizeColumn="(w, col) => {
				col.width = w
			}"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen(undefined, searchFormState)">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchDevice" />
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'status'">
					<a-tag v-if="record.status" color="#87d068">在线</a-tag>
					<a-tag v-else color="red">离线</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a @click="formRef.onOpen(record, searchFormState)">编辑</a>
					<a-divider type="vertical" />
					<a-popconfirm title="删除此设备吗？" @confirm="deleteDevice(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="refresh" />
</template>

<script setup name="aiDevice">
	import bizAiPollingApi from "@/api/biz/ai/bizAiPollingApi";
	import bizHouseApi from "@/api/biz/bizHouseApi";
	import Form from './form.vue'
	const info = ref({
		deviceNum: 0,
		offDeviceNum: 0,
		onDeviceNum: 0,
		onlineRate: 0
	})
	const initCount = () => {
		bizAiPollingApi.getDeviceCount().then((res) => {
			info.value = res
		})
	}
	const typeList = ref([])
	const initType = () => {
		bizAiPollingApi.getDeviceType().then(res => {
			typeList.value = res
		})
	}
	const treeData = ref([])
	const initHouse = () => {
		bizHouseApi.basePigpenTree().then(res => {
			treeData.value = res
		})
	}

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

	const table = ref()
	const formRef = ref()
	const giveRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const show = ref(true)
	const columns = [
		{
			title: '设备编号',
			dataIndex: 'deviceCode',
			width: 150,
			fixed: true
		},
		{
			title: '设备名称',
			dataIndex: 'deviceName',
			width: 200,
			resizable: true,
			ellipsis: true
		},
		{
			title: '设备类型',
			dataIndex: 'deviceType',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '安装位置',
			dataIndex: 'buildName',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '风机数量',
			dataIndex: 'fanNum',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '水帘数量',
			dataIndex: 'curtainNum',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '设备状态',
			dataIndex: 'status',
			width: 150,
			resizable: true,
			ellipsis: true,
			sorter: true
		},
		{
			title: '操作',
			dataIndex: 'action',
			width: 200,
			fixed: 'right'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['basePigpenEdit', 'basePigpenDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	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) => {
		return bizAiPollingApi.getDevicePage(Object.assign(parameter, searchFormState.value)).then(data => {
			return data
		})
	}

	const deleteBatchDevice = (params) => {
		bizAiPollingApi.delDevice(params).then(() => {
			table.value.clearRefreshSelected()
			initCount()
		})
	}

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

	const refresh = () => {
		table.refresh(true)
		initCount()
	}

	onMounted(() => {
		initCount()
		// initType()
		initHouse()
	})
</script>

<style scoped>
.snowy-monitor-card {
	height: 150px;
}
.snowy-monitor-card-span {
	text-align: center;
	font-size: 36px;
}
.green {
	color: #87d068;
}
.red {
	color: #f50;
}
</style>
