<template>
	<div style="margin-bottom: 10px">
		<a-row :gutter="16">
			<a-col :span="6">
				<a-card class="snowy-monitor-card" :bordered="false" title="设备总数">
					<div class="snowy-monitor-card-span">{{info.envCount}}</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.onliveCount}}</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.offliveCount}}</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.rank}}</div>
				</a-card>
			</a-col>
		</a-row>
	</div>
	<a-card :bordered="false">
		<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="unitId" label="安装位置">
						<a-tree-select
							v-model:value="searchFormState.unitId"
							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 === 'deviceType'">
					<span>{{getTypeName(record)}}</span>
				</template>
				<template v-if="column.dataIndex === 'deviceStatus'">
					<a-tag v-if="record.deviceStatus" 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="feedDevice">
	import bizFeedDeviceApi from "@/api/biz/feed/bizFeedDeviceApi"
	import Form from './form.vue'
	import bizHouseApi from "@/api/biz/bizHouseApi";
	const info = ref({
		envCount: 0,
		offliveCount: 0,
		onliveCount: 0,
		rank: 0
	})
	const initCount = () => {
		bizFeedDeviceApi.getDeviceCount().then((res) => {
			info.value = res
		})
	}

	const typeList = ref([])
	const initType = () => {
		bizFeedDeviceApi.getDeviceType().then((res) => {
			typeList.value = res
		})
	}
	const getTypeName = (row) => {
		let str = ''
		typeList.value.forEach((item) => {
			if (item.id === row.deviceType) {
				str = item.configName
			}
		})
		return str
	}

	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: 'unitName',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '关联母猪耳标号',
			dataIndex: 'eartagNum',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '设备状态',
			dataIndex: 'deviceStatus',
			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 = (parameter) => {
		return bizFeedDeviceApi.getDevicePage(Object.assign(parameter, searchFormState.value)).then((res) => {
			return res
		})
	}

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

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

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

	const treeData = ref([])
	const initHouse = () => {
		bizHouseApi.basePigpenTree().then((res) => {
			treeData.value = res
		})
	}

	onMounted(() => {
		initCount()
		initHouse()
		initType()
	})

</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>
