zhouwx
2024-12-25 c53fc4edf8587a1ba44d5a57139baee3dcaba0b0
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
<template>
  <div class="system-add-gas-container">
    <el-dialog
        :title="state.title"
        v-model="state.isShowUserDialog"
        width="500px"
    >
      <el-form :model="state.gasForm" size="default" ref="gasRef" :rules="state.title == '新增气体'  || state.title == '编辑气体'? state.gasFormRules : ''" label-width="110px">
        <el-form-item label="气体名称:" prop="gasName">
          <el-input v-model.trim="state.gasForm.gasName" :disabled="state.disabled" ></el-input>
        </el-form-item>
        <el-form-item label="气体分子式:" prop="gasMolecularFormula">
          <el-input v-model.trim="state.gasForm.gasMolecularFormula" :disabled="state.disabled"></el-input>
        </el-form-item>
        <el-form-item label="气体阈值:" prop="gasThreshold">
          <el-input v-model.trim="state.gasForm.gasThreshold" :disabled="state.disabled"></el-input>
        </el-form-item>
        <el-form-item label="气体单位:" prop="gasUnit">
          <el-input v-model.trim="state.gasForm.gasUnit" :disabled="state.disabled"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="handleClose" size="default">取 消</el-button>
                    <el-button type="primary"  @click="onSubmit" size="default">确 定</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup lang="ts">
import {reactive, ref} from "vue";
import { GasState } from "/@/types/gasManage";
import {gasManageApi} from "/@/api/basicDataManage/gasManage";
import {ElMessage} from "element-plus";
 
const gasRef = ref();
const emit = defineEmits(["getGasData"]);
const state = reactive<GasState>({
  disabled: false,
  title: '',
  isShowUserDialog: false,
  gasForm: {
    id: '',
    gasName: '',
    gasMolecularFormula: '',
    gasThreshold: '',
    gasUnit: ''
  },
  gasFormRules:{
    gasName: [{ required: true, message: '请填写气体名称', trigger: 'blur' }],
    gasMolecularFormula: [{ required: true, message: '请填写气体分子式', trigger: 'blur' }],
    gasThreshold: [{ required: true, message: '请填写阈值', trigger: 'blur' }],
    gasUnit: [{ required: true, message: '请选择气体单位', trigger: 'blur' }],
  },
});
const openDialog = (type: string, value: any) => {
  state.isShowUserDialog = true;
  if (type === '新增') {
    state.disabled = false;
    state.title = '新增气体';
    state.gasForm = {
      id: '',
      gasName: '',
      gasMolecularFormula: '',
      gasThreshold: '',
      gasUnit: ''
    };
  } else if (type === '编辑'){
    state.disabled = false;
    state.title = '编辑气体';
    state.gasForm = JSON.parse(JSON.stringify(value));
  } else {
    state.disabled = true;
    state.title = '查看气体';
    state.gasForm = JSON.parse(JSON.stringify(value));
  }
};
const onSubmit = async () => {
  if(state.title == '新增气体'){
    const valid = await gasRef.value.validate();
    if(valid) {
      const param = {
        molecularFormula: state.gasForm.gasMolecularFormula,
        name: state.gasForm.gasName,
        unit: state.gasForm.gasUnit,
        threshold: state.gasForm.gasThreshold
      }
      let res = await gasManageApi().addGas(param);
      if (res.data.code === 100) {
        ElMessage({
          type: 'success',
          message: '新增成功'
        });
        gasRef.value.clearValidate();
        state.isShowUserDialog = false;
        emit('getGasData');
      } else {
        ElMessage({
          type: 'warning',
          message: res.data.msg
        });
      }
    }
  }else if(state.title == '编辑气体') {
    const valid = await gasRef.value.validate();
    if(valid) {
      const param = {
        id: state.gasForm.id,
        molecularFormula: state.gasForm.gasMolecularFormula,
        name: state.gasForm.gasName,
        unit: state.gasForm.gasUnit,
        threshold: state.gasForm.gasThreshold
      }
      let res = await gasManageApi().editGas(param);
      if (res.data.code === 100) {
        ElMessage({
          type: 'success',
          message: '编辑成功'
        });
        gasRef.value.clearValidate();
        state.isShowUserDialog = false;
        emit('getGasData');
      } else {
        ElMessage({
          type: 'warning',
          message: res.data.msg
        });
      }
    }
  } else {
    gasRef.value.clearValidate();
    state.isShowUserDialog = false;
    emit('getGasData');
  }
};
 
const handleClose = () => {
  gasRef.value.clearValidate();
  state.isShowUserDialog = false;
  emit('getGasData');
}
 
defineExpose({
  openDialog
});
</script>
<style scoped lang="scss">
 
</style>