<template>
	<a-card :bordered="false">
		<a-card style="border:none;margin-bottom: 15px">
			<a-form :model="countGroupType">
				<a-row :gutter="24">
					<a-col :span="2">
						待处理统计：
						<br>
						点击处理👉
					</a-col>
					<a-col :span="4">
						<router-link :to="{name:'depotWorkAlarm'}">
							<div class="tittle_size">出库入库：</div>
							<span class="number_circle"> {{ countGroupType.chuRuKuCount }} </span>
						</router-link>
					</a-col>
					<a-col :span="4">
						<router-link :to="{name:'depotGrainInfoWarn'}">
							<div class="tittle_size">风险预警：</div>
							<span class="number_circle"> {{ countGroupType.loanCount }} </span>
						</router-link>
					</a-col>
					<a-col :span="4">
						<router-link :to="{name:'depotDeviceWarn'}">
							<div class="tittle_size">设备离线：</div>
							<span class="number_circle"> {{ countGroupType.deviceCount }} </span>
						</router-link>
					</a-col>
					<a-col :span="4">
						<router-link :to="{name:'depotFlyerSiteLogWarn'}">
							<div class="tittle_size">飞行监管：</div>
							<span class="number_circle"> {{ countGroupType.flyerCount }} </span>
						</router-link>
					</a-col>
					<a-col :span="4">
						<router-link :to="{name:'depotAlarm'}">
							<div class="tittle_size">项目离线：</div>
							<span class="number_circle"> {{ countGroupType.projectCount }} </span>
						</router-link>
					</a-col>
				</a-row>
			</a-form>
		</a-card>
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="企业客户" name="depotId">
						<a-select v-model:value="searchFormState.depotId" placeholder="请选择企业客户" :options="depotOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="异常分类" name="alarmType">
						<a-select v-model:value="searchFormState.alarmType" placeholder="请选择异常分类" :options="alarmTypeOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="处理状态" name="saveState">
						<a-select v-model:value="searchFormState.saveState" placeholder="请选择处理状态" :options="saveStateOptions" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="异常信息" name="alarmMsg">
						<a-input v-model:value="searchFormState.alarmMsg" placeholder="请输入异常信息"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
				<template v-if="advanced">
					<a-col :span="6">
						<a-form-item label="创建时间" name="createTime">
							<a-range-picker v-model:value="searchFormState.createTime" show-time value-format="YYYY-MM-DD HH:mm:ss"/>
						</a-form-item>
					</a-col>
				</template>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
		>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'alarmMsg'">
					<a-tag v-if="record.colourLv === '黄' && record.saveState === '待处理'" color="yellow">
						{{ record.alarmMsg }}
					</a-tag>
					<a-tag v-else-if="record.colourLv === '红' && record.saveState === '待处理'" color="red">
						{{ record.alarmMsg }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('depotAlarmEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['depotAlarmEdit', 'depotAlarmDelete'], 'and')"/>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteDepotAlarm(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('depotAlarmDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)"/>
</template>

<script setup name="depotAlarm">
import {message} from 'ant-design-vue'
import tool from '@/utils/tool'
import Form from './form.vue'
import depotAlarmApi from '@/api/biz/depotAlarmApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
import depotTruckBookApi from "@/api/biz/depotTruckBookApi";
import {useRoute} from 'vue-router';

let searchFormState = reactive({})
const countGroupType = ref({})
const searchFormRef = ref()
const table = ref()
const formRef = ref()
const depotOptions = ref([])
const toolConfig = {refresh: true, height: true, columnSetting: true, striped: false}
// 查询区域显示更多控制
const advanced = ref(false)
const toggleAdvanced = () => {
	advanced.value = !advanced.value
}
const columns = [
	{
		title: '企业客户',
		dataIndex: 'depotGrainName',
		width: 200
	},
	{
		title: '异常信息',
		dataIndex: 'alarmMsg',
		width: 800
	},
	{
		title: '异常分类',
		dataIndex: 'alarmType',
		width: 80
	},
	{
		title: '处理状态',
		dataIndex: 'saveState',
		width: 80
	},
	{
		title: '创建时间',
		dataIndex: 'createTime',
		width: 150
	},
]
// 操作栏通过权限判断是否显示
/*if (hasPerm(['depotAlarmEdit', 'depotAlarmDelete'])) {
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	})
}*/
let selectedRowKeys = ref([])
// 列表选择配置
const options = {
	alert: {
		show: false,
		clear: () => {
			selectedRowKeys = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
dictOptionsApi.options(Object.assign('', {'dictValue': '粮库'})).then((data) => {
	depotOptions.value = data
})
const loadData = (parameter) => {
	const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
	//searchFormParam.saveState = searchFormParam.saveState && searchFormParam.saveState.toString().indexOf(',') < 0 ? searchFormParam.saveState.toString() : null
	if (searchFormParam.createTime) {
		searchFormParam.startCreateTime = searchFormParam.createTime[0]
		searchFormParam.endCreateTime = searchFormParam.createTime[1]
		delete searchFormParam.createTime
	}
	getAlarmAddCountGroupType(searchFormParam)
	// createTime范围查询条件重载
	return depotAlarmApi.depotAlarmPage(Object.assign(parameter, searchFormParam)).then((data) => {
		return data
	})
}
//待处理统计
const getAlarmAddCountGroupType = (searchFormParam) => {
	depotAlarmApi.getAlarmAddCountGroupType(searchFormParam).then((data) => {
		countGroupType.value = data
	})
}
// 删除
const deleteDepotAlarm = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	depotAlarmApi.depotAlarmDelete(params).then(() => {
		table.value.refresh(true)
	})
}
// 批量删除
const deleteBatchDepotAlarm = () => {
	if (selectedRowKeys.value.length < 1) {
		message.warning('请选择一条或多条数据')
		return false
	}
	const params = selectedRowKeys.value.map((m) => {
		return {
			id: m
		}
	})
	depotAlarmApi.depotAlarmDelete(params).then(() => {
		table.value.clearRefreshSelected()
	})
}
const alarmTypeOptions = tool.dictList('异常分类')
const saveStateOptions = tool.dictList('处理汇总状态')
</script>
<style>
.tittle_size {
	font-size: 20px;
	float: left;
}

.number_circle {
	display: table-cell;
	border-radius: 18px;
	width: 25px;
	height: 25px;
	text-align: center;
	vertical-align: middle;
	border: 1px solid red;
	color: red;
	padding: 0px 10px;
}
</style>
