<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: lb
 * @LastEditTime: 2024-02-22 11:02:43
 * @Description:
-->
<template>
	<el-drawer
		:visible.sync="visible"
		:show-close="false"
		:wrapper-closable="true"
		class="drawer"
		size="50%">
		<small-title slot="title" :no-padding="true">
			{{ isdetail ? '详情' : '添加巡检' }}
		</small-title>
		<div class="content">
			<div class="visual-part">
				<el-form
					:model="dataForm"
					:rules="dataRule"
					ref="dataForm"
					@keyup.enter.native="dataFormSubmit()"
					label-position="top">
					<el-row :gutter="20">
						<!-- <el-col :span="8">
							<el-form-item label="设备大类" prop="equipmentCategory">
								<span>
									{{
										['-', '安全设备', '消防设备', '特种设备'][
											dataForm.equipmentCategory || 0
										]
									}}
								</span>
							</el-form-item>
						</el-col> -->
						<el-col :span="8">
							<el-form-item label="巡检单名称" prop="name">
								{{ dataForm.name }}
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="部门" prop="department">
								{{ dataForm.department }}
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="巡检时间" prop="planCheckTime">
								{{ dataForm.planCheckTime | parseTime }}
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>

			<el-divider />

			<div class="attr-list">
				<small-title
					style="margin: 16px 0; padding-left: 8px"
					:no-padding="true">
					巡检项目
				</small-title>

				<div v-if="!isdetail" class="action_btn">
					<template>
						<span style="display: inline-block">
							<el-button type="text" @click="addNew()" icon="el-icon-plus">
								添加
							</el-button>
						</span>
					</template>
				</div>
				<base-table
					:table-props="tableProps"
					:page="listQuery.pageNo"
					:limit="listQuery.pageSize"
					:table-data="checkDetList">
					<method-btn
						v-if="!isdetail"
						slot="handleBtn"
						:width="120"
						label="操作"
						:method-list="tableBtn"
						@clickBtn="handleClick" />
				</base-table>
				<pagination
					v-show="checkDetList.total > 0"
					:total="checkDetList.total"
					:page.sync="listQuery.pageNo"
					:limit.sync="listQuery.pageSize"
					:page-sizes="[5, 10, 15]"
					@pagination="getList" />

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

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

<script>
import SmallTitle from './SmallTitle';
import attrAdd from './attr-add';
import { DICT_TYPE, getDictDatas } from '@/utils/dict';
import { parseTime } from '../../core/mixins/code-filter';

const tableBtn = [
	{
		type: 'edit',
		btnName: '编辑',
	},
	{
		type: 'delete',
		btnName: '删除',
	},
];
const tableProps = [
	{
		prop: 'equipmentName',
		label: '设备名称',
	},
	{
		prop: 'program',
		label: '检查项目',
	},
];

export default {
	components: { SmallTitle, attrAdd },
	filters: {
		parseTime,
	},
	data() {
		return {
			tableBtn,
			tableProps,
			addOrUpdateVisible: false,
			listQuery: {
				pageSize: 10,
				pageNo: 1,
			},
			dataForm: {
				id: undefined,
				name: undefined,
				department: undefined,
				planCheckTime: undefined,
			},
			checkDetList: [],
			visible: false,
			isdetail: false,
			dataRule: {
				code: [
					{ required: true, message: '物料编码不能为空', trigger: 'blur' },
				],
				name: [
					{ required: true, message: '物料名称不能为空', trigger: 'blur' },
				],
				materialType: [
					{ required: true, message: '物料类型不能为空', trigger: 'change' },
				],
				productType: [
					{ required: true, message: '产品类型不能为空', trigger: 'change' },
				],
				processTime: [
					{
						required: true,
						message: '产线生产单位用时不能为空',
						trigger: 'blur',
					},
				],
			},
		};
	},
	mounted() {},
	methods: {
		initData() {
			this.checkDetList.splice(0);
			this.checkDetList.total = 0;
		},
		handleClick(raw) {
			if (raw.type === 'delete') {
				this.$confirm(
					`是否确认删除巡检项目名称为"${raw.data.program}"的数据项?`,
					'提示',
					{
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					}
				)
					.then(() => {
						deleteCheckDet(raw.data.id).then(({ data }) => {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.getList();
								},
							});
						});
					})
					.catch(() => {});
			} else {
				console.log('handle click....', raw);
				this.addNew(raw.data);
			}
		},
		async getList() {
			// 获取巡检项目分页
			const res = await this.$axios({
				url: '/base/equipment-check-order-det/page',
				params: { ...this.listQuery, orderId: this.dataForm.id },
			});
			if (res.code == 0) {
				this.checkDetList = res.data.list;
				this.checkDetList.total = res.data.total;
			}
		},
		init(row, isdetail) {
			this.initData();
			this.isdetail = isdetail || false;
			this.dataForm.id = row.id;
			this.visible = true;

			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
				if (this.dataForm.id) {
					// 获取设备项目列表
					Object.keys(this.dataForm).forEach((key) => {
						this.dataForm[key] = row[key];
					});
					this.getList();
				}
			});
		},
		goback() {
			this.$emit('refreshDataList');
			this.visible = false;
			// this.initData();
		},
		goEdit() {
			this.isdetail = false;
		},
		// 新增 / 修改
		addNew(id) {
			this.addOrUpdateVisible = true;
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(id);
			});
		},
	},
};
</script>
<style scoped>
.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; */
	padding-right: 10px; /* 调整滚动条样式 */
}

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

.drawer-body__footer {
	display: flex;
	justify-content: flex-end;
	padding: 18px;
}
.action_btn {
	float: right;
	margin: -40px 15px;
	font-size: 14px;
}
.add {
	color: #0b58ff;
}
</style>
