<!-- 
    filename: Repair.vue
    author: liubin
    date: 2023-12-12 13:54:53
    description: 
-->

<template>
	<div class="app-container SpecialEquipmentRepair">
		<!-- 搜索工作栏 -->
		<SearchBar
			:formConfigs="searchBarFormConfig"
			ref="search-bar"
			@select-changed="handleSearchBarChange"
			@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="60%"
			@close="cancel"
			@cancel="cancel"
			@confirm="submitForm">
			<DialogForm
				v-if="open"
				ref="form"
				v-model="form"
				:disabled="mode == 'detail'" />
		</base-dialog>
		<DetailDrawer
			v-if="addOrUpdateVisible"
			ref="addOrUpdate"
			@refreshDataList="getList"
			@destroy="addOrUpdateVisible = false" />
	</div>
</template>

<script>
import basicPageMixin from '@/mixins/lb/basicPageMixin';
import DetailDrawer from './CustomDialogForm.vue';
import {
	deleteRepair,
	exportRepairLogExcel,
} from '@/api/equipment/base/repair';
import { parseTime } from '@/utils/ruoyi';
import htmls from './htmls.vue';
import DialogForm from './Repair--add.vue';
import BaseDialogWrapperVue from '../components/BaseDialogWrapper.vue';

export default {
	name: 'SpecialEquipmentRepair',
	components: { DetailDrawer, DialogForm, BaseDialog: BaseDialogWrapperVue },
	mixins: [basicPageMixin],
	data() {
		return {
			addOrUpdateVisible: false,
			searchBarKeys: [
				'maintenanceResult',
				'createTime',
				'equipmentId',
				'specialType',
			],
			tableBtn: [
				this.$auth.hasPermi('equipment:repair:finish')
					? {
							type: 'finish',
							btnName: '完成',
					  }
					: undefined,
				this.$auth.hasPermi('equipment:repair:update')
					? {
							type: 'detail',
							btnName: '详情',
					  }
					: undefined,
				// this.$auth.hasPermi('equipment:repair:update')
				// 	? {
				// 			type: 'edit',
				// 			btnName: '修改',
				// 	  }
				// 	: undefined,
				this.$auth.hasPermi('equipment:repair:delete')
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			tableProps: [
				// {
				// 	prop: 'createTime',
				// 	label: '添加时间',
				// 	fixed: true,
				// 	width: 150,
				// 	filter: parseTime,
				// },
				{
					prop: 'repairOrderNumber',
					label: '设备维修单号',
					minWidth: 120,
					showOverflowtooltip: true,
				},
				{
					prop: 'lineName',
					label: '产线名',
					minWidth: 120,
					showOverflowtooltip: true,
				},
				{
					prop: 'sectionName',
					label: '工段名',
					minWidth: 120,
					showOverflowtooltip: true,
				},
				{
					prop: 'equipmentName',
					label: '设备名称',
					minWidth: 120,
					showOverflowtooltip: true,
				},
				{
					prop: 'faultDetail',
					label: '故障明细',
					minWidth: 120,
					showOverflowtooltip: true,
					subcomponent: htmls,
				},
				{
					prop: 'maintenanceStartTime',
					label: '维修开始时间',
					filter: parseTime,
					minWidth: 150,
					showOverflowtooltip: true,
				},
				{
					prop: 'maintenanceFinishTime',
					label: '维修结束时间',
					filter: parseTime,
					minWidth: 150,
					showOverflowtooltip: true,
				},
				{
					prop: 'maintenanceResult',
					label: '维修结果',
					showOverflowtooltip: true,
					filter: (v) => (v != null ? ['成功', '失败'][v] : ''),
				},
				{
					prop: 'maintenanceDetail',
					label: '维修描述',
					showOverflowtooltip: true,
					width: 110,
					subcomponent: htmls,
				},
				{
					prop: 'remark',
					label: '备注',
					minWidth: 120,
					showOverflowtooltip: true,
				},
				// {
				// 	prop: 'maintenanceStatus',
				// 	label: '维修状态',
				// 	filter: (v) => (v != null ? ['未完成', '完成', '进行中'][v] : ''),
				// },
				// { prop: 'maintenanceDuration', label: '维修时长(h)', width: 110 },
				// {
				// 	prop: 'equipmentName',
				// 	label: '设备名称',
				// 	minWidth: 100,
				// 	showOverflowtooltip: true,
				// },
				// {
				// 	prop: 'maintenanceDetail',
				// 	label: '维修明细',
				// 	subcomponent: htmls,
				// 	minWidth: 100,
				// 	showOverflowtooltip: true,
				// },
				// {
				// 	prop: 'repairman',
				// 	label: '维修工',
				// 	minWidth: 100,
				// 	showOverflowtooltip: true,
				// },
				// {
				// 	prop: 'repairmanPhone',
				// 	label: '联系方式',
				// 	minWidth: 100,
				// 	showOverflowtooltip: true,
				// },
			],
			searchBarFormConfig: [
				// {
				// 	type: 'select',
				// 	label: '设备大类',
				// 	placeholder: '请选择设备大类',
				// 	param: 'specialType',
				// 	onchange: true,
				// 	selectOptions: [
				// 		{ id: 1, name: '安全设备' },
				// 		{ id: 2, name: '消防设备' },
				// 		{ id: 3, name: '特种设备' },
				// 	],
				// 	filterable: true,
				// },
				{
					type: 'select',
					label: '设备',
					placeholder: '请选择设备',
					param: 'equipmentId',
					filterable: true,
				},
				{
					type: 'select',
					label: '维修结果',
					placeholder: '请选择维修结果',
					param: 'maintenanceResult',
					selectOptions: [
						{ name: '成功', id: '0' },
						{ name: '失败', id: '1' },
					],
				},
				// 时间段
				{
					type: 'datePicker',
					label: '时间段',
					dateType: 'daterange', // datetimerange
					format: 'yyyy-MM-dd',
					valueFormat: 'yyyy-MM-dd HH:mm:ss',
					// valueFormat: 'timestamp',
					rangeSeparator: '-',
					startPlaceholder: '开始日期',
					endPlaceholder: '结束日期',
					defaultTime: ['00:00:00', '23:59:59'],
					param: 'createTime',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('equipment:repair:export') ? 'button' : '',
					btnName: '导出',
					name: 'export',
					plain: true,
					color: 'primary',
				},
				{
					type: this.$auth.hasPermi('equipment:repair:create') ? 'button' : '',
					btnName: '新增',
					name: 'add',
					plain: true,
					color: 'success',
				},
			],
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 10,
				// maintenanceStatus: null,
				maintenanceResult: null,
				createTime: null,
				equipmentId: null,
				special: true,
				specialType: null,
			},
			// 表单参数
			form: {},
			basePath: '/base/equipment-repair-log',
			mode: null,
			allSpecialEquipments: [],
		};
	},
	created() {
		this.initSearchBar();
		this.getList();
	},
	methods: {
		initSearchBar() {
			this.http('/base/core-equipment/listAll', 'get').then(({ data }) => {
				this.allSpecialEquipments = data.filter((item) => item.special);
				this.setSearchBarEquipmentList(data.filter((item) => item.special));
			});
		},
		/** 查询列表 */
		getList() {
			this.loading = true;
			// 执行查询
			this.recv(this.queryParams).then((response) => {
				this.list = response.data.list;
				this.total = response.data.total;
				this.loading = false;
			});
		},
		/** 取消按钮 */
		cancel() {
			this.open = false;
			this.mode = null;
			this.reset();
		},
		/** 表单重置 */
		reset() {
			this.form = {
				id: null,
				repairOrderNumber: null,
				equipmentId: null,
				repairman: null,
				repairmanPhone: null,
				faultTime: null,
				faultLevel: null,
				maintenanceStartTime: null,
				maintenanceFinishTime: null,
				faultType: null,
				repairMode: null,
				maintenanceStatus: null,
				faultDetail: null,
				maintenanceDetail: null,
				remark: null,
				files: [
					// {
					// 	fileName: '',
					// 	fileType: '',
					// 	fileUrl: '',
					// },
				],
			};
			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;
			// this.info({ id }).then((response) => {
			// 	this.form = response.data;
			// 	// this.form.repairman = this.form.repairman.split(',')
			// 	this.open = true;
			// 	this.title = '修改维修记录';
			// });
			this.addOrUpdateVisible = true;
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init({ id: row.id });
			});
		},
		/** 完成按钮操作 */
		handlFinish(row) {
			this.addOrUpdateVisible = true;
			const params = {
				id: row.id,
				maintenanceStatus: 1,
			};
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(params);
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (!valid) {
					return;
				}

				// 修改的提交
				if (this.form.id != null) {
					this.put({
						...this.form,
						repairman: this.form.repairman.join(','),
					}).then((response) => {
						this.$modal.msgSuccess('修改成功');
						this.open = false;
						this.getList();
					});
					return;
				}
				// 添加的提交
				this.post({
					...this.form,
					repairman: this.form.repairman.join(','),
				}).then((response) => {
					this.$modal.msgSuccess('新增成功');
					this.open = false;
					this.getList();
				});
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const id = row.id;
			this.$modal
				.confirm('是否确认删除维修单号为"' + row.repairOrderNumber + '"的数据?')
				.then(() => {
					return deleteRepair(id);
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		handleDetail({ id }) {
			this.addOrUpdateVisible = true;
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init({ id: id }, true);
			});
		},
		/** 导出按钮操作 */
		handleExport() {
			// 处理查询参数
			let params = { ...this.queryParams };
			params.pageNo = undefined;
			params.pageSize = undefined;
			this.$modal
				.confirm('是否确认导出所有维修记录?')
				.then(() => {
					this.exportLoading = true;
					return exportRepairLogExcel(params);
				})
				.then((response) => {
					this.$download.excel(response, '设备维修.xls');
					this.exportLoading = false;
				})
				.catch(() => {});
		},
		// 处理表格按钮
		handleTableBtnClick({ data, type }) {
			console.log('nihc', data, type);
			switch (type) {
				case 'edit':
					this.handleUpdate(data);
					break;
				case 'delete':
					this.handleDelete(data);
					break;
				case 'detail':
					this.handleDetail(data);
					break;
				case 'finish':
					this.handlFinish(data);
					break;
			}
		},
		// 设备大类改变
		handleSearchBarChange({ param, value }) {
			if ('specialType' === param) {
				if (!value) {
					this.setSearchBarEquipmentList(this.allSpecialEquipments);
					return;
				}
				this.setSearchBarEquipmentList(
					this.allSpecialEquipments.filter((item) => item.specialType == value)
				);
			}
		},
		setSearchBarEquipmentList(eqList) {
			this.$set(
				this.searchBarFormConfig[0],
				'selectOptions',
				eqList.map((item) => ({
					name: item.name,
					id: item.id,
				}))
			);
		},
	},
};
</script>

<style scoped lang="scss">
.SpecialEquipmentRepair {
}
</style>
