<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: DY
 * @LastEditTime: 2024-02-28 19:52:13
 * @Description:
-->
<template>
	<!-- <el-drawer
		:visible.sync="visible"
		:show-close="false"
		:wrapper-closable="isdetail"
		class="drawer"
		size="60%">
		<small-title slot="title" :no-padding="true">
			{{ isdetail ? '详情' : !dataForm.id ? '新增' : '编辑' }}
		</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="name">
							<el-input v-model="dataForm.name" :disabled="isdetail" clearable placeholder="请输入保养计划名称" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="保养计划编码" prop="code">
							<el-input
								v-model="dataForm.code"
								clearable
								:disabled="isdetail"
								placeholder="请输入保养计划编码" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="部门" prop="departmentId">
							<treeselect v-model="dataForm.departmentId" :options="menuOptions" :normalizer="normalizer" :show-count="true" placeholder="选择部门"/>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="产线名" prop="lineId">
							<el-select
								v-model="dataForm.lineId"
								filterable
								placeholder="请选择产线"
								style="width: 100%">
								<el-option
									v-for="dict in proLineList"
									:key="dict.id"
									:label="dict.name"
									:value="dict.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="保养频率(天/次)" prop="maintenancePeriod">
							<el-input-number v-model="dataForm.maintenancePeriod" :min="0" controls-position="right" style="width: 100%" :disabled="isdetail" clearable placeholder="请输入保养频率" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="确认时限(h)" prop="confirmTimeLimit">
							<el-input-number v-model="dataForm.confirmTimeLimit" :min="0" controls-position="right" style="width: 100%" :disabled="isdetail" clearable placeholder="请输入单位平方数" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20">
					<el-col :span="8">
						<el-form-item label="保养时长(h)" prop="maintainDuration">
							<el-input-number v-model="dataForm.maintainDuration" :min="0" controls-position="right" style="width: 100%" :disabled="isdetail" clearable placeholder="请输入保养时长" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="首次保养时间" prop="firstMaintenanceTime">
							<el-date-picker
								v-model="dataForm.firstMaintenanceTime"
								type="date"
								format='yyyy-MM-dd'
								:disabled="isedit"
								value-format='timestamp'
								placeholder="选择首次保养时间"
								style="width: 100%" />
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="计划保养人员" prop="maintainer">
							<el-select v-model="dataForm.maintainer" placeholder="请选择计划保养人员" style="width: 100%;" :disabled="isdetail" multiple filterable>
								<el-option
									v-for="item in personList"
									:key="item.id"
									:label="item.name"
									:value="item.name">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="dataForm.remark" :disabled="isdetail" clearable placeholder="请输入备注" />
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
import basicAdd from '../../../../core/mixins/basic-add';
// import { getCoreProductAttrPage, deleteCoreProductAttr } from "@/api/base/coreProduct";
import { getWorkerList } from '@/api/base/worker'
import { getCoreDepartmentList } from "@/api/base/coreDepartment";
import { createPlan, updatePlan, getCode, getPlan } from '@/api/equipment/base/maintain/planconfig';
import { getCorePLList } from '@/api/base/coreProductionLine';
import { parseTime } from '../../../../core/mixins/code-filter';
import attrAdd from './attr-add';
// import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
	mixins: [basicAdd],
	components: { attrAdd, Treeselect },
	data() {
		return {
			addOrUpdateVisible: false,
			urlOptions: {
			  isGetCode: true,
        codeURL: getCode,
				createURL: createPlan,
				updateURL: updatePlan,
				infoURL: getPlan
			},
			dataForm: {
        id: undefined,
        code: undefined,
        name: '',
				departmentId: undefined,
				lineId: undefined,
        maintenancePeriod: undefined,
				confirmTimeLimit: undefined,
				maintainDuration: undefined,
        remark: undefined,
				firstMaintenanceTime: undefined,
				maintainer: undefined
			},
			menuOptions: [],
			proLineList: [],
			personList: [],
			visible: false,
			isdetail: false,
			isedit: false,
			dataRule: {
        code: [{ required: true, message: "保养计划编码不能为空", trigger: "blur" }],
        name: [{ required: true, message: "保养计划名称不能为空", trigger: "blur" }],
				departmentId: [{ required: true, message: "部门不能为空", trigger: "change" }],
				lineId: [{ required: true, message: "产线不能为空", trigger: "change" }],
				maintenancePeriod: [{ required: true, message: "保养频率不能为空", trigger: "blur" }],
				confirmTimeLimit: [{ required: true, message: "确认时限不能为空", trigger: "blur" }],
				maintainDuration: [{ required: true, message: "保养时长不能为空", trigger: "blur" }],
				firstMaintenanceTime: [{ required: true, message: "首次保养时间不能为空", trigger: "blur" }],
				maintainer: [{ required: true, message: "计划保养人员不能为空", trigger: "blur" }]
      }
		};
	},
	mounted() {
		this.getDict()
	},
	methods: {
		async getDict() {
			// 部门列表
			this.menuOptions = []
			const res = await getCoreDepartmentList();
			this.departmentlList = res.data.map(item => {
				item.parentId = item.parentId ? item.parentId : 0
				return item
			});
			// const menu = { id: 0, name: '总部门', children: [] };
			// menu.children = this.handleTree(this.departmentlList, "id")
			this.menuOptions = this.handleTree(this.departmentlList, "id")
			// 产线列表
			const resline = await getCorePLList();
			this.proLineList = resline.data;
			// 保养人员
			const personres = await getWorkerList()
      this.personList = personres.data || []
		},
		getList() {
			// 获取产品属性列表
			// getCoreProductAttrPage({
			// 	...this.listQuery,
			// 	productId: this.dataForm.id,
			// }).then((response) => {
			// 	this.productAttrList = response.data.list;
			// 	this.listQuery.total = response.data.total;
			// });
		},
		init(id, isdetail) {
			// this.initData();
			this.isdetail = isdetail || false;
			this.dataForm.id = id || undefined;
			this.isedit = id ? true : false;
			this.visible = true;
			// if (id) {
			// 	this.idAttrShow = true
			// } else {
			// 	this.idAttrShow = false
			// }

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

				if (this.dataForm.id) {
					// 获取计划详情
					this.urlOptions.infoURL({ id: this.dataForm.id }).then(response => {
            this.dataForm = response.data
						if (response.data?.maintainer) {
							this.dataForm.maintainer = response.data?.maintainer.split(',') || undefined
						} else {
							this.$set(this.dataForm, 'maintainer', undefined)
							this.dataForm.maintainer = undefined
						}
          });
					// 获取产品属性列表
					// 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);
			});
		},
		/** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
		// 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        // 修改的提交
        if (this.dataForm.id) {
          this.urlOptions.updateURL({
						...this.dataForm,
						maintainer: this.dataForm.maintainer.join(',')
					}).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.visible = false; 
            this.$emit("refreshDataList");
          });
          return;
        }
        // 添加的提交
        this.urlOptions.createURL({
					...this.dataForm,
					maintainer: this.dataForm.maintainer.join(',')
				}).then(response => {
          this.$modal.msgSuccess("新增成功");
					// this.idAttrShow = true
					this.dataForm.id = response.data
          this.visible = false;
          this.$emit("refreshDataList");
        });
      });
    }
	}
};
</script>
<style scoped>
</style>
