From 06d627bc28e77e1a314d8e35914512d63dbab1bd Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 20 三月 2026 17:14:48 +0800
Subject: [PATCH] fix: 点击取消时将表单子元素全部销毁

---
 src/views/productionManagement/processRoute/New.vue |  330 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 180 insertions(+), 150 deletions(-)

diff --git a/src/views/productionManagement/processRoute/New.vue b/src/views/productionManagement/processRoute/New.vue
index 856a2a4..981ae89 100644
--- a/src/views/productionManagement/processRoute/New.vue
+++ b/src/views/productionManagement/processRoute/New.vue
@@ -1,65 +1,67 @@
 <template>
   <div>
-    <el-dialog
-        v-model="isShow"
-        title="鏂板宸ヨ壓璺嚎"
-        width="400"
-        @close="closeModal"
-    >
-      <el-form label-width="140px" :model="formState" label-position="top" ref="formRef">
-        <el-form-item
-            label="浜у搧澶х被锛�"
-            prop="productId"
-            :rules="[
+    <el-dialog v-model="isShow"
+               title="鏂板宸ヨ壓璺嚎"
+               width="400"
+               @close="closeModal">
+      <el-form label-width="140px"
+               :model="formState"
+               label-position="top"
+               ref="formRef">
+        <el-form-item label="浜у搧绫诲瀷"
+                      prop="dictCode"
+                      :rules="[
                 {
                 required: true,
-                message: '璇烽�夋嫨浜у搧澶х被',
+                message: '璇烽�夋嫨浜у搧绫诲瀷',
+                trigger: 'change',
               }
-            ]"
-        >
-          <el-tree-select
-              v-model="formState.productId"
-              placeholder="璇烽�夋嫨"
-              clearable
-              check-strictly
-              @change="getModels"
-              :data="productOptions"
-              :render-after-expand="false"
-              style="width: 100%"
-          />
-        </el-form-item>
-
-        <el-form-item
-            label="瑙勬牸鍨嬪彿锛�"
-            prop="productModelId"
-            :rules="[
-                {
-                required: true,
-                message: '璇烽�夋嫨瑙勬牸鍨嬪彿',
-              }
-            ]"
-        >
-          <el-select
-              v-model="formState.productModelId"
-              placeholder="璇烽�夋嫨"
-              clearable
-          >
-            <el-option
-                v-for="item in productModelsOptions"
-                :key="item.id"
-                :label="item.model"
-                :value="item.id"
-            />
+            ]">
+          <el-select v-model="formState.dictCode"
+                     placeholder="璇烽�夋嫨浜у搧绫诲瀷"
+                     clearable
+                     style="width: 100%"
+                     @change="handleProductTypeChange">
+            <el-option v-for="item in productTypeOptions"
+                       :key="item.dictCode"
+                       :label="item.dictLabel"
+                       :value="item.dictCode" />
           </el-select>
         </el-form-item>
-
-        <el-form-item label="澶囨敞" prop="description">
-          <el-input v-model="formState.description" type="textarea" />
+        <el-form-item label="BOM"
+                      prop="bomId"
+                      :rules="[
+                {
+                required: true,
+                message: '璇烽�夋嫨BOM',
+                trigger: 'change',
+              }
+            ]">
+          <el-select v-model="formState.bomId"
+                     placeholder="璇烽�夋嫨BOM"
+                     clearable
+                     :disabled="!formState.dictCode || bomOptions.length === 0"
+                     style="width: 100%">
+            <el-option v-for="item in bomOptions"
+                       :key="item.id"
+                       :label="item.bomNo || `BOM-${item.id}`"
+                       :value="item.id" />
+          </el-select>
+        </el-form-item>
+        <el-form-item label="澶囨敞"
+                      prop="description">
+          <el-input v-model="formState.description"
+                    type="textarea" />
         </el-form-item>
       </el-form>
+      <!-- 浜у搧閫夋嫨寮圭獥 -->
+      <ProductSelectDialog v-model="showProductSelectDialog"
+                           @confirm="handleProductSelect"
+                           single />
       <template #footer>
         <div class="dialog-footer">
-          <el-button type="primary" @click="handleSubmit">纭</el-button>
+          <el-button type="primary"
+                     @click="handleSubmit">纭</el-button>
           <el-button @click="closeModal">鍙栨秷</el-button>
         </div>
       </template>
@@ -68,114 +70,142 @@
 </template>
 
 <script setup>
-import {ref, computed, getCurrentInstance, onMounted} from "vue";
-import {add} from "@/api/productionManagement/processRoute.js";
-import {modelList, productTreeList} from "@/api/basicData/product.js";
+  import { ref, computed, getCurrentInstance, onMounted } from "vue";
+  import { add } from "@/api/productionManagement/processRoute.js";
+  import { listPage } from "@/api/productionManagement/productBom.js";
+  import { getDicts } from "@/api/system/dict/data.js";
 
-const props = defineProps({
-  visible: {
-    type: Boolean,
-    required: true,
-  },
-});
-
-const emit = defineEmits(['update:visible', 'completed']);
-
-// 鍝嶅簲寮忔暟鎹紙鏇夸唬閫夐」寮忕殑 data锛�
-const formState = ref({
-  productId: undefined,
-  productModelId: undefined,
-  description: '',
-});
-
-const isShow = computed({
-  get() {
-    return props.visible;
-  },
-  set(val) {
-    emit('update:visible', val);
-  },
-});
-
-const productModelsOptions = ref([])
-const productOptions = ref([])
-
-let { proxy } = getCurrentInstance()
-
-const closeModal = () => {
-  isShow.value = false;
-};
-
-const getProductOptions = () => {
-  productTreeList().then((res) => {
-    productOptions.value = convertIdToValue(res);
+  const props = defineProps({
+    visible: {
+      type: Boolean,
+      required: true,
+    },
   });
-};
-const getModels = (value) => {
-  formState.value.productId = undefined;
-  formState.value.productModelId = undefined;
-  productModelsOptions.value = [];
 
-  if (value) {
-    formState.value.productId = findNodeById(productOptions.value, value) || undefined;
-    modelList({ id: value }).then((res) => {
-      productModelsOptions.value = res;
-    });
-  }
-};
+  const emit = defineEmits(["update:visible", "completed"]);
 
-const findNodeById = (nodes, productId) => {
-  for (let i = 0; i < nodes.length; i++) {
-    if (nodes[i].value === productId) {
-      return nodes[i].label; // 鎵惧埌鑺傜偣锛岃繑鍥炶鑺傜偣鐨刲abel
-    }
-    if (nodes[i].children && nodes[i].children.length > 0) {
-      const foundNode = findNodeById(nodes[i].children, productId);
-      if (foundNode) {
-        return foundNode; // 鍦ㄥ瓙鑺傜偣涓壘鍒帮紝鐩存帴杩斿洖锛堝凡缁忔槸label瀛楃涓诧級
-      }
-    }
-  }
-  return null; // 娌℃湁鎵惧埌鑺傜偣锛岃繑鍥瀗ull
-};
-
-function convertIdToValue(data) {
-  return data.map((item) => {
-    const { id, children, ...rest } = item;
-    const newItem = {
-      ...rest,
-      value: id, // 灏� id 鏀逛负 value
-    };
-    if (children && children.length > 0) {
-      newItem.children = convertIdToValue(children);
-    }
-
-    return newItem;
+  // 鍝嶅簲寮忔暟鎹紙鏇夸唬閫夐」寮忕殑 data锛�
+  const formState = ref({
+    dictCode: undefined,
+    dictLabel: "",
+    bomId: undefined,
+    description: "",
   });
-}
 
-const handleSubmit = () => {
-  proxy.$refs["formRef"].validate(valid => {
-    if (valid) {
-      add(formState.value).then(res => {
-        // 鍏抽棴妯℃�佹
-        isShow.value = false;
-        // 鍛婄煡鐖剁粍浠跺凡瀹屾垚
-        emit('completed');
-        proxy.$modal.msgSuccess("鎻愪氦鎴愬姛");
+  const isShow = computed({
+    get() {
+      return props.visible;
+    },
+    set(val) {
+      emit("update:visible", val);
+    },
+  });
+
+  const productTypeOptions = ref([]);
+  const bomOptions = ref([]);
+
+  let { proxy } = getCurrentInstance();
+
+  // 鑾峰彇浜у搧绫诲瀷瀛楀吀
+  const getProductTypeOptions = () => {
+    getDicts("product_type")
+      .then(res => {
+        if (res.code === 200) {
+          productTypeOptions.value = res.data;
+        }
       })
+      .catch(err => {
+        console.error("鑾峰彇浜у搧绫诲瀷瀛楀吀澶辫触锛�", err);
+      });
+  };
+
+  // 鏍规嵁浜у搧绫诲瀷鑾峰彇BOM鍒楄〃
+  const getBomListByProductType = async dictCode => {
+    if (!dictCode) {
+      bomOptions.value = [];
+      return;
     }
-  })
-};
+    try {
+      // 浣跨敤listPage鎺ュ彛锛屾牴鎹甦ictCode鏌ヨBOM
+      const res = await listPage({ dictCode });
+      // 澶勭悊杩斿洖鐨凚OM鏁版嵁
+      let bomList = [];
+      if (res.data && res.data.records) {
+        bomList = res.data.records;
+      }
+      bomOptions.value = bomList;
+      if (bomList.length === 0) {
+        proxy.$modal.msgError("璇ヤ骇鍝佺被鍨嬫病鏈塀OM锛岃鍏堝垱寤築OM");
+      }
+    } catch (error) {
+      // 濡傛灉鎺ュ彛杩斿洖404鎴栧叾浠栭敊璇紝璇存槑娌℃湁BOM
+      proxy.$modal.msgError("璇ヤ骇鍝佺被鍨嬫病鏈塀OM锛岃鍏堝垱寤築OM");
+      bomOptions.value = [];
+    }
+  };
 
+  const closeModal = () => {
+    // 閲嶇疆琛ㄥ崟鏁版嵁
+    formState.value = {
+      dictCode: undefined,
+      dictLabel: "",
+      bomId: undefined,
+      description: "",
+    };
+    bomOptions.value = [];
+    isShow.value = false;
+  };
 
-defineExpose({
-  closeModal,
-  handleSubmit,
-  isShow,
-});
+  // 浜у搧绫诲瀷閫夋嫨澶勭悊
+  const handleProductTypeChange = async dictCode => {
+    if (dictCode) {
+      const selectedType = productTypeOptions.value.find(
+        item => item.dictCode === dictCode
+      );
+      if (selectedType) {
+        formState.value.dictLabel = selectedType.dictLabel;
+      }
+      formState.value.bomId = undefined; // 閲嶇疆BOM閫夋嫨
+      await getBomListByProductType(dictCode);
+      // 瑙﹀彂琛ㄥ崟楠岃瘉鏇存柊
+      proxy.$refs["formRef"]?.validateField("dictCode");
+    } else {
+      formState.value.dictLabel = "";
+      bomOptions.value = [];
+    }
+  };
 
-onMounted(() => {
-  getProductOptions()
-})
+  const handleSubmit = () => {
+    proxy.$refs["formRef"].validate(valid => {
+      if (valid) {
+        // 楠岃瘉鏄惁閫夋嫨浜嗕骇鍝佺被鍨嬪拰BOM
+        if (!formState.value.dictCode) {
+          proxy.$modal.msgError("璇烽�夋嫨浜у搧绫诲瀷");
+          return;
+        }
+        if (!formState.value.bomId) {
+          proxy.$modal.msgError("璇烽�夋嫨BOM");
+          return;
+        }
+        add(formState.value).then(res => {
+          // 鍏抽棴妯℃�佹
+          isShow.value = false;
+          // 鍛婄煡鐖剁粍浠跺凡瀹屾垚
+          emit("completed");
+          proxy.$modal.msgSuccess("鎻愪氦鎴愬姛");
+        });
+      }
+    });
+  };
+
+  // 缁勪欢鎸傝浇鏃惰幏鍙栦骇鍝佺被鍨嬪瓧鍏�
+  onMounted(() => {
+    getProductTypeOptions();
+  });
+
+  defineExpose({
+    closeModal,
+    handleSubmit,
+    isShow,
+  });
 </script>

--
Gitblit v1.9.3