<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<SearchBar
			:formConfigs="searchBarFormConfig"
			ref="search-bar"
			@headBtnClick="handleSearchBarBtnClick" />

		<!-- 列表 -->
		<base-table
			:table-props="tableProps"
			:page="queryParams.pageNo"
			:limit="queryParams.pageSize"
			:table-data="list"
			@emitFun="handleEmitFun">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				label="操作"
				:width="120"
				:method-list="tableBtn"
				@clickBtn="handleTableBtnClick" />
		</base-table>

		<!-- 分页组件 -->
		<pagination
			v-show="total > 0"
			:total="total"
			:page.sync="queryParams.pageNo"
			:limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 对话框(添加 / 修改) -->
		<base-dialog
			:dialogTitle="title"
			:dialogVisible="open"
			width="500px"
			@close="cancel"
			@cancel="cancel"
			@confirm="submitForm">
			<DialogForm v-if="open" ref="form" v-model="form" :rows="rows" />
		</base-dialog>

		<!-- 抽屉 详情 -->
		<BasicDrawer
			v-if="editVisible"
			ref="drawer"
			:default-mode="editMode"
			:data-id="alarmForm.id"
			:sections="[
				{
					name: '基本信息',
					key: 'base',
					rows: drawerBaseInfoRows,
					url: '/base/equipment-group/get',
					urlUpdate: '/base/equipment-group/update',
					urlCreate: '/base/equipment-group/create',
					queryParams: { id: alarmForm.id },
				},
				{
					name: '报警明细',
					key: 'attrs',
					props: drawerListProps,
					url: '/base/equipment-group-alarm/page',
					urlCreate: '/base/equipment-group-alarm/create',
					urlUpdate: '/base/equipment-group-alarm/update',
					urlDelete: '/base/equipment-group-alarm/delete',
					urlDetail: '/base/equipment-group-alarm/get',
					queryParams: {
						equipmentGroupId: alarmForm.id,
						pageNo: 1,
						pageSize: 10,
					},
					tableBtn: [
						this.$auth.hasPermi('equipment:alarm-group:update')
							? {
									type: 'edit',
									btnName: '修改',
							  }
							: undefined,
						this.$auth.hasPermi('equipment:alarm-group:delete')
							? {
									type: 'delete',
									btnName: '删除',
							  }
							: undefined,
					].filter((v) => v),
					allowAdd: true,
				},
			]"
			@refreshDataList="getList"
			@cancel="editVisible = false"
			@destroy="editVisible = false" />
	</div>
</template>

<script>
import {
	createEquipmentGroup,
	updateEquipmentGroup,
	deleteEquipmentGroup,
	getEquipmentGroup,
	getEquipmentGroupPage,
	exportEquipmentGroupExcel,
} from '@/api/base/equipmentGroup';
import moment from 'moment';
import { publicFormatter } from '@/utils/dict';
import basicPageMixin from '@/mixins/lb/basicPageMixin';
// import { getAccessToken } from '@/utils/auth';
import BasicDrawer from './components/BasicDrawer.vue';

export default {
	name: 'EquipmentGroup',
	mixins: [basicPageMixin],
	components: { BasicDrawer },
	data() {
		return {
			editVisible: false,
			editMode: '',
			searchBarKeys: ['name', 'code'],
			tableBtn: [
				this.$auth.hasPermi('equipment:alarm-group:update')
					? {
							type: 'detail',
							btnName: '查看报警',
					  }
					: undefined,
				this.$auth.hasPermi('equipment:alarm-group:update')
					? {
							type: 'edit',
							btnName: '修改',
					  }
					: undefined,
				this.$auth.hasPermi('equipment:alarm-group:delete')
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			tableProps: [
				{
					prop: 'createTime',
					label: '添加时间',
					fixed: true,
					width: 180,
					filter: (val) => moment(val).format('yyyy-MM-DD HH:mm:ss'),
				},
				{ prop: 'name', label: '设备分组名称' },
				{ prop: 'code', label: '设备分组编码' },
				{ prop: 'remark', label: '备注' },
				// {
				// 	_action: 'equipment-group-show-alert',
				// 	label: '报警',
				// 	subcomponent: {
				// 		props: ['injectData'],
				// 		render: function (h) {
				// 			const _this = this;
				// 			return h(
				// 				'el-button',
				// 				{
				// 					props: { type: 'text' },
				// 					on: {
				// 						click: function () {
				// 							console.log('inejctdata', _this.injectData);
				// 							_this.$emit('emitData', {
				// 								action: _this.injectData._action,
				// 								// value: _this.injectData.id,
				// 								value: _this.injectData,
				// 							});
				// 						},
				// 					},
				// 				},
				// 				'查看报警'
				// 			);
				// 		},
				// 	},
				// },
			],
			drawerBaseInfoRows: [
				[
					{
						input: true,
						label: '设备分组名称',
						prop: 'name',
						rules: [{ required: true, message: '设备分组名称不能为空', trigger: 'blur' }],
					},
					{
						input: true,
						label: '设备分组编码',
						prop: 'code',
						// url: '/base/core-equipment/getCode',
					},
				],
			],
			drawerListProps: [
				{
					prop: 'createTime',
					label: '添加时间',
					fixed: true,
					filter: (val) => moment(val).format('yyyy-MM-DD HH:mm:ss'),
				},
				{ prop: 'code', label: '报警编码' },
				{
					prop: 'type',
					label: '报警类型',
					filter: (val) =>
						val != null ? ['-', '字符型', '布尔型', '-'][val] : '-',
				},
				{
					prop: 'grade',
					label: '报警级别',
					filter: publicFormatter(this.DICT_TYPE.EQU_ALARM_LEVEL),
				},
				{ prop: 'alarmCode', label: '设备报警编码' },
				{ prop: 'plcParamName', label: '参数列名' },
				{ prop: 'alarmContent', label: '报警内容' },
			],
			alarmForm: {
				id: undefined,
				equipmentGroupCode: undefined,
				equipmentGroupName: undefined,
			},
			searchBarFormConfig: [
				{
					type: 'input',
					label: '分组名称',
					placeholder: '请输入设备分组名称',
					param: 'name',
				},
				{
					type: 'input',
					label: '分组编码',
					placeholder: '请输入设备分组编码',
					param: 'code',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('equipment:alarm-group:create')
						? 'button'
						: '',
					btnName: '新增',
					name: 'add',
					plain: true,
					color: 'success',
				},
				// {
				// 	type: this.$auth.hasPermi('equipment:alarm-group:export') ? 'button' : '',
				// 	btnName: '导出',
				// 	name: 'export',
				// 	color: 'warning',
				// },
			],
			rows: [
				[
					{
						input: true,
						label: '分组名称',
						prop: 'name',
						rules: [{ required: true, message: '分组名称不能为空', trigger: 'blur' }],
						// bind: {
						// 	disabled: true, // some condition, like detail mode...
						// }
					},
				],
				[
					{
						input: true,
						label: '分组编码',
						prop: 'code',
						url: '/base/equipment-group/getCode',
						rules: [{ required: true, message: '分组编码不能为空', trigger: 'blur' }],
					},
				],
				[
					{
						input: true,
						label: '备注',
						prop: 'remark',
						bind: {
							placeholder: '请输入备注',
						},
					},
				],
			],
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 10,
				code: null,
				name: null,
			},
			// 表单参数
			form: {},
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 覆盖 handleEmitFun 的默认实现 */
		handleEmitFun({ action, value }) {
			const {
				id: equipmentGroupId,
				name: equipmentGroupName,
				code: equipmentGroupCode,
			} = value;
			switch (action) {
				case 'equipment-group-show-alert':
					// this.$router.push({ path: '/equipment/base/equipment-group-alarm' });
					this.$router.push({
						name: 'EquipmentGroupAlarm',
						params: {
							equipmentGroupId,
							equipmentGroupCode,
							equipmentGroupName,
						},
					});
					break;
			}
		},
		/** 查询列表 */
		getList() {
			this.loading = true;
			// 执行查询
			getEquipmentGroupPage(this.queryParams).then((response) => {
				this.list = response.data.list;
				this.total = response.data.total;
				this.loading = false;
			});
		},
		/** 取消按钮 */
		cancel() {
			this.open = false;
			this.reset();
		},
		/** 表单重置 */
		reset() {
			this.form = {
				id: undefined,
				code: undefined,
				name: undefined,
				remark: undefined,
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNo = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '添加设备分组';
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id;
			getEquipmentGroup(id).then((response) => {
				this.form = response.data;
				this.open = true;
				this.title = '修改设备分组';
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (!valid) {
					return;
				}
				// 修改的提交
				if (this.form.id != null) {
					updateEquipmentGroup(this.form).then((response) => {
						this.$modal.msgSuccess('修改成功');
						this.open = false;
						this.getList();
					});
					return;
				}
				// 添加的提交
				createEquipmentGroup(this.form).then((response) => {
					this.$modal.msgSuccess('新增成功');
					this.open = false;
					this.getList();
				});
			});
		},
		// 查看报警
		handleDetail(row) {
			// debugger;
			const { id, code, name, createTime } = row;
			// 打开抽屉
			this.editMode = 'detail';
			this.alarmForm.id = id;
			this.alarmForm.equipmentGroupCode = code;
			this.alarmForm.equipmentGroupName = name;
			this.editVisible = true;
			this.$nextTick(() => {
				this.$refs['drawer'].init();
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const id = row.id;
			this.$modal
				.confirm('是否确认删除设备分组 "' + row.name + '"?')
				.then(function () {
					return deleteEquipmentGroup(id);
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			// 处理查询参数
			let params = { ...this.queryParams };
			params.pageNo = undefined;
			params.pageSize = undefined;
			this.$modal
				.confirm('是否确认导出所有设备分组数据项?')
				.then(() => {
					this.exportLoading = true;
					return exportEquipmentGroupExcel(params);
				})
				.then((response) => {
					this.$download.excel(response, '设备分组.xls');
					this.exportLoading = false;
				})
				.catch(() => {});
		},
	},
};
</script>
