<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="700px"
			@close="cancel"
			@cancel="cancel"
			@confirm="submitForm">
			<DialogForm v-if="open" ref="form" :dataForm="form" :rows="rows" />
		</base-dialog>
	</div>
</template>

<script>
import {
	createEquipmentPlcParam,
	updateEquipmentPlcParam,
	deleteEquipmentPlcParam,
	getEquipmentPlcParam,
	getEquipmentPlcParamPage,
	exportEquipmentPlcParamExcel,
} from '@/api/base/equipmentPlcParam';
import moment from 'moment';
import basicPageMixin from '@/mixins/lb/basicPageMixin';
import { publicFormatter } from '@/utils/dict';

export default {
	name: 'EquipmentPlcParam',
	mixins: [basicPageMixin],
	data() {
		return {
			tableProps: [
				// {
				// 	prop: 'createTime',
				// 	label: '添加时间',
				// 	fixed: true,
				// 	width: 180,
				// 	filter: (val) => moment(val).format('yyyy-MM-DD HH:mm:ss'),
				// },
				{ prop: 'plcParamName', label: '参数列名' },
				{ prop: 'name', label: '参数名称' },
				{
					prop: 'unit',
					label: '单位',
					filter: publicFormatter('unit_dict'),
				},
				{
					prop: 'collection',
					label: '是否采集',
					filter: (val) => (val != null ? ['否', '是'][val] : '-'),
				},
				{ prop: 'minValue', label: '最小值' },
				{ prop: 'maxValue', label: '最大值' },
				{ prop: 'defaultValue', label: '标准值' },
				{ prop: 'description', label: '描述' },
				{ prop: 'remark', label: '备注' },
				// {
				// 	_action: 'params-bind',
				// 	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,
				// 								payload: _this.injectData,
				// 							});
				// 						},
				// 					},
				// 				},
				// 				'查看绑定'
				// 			);
				// 		},
				// 	},
				// },
			],
			rows: [
				[
					{
						input: true,
						label: '参数列名',
						prop: 'plcParamName',
						rules: [{ required: true, message: '参数列名不能为空', trigger: 'blur' }],
					},
					{
						input: true,
						label: '参数名称',
						prop: 'name',
						rules: [{ required: true, message: '参数名称不能为空', trigger: 'blur' }],
					},
				],
				[
					{
						select: true,
						label: '单位',
						prop: 'unit',
						options: this.getDictDatas(this.DICT_TYPE.UNIT_DICT),
					},
					{
						switch: true,
						label: '是否采集',
						prop: 'collection',
						bind: {
							'active-value': 1,
							'inactive-value': 0,
						},
					},
				],
				[
					{
						input: true,
						label: '最小值',
						prop: 'minValue',
						rules: [
							{
								type: 'number',
								message: '请输入正确的数字',
								trigger: 'change',
								transform: (val) => Number(val),
							},
						],
					},
					{
						input: true,
						label: '最大值',
						prop: 'maxValue',
						rules: [
							{
								required: false,
							},
							{
								type: 'number',
								message: '请输入正确的数字',
								trigger: 'change',
								transform: (val) => Number(val),
							},
						],
					},
				],
				[
					{
						input: true,
						label: '标准值',
						prop: 'defaultValue',
					},
					{
						input: true,
						label: '描述',
						prop: 'description',
					},
				],
				[
					{
						input: true,
						label: '备注',
						prop: 'remark',
					},
				],
			],
			open: false,
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 10,
				connectId: null,
			},
			// 表单参数
			form: {},
			connectId: null,
			equipmentName: '',
			plcTableName: '',
			searchBarFormConfig: [
				{
					type: 'input',
					label: '设备名',
					disabled: true,
					param: 'equipmentName',
					defaultSelect: '',
				},
				{
					type: 'input',
					label: '关联表名',
					disabled: true,
					param: 'plcName',
					defaultSelect: '',
				},
				{
					type:
						this.$auth.hasPermi('base:equipment-plc-param:create') &&
						!this.isDetailPage
							? 'button'
							: '',
					btnName: '新增',
					name: 'add',
					plain: true,
					color: 'success',
				},
			],
		};
	},
	computed: {
		isDetailPage() {
			return this.$route.params.detail === true;
		},
		tableBtn() {
			return [
				this.$auth.hasPermi('base:equipment-plc-param:update') &&
				!this.isDetailPage
					? {
							type: 'edit',
							btnName: '修改',
					  }
					: undefined,
				this.$auth.hasPermi('base:equipment-plc-param:delete') &&
				!this.isDetailPage
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v);
		},
		// searchBarFormConfig() {
		// 	console.log(
		// 		'ers',
		// 		JSON.stringify({ eid: this.equipmentName, plc: this.plcTableName })
		// 	);
		// 	return [
		// 		{
		// 			type: 'input',
		// 			label: '设备名',
		// 			disabled: true,
		// 			param: 'equipmentName',
		// 			defaultSelect: this.equipmentName,
		// 		},
		// 		{
		// 			type: 'input',
		// 			label: '关联表名',
		// 			disabled: true,
		// 			param: 'plcName',
		// 			defaultSelect: this.plcTableName,
		// 		},
		// 		{
		// 			type:
		// 				this.$auth.hasPermi('base:equipment-plc-param:create') &&
		// 				!this.isDetailPage
		// 					? 'button'
		// 					: '',
		// 			btnName: '新增',
		// 			name: 'add',
		// 			plain: true,
		// 			color: 'success',
		// 		},
		// 	];
		// },
	},
	activated() {
		console.log('activated,,,');
		// 设置顶部搜索栏信息
		const { equipmentName, id: connectId, plcTableName } = this.$route.params;
		// this.equipmentName = equipmentName;
		// this.plcTableName = plcTableName;
		this.setSearchBarFormValue('equipmentName', equipmentName);
		this.setSearchBarFormValue('plcName', plcTableName);
		this.queryParams.connectId = connectId;
		// if (!equipmentGroupId) this.getList(); // 拦截
		this.getList();
	},
	deactivated() {
		console.log('deactivated,,,');
		this.setSearchBarFormValue('equipmentName', '');
		this.setSearchBarFormValue('plcName', '');
		// this.equipmentName = null;
		// this.plcTableName = null;
		this.queryParams.connectId = null;
	},
	methods: {
		/** 设置 searchBarForm 的默认值 - 用得比较少 */
		setSearchBarFormValue(param, value) {
			this.searchBarFormConfig.forEach((config) => {
				if (config.param == param) {
					config.defaultSelect = value;
				}
			});
		},
		/** 查询列表 */
		getList() {
			this.loading = true;
			// 执行查询
			getEquipmentPlcParamPage(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,
				connectId: undefined,
				plcParamName: undefined,
				name: undefined,
				unit: undefined,
				minValue: null,
				maxValue: null,
				defaultValue: undefined,
				collection: undefined,
				description: 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;
			getEquipmentPlcParam(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) {
					updateEquipmentPlcParam({
						...this.form,
						connectId: this.queryParams.connectId,
					}).then((response) => {
						this.$modal.msgSuccess('修改成功');
						this.open = false;
						this.getList();
					});
					return;
				}
				// 添加的提交
				createEquipmentPlcParam({
					...this.form,
					connectId: this.queryParams.connectId,
				}).then((response) => {
					this.$modal.msgSuccess('新增成功');
					this.open = false;
					this.getList();
				});
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const id = row.id;
			this.$modal
				.confirm('是否确认删除"' + row.name + '"的参数绑定?')
				.then(function () {
					return deleteEquipmentPlcParam(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 exportEquipmentPlcParamExcel(params);
				})
				.then((response) => {
					this.$download.excel(response, '设备数采详情.xls');
					this.exportLoading = false;
				})
				.catch(() => {});
		},
	},
};
</script>