<!--
    filename: index.vue
    author: liubin
    date: 2023-09-04 09:34:52
    description: 设备异常分析
-->

<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"
			:max-height="tableH"
			@emitFun="handleEmitFun">
			<!-- <method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleTableBtnClick" /> -->
		</base-table>

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

<script>
import moment from 'moment';
import basicPageMixin from '@/mixins/lb/basicPageMixin';
import tableHeightMixin from '@/mixins/lb/tableHeightMixin';

export default {
	name: 'ExceptionAnalysis',
	mixins: [basicPageMixin, tableHeightMixin],
	components: {},
	props: {},
	data() {
		return {
			searchBarKeys: ['name', 'code'],
			// tableBtn: [
			// 	this.$auth.hasPermi('base:equipment-group:update')
			// 		? {
			// 				type: 'edit',
			// 				btnName: '修改',
			// 		  }
			// 		: undefined,
			// 	this.$auth.hasPermi('base:equipment-group:delete')
			// 		? {
			// 				type: 'delete',
			// 				btnName: '删除',
			// 		  }
			// 		: undefined,
			// ].filter((v) => v),
			tableBtn: [
				{
					type: 'edit',
					btnName: '修改',
				},
				{
					type: 'delete',
					btnName: '删除',
				},
			],
			tableProps: [
				{ prop: 'lineName', label: '产线' },
				{ prop: 'sectionName', label: '工段' },
				{ prop: 'equipmentName', label: '设备' },
				{
					width: 240,
					prop: 'mtbf',
					label: '平均故障间隔时间[MTBF](h)',
          filter: (val) => (val != null ? val.toFixed(2) : '-'),
				},
				{
					width: 240,
					prop: 'mttr',
					label: '平均维修时间[MTTR](h)',
          filter: (val) => (val != null ? val.toFixed(2) : '-'),
				},
				{ width: 128, prop: 'workTime', label: '工作时长(h)',
        filter: (val) => (val != null ? val.toFixed(2) : '-'), },
				{ width: 128, prop: 'downTime', label: '故障时长(h)',
        filter: (val) => (val != null ? val.toFixed(2) : '-'), },
				{ prop: 'downCount', label: '故障次数' },
			],
			searchBarFormConfig: [
				{
					type: 'select',
					label: '请选择月份',
					placeholder: '请选择月份',
					param: 'month',
					selectOptions: Array(12)
						.fill(0)
						.map((v, i) => ({
							id: i + 1,
							name: `${i + 1}月`,
						})),
				},
				{
					__index: 'line',
					type: 'select',
					label: '产线',
					placeholder: '请选择产线',
					param: 'lineId',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
			],
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 10,
				lineId: null,
				factoryId: null,
				recordTime: null,
			},
		};
	},
	computed: {},
	created() {
		this.fillLineOptions();
	},
	methods: {
		async fillLineOptions() {
			const { data } = await this.$axios({
				url: '/base/production-line/listAll',
				method: 'get',
			});
			const cfg = this.searchBarFormConfig.find(
				(item) => item.__index == 'line'
			);
			this.$set(
				cfg,
				'selectOptions',
				data.map((item) => ({
					id: item.id,
					name: item.name,
				}))
			);
		},

		async getList() {
			this.loading = true;
			// 执行查询
			const { code, data } = await this.$axios({
				url: '/analysis/equipment-analysis/efficiency',
				method: 'get',
				params: {
					lineId: this.queryParams.lineId || null,
					recordTime: this.queryParams.recordTime || null,
				},
			});
			if (code === 0) {
				this.list = data;
			} else {
				this.list.splice(0);
			}
		},

		handleSearchBarBtnClick(btn) {
			switch (btn.btnName) {
				case 'search':
					if (btn.month) {
						this.queryParams.recordTime = [
							moment()
								.month(btn.month - 1)
								.format('YYYY-MM') + '-01 00:00:00',
							moment().month(btn.month).format('YYYY-MM') + '-01 00:00:00',
						];
					} else {
						this.queryParams.recordTime = null;
					}
					this.queryParams.lineId = btn.lineId || null;
					this.handleQuery();
					break;
			}
		},

		/** 搜索按钮操作 */
		handleQuery() {
			// this.queryParams.pageNo = 1;
			this.getList();
		},

		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
	},
};
</script>

<style scoped lang="scss"></style>
