<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />
		<!-- 列表 -->
		<base-table
			:page="queryParams.pageNo"
			:limit="queryParams.pageSize"
			:table-props="tableProps"
			:table-data="list"
			:max-height="tableH">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="80"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:page.sync="queryParams.pageNo"
			:limit.sync="queryParams.pageSize"
			:total="total"
			@pagination="getList" />
		<!-- 新增 -->
		<base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="centervisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel"
			width="50%">
			<energy-limit-add
				ref="energyLimit"
				@successSubmit="successSubmit"
				:energyTypeList="energyTypeList"
				:objList="objList" />
		</base-dialog>
	</div>
</template>

<script>
import {
	getEnergyLimitPage,
	deleteEnergyLimit,
} from '@/api/monitoring/energyLimit';
import { getEnergyTypeListAll } from '@/api/base/energyType';
import { getTree } from '@/api/analysis/energyAnalysis';
import { publicFormatter } from '@/utils/dict';
import EnergyLimitAdd from './components/energyLimitAdd';
import tableHeightMixin from '@/mixins/lb/tableHeightMixin';
const tableProps = [
	{
		prop: 'method',
		label: '抄表方式',
		filter: publicFormatter('method'),
	},
	{
		prop: 'objName',
		label: '监控对象',
		minWidth: 100,
		showOverflowtooltip: true,
	},
	{
		prop: 'objCode',
		label: '对象编码',
		minWidth: 140,
		showOverflowtooltip: true,
	},
	{
		prop: 'tableName',
		label: '能源表名',
		filter: publicFormatter('table_name'),
		minWidth: 120,
		showOverflowtooltip: true,
	},
	{
		prop: 'energyType',
		label: '能源类型',
		filter: publicFormatter('energy_type'),
		showOverflowtooltip: true,
	},
	{
		prop: 'type',
		label: '监控模式',
		filter: (val) => (val != null ? ['合并', '详细'][val - 1] : '-'),
	},
	{
		prop: 'plcParamName',
		label: '监控参数',
	},
	{
		prop: 'limitType',
		label: '指标类型',
		filter: publicFormatter('monitor_index_type'),
	},
	{
		prop: 'limitValue',
		label: '阈值',
	},
];
export default {
	name: 'EnergyLimit',
	components: { EnergyLimitAdd },
	mixins: [tableHeightMixin],
	data() {
		return {
			formConfig: [
				{
					type: 'select',
					label: '能源类型',
					selectOptions: [],
					labelField: 'labelName',
					param: 'energyTypeId',
				},
				{
					type: 'select',
					label: '指标类型',
					selectOptions: this.getDictDatas(this.DICT_TYPE.MONITOR_INDEX_TYPE),
					labelField: 'label',
					valueField: 'value',
					param: 'limitType',
				},
				{
					type: this.$auth.hasPermi('monitoring:energy-limit:query')
						? 'button'
						: '',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: this.$auth.hasPermi('monitoring:energy-limit:create')
						? 'separate'
						: '',
				},
				{
					type: this.$auth.hasPermi('monitoring:energy-limit:create')
						? 'button'
						: '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true,
				},
			],
			tableProps,
			tableBtn: [
				this.$auth.hasPermi('monitoring:energy-limit:update')
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
				this.$auth.hasPermi('monitoring:energy-limit:delete')
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			// 总条数
			total: 0,
			// 班次基础信息列表
			list: [],
			// 弹出层标题
			addOrEditTitle: '',
			// 是否显示弹出层
			centervisible: false,
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 20,
				energyTypeId: null,
				limitType: null,
			},
			energyTypeList: [],
			typeList: [
				{ label: '合并', value: '1' },
				{ label: '详细', value: '2' },
			],
			objList: [],
		};
	},
	created() {
		this.getList();
		this.getTypeList();
		// 获取对象树形结构
		this.getObjTree();
	},
	methods: {
		getTypeList() {
			getEnergyTypeListAll().then((res) => {
				this.formConfig[0].selectOptions = res.data || [];
				this.energyTypeList = res.data || [];
			});
		},
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.queryParams.pageNo = 1;
					this.queryParams.energyTypeId = val.energyTypeId;
					this.queryParams.limitType = val.limitType;
					this.getList();
					break;
				default:
					this.addOrEditTitle = '新增';
					this.centervisible = true;
					this.$nextTick(() => {
						this.$refs.energyLimit.init();
					});
			}
		},
		/** 查询列表 */
		getList() {
			getEnergyLimitPage(this.queryParams).then((response) => {
				let arr = response.data.list || [];
				arr &&
					arr.map((item) => {
						this.typeList.map((i) => {
							if (item.type === i.value) {
								item.type = i.label;
							}
						});
						if (item.minValue && item.maxValue) {
							item.limitValue = item.minValue + '-' + item.maxValue;
						} else if (item.minValue) {
							item.limitValue = '最小值' + item.minValue;
						} else if (item.maxValue) {
							item.limitValue = '最大值' + item.maxValue;
						} else {
							item.limitValue = '';
						}
					});
				this.list = arr;
				this.total = response.data.total;
			});
		},
		handleClick(val) {
			switch (val.type) {
				case 'edit':
					this.addOrEditTitle = '编辑';
					this.$nextTick(() => {
						this.$refs.energyLimit.init(val.data.id);
					});
					this.centervisible = true;
					break;
				default:
					this.handleDelete(val.data);
			}
		},
		handleCancel() {
			this.$refs.energyLimit.formClear();
			this.centervisible = false;
			this.addOrEditTitle = '';
		},
		handleConfirm() {
			this.$refs.energyLimit.submitForm();
		},
		successSubmit() {
			this.handleCancel();
			this.getList();
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			this.$modal
				.confirm('是否确认删除监控对象为"' + row.objName + '"的数据项?')
				.then(function () {
					return deleteEnergyLimit(row.id);
				})
				.then(() => {
					this.queryParams.pageNo = 1;
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		getObjTree() {
			getTree().then((res) => {
				this.objList = res.data || [];
			});
		},
	},
};
</script>
