<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: DY
 * @LastEditTime: 2023-11-24 18:38:22
 * @Description:
-->
<template>
	<el-drawer
		:visible.sync="visible"
		:show-close="false"
		:wrapper-closable="false"
		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-width="100px"
					label-position="top">
					<el-row :gutter="20">
						<el-col :span="12">
							<el-form-item label="设备名称" prop="equipmentName">
								<el-input v-model="dataForm.equipmentName" disabled clearable placeholder="请输入设备名称" />
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="设备编码" prop="equipmentCode">
								<el-input
									v-model="dataForm.equipmentCode"
									clearable
									disabled
									placeholder="请输入设备编码" />
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>

			<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="listQuery.total > 0"
					:total="listQuery.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"
			:config-id="dataForm.id"
			@refreshDataList="getList" />
	</el-drawer>
</template>

<script>
import { getEqCheck, getCheckDetPage, deleteCheckDet } from "@/api/equipment/base/inspection/settings";
import SmallTitle from './SmallTitle';
import attrAdd from './attr-add';
import {DICT_TYPE, getDictDatas} from "@/utils/dict";

const tableBtn = [
	{
		type: 'edit',
		btnName: '编辑',
	},
	{
		type: 'delete',
		btnName: '删除',
	},
];
const tableProps = [
	{
		prop: 'program',
		label: '巡检项目',
	},
	{
		prop: 'content',
		label: '巡检内容',
	},
	{
		prop: 'code',
		label: '巡检内容编码',
	},
	{
		prop: 'description',
		label: '备注',
	},
];

export default {
	components: { SmallTitle, attrAdd },
	data() {
		return {
			tableBtn,
			tableProps,
			addOrUpdateVisible: false,
			urlOptions: {
				infoURL: getEqCheck,
			},
			listQuery: {
				pageSize: 10,
				pageNo: 1,
				total: 0,
			},
			dataForm: {
        id: undefined,
        code: undefined,
        name: '',
				materialType: undefined,
				productType: undefined,
        area: undefined,
				specifications: undefined,
				processTime: 0,
        remark: undefined,
				unit: 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.listQuery.total = 0;
		},
		handleClick(raw) {
			if (raw.type === 'delete') {
				this.$confirm(
					`确定对${
						raw.data.attrName
							? '[名称=' + raw.data.attrName + ']'
							: '[序号=' + raw.data._pageIndex + ']'
					}进行删除操作?`,
					'提示',
					{
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					}
				)
					.then(() => {
						deleteCheckDet(raw.data.id).then(({ data }) => {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.getList();
								},
							});
						});
					})
					.catch(() => {});
			} else {
				this.addNew(raw.data.id);
			}
		},
		getList() {
			// 获取巡检项目分页
			getCheckDetPage({
				...this.listQuery,
				configId: this.dataForm.id,
			}).then((response) => {
				this.checkDetList = response.data.list;
				this.listQuery.total = response.data.total;
			});
		},
		init(id, isdetail) {
			this.initData();
			this.isdetail = isdetail || false;
			this.dataForm.id = id || undefined;
			this.visible = true;

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

				if (this.dataForm.id) {
					// 获取设备巡检详情
					this.urlOptions.infoURL(id).then(response => {
            this.dataForm = response.data
          });
					// 获取设备项目列表
					this.getList();
				} else {
					if (this.urlOptions.isGetCode) {
            this.getCode()
          }
				}
			});
		},
		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;
	max-height: 16vh;
	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;
}
.action_btn {
  float: right;
  margin: -40px 15px;
  font-size: 14px;
}
.add {
	color: #0b58ff;
}
</style>
