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

<template>
	<el-drawer
		:visible.sync="visible"
		:show-close="false"
		:wrapper-closable="disabled"
		class="drawer"
		size="60%"
		@closed="$emit('destroy')">
		<small-title slot="title" :no-padding="true">
			{{ disabled ? '查看备件' : '添加备件' }}
		</small-title>
		<div class="content">
			<el-form
				ref="form"
				:model="dataForm"
				label-width="100px"
				label-position="top"
				v-loading="formLoading">
				<el-row :gutter="20">
					<el-col :span="6">
						<el-form-item label="配置名" prop="name">
							<span>{{ dataForm.name }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="设备名称" prop="equipmentName">
							<span>{{ dataForm.equipmentName }}</span>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="负责人" prop="responsible">
							<span>{{ dataForm.responsible }}</span>
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="描述" prop="description">
					<div v-html="dataForm.description" style="padding: 5px; margin: 0; border: 1px solid #dfdfdf" />
					<!-- <editor v-model="dataForm.description" read-only :min-height="200"/> -->
				</el-form-item>
			</el-form>
			
			<div>
				<small-title style="margin: 16px 0; padding-left: 8px" :no-padding="true">
					{{ '备品备件' }}
				</small-title>
				<div v-if="!disabled" class="action_btn">
					<template>
						<span style="display: inline-block;" @click="addNew()">
							<svg-icon style="width: 14px; height: 14px" class="item-icon" icon-class="table_add" />
							<span class="add">添加</span>
						</span>
					</template>
				</div>
				<base-table
					:table-props="tableProps"
					:page="listQuery.pageNo"
					:limit="listQuery.pageSize"
					:table-data="list">
					<method-btn
						v-if="!disabled"
						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-if="!disabled" class="drawer-body__footer">
				<el-button type="primary" @click="goback()">关闭</el-button>
			</div>
		</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, deleteConfigDet,  getConfigDetPage } from '@/api/equipment/base/spare-parts/config'
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: 'delete',
		btnName: '删除',
	},
];

const tableProps = [
	{
		prop: 'createTime',
		label: '添加时间',
		filter: parseTime,
	},
	{
		prop: 'sparePartName',
		label: '备品备件',
	},
	{
		prop: 'life',
		label: '使用寿命(h)',
	},
	{
		prop: 'remark',
		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,
			addOrUpdateVisible: false,
			formLoading: true,
			visible: false,
			disabled: false,
			dataForm: {},
			list: [],
			listQuery: {
				pageSize: 10,
				pageNo: 1,
				total: 0,
			},
		};
	},
	mounted() {},
	methods: {
		goback() {
			this.$emit('refreshDataList');
			this.visible = false;
		},
		goEdit() {
			this.disabled = false;
		},
		/** 模拟透传 ref  */
		validate(cb) {
			return this.$refs.form.validate(cb);
		},
		resetFields(args) {
			return this.$refs.form.resetFields(args);
		},
		init(id, isdetail) {
			// this.initData();
			this.disabled = isdetail || 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;
						this.getList()
          });
				} else {
					// if (this.urlOptions.isGetCode) {
          //   this.getCode()
          // }
				}
			});
			this.formLoading = false
		},
		handleClick(raw) {
			if (raw.type === 'delete') {
				this.$confirm(
					`确定要删除名称为${raw.data.sparePartName}的数据?`,
					'提示',
					{
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					}
				)
					.then(() => {
						deleteConfigDet(raw.data.id).then(({ data }) => {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.getList();
								},
							});
						});
					})
					.catch(() => {});
			} else {
				this.addNew(raw.data.id);
			}
		},
		getList() {
			// 获取备品备件详情分页
			getConfigDetPage({
				...this.listQuery,
				configId: this.dataForm.id,
			}).then((response) => {
				this.list = response.data.list;
				this.listQuery.total = response.data.total;
			});
		},
		// 新增 / 修改
		addNew(id) {
			this.addOrUpdateVisible = true;
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(id);
			});
		}
	},
};
</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;
	display: flex;
	justify-content: right;
  margin: -35px 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>
