<!--
 * @Author: zwq
 * @Date: 2025-06-25 09:53:10
 * @LastEditors: zwq
 * @LastEditTime: 2026-04-28 10:59:33
 * @Description:
-->

<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<el-form
			:inline="true"
			ref="searchBarForm"
			:model="queryParams"
			class="searchBar">
			<span class="blue-block"></span>
			<el-form-item label="时间维度">
				<el-radio-group size="small" v-model="queryParams.radio">
					<el-radio :label="1">周期报表</el-radio>
					<el-radio :label="2">累计</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="时间维度">
				<el-select
					v-model="queryParams.type"
					size="small"
					:disabled="queryParams.radio === 2"
					placeholder="请选择">
					<el-option
						v-for="item in typeoptions"
						:key="item.value"
						:label="item.label"
						:value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="产线/工段">
				<el-cascader
					size="small"
					:options="options"
					ref="cascaderRef"
					collapse-tags
					@change="handleCascaderChange"
					:props="{
						value: 'id',
						label: 'name',
						checkStrictly: true,
						multiple: true,
						emitPath: false,
					}"></el-cascader>
			</el-form-item>
			<el-form-item label="时间范围" prop="recordTime">
				<el-date-picker
					size="small"
					v-model="queryParams.recordTime"
					type="daterange"
					format="yyyy-MM-dd"
					value-format="yyyy-MM-dd HH:mm:ss"
					range-separator="-"
					start-placeholder="开始时间"
					end-placeholder="结束时间"></el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button size="small" type="primary" @click="handleRecordSearch">
					查询
				</el-button>
				<el-button size="small" type="warning" @click="handleExport1">
					导出
				</el-button>
			</el-form-item>
		</el-form>

		<!-- 列表 -->
		<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';
import { getGroupPlanTree } from '@/api/group/Schedule';
import {
	exportReportExcel,
	exportQueryExcel,
	getReportPage,
	getQueryPage,
} from '@/api/analysis/exception';
import { parseTime } from '@/utils/ruoyi';

export default {
	name: 'ExceptionAnalysis',
	mixins: [basicPageMixin, tableHeightMixin],
	components: {},
	props: {},
	data() {
		return {
			tableBtn: [
				{
					type: 'edit',
					btnName: '修改',
				},
				{
					type: 'delete',
					btnName: '删除',
				},
			],
			tableProps: [
				{ prop: 'lineName', label: '产线' },
				{ prop: 'sectionName', label: '工段' },
				{ prop: 'equipmentName', label: '设备' },
				{ prop: 'createTime', filter: parseTime, label: '日期' },
				{
					width: 128,
					prop: 'actualRunningHours',
					label: '运行时长(h)',
					filter: (val) => (val != null ? val.toFixed(2) : '-'),
				},
				{ prop: 'downtimeCount', label: '故障次数' },
				{
					width: 240,
					prop: 'avgDowntimeDuration',
					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: 'downTime',
				// 	label: '故障时长(h)',
				// 	filter: (val) => (val != null ? val.toFixed(2) : '-'),
				// },
			],
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 10,
				radio: 1,
				type: null,
				factoryId: null,
				lineId: null,
				sectionId: null,
				equId: null,
				recordTime: null,
			},
			typeoptions: [
				{
					value: '4',
					label: '周',
				},
				{
					value: '5',
					label: '月',
				},
				{
					value: '6',
					label: '年',
				},
			],
			options: [], // 产线/工段选项
		};
	},
	computed: {},
	created() {
		getGroupPlanTree().then((res) => {
			this.options = res.data;
		});
		this.getList();
	},
	methods: {
		handleCascaderChange() {
			const checkedNodes = this.$refs.cascaderRef.getCheckedNodes();
			this.queryParams.factoryId = [];
			this.queryParams.lineId = [];
			this.queryParams.sectionId = [];
			this.queryParams.equId = [];
			checkedNodes.forEach((node) => {
				if (node.checked) {
					if (node.level === 1) {
						this.queryParams.factoryId.push(node.value);
					} else if (node.level === 2) {
						this.queryParams.lineId.push(node.value);
					} else if (node.level === 3) {
						this.queryParams.sectionId.push(node.value);
					} else if (node.level === 4) {
						this.queryParams.equId.push(node.value);
					}
				}
			});
		},
		handleRecordSearch() {
			if (
				this.queryParams.recordTime &&
				this.queryParams.recordTime.length > 0
			) {
				this.queryParams.startTime = this.queryParams.recordTime[0];
				this.queryParams.endTime = this.queryParams.recordTime[1];
			}
			this.getList();
		},

		async getList() {
			const params = {
				lineId: this.queryParams.lineId || null,
				sectionId: this.queryParams.sectionId || null,
				equipmentId: this.queryParams.equId || null,
				startTime: this.queryParams.startTime || null,
				endTime: this.queryParams.endTime || null,
				type:
					this.queryParams.radio === 1 ? this.queryParams.type || null : null,
			};
			this.loading = true;
			// 执行查询
			const { code, data } = await (this.queryParams.radio === 1
				? getReportPage(params)
				: getQueryPage(params));
			if (code === 0) {
				this.list = data;
			} else {
				this.list.splice(0);
			}
		},
		handleExport1() {
			this.handleRecordSearch();
			// 处理查询参数
			const params = {
				lineId: this.queryParams.lineId || null,
				sectionId: this.queryParams.sectionId || null,
				equipmentId: this.queryParams.equId || null,
				startTime: this.queryParams.startTime || null,
				endTime: this.queryParams.endTime || null,
				type:
					this.queryParams.radio === 1 ? this.queryParams.type || null : null,
			};
			this.$modal
				.confirm('是否确认导出所有数据项?')
				.then(() => {
					this.exportLoading = true;
					return this.queryParams.radio === 1
						? exportReportExcel(params)
						: exportQueryExcel(params);
				})
				.then((response) => {
					this.$download.excel(response, '报表.xls');
					this.exportLoading = false;
				})
				.catch(() => {});
		},
	},
};
</script>

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