吴羡
8 天以前 ab3cb96ebe3b3134e897b002d1c0b3576f8aaa37
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
<template>
  <div>
    <el-dialog
        v-model="dialogFormVisible"
        :title="operationType === 'add' ? '新增原材料检验' : operationType === 'view' ? '查看原材料检验' : '编辑原材料检验'"
        width="70%"
        @close="closeDia"
    >
      <el-form :model="form" label-position="top" :rules="rules" ref="formRef">
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="项目:" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入" clearable :disabled="isViewMode"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称:" prop="productId">
              <el-tree-select
                  v-model="form.productId"
                  placeholder="请选择"
                  clearable
                  check-strictly
                  @change="getModels"
                  :data="productOptions"
                  :render-after-expand="false"
                  :disabled="isViewMode || operationType === 'edit'"
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="检验依据:" prop="acceptanceBasis">
              <el-input v-model="form.acceptanceBasis" placeholder="请输入" clearable :disabled="isViewMode"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量:" prop="quantity">
              <el-input v-model="form.quantity" placeholder="请输入" clearable :disabled="isViewMode"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="抽检数量:" prop="samplingCount">
              <el-input v-model="form.samplingCount" placeholder="0.00" clearable :disabled="isViewMode"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验日期:" prop="checkTime">
              <el-date-picker
                  v-model="form.checkTime"
                  type="date"
                  placeholder="请选择日期"
                  value-format="YYYY-MM-DD"
                  format="YYYY-MM-DD"
                  clearable
                  style="width: 100%"
                  :disabled="isViewMode"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="异常备注:" prop="abnormalRemark">
              <el-input v-model="form.abnormalRemark" placeholder="请输入" clearable :disabled="isViewMode"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="判定:" prop="verdict">
              <el-select v-model="form.verdict" placeholder="请选择" clearable :disabled="isViewMode" style="width: 100%">
                <el-option label="合格" value="合格" />
                <el-option label="不合格" value="不合格" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <template v-if="!isViewMode">
            <el-button type="primary" @click="submitForm">确认</el-button>
            <el-button @click="closeDia">取消</el-button>
          </template>
          <el-button v-else @click="closeDia">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import {ref, reactive, toRefs, computed, getCurrentInstance, nextTick} from "vue";
import {productTreeList} from "@/api/basicData/product.js";
import {qualityInspectAdd, qualityInspectUpdate} from "@/api/qualityManagement/rawMaterialInspection.js";
const { proxy } = getCurrentInstance()
const emit = defineEmits(['close'])
 
const dialogFormVisible = ref(false);
const operationType = ref("");
const data = reactive({
  form: {
    id: "",
    checkTime: "",
    productId: "",
    productName: "",
    projectName: "",
    acceptanceBasis: "",
    quantity: undefined,
    samplingCount: undefined,
    abnormalRemark: "",
    verdict: "",
  },
  rules: {
    productId: [{ required: true, message: "请选择", trigger: "change" }],
  },
});
const { form, rules } = toRefs(data);
// 已提交的记录点编辑时按只读处理
const readOnlyEdit = ref(false);
// 是否为只读模式(查看 或 已提交记录点编辑)
const isViewMode = computed(() => operationType.value === 'view' || readOnlyEdit.value);
const productOptions = ref([]);
const currentProductId = ref(0);
 
// 打开弹框
const openDialog = async (type, row) => {
  readOnlyEdit.value = type === 'edit' && row?.inspectState == 1;
  operationType.value = type;
  // 先重置表单,避免异步加载失败时残留上一次会话的数据(含id)
  form.value = { ...data.form };
  if (operationType.value === 'edit' || operationType.value === 'view') {
    form.value = { ...form.value, ...row };
  }
  dialogFormVisible.value = true;
  await nextTick();
  proxy.$refs.formRef?.clearValidate();
 
  await getProductOptions();
 
  if (operationType.value === 'edit' || operationType.value === 'view') {
    currentProductId.value = row.productId || 0;
    if (currentProductId.value) {
      form.value.productName = findNodeById(productOptions.value, currentProductId.value);
    }
  }
  nextTick(() => {
    proxy.$refs.formRef?.clearValidate();
  });
};
const getProductOptions = () => {
  return productTreeList().then((res) => {
    productOptions.value = convertIdToValue(res);
  });
};
const getModels = (value) => {
  currentProductId.value = value
  form.value.productName = findNodeById(productOptions.value, value);
};
 
const findNodeById = (nodes, productId) => {
  for (let i = 0; i < nodes.length; i++) {
    if (nodes[i].value === productId) {
      return nodes[i].label; // 找到节点,返回该节点
    }
    if (nodes[i].children && nodes[i].children.length > 0) {
      const foundNode = findNodeById(nodes[i].children, productId);
      if (foundNode) {
        return foundNode; // 在子节点中找到,返回该节点
      }
    }
  }
  return null; // 没有找到节点,返回null
};
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;
  });
}
// 提交产品表单
const submitForm = () => {
  proxy.$refs.formRef.validate(valid => {
    if (valid) {
      const data = { ...form.value, inspectType: 0 };
      if (operationType.value === "add") {
        delete data.id;
        qualityInspectAdd(data).then(res => {
          proxy.$modal.msgSuccess("提交成功");
          closeDia();
        });
      } else {
        qualityInspectUpdate(data).then(res => {
          proxy.$modal.msgSuccess("提交成功");
          closeDia();
        });
      }
    }
  });
}
// 关闭弹框
const closeDia = () => {
  proxy.resetForm("formRef");
  readOnlyEdit.value = false;
  dialogFormVisible.value = false;
  emit('close');
}
defineExpose({
  openDialog,
});
</script>
 
<style scoped>
 
</style>