| | |
| | | width="30%" |
| | | @close="close" |
| | | > |
| | | <PlanForm ref="planFormRef"></PlanForm> |
| | | <el-form :model="form" :rules="rules" ref="formRef" label-width="110px"> |
| | | <el-form-item label="设备名称" prop="deviceLedgerId"> |
| | | <el-select |
| | | v-model="form.deviceLedgerId" |
| | | filterable allow-create default-first-option |
| | | @change="setDeviceModel" |
| | | placeholder="请选择设备" |
| | | > |
| | | <el-option |
| | | v-for="(item, index) in deviceOptions" |
| | | :key="index" |
| | | :label="item.deviceName" |
| | | :value="item.id" |
| | | ></el-option> |
| | | </el-select> |
| | | </el-form-item> |
| | | <el-form-item label="规格型号"> |
| | | <el-input |
| | | v-model="form.deviceModel" |
| | | placeholder="请输入规格型号" |
| | | disabled |
| | | /> |
| | | </el-form-item> |
| | | <el-form-item label="计划保养日期" prop="maintenancePlanTime"> |
| | | <el-date-picker |
| | | style="width: 100%" |
| | | v-model="form.maintenancePlanTime" |
| | | format="YYYY-MM-DD" |
| | | value-format="YYYY-MM-DD HH:mm:ss" |
| | | type="date" |
| | | placeholder="请选择计划保养日期日期" |
| | | clearable |
| | | /> |
| | | </el-form-item> |
| | | </el-form> |
| | | <template #footer> |
| | | <el-button type="primary" @click="sendForm" :loading="loading"> |
| | | {{ modalOptions.confirmText }} |
| | |
| | | </template> |
| | | |
| | | <script setup> |
| | | import { ref, nextTick, watch } from "vue"; |
| | | import { useModal } from "@/hooks/useModal"; |
| | | import PlanForm from "../Form/PlanForm"; |
| | | import useFormData from "@/hooks/useFormData"; |
| | | import { getDeviceLedger } from "@/api/equipmentManagement/ledger"; |
| | | import dayjs from "dayjs"; |
| | | import { |
| | | addUpkeep, |
| | | editUpkeep, |
| | |
| | | }); |
| | | |
| | | const emits = defineEmits(["ok"]); |
| | | const planFormRef = ref(); |
| | | const { |
| | | id, |
| | | visible, |
| | |
| | | closeModal, |
| | | } = useModal({ title: "设备保养计划" }); |
| | | |
| | | const openEdit = async (id) => { |
| | | const { data } = await getUpkeepById(id); |
| | | openModal(id); |
| | | const deviceOptions = ref([]); |
| | | const formRef = ref(); |
| | | const { form, resetForm } = useFormData({ |
| | | deviceLedgerId: undefined, // 设备Id |
| | | deviceName: undefined, // 设备名称 |
| | | deviceModel: undefined, // 规格型号 |
| | | maintenancePlanTime: undefined, // 计划保养日期 |
| | | }); |
| | | |
| | | const rules = { |
| | | deviceLedgerId: [{ required: true, message: "请选择设备", trigger: "change" }], |
| | | maintenancePlanTime: [ |
| | | { required: true, message: "请选择计划保养日期", trigger: "change" }, |
| | | ], |
| | | }; |
| | | |
| | | const loadDeviceName = async () => { |
| | | const { data } = await getDeviceLedger(); |
| | | deviceOptions.value = data; |
| | | }; |
| | | |
| | | const setDeviceModel = (id) => { |
| | | const option = deviceOptions.value.find((item) => item.id === id); |
| | | form.deviceModel = option ? option.deviceModel : undefined; |
| | | }; |
| | | |
| | | const setForm = (data) => { |
| | | form.deviceLedgerId = data.deviceLedgerId; |
| | | form.deviceName = data.deviceName; |
| | | form.deviceModel = data.deviceModel; |
| | | form.maintenancePlanTime = dayjs(data.maintenancePlanTime).format( |
| | | "YYYY-MM-DD HH:mm:ss" |
| | | ); |
| | | }; |
| | | |
| | | const openEdit = async (editId) => { |
| | | const { data } = await getUpkeepById(editId); |
| | | openModal(editId); |
| | | await nextTick(); |
| | | await planFormRef.value.setForm(data); |
| | | await loadDeviceName(); |
| | | setForm(data); |
| | | }; |
| | | |
| | | const sendForm = async () => { |
| | | loading.value = true; |
| | | const form = await planFormRef.value.getForm(); |
| | | const { code } = id.value |
| | | ? await editUpkeep({ id: unref(id), ...form }) |
| | | : await addUpkeep(form); |
| | | if (code == 200) { |
| | | ElMessage.success(`${id ? "编辑" : "新增"}计划成功`); |
| | | closeModal(); |
| | | emits("ok"); |
| | | } |
| | | loading.value = false; |
| | | await formRef.value.validate(async (valid) => { |
| | | if (!valid) return; |
| | | loading.value = true; |
| | | const payload = { ...form }; |
| | | const { code } = id.value |
| | | ? await editUpkeep({ id: id.value, ...payload }) |
| | | : await addUpkeep(payload); |
| | | if (code == 200) { |
| | | ElMessage.success(`${id.value ? "编辑" : "新增"}计划成功`); |
| | | closeModal(); |
| | | emits("ok"); |
| | | } |
| | | loading.value = false; |
| | | }); |
| | | }; |
| | | |
| | | const close = () => { |
| | | planFormRef.value.resetForm(); |
| | | resetForm(); |
| | | closeModal(); |
| | | }; |
| | | |
| | | // load device options whenever the dialog opens (covers add and edit) |
| | | watch( |
| | | () => visible.value, |
| | | async (val) => { |
| | | if (val) { |
| | | await nextTick(); |
| | | await loadDeviceName(); |
| | | } |
| | | } |
| | | ); |
| | | |
| | | defineExpose({ |
| | | openModal, |
| | | openEdit, |