From 38bf4054f02933390d2bbdd44b5eaa91c83de1f3 Mon Sep 17 00:00:00 2001
From: zhangwencui <1064582902@qq.com>
Date: 星期一, 02 三月 2026 17:34:27 +0800
Subject: [PATCH] 设备保养的设备备件修改

---
 src/pages/equipmentManagement/upkeep/maintain.vue |  156 ++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 128 insertions(+), 28 deletions(-)

diff --git a/src/pages/equipmentManagement/upkeep/maintain.vue b/src/pages/equipmentManagement/upkeep/maintain.vue
index 9edeff4..e6be078 100644
--- a/src/pages/equipmentManagement/upkeep/maintain.vue
+++ b/src/pages/equipmentManagement/upkeep/maintain.vue
@@ -190,9 +190,6 @@
       <view class="spare-part-popup">
         <view class="popup-header">
           <text class="popup-title">閫夋嫨璁惧澶囦欢</text>
-          <up-button type="primary"
-                     size="small"
-                     @click="confirmSparePartSelection">纭畾</up-button>
         </view>
         <view class="spare-part-options">
           <view v-for="(item, index) in sparePartOptions"
@@ -206,6 +203,10 @@
                     color="#2c7be5" />
           </view>
         </view>
+        <up-button type="primary"
+                   size="small"
+                   :customStyle="{ borderRadius: '6px', padding: '4px 12px' }"
+                   @click="confirmSparePartSelection">纭畾</up-button>
       </view>
     </up-popup>
   </view>
@@ -338,6 +339,7 @@
 
   // 鎻愪氦琛ㄥ崟
   const sendForm = async () => {
+    console.log(form.value.sparePartsIds, "form.value.sparePartsIds");
     try {
       // 鎵嬪姩楠岃瘉琛ㄥ崟
       let isValid = true;
@@ -383,7 +385,9 @@
       const submitData = {
         ...form.value,
         imagesFile: form.value.status == "1" ? uploadFiles.value : [],
-        sparePartsIds: form.sparePartsIds ? form.sparePartsIds.join(",") : "",
+        sparePartsIds: form.value.sparePartsIds
+          ? form.value.sparePartsIds.join(",")
+          : "",
       };
       const { code } = await addMaintenance({ id: id, ...submitData });
 
@@ -416,6 +420,7 @@
     return uni.getStorageSync("repairId");
   };
 
+  const dataform = ref({});
   // 鑾峰彇璁惧淇℃伅
   const getUpkeepItemData = () => {
     try {
@@ -423,6 +428,8 @@
       if (!dataStr) {
         return null;
       }
+      dataform.value = JSON.parse(dataStr);
+      fetchSparePartOptions(dataform.value.deviceLedgerId);
       return JSON.parse(dataStr);
     } catch (e) {
       console.error("瑙f瀽璁惧鏁版嵁澶辫触:", e);
@@ -463,14 +470,17 @@
   };
 
   // 妫�鏌ュ浠舵槸鍚﹀凡閫変腑
-  const isSparePartSelected = value => {
-    return tempSelectedSpareParts.value.some(item => item.value === value);
+  const isSparePartSelected = id => {
+    return tempSelectedSpareParts.value.some(
+      item => item.id === id || item.value === id
+    );
   };
 
   // 鍒囨崲澶囦欢閫変腑鐘舵��
   const toggleSparePartSelection = item => {
+    const itemId = item.id || item.value;
     const index = tempSelectedSpareParts.value.findIndex(
-      selected => selected.value === item.value
+      selected => selected.id === itemId || selected.value === itemId
     );
     if (index > -1) {
       tempSelectedSpareParts.value.splice(index, 1);
@@ -482,16 +492,16 @@
   // 纭澶囦欢閫夋嫨
   const confirmSparePartSelection = () => {
     selectedSpareParts.value = [...tempSelectedSpareParts.value];
-    form.value.sparePartsIds = selectedSpareParts.value.map(item => item.value);
+    form.value.sparePartsIds = selectedSpareParts.value.map(item => item.id);
     showSparePart.value = false;
   };
 
   // 绉婚櫎宸查�夊浠�
   const removeSparePart = index => {
     selectedSpareParts.value.splice(index, 1);
-    form.value.sparePartsIds = selectedSpareParts.value.map(item => item.value);
+    form.value.sparePartsIds = selectedSpareParts.value.map(item => item.id);
   };
-
+  const sparePartsIds = ref([]);
   // 鍒濆鍖栬〃鍗曟暟鎹�
   const initForm = () => {
     // 鑾峰彇璁惧淇℃伅
@@ -545,18 +555,10 @@
         maintenancestatusText.value = statusMap[itemData.status] || "";
       }
       // 濉厖澶囦欢鏁版嵁
-      if (itemData.spareParts && itemData.spareParts.length > 0) {
-        selectedSpareParts.value = itemData.spareParts.map(sparePart => ({
-          id: sparePart.id || sparePart.sparePartId || sparePart.value,
-          name: sparePart.name || sparePart.sparePartName,
-          code: sparePart.code || sparePart.sparePartCode,
-          value: sparePart.id || sparePart.sparePartId || sparePart.value,
-        }));
-        // 璁剧疆澶囦欢IDs
-        form.value.sparePartsIds = selectedSpareParts.value
-          .map(item => item.value)
-          .join(",");
-      }
+
+      // 澶勭悊瀛楃涓叉牸寮忕殑澶囦欢IDs
+      sparePartsIds.value = itemData.sparePartsIds;
+
       // 濉厖闄勪欢鏁版嵁
       if (itemData.files && itemData.files.length > 0) {
         uploadFiles.value = itemData.files.map(file => ({
@@ -582,17 +584,52 @@
     }
   };
 
-  onShow(() => {
+  onShow(async () => {
+    // 鍏堣幏鍙栧浠堕�夐」锛屽啀鍒濆鍖栬〃鍗�
+    const pageId = getPageId();
+    if (pageId) {
+      await fetchSparePartOptions(pageId);
+    }
     // 椤甸潰鏄剧ず鏃跺垵濮嬪寲琛ㄥ崟
     initForm();
-    fetchSparePartOptions(getPageId());
   });
   const sparePartOptions = ref([]);
   const fetchSparePartOptions = deviceLedgerId => {
-    getSparePartsOptions({ deviceLedgerId: deviceLedgerId }).then(res => {
-      if (res.code == 200) {
-        sparePartOptions.value = res.data || [];
-      }
+    return new Promise((resolve, reject) => {
+      getSparePartsOptions({ deviceLedgerId: deviceLedgerId })
+        .then(res => {
+          if (res.code == 200) {
+            sparePartOptions.value = res.data || [];
+            const idArray =
+              typeof sparePartsIds.value === "string"
+                ? sparePartsIds.value.split(",")
+                : sparePartsIds.value;
+
+            if (idArray.length > 0) {
+              selectedSpareParts.value = sparePartOptions.value
+                .filter(
+                  option =>
+                    idArray.includes(option.id.toString()) ||
+                    idArray.includes(option.value?.toString())
+                )
+                .map(option => ({
+                  id: option.id || option.value,
+                  name: option.name,
+                  code: option.code,
+                  value: option.id || option.value,
+                }));
+              // 璁剧疆澶囦欢IDs
+              form.value.sparePartsIds = idArray.join(",");
+            }
+            resolve(res.data);
+          } else {
+            resolve([]);
+          }
+        })
+        .catch(err => {
+          console.error("鑾峰彇澶囦欢閫夐」澶辫触:", err);
+          resolve([]);
+        });
     });
   };
 
@@ -1069,6 +1106,69 @@
     font-weight: 500;
   }
 
+  /* 澶囦欢閫夋嫨寮圭獥鏍峰紡 */
+  .spare-part-popup {
+    width: 100%;
+    max-height: 80vh;
+    background: #fff;
+    border-radius: 16px 16px 0 0;
+    overflow: hidden;
+  }
+
+  .popup-header {
+    display: flex;
+    justify-content: space-between;
+    align-items: center;
+    padding: 16px 20px;
+    border-bottom: 1px solid #f0f0f0;
+    background: #f8f9fa;
+  }
+
+  .popup-title {
+    font-size: 16px;
+    font-weight: 600;
+    color: #333;
+  }
+
+  .spare-part-options {
+    padding: 10px 0;
+    max-height: 60vh;
+    overflow-y: auto;
+  }
+
+  .spare-part-option {
+    position: relative;
+    display: flex;
+    justify-content: space-between;
+    align-items: center;
+    padding: 14px 20px;
+    border-bottom: 1px solid #f0f0f0;
+    transition: all 0.2s ease;
+  }
+
+  .spare-part-option:last-child {
+    border-bottom: none;
+  }
+
+  .spare-part-option:hover {
+    background: #f8f9fa;
+  }
+
+  .spare-part-option.selected {
+    background: #e6f7ff;
+    color: #1890ff;
+  }
+
+  .spare-part-option.selected::before {
+    content: "";
+    position: absolute;
+    left: 0;
+    top: 0;
+    bottom: 0;
+    width: 4px;
+    background: #1890ff;
+  }
+
   /* 鏂囦欢涓婁紶鏍峰紡 */
   .simple-upload-area {
     width: 100%;

--
Gitblit v1.9.3