ZN
6 天以前 481e405e566642a93394b1e28af023448286d599
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<template>
  <view class="after-sales-edit">
    <PageHeader :title="pageTitle" @back="goBack" />
 
    <view class="form-container">
      <up-form ref="formRef" :model="form" :rules="isReadonly ? {} : rules" label-width="100">
        <up-form-item label="客户名称" prop="customerName" required @click="openCustomerPicker">
          <up-input v-model="form.customerName" readonly placeholder="请选择客户" />
          <template #right>
            <up-icon v-if="!isReadonly" name="arrow-right" @click="openCustomerPicker"></up-icon>
          </template>
        </up-form-item>
 
        <up-form-item label="售后类型" prop="serviceType" required @click="openServiceTypePicker">
          <up-input v-model="serviceTypeLabel" readonly placeholder="请选择售后类型" />
          <template #right>
            <up-icon v-if="!isReadonly" name="arrow-right" @click="openServiceTypePicker"></up-icon>
          </template>
        </up-form-item>
 
        <up-form-item label="关联销售单" prop="salesContractNo" required @click="openSalesOrderPicker">
          <up-input v-model="form.salesContractNo" readonly placeholder="请选择销售单号" />
          <template #right>
            <up-icon v-if="!isReadonly" name="arrow-right" @click="openSalesOrderPicker"></up-icon>
          </template>
        </up-form-item>
 
        <up-form-item label="紧急程度" prop="urgency" required @click="openUrgencyPicker">
          <up-input v-model="urgencyLabel" readonly placeholder="请选择紧急程度" />
          <template #right>
            <up-icon v-if="!isReadonly" name="arrow-right" @click="openUrgencyPicker"></up-icon>
          </template>
        </up-form-item>
 
        <up-form-item label="问题描述" prop="disRes">
          <up-textarea v-model="form.disRes" :disabled="isReadonly" placeholder="请输入问题描述" count autoHeight></up-textarea>
        </up-form-item>
      </up-form>
 
      <!-- 关联产品区域 -->
      <view class="product-section">
        <view class="section-header">
          <up-button type="primary" size="small" @click="showProductSelect = true" v-if="!isReadonly && form.salesContractNo">选择产品</up-button>
        </view>
 
        <view class="product-list" v-if="tableData.length > 0">
          <view v-for="(item, index) in tableData" :key="index" class="product-item">
            <view class="product-info">
              <view class="info-row">
                <text class="info-label">产品分类:</text>
                <text class="info-value">{{ item.productCategory }}</text>
              </view>
              <view class="info-row">
                <text class="info-label">规格型号:</text>
                <text class="info-value">{{ item.specificationModel }}</text>
              </view>
              <view class="info-row">
                <text class="info-label">单位:</text>
                <text class="info-value">{{ item.unit }}</text>
              </view>
              <view class="info-row">
                <text class="info-label">数量:</text>
                <text class="info-value">{{ item.quantity }}</text>
              </view>
            </view>
            <view class="product-action" v-if="!isReadonly">
              <up-icon name="trash" color="#fa3534" size="20" @click="removeProduct(index)"></up-icon>
            </view>
          </view>
        </view>
        <view v-else class="no-product">
          <text>暂无关联产品</text>
        </view>
      </view>
    </view>
 
    <!-- 各种选择器 -->
    <up-action-sheet :show="showCustomerPicker" :actions="customerActions" title="选择客户" @select="onCustomerSelect" @close="showCustomerPicker = false" />
    <up-action-sheet :show="showServiceTypePicker" :actions="serviceTypeActions" title="选择售后类型" @select="onServiceTypeSelect" @close="showServiceTypePicker = false" />
    <up-action-sheet :show="showSalesOrderPicker" :actions="salesOrderActions" title="选择关联销售单" @select="onSalesOrderSelect" @close="showSalesOrderPicker = false" />
    <up-action-sheet :show="showUrgencyPicker" :actions="urgencyActions" title="选择紧急程度" @select="onUrgencySelect" @close="showUrgencyPicker = false" />
 
    <!-- 产品选择弹窗 -->
    <up-popup :show="showProductSelect" mode="bottom" @close="showProductSelect = false" round="10">
      <view class="product-select-popup">
        <view class="popup-header">
          <text class="popup-title">选择产品</text>
          <up-icon name="close" size="20" @click="showProductSelect = false"></up-icon>
        </view>
        <scroll-view scroll-y class="product-scroll">
          <view v-for="(item, index) in availableProducts" :key="index" class="selectable-product" @click="toggleProduct(item)">
            <view class="product-checkbox">
              <up-icon :name="isProductSelected(item) ? 'checkbox-mark' : 'minus-circle'" :color="isProductSelected(item) ? '#2979ff' : '#ccc'" size="20"></up-icon>
            </view>
            <view class="product-details">
              <text class="p-name">{{ item.productCategory }}</text>
              <text class="p-model">{{ item.specificationModel }} | {{ item.unit }}</text>
            </view>
          </view>
        </scroll-view>
        <view class="popup-footer">
          <up-button type="primary" @click="showProductSelect = false">确定</up-button>
        </view>
      </view>
    </up-popup>
 
    <FooterButtons :show="!isReadonly" @cancel="goBack" @confirm="submitForm" />
  </view>
</template>
 
<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { getAllCustomerList, getSalesLedger, afterSalesServiceAdd, afterSalesServiceUpdate } from '@/api/customerService/index';
import PageHeader from '@/components/PageHeader.vue';
import FooterButtons from '@/components/FooterButtons.vue';
import { useDict } from '@/utils/dict';
import useUserStore from '@/store/modules/user';
 
const userStore = useUserStore();
const { post_sale_waiting_list, degree_of_urgency } = useDict('post_sale_waiting_list', 'degree_of_urgency');
 
const operationType = ref('add');
const isReadonly = computed(() => operationType.value === 'view');
const pageTitle = computed(() => {
  if (operationType.value === 'view') return '售后单详情';
  if (operationType.value === 'add') return '新增售后单';
  return '编辑售后单';
});
const formRef = ref(null);
const form = reactive({
  id: null,
  customerName: '',
  customerId: null,
  serviceType: '',
  salesContractNo: '',
  salesLedgerId: null,
  urgency: '',
  disRes: '',
  productModelIds: '',
  checkUserId: null,
  feedbackDate: '',
});
 
const rules = {
  customerName: [{ required: true, message: '请选择客户', trigger: 'change' }],
  serviceType: [{ required: true, message: '请选择售后类型', trigger: 'change' }],
  salesContractNo: [{ required: true, message: '请选择关联销售单', trigger: 'change' }],
  urgency: [{ required: true, message: '请选择紧急程度', trigger: 'change' }],
};
 
const tableData = ref([]);
const customerList = ref([]);
const salesOrderList = ref([]);
const availableProducts = ref([]);
 
const showCustomerPicker = ref(false);
const showServiceTypePicker = ref(false);
const showSalesOrderPicker = ref(false);
const showUrgencyPicker = ref(false);
const showProductSelect = ref(false);
 
const customerActions = computed(() => customerList.value.map(item => ({ name: item.customerName, value: item.id })));
const serviceTypeActions = computed(() => (post_sale_waiting_list.value || []).map(item => ({ name: item.label, value: item.value })));
const salesOrderActions = computed(() => salesOrderList.value.map(item => ({ name: item.salesContractNo, value: item.salesContractNo, id: item.id, products: item.productData })));
const urgencyActions = computed(() => (degree_of_urgency.value || []).map(item => ({ name: item.label, value: item.value })));
 
const serviceTypeLabel = computed(() => {
  const item = (post_sale_waiting_list.value || []).find(i => i.value == form.serviceType);
  return item ? item.label : '';
});
 
const urgencyLabel = computed(() => {
  const item = (degree_of_urgency.value || []).find(i => i.value == form.urgency);
  return item ? item.label : '';
});
 
const goBack = () => {
  uni.navigateBack();
};
 
const openCustomerPicker = () => {
  if (isReadonly.value) return;
  showCustomerPicker.value = true;
};
const openServiceTypePicker = () => {
  if (isReadonly.value) return;
  showServiceTypePicker.value = true;
};
const openSalesOrderPicker = () => {
  if (isReadonly.value) return;
  if (!form.customerName) {
    uni.showToast({ title: '请先选择客户', icon: 'none' });
    return;
  }
  showSalesOrderPicker.value = true;
};
const openUrgencyPicker = () => {
  if (isReadonly.value) return;
  showUrgencyPicker.value = true;
};
 
const onCustomerSelect = (item) => {
  form.customerName = item.name;
  form.customerId = item.value;
  form.salesContractNo = '';
  form.salesLedgerId = null;
  tableData.value = [];
  availableProducts.value = [];
  fetchSalesOrders(item.name);
};
 
const onServiceTypeSelect = (item) => {
  form.serviceType = item.value;
};
 
const onSalesOrderSelect = (item) => {
  form.salesContractNo = item.name;
  form.salesLedgerId = item.id;
  setProductsFromSalesOrder(item, true);
};
 
const onUrgencySelect = (item) => {
  form.urgency = item.value;
};
 
const normalizeProduct = (p) => {
  return {
    ...p,
    id: p.id || p.productModelId || p.modelId,
    productCategory: p.productCategory || p.productName || '',
    specificationModel: p.specificationModel || p.model || '',
  };
};
 
const setProductsFromSalesOrder = (orderAction, selectAll = false) => {
  const products = Array.isArray(orderAction?.products) ? orderAction.products : [];
  const normalizedProducts = products.map(p => normalizeProduct(p));
  availableProducts.value = normalizedProducts;
  if (selectAll) {
    tableData.value = normalizedProducts.slice();
    return;
  }
  const ids = String(form.productModelIds || '')
    .split(',')
    .map(s => s.trim())
    .filter(Boolean);
  if (ids.length === 0) {
    tableData.value = normalizedProducts.slice();
    return;
  }
  const idSet = new Set(ids.map(String));
  tableData.value = normalizedProducts.filter(p => idSet.has(String(p.id)));
};
 
const fetchCustomers = () => {
  getAllCustomerList({ current: 1, size: 1000 }).then(res => {
    const records = res?.records ?? res?.data?.records ?? [];
    customerList.value = Array.isArray(records) ? records : [];
  });
};
 
const fetchSalesOrders = (customerName) => {
  getSalesLedger({ customerName }).then(res => {
    const records = res?.records ?? res?.data?.records ?? [];
    salesOrderList.value = Array.isArray(records) ? records : [];
    if (form.salesContractNo) {
      const match = salesOrderList.value.find(i => String(i.salesContractNo) === String(form.salesContractNo));
      if (match) {
        setProductsFromSalesOrder({ id: match.id, name: match.salesContractNo, products: match.productData }, false);
        form.salesLedgerId = match.id;
      }
    }
  });
};
 
const removeProduct = (index) => {
  tableData.value.splice(index, 1);
};
 
const isProductSelected = (product) => {
  const id = product.id || product.productModelId || product.modelId;
  return tableData.value.some(p => (p.id || p.productModelId || p.modelId) === id);
};
 
const toggleProduct = (product) => {
  if (isReadonly.value) return;
  const id = product.id || product.productModelId || product.modelId;
  const index = tableData.value.findIndex(p => (p.id || p.productModelId || p.modelId) === id);
  if (index > -1) {
    tableData.value.splice(index, 1);
  } else {
    tableData.value.push(normalizeProduct(product));
  }
};
 
const submitForm = () => {
  if (isReadonly.value) return;
  formRef.value.validate().then(valid => {
    if (valid) {
      form.productModelIds = tableData.value.map(p => p.id || p.productModelId || p.modelId).join(',');
      if (!form.checkUserId) {
        form.checkUserId = userStore.id;
      }
      if (!form.feedbackDate) {
        const now = new Date();
        form.feedbackDate = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
      }
      const api = operationType.value === 'add' ? afterSalesServiceAdd : afterSalesServiceUpdate;
      
      api(form).then((res) => {
        if (res?.code === 200 || res?.code === undefined) {
          uni.showToast({ title: operationType.value === 'add' ? '新增成功' : '修改成功', icon: 'success' });
          setTimeout(() => goBack(), 500);
          return;
        }
        uni.showToast({ title: res?.msg || '操作失败', icon: 'none' });
      }).catch(() => {});
    }
  });
};
 
onMounted(() => {
  operationType.value = uni.getStorageSync('afterSalesOperationType') || 'add';
  const editDataStr = uni.getStorageSync('afterSalesEditData');
  
  fetchCustomers();
  
  if (editDataStr) {
    const editData = JSON.parse(editDataStr);
    Object.assign(form, editData);
    if (form.customerName) {
      fetchSalesOrders(form.customerName);
    }
  } else {
    form.checkUserId = userStore.id;
    const now = new Date();
    form.feedbackDate = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
  }
});
</script>
 
<style scoped lang="scss">
.after-sales-edit {
  min-height: 100vh;
  background: #f8f9fa;
  padding-bottom: 100px;
}
 
.form-container {
  background: #ffffff;
  padding: 10px 20px;
  margin-top: 10px;
}
 
.product-section {
  margin-top: 20px;
  border-top: 1px solid #f0f0f0;
  padding-top: 20px;
}
 
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}
 
.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}
 
.product-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
 
.product-item {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
 
.product-info {
  flex: 1;
}
 
.info-row {
  display: flex;
  margin-bottom: 4px;
  font-size: 13px;
}
 
.info-label {
  color: #909399;
  width: 70px;
}
 
.info-value {
  color: #303133;
  flex: 1;
}
 
.no-product {
  text-align: center;
  padding: 30px 0;
  color: #999;
  font-size: 14px;
}
 
.product-select-popup {
  height: 60vh;
  display: flex;
  flex-direction: column;
  background: #fff;
}
 
.popup-header {
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
}
 
.popup-title {
  font-size: 16px;
  font-weight: 600;
}
 
.product-scroll {
  flex: 1;
  padding: 10px 20px;
}
 
.selectable-product {
  display: flex;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #f5f5f5;
}
 
.product-checkbox {
  margin-right: 15px;
}
 
.product-details {
  display: flex;
  flex-direction: column;
}
 
.p-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
}
 
.p-model {
  font-size: 12px;
  color: #999;
}
 
.popup-footer {
  padding: 20px;
  border-top: 1px solid #f0f0f0;
}
</style>