1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<template>
  <el-dialog
    v-model="visible"
    :title="modalOptions.title"
    width="30%"
    @close="close"
  >
    <el-form :model="form" :rules="rules" ref="formRef" label-width="110px">
      <el-form-item label="设备名称" prop="deviceLedgerId">
        <el-select
          v-model="form.deviceLedgerId"
          @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 }}
            </el-button>
      <el-button @click="closeModal">{{ modalOptions.cancelText }}</el-button>
    </template>
  </el-dialog>
</template>
 
<script setup>
import { ref, nextTick, watch } from "vue";
import { useModal } from "@/hooks/useModal";
import useFormData from "@/hooks/useFormData";
import { getDeviceLedger } from "@/api/equipmentManagement/ledger";
import dayjs from "dayjs";
import {
  addUpkeep,
  editUpkeep,
  getUpkeepById,
} from "@/api/equipmentManagement/upkeep";
import { ElMessage } from "element-plus";
 
defineOptions({
  name: "设备保养新增计划",
});
 
const emits = defineEmits(["ok"]);
const {
  id,
  visible,
  loading,
  openModal,
  modalOptions,
  handleConfirm,
  closeModal,
} = useModal({ title: "设备保养计划" });
 
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 loadDeviceName();
  setForm(data);
};
 
const sendForm = async () => {
  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 = () => {
  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,
});
</script>
 
<style lang="scss" scoped></style>