<!-- 
    filename: dialogForm.vue
    author: liubin
    date: 2023-10-31 15:55:13
    description: 
-->

<template>
	<el-drawer
		:visible.sync="visible"
		:show-close="false"
		:wrapper-closable="!isLog"
		class="drawer"
		size="60%">
		<small-title slot="title" :no-padding="true">
			{{ !isLog ? '操作页面' : '更换记录' }}
		</small-title>
		<div class="content">
			<el-form
				ref="form"
				:model="dataForm"
				label-width="90px"
				v-loading="formLoading">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item v-if="!isLog" label="配置名" prop="name">
							<span style="margin-left: 5px">{{ dataForm.name }}</span>
						</el-form-item>
						<el-form-item v-else label="产线名称" prop="lineName">
							<span style="margin-left: 5px">{{ dataForm.lineName }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="设备名称" prop="equipmentName">
							<span style="margin-left: 5px">{{ dataForm.equipmentName }}</span>
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="描述" prop="description">
					<div v-html="dataForm.description" style="padding: 5px; margin-left: 5px; border: 1px solid #dfdfdf" />
					<!-- <editor v-model="dataForm.description" :read-only="true" :min-height="200"/> -->
				</el-form-item>
			</el-form>
			
			<div v-if="!isLog">
				<base-table
					:table-props="tableProps"
					:page="listQuery.pageNo"
					:limit="listQuery.pageSize"
					:table-data="list">
					<method-btn
						v-if="!isLog"
						slot="handleBtn"
						:width="120"
						label="操作"
						:method-list="tableBtn"
						@clickBtn="handleClick" />
				</base-table>
				<pagination
					v-show="listQuery.total > 0"
					:total="listQuery.total"
					:page.sync="listQuery.pageNo"
					:limit.sync="listQuery.pageSize"
					:page-sizes="[5, 10, 15]"
					@pagination="getList" />
			</div>

			<div v-else>
				<!-- 搜索工作栏 -->
				<SearchBar
					:formConfigs="formConfig"
					ref="search-bar"
					@headBtnClick="buttonClick" />
				<base-table
					:table-props="tableProps1"
					:page="listQuery1.pageNo"
					:limit="listQuery1.pageSize"
					:table-data="list1" />
				<pagination
					v-show="listQuery1.total > 0"
					:total="listQuery1.total"
					:page.sync="listQuery1.pageNo"
					:limit.sync="listQuery1.pageSize"
					:page-sizes="[5, 10, 15]"
					@pagination="getList1" />
			</div>
		</div>

		<div v-if="isLog" class="drawer-body__footer">
			<el-button type="primary" @click="goback()">关闭</el-button>
		</div>

		<attr-add
			v-if="addOrUpdateVisible"
			ref="addOrUpdate"
			:config-id="dataForm.id"
			@refreshDataList="getList" />
	</el-drawer>
</template>

<script>
import SmallTitle from '../../../base/alarm/Record/SmallTitle.vue';
import { getSparePartConfig } from '@/api/equipment/base/spare-parts/config'
import { getConfigDetMonitorPage, getSparePartLogPage, exportSparePartLogExcel } from '@/api/equipment/base/spare-parts/monitor'
import Editor from "@/components/Editor";
// import FileUpload from "@/components/FileUpload";
import { parseTime } from '../../../../core/mixins/code-filter';
import attrAdd from './attr-add';

const tableBtn = [
	{
		type: 'change',
		btnName: '更换',
	},
];

const tableProps = [
	{
		prop: 'sparePartName',
		label: '备品备件',
	},
	{
		prop: 'life',
		label: '使用寿命(h)',
	},
	{
		prop: 'timeUsed',
		label: '更换耗时(min)',
	},
	{
		prop: 'replacementTime',
		label: '更换时间',
		filter: parseTime
	},
	{
		prop: 'nextReplacementTime',
		label: '下次更换时间',
		filter: parseTime
	},
	{
		prop: 'color',
		label: '是否超期',
	},
	{
		prop: 'responsible',
		label: '操作人',
	},
	{
		prop: 'description',
		label: '备注'
	},
];

const tableProps1 = [
	{
		prop: 'sparePartName',
		label: '备品备件',
	},
	{
		prop: 'life',
		label: '使用寿命(h)',
	},
	{
		prop: 'timeUsed',
		label: '更换耗时(min)',
	},
	{
		prop: 'replacementTime',
		label: '更换时间',
		filter: parseTime
	},
	{
		prop: 'nextReplacementTime',
		label: '下次更换时间',
		filter: parseTime
	},
	{
		prop: 'responsible',
		label: '操作人',
	},
	{
		prop: 'description',
		label: '备注'
	},
];

export default {
	name: 'DialogForm',
	model: {
		prop: 'dataForm',
		event: 'update',
	},
	emits: ['update'],
	components: { SmallTitle, Editor, attrAdd },
	props: {
		// dataForm: {
		// 	type: Object,
		// 	default: () => ({}),
		// },
		// disabled: {
		// 	type: Boolean,
		// 	default: false
		// },
	},
	data() {
		return {
			tableBtn,
			tableProps,
			tableProps1,
			addOrUpdateVisible: false,
			formLoading: true,
			visible: false,
			isLog: false,
			dataForm: {},
			list: [],
			list1: [],
			listQuery: {
				pageSize: 10,
				pageNo: 1,
				total: 0,
			},
			listQuery1: {
				pageSize: 10,
				pageNo: 1,
				total: 0,
				replacementTime: undefined
			},
			formConfig: [
				{
					type: 'datePicker',
					label: '更换时间段',
					dateType: 'daterange', // datetimerange
					format: 'yyyy-MM-dd',
					valueFormat: 'yyyy-MM-dd HH:mm:ss',
					rangeSeparator: '-',
					startPlaceholder: '开始日期',
					endPlaceholder: '结束日期',
					defaultTime: ['00:00:00', '23:59:59'],
					param: 'replacementTime',
					// width: 350,
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: 'button',
					btnName: '导出',
					name: 'export',
					color: 'primary',
					plain: true
				},
			],
		};
	},
	mounted() {},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery1.pageNo = 1;
					this.listQuery1.pageSize = 10;
					this.listQuery1.replacementTime = val.replacementTime ? val.replacementTime : undefined;
					this.getList1();
					break;
				case 'export':
					this.handleExport();
					break;
				default:
					console.log(val);
			}
		},
		goback() {
			this.$emit('refreshDataList');
			this.visible = false;
		},
		/** 模拟透传 ref  */
		validate(cb) {
			return this.$refs.form.validate(cb);
		},
		resetFields(args) {
			return this.$refs.form.resetFields(args);
		},
		init(id, isLog) {
			// this.initData();
			this.isLog = isLog || false;
			this.dataForm.id = id || undefined;
			this.visible = true;

			this.$nextTick(() => {
				this.$refs['form'].resetFields();

				if (this.dataForm.id) {
					// 获取备品备件
					getSparePartConfig(this.dataForm.id).then(response => {
						this.formLoading = false
            this.dataForm = response.data;
          });
				} else {
					// if (this.urlOptions.isGetCode) {
          //   this.getCode()
          // }
				}
			});
			this.getList()
			this.formLoading = false
		},
		handleClick(raw) {
			if (raw.type === 'change') {
				// 更换
				console.log(raw)
				this.addNew(raw.data.sparePartId);
			} else {}
		},
		getList() {
			// 获取备品备件详情分页
			getConfigDetMonitorPage({
				...this.listQuery,
				configId: this.dataForm.id,
			}).then((response) => {
				this.list = response.data;
				this.listQuery.total = response.data.length;
			});
		},
		getList1() {
			// 获取备品备件更换log分页
			getSparePartLogPage({
				...this.listQuery1,
				configId: this.dataForm.id,
			}).then((response) => {
				this.list1 = response.data.list;
				this.listQuery1.total = response.data.total;
			});
		},
		// 新增 / 修改
		addNew(sparePartId) {
			this.addOrUpdateVisible = true;
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(sparePartId);
			});
		},
		/** 导出按钮操作 */
		handleExport() {
			// 处理查询参数
			let params = { ...this.listQuery1 };
			params.pageNo = undefined;
			params.pageSize = undefined;
			this.$modal
				.confirm('是否确认导出所有设备备品备件更换log数据项?')
				.then(() => {
					this.exportLoading = true;
					return exportSparePartLogExcel(params);
				})
				.then((response) => {
					this.$download.excel(response, '设备备品备件更换log.xls');
					this.exportLoading = false;
				})
				.catch(() => {});
		},
	},
};
</script>

<style scoped>
.el-date-editor,
.el-select {
	width: 100%;
}
.drawer-body__footer {
	display: flex;
	justify-content: flex-end;
	padding: 18px;
}
.action_btn {
  float: right;
  margin: 5px 15px;
  font-size: 14px;
}
.add {
	color: #0b58ff;
}
.drawer >>> .el-drawer {
	border-radius: 8px 0 0 8px;
	display: flex;
	flex-direction: column;
}

.drawer >>> .el-form-item__label {
	padding: 0;
}

.drawer >>> .el-drawer__header {
	margin: 0;
	padding: 32px 32px 24px;
	border-bottom: 1px solid #dcdfe6;
}
.drawer >>> .el-drawer__body {
	flex: 1;
	height: 1px;
	display: flex;
	flex-direction: column;
}

.drawer >>> .content {
	padding: 30px 24px;
	flex: 1;
	display: flex;
	flex-direction: column;
	/* height: 100%; */
}

.drawer >>> .visual-part {
	flex: 1 auto;
	max-height: 76vh;
	overflow: hidden;
	overflow-y: scroll;
	padding-right: 10px; /* 调整滚动条样式 */
}

.drawer >>> .el-form,
.drawer >>> .attr-list {
	padding: 0 16px;
}

.drawer-body__footer {
	display: flex;
	justify-content: flex-end;
	padding: 18px;
}
.content {
	padding: 0 20px;
}
</style>
