chenhaozhe
8 天以前 cfd0b53967daca92d98cd304e4d4e0577e2efbd3
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
<template>
  <div v-loading="formLoading" v-if="formShow">
    <div style="margin-bottom: 10px; border-bottom: 1px solid #f6f6f6">
      <el-button type="primary" @click="submitForm" :disabled="subDisabled"
        >保 存</el-button
      >
      <el-button 
        type="primary" 
        @click="setCheckBill" 
        :disabled="!isAuditable || subDisabled"
      >审 核</el-button>
      <el-button type="primary" @click="close">退 出</el-button>
    </div>
    
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row>
            <el-col :span="8">
              <el-form-item label="代码:" prop="HNumber">
                <el-input 
                  v-model="form.HNumber" 
                  placeholder="请输入计量单位代码"
                  :disabled="isEditMode && form.HItemID > 0"
                />
                <el-input v-model="form.HItemID" type="hidden" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="名称:" prop="HName">
                <el-input 
                  v-model="form.HName" 
                  placeholder="请输入计量单位名称" 
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="助记码:">
                <el-input 
                  v-model="form.HHelpCode" 
                  placeholder="请输入助记码" 
                />
              </el-form-item>
            </el-col>
          </el-row>
          
          <el-row>
            <el-col :span="8">
              <el-form-item label="换算比例:" prop="HRate">
                <el-input 
                  v-model="form.HRate" 
                  type="number"
                  placeholder="请输入换算比例" 
                  min="0"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="默认单位:">
                <el-checkbox v-model="form.HStandard">设为默认单位</el-checkbox>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="禁用标志:">
                <el-checkbox v-model="form.HStopflag" disabled>禁用</el-checkbox>
              </el-form-item>
            </el-col>
          </el-row>
          
          <el-row>
            <el-col :span="8">
                <el-form-item label="计量单位组:">
                <div style="white-space: nowrap;">
                    <el-input 
                    v-model="form.HUnitGroupName" 
                    placeholder="请选择计量单位组"
                    readonly
                    style="width: 200px; display: inline-block;"
                    />
                    <el-input v-model="form.HUnitGroupID" type="hidden" />
                    <el-button 
                    type="primary" 
                    icon="el-icon-search"
                    @click="selectUnitGroup"
                    style="width: 40px; margin-left: -30px; display: inline-block;"
                    ></el-button>
                </div>
                </el-form-item>
            </el-col>
            </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注:">
                <el-input 
                  v-model="form.HRemark" 
                  type="textarea"
                  placeholder="请输入备注" 
                  :rows="3"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        
        <el-tab-pane label="制单信息" name="second">
          <el-row>
            <el-col :span="8">
              <el-form-item label="制单人:">
                <el-input 
                  v-model="form.HMakeEmp" 
                  readonly
                  :class="{ 'readonly-input': true }"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="制单日期:">
                <el-input 
                  v-model="form.HMakeDate" 
                  readonly
                  :class="{ 'readonly-input': true }"
                />
              </el-form-item>
            </el-col>
          </el-row>
          
          <el-row>
            <el-col :span="8">
              <el-form-item label="审核人:">
                <el-input 
                  v-model="form.HCheckEmp" 
                  readonly
                  :class="{ 'readonly-input': true }"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审核日期:">
                <el-input 
                  v-model="form.HCheckDate" 
                  readonly
                  :class="{ 'readonly-input': true }"
                />
              </el-form-item>
            </el-col>
          </el-row>
          
          <el-row>
            <el-col :span="8">
              <el-form-item label="禁用人:">
                <el-input 
                  v-model="form.HStopEmp" 
                  readonly
                  :class="{ 'readonly-input': true }"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="禁用日期:">
                <el-input 
                  v-model="form.HStopDate" 
                  readonly
                  :class="{ 'readonly-input': true }"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    
    <!-- 计量单位组选择弹窗 -->
    <el-dialog
      title="计量单位组列表"
      :visible.sync="unitGroupDialogVisible"
      width="90%"
      top="5vh"
      :close-on-click-modal="false"
      append-to-body
    >
      <div style="height: 70vh;">
        <iframe 
          :src="unitGroupIframeUrl" 
          frameborder="0" 
          width="100%" 
          height="100%"
        ></iframe>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="unitGroupDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmUnitGroup">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
import axios from "axios";
import dayjs from "dayjs";
 
export default {
  name: "GyUnitEdit",
  components: {},
  props: {
    OperationType: { type: Number }, // 1:新增, 3:编辑, 2:复制
    linterid: { type: Number }, // 编辑时的ID
    HOrgID: { type: Number }, // 组织ID
  },
  data() {
    return {
      formShow: false,
      formLoading: true,
      subDisabled: false,
      isEditMode: false,
      isAuditable: false,
      activeName: "first",
      isIframe: false,
      
      // 表单参数
      form: {
        HItemID: 0,
        HNumber: "",
        HName: "",
        HHelpCode: "",
        HRate: "1",
        HStandard: false,
        HStopflag: false,
        HRemark: "",
        HUnitGroupID: "0",
        HUnitGroupName: "",
        HMakeEmp: "",
        HMakeDate: "",
        HCheckEmp: "",
        HCheckDate: "",
        HStopEmp: "",
        HStopDate: "",
        HUserName: "",
        OrganizationID: ""
      },
      
      // 计量单位组选择
      unitGroupDialogVisible: false,
      unitGroupIframeUrl: "",
      selectedUnitGroup: null,
      
      // 表单校验
      rules: {
        HNumber: [
          { required: true, message: "计量单位代码不能为空", trigger: "blur" },
          { max: 50, message: "长度不能超过50个字符", trigger: "blur" }
        ],
        HName: [
          { required: true, message: "计量单位名称不能为空", trigger: "blur" },
          { max: 100, message: "长度不能超过100个字符", trigger: "blur" }
        ],
        HRate: [
          { required: true, message: "换算比例不能为空", trigger: "blur" },
          { 
            validator: (rule, value, callback) => {
              if (value === '' || value === null) {
                callback(new Error('换算比例不能为空'));
              } else if (isNaN(value) || parseFloat(value) <= 0) {
                callback(new Error('请输入大于0的数字'));
              } else {
                callback();
              }
            },
            trigger: 'blur'
          }
        ]
      },
      
      baseURL: process.env.VUE_APP_BASE_API || "http://47.96.97.237/API/",
      user: "admin",
      organizationID: ""
    };
  },
  created() {
    this.reset();
    this.fetchData();
    this.checkIframe();
  },
  mounted() {
    this.user = sessionStorage["HUserName"] || "admin";
    this.organizationID = sessionStorage["OrganizationID"] || "";
  },
  methods: {
    // 检查是否在iframe中
    checkIframe() {
      this.isIframe = window.self !== window.top;
    },
    
    fetchData() {
      // 从URL获取参数
      const urlParams = this.getUrlParams();
      const hID = urlParams.HInterID ? parseInt(urlParams.HInterID) : 0;
      const opType = urlParams.OperationType ? parseInt(urlParams.OperationType) : 1;
      
      // 如果props没有传递,使用URL参数
      const operationType = this.OperationType || opType;
      const linterid = this.linterid || hID;
      
      if (operationType !== 1 && linterid) {
        // 编辑或复制模式
        this.getDetailData(linterid, operationType);
      } else {
        // 新增模式
        this.formShow = true;
        this.formLoading = false;
        this.form.HItemID = 0;
        this.isEditMode = false;
        this.isAuditable = false;
        this.setDefaultValues();
      }
    },
    
    // 从URL获取参数
    getUrlParams() {
      const params = {};
      const queryString = window.location.search.substring(1);
      const pairs = queryString.split('&');
      
      for (let i = 0; i < pairs.length; i++) {
        const pair = pairs[i].split('=');
        if (pair.length === 2) {
          params[pair[0]] = decodeURIComponent(pair[1]);
        }
      }
      
      return params;
    },
    
    getDetailData(hID, operationType) {
      this.formLoading = true;
      axios
        .get(this.baseURL + "GetGy_UnitDetail", {
          params: {
            HID: hID,
          },
        })
        .then((response) => {
          let result = response.data;
          if (result.code == 1) {
            var data = result.data.h_v_Edit_Gy_UnitList[0];
            console.log("返回的数据:", data);
            
            // 处理布尔值
            const isStopped = data.禁用标记 === "true";
            const isStandard = data.默认标记 === "true";
            
            this.form = {
              HItemID: operationType == 2 ? 0 : data.HItemID, // 复制时ID设为0
              HNumber: data.计量单位代码,
              HName: data.计量单位名称,
              HHelpCode: data.助记代码,
              HRate: data.换算比例 || "1",
              HStandard: isStandard,
              HStopflag: isStopped,
              HRemark: data.备注 || "",
              HUnitGroupID: data.HUnitGroupID || "0",
              HUnitGroupName: data.计量单位组名称 || "",
              HMakeEmp: data["创建人"] || "",
              HMakeDate: data["创建日期"] ? dayjs(data["创建日期"]).format("YYYY-MM-DD") : "",
              HCheckEmp: data["审核人"] || "",
              HCheckDate: data["审核日期"] ? dayjs(data["审核日期"]).format("YYYY-MM-DD") : "",
              HStopEmp: data["禁用人"] || "",
              HStopDate: data["禁用日期"] ? dayjs(data["禁用日期"]).format("YYYY-MM-DD") : "",
              HUserName: this.user,
              OrganizationID: this.organizationID
            };
            
            this.isEditMode = operationType === 3;
            this.isAuditable = !data["审核人"] && operationType === 3;
            
            // 如果是已审核单据,禁用保存按钮
            if (data["审核人"]) {
              this.subDisabled = true;
            }
          } else {
            this.$modal.msgError(result.msg || "获取数据失败");
          }
        })
        .catch((error) => {
          this.$modal.msgError("接口请求失败!");
        })
        .finally(() => {
          this.formShow = true;
          this.formLoading = false;
        });
    },
    
    // 设置默认值
    setDefaultValues() {
      this.form.HMakeEmp = this.user;
      this.form.HMakeDate = dayjs().format("YYYY-MM-DD");
      this.form.OrganizationID = this.organizationID;
    },
    
    // 重置表单
    reset() {
      this.form = {
        HItemID: 0,
        HNumber: "",
        HName: "",
        HHelpCode: "",
        HRate: "1",
        HStandard: false,
        HStopflag: false,
        HRemark: "",
        HUnitGroupID: "0",
        HUnitGroupName: "",
        HMakeEmp: "",
        HMakeDate: "",
        HCheckEmp: "",
        HCheckDate: "",
        HStopEmp: "",
        HStopDate: "",
        HUserName: this.user,
        OrganizationID: this.organizationID
      };
      this.subDisabled = false;
    },
    
    // 选择计量单位组
    selectUnitGroup() {
      this.unitGroupIframeUrl = `/iframe/GyUnitGroup?type=select`;
      this.unitGroupDialogVisible = true;
      
      // 监听来自iframe的消息
      window.addEventListener('message', this.handleUnitGroupMessage);
    },
    
    // 处理计量单位组选择消息
    handleUnitGroupMessage(event) {
      if (event.data && event.data.action === 'selectUnitGroup') {
        this.selectedUnitGroup = event.data.data;
      }
    },
    
    // 确认选择计量单位组
    confirmUnitGroup() {
      if (this.selectedUnitGroup) {
        this.form.HUnitGroupID = this.selectedUnitGroup.HItemID;
        this.form.HUnitGroupName = this.selectedUnitGroup.计量单位组;
      }
      this.unitGroupDialogVisible = false;
      window.removeEventListener('message', this.handleUnitGroupMessage);
    },
    
    // 退出
    close() {
      if (this.isIframe) {
        // 在iframe中,通知父窗口关闭或返回
        if (window.parent.editGyClose) {
          window.parent.editGyClose();
        } else {
          // 尝试关闭iframe
          try {
            const index = parent.layer && parent.layer.getFrameIndex ? parent.layer.getFrameIndex(window.name) : null;
            if (index !== null && index !== undefined) {
              parent.layer.close(index);
            } else {
              // 如果没有layer,使用其他方式
              window.parent.postMessage({ action: 'closeIframe' }, '*');
            }
          } catch (e) {
            console.log("关闭iframe失败:", e);
            this.$router.back();
          }
        }
      } else {
        // 不在iframe中,使用路由返回或跳转
        if (this.$route) {
          this.$router.back();
        } else if (this.OperationType === 1) {
          // 新增模式,返回首页
          window.location.href = "/index.html";
        } else {
          window.history.back();
        }
      }
    },
    
    // 提交保存
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.subDisabled = true;
          
          // 准备数据
          const formData = {
            ...this.form,
            HStandard: this.form.HStandard ? "true" : "false",
            HStopflag: this.form.HStopflag ? "true" : "false",
            HRate: this.form.HRate.toString(), // 确保是字符串
            HUserName: this.user,
            OrganizationID: this.organizationID
          };
          
          // 构建与原页面相同的格式
          const sMainStr = JSON.stringify(formData);
          const additionalParams = `${this.user};${this.organizationID};${this.form.HRate}`;
          const fullData = sMainStr + ';' + additionalParams;
          
          const requestData = {
            msg: fullData,
          };
          
          axios({
            method: "POST",
            url: this.baseURL + "SaveGy_UnitList",
            data: requestData,
            headers: {
              'Content-Type': 'application/json',
            },
          })
            .then((response) => {
              if (response.data.count == 1) {
                this.$modal.msgSuccess(response.data.Message || "保存成功");
                this.subDisabled = true;
                this.form.HItemID = response.data.data; // 获取返回的ID
                
                // 如果是自动审核,执行审核
                if (response.data.Verify === "Y") {
                  this.setCheckBillNoLayer();
                }
                
                // 通知父组件刷新
                if (window.parent && window.parent.handleQuery) {
                  window.parent.handleQuery();
                }
                
              } else {
                this.$modal.msgError(response.data.Message || "保存失败");
                this.subDisabled = false;
              }
            })
            .catch((error) => {
              this.$modal.msgError("保存失败: " + (error.message || "未知错误"));
              this.subDisabled = false;
            });
        } else {
          this.$modal.msgError("请检查表单数据");
        }
      });
    },
    
    // 审核(带确认框)
    setCheckBill() {
      this.$confirm('确认要审核吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.setCheckBillNoLayer();
      }).catch(() => {
        // 用户取消
      });
    },
    
    // 审核(不带确认框)
    setCheckBillNoLayer() {
      if (!this.form.HItemID || this.form.HItemID == 0) {
        this.$modal.msgError("请先保存单据");
        return;
      }
      
      axios({
        method: "GET",
        url: this.baseURL + "Gy_Unit/AuditGy_Unit",
        params: {
          HInterID: this.form.HItemID,
          Type: 1,
          user: this.user
        }
      })
        .then((response) => {
          if (response.data.count == 1) {
            this.$modal.msgSuccess("审核成功");
            this.isAuditable = false;
            this.subDisabled = true;
            
            // 更新审核信息
            this.form.HCheckEmp = this.user;
            this.form.HCheckDate = dayjs().format("YYYY-MM-DD");
            
            // 通知父组件刷新
            if (window.parent && window.parent.handleQuery) {
              window.parent.handleQuery();
            }
          } else {
            this.$modal.msgError(response.data.Message || "审核失败");
          }
        })
        .catch((error) => {
          this.$modal.msgError("审核失败: " + (error.message || "未知错误"));
        });
    }
  },
  beforeDestroy() {
    // 移除事件监听器
    window.removeEventListener('message', this.handleUnitGroupMessage);
  }
};
</script>
 
<style scoped>
.el-form-item {
  margin-bottom: 22px;
}
 
.el-row {
  margin-bottom: 10px;
}
 
.hidden-input {
  display: none;
}
 
.readonly-input >>> .el-input__inner {
  background-color: #f5f7fa;
  color: #909399;
  cursor: not-allowed;
}
</style>