<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<SearchBarNew v-model="searchBarForm" @action="handleSearchBarActions" />

		<!-- 列表 -->
		<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="60%"
			@close="cancel"
			@cancel="cancel"
			@confirm="submitForm">
			<!-- <DialogForm
				v-if="open"
				ref="form"
				v-model="form"
				:disabled="mode == 'detail'"
				:has-files="false"
				:rows="rows" /> -->
		</base-dialog>
	</div>
</template>

<script>
import basicPageMixin from '@/mixins/lb/basicPageMixin';
import { parseTime } from '@/utils/ruoyi';
import SearchBarNew from '../SearchBar.vue';

const btn = {
	name: 'tableBtn',
	props: ['injectData'],
	components: {},
	data() {
		return {};
	},
	methods: {
		handleClick() {
			this.$emit('emitData', {
				action: this.injectData.label,
				value: this.injectData,
			});
		},
	},
	render: function (h) {
		return (
			<el-button type="text" onClick={this.handleClick}>
				{this.injectData.name}
			</el-button>
		);
	},
};

// monitoring/equipment-monitor/realtime-page
export default {
	name: 'StatusAndParams',
	mixins: [basicPageMixin],
	components: { SearchBarNew },
	data() {
		return {
			tableProps: [
				{ prop: 'equipmentName', label: '设备名称' },
				{ prop: 'equipmentCode', label: '设备编码' },
				{ prop: 'inQuantity', label: '投入数' },
				{ prop: 'outQuantity', label: '产出数' },
				{
					prop: 'run',
					label: '是否运行',
					filter: (val) => (val != null ? (val ? '是' : '否') : ''),
				},
				{
					prop: 'status',
					label: '状态',
					filter: (val) =>
						val != null ? ['正常', '计划停机', '故障'][val] : '',
				},
				{
					prop: 'error',
					label: '是否故障',
					filter: (val) => (val != null ? (val ? '是' : '否') : ''),
				},
				{
					prop: 'quantityRecordTime',
					label: '生产量记录时间',
					filter: parseTime,
				},
				{ prop: 'statusRecordTime', label: '状态记录时间', filter: parseTime },
				{ prop: 'opt1', label: '状态可视化', name: '查看', subcomponent: btn },
				{ prop: 'opt2', label: '参数监控', name: '查看', subcomponent: btn },
			],
			searchBarForm: {
				productionLineId: null,
				equipmentId: null,
			},
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 10,
				equipmentId: null,
				productionLineId: null,
			},
			list: [],
		};
	},
	mounted() {
		this.getList();
	},
	methods: {
		/**
		 * 处理表格按钮点击事件
		 * @param {string} action 按钮名称
		 * @param {object} value 按钮数据(行数据) { equipmentId, equipmentCode, equipmentName, inQuantity, outQuantity, run, status, error, quantityRecordTime, statusRecordTime}
		 */
		handleEmitFun({ action, value: row } = {}) {
			if (action === '状态可视化') {
				// 状态可视化
			} else if (action === '参数监控') {
				// 参数监控
				// 接口需要 equipmentId recordTime workOrderId 工单id
				this.$router.push({
					// path: '/equipment/monitor/full-params',
					name: 'EquipmentFullParams',
					params: {
						equipmentId: row.equipmentId,
						equipmentCode: row.equipmentCode,
						equipmentName: row.equipmentName,
					},
				});
			} else {
			}
		},
		/** 查询列表 */
		getList() {
			this.loading = true;
			// 执行查询
			this.http(
				'/monitoring/equipment-monitor/realtime-page',
				'get',
				this.queryParams
			).then((response) => {
				this.list = response.data.list;
				this.total = response.data.total;
				this.loading = false;
			});
		},
		successSubmit() {
			this.cancel();
			this.getList();
		},
		/** 取消按钮 */
		cancel() {
			this.open = false;
			// this.mode = null;
			// this.reset();
		},
		/** 表单重置 */
		// reset() {
		// 	this.form = {
		// 		id: null,
		// 		name: null,
		// 		equipmentId: null,
		// 		description: null,
		// 		responsible: null
		// 	};
		// 	this.resetForm('form');
		// },
		handleSearchBarActions({ action, payload }) {
			switch (action) {
				case 'search':
					this.handleQuery();
					break;
				case 'reset':
					this.resetQuery();
					break;
				default:
					break;
			}
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams = {
				...this.queryParams,
				...this.searchBarForm,
				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;
			this.info({ id }).then((response) => {
				this.form = response.data;
				this.open = true;
				this.title = '修改备品备件配置';
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs.addOrUpdate.dataFormSubmit();
			// this.$refs['form'].validate((valid) => {
			// 	if (!valid) {
			// 		return;
			// 	}
			// 	// 修改的提交
			// 	if (this.form.id != null) {
			// 		this.put(this.form).then((response) => {
			// 			this.$modal.msgSuccess('修改成功');
			// 			this.open = false;
			// 			this.getList();
			// 		});
			// 		return;
			// 	}
			// 	// 添加的提交
			// 	this.post(this.form).then((response) => {
			// 		this.$modal.msgSuccess('新增成功');
			// 		this.open = false;
			// 		this.getList();
			// 	});
			// });
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const id = row.id;
			this.$modal
				.confirm('是否确认删除记录"' + row.name + '"?')
				.then(() => {
					return this.del({ id });
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		handleDetail({ id }) {
			this.reset();
			this.mode = 'detail';
			this.info({ id }).then((response) => {
				this.form = response.data;
				this.open = true;
				this.title = '修改备品备件配置';
			});
		},
		/** 导出按钮操作 */
		handleExport() {
			// 处理查询参数
			let params = { ...this.queryParams };
			params.pageNo = undefined;
			params.pageSize = undefined;
			this.$modal
				.confirm('是否确认导出所有备品备件配置?')
				.then(() => {
					this.exportLoading = true;
					return exportEquipmentTypeExcel(params);
				})
				.then((response) => {
					this.$download.excel(response, '备品备件配置.xls');
					this.exportLoading = false;
				})
				.catch(() => {});
		},
	},
};
</script>
