马宇豪
2025-03-04 1b9fea7d4af68d8f933b2dc42bf6084b9646f64c
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
<template>
    <div class="system-add-menu-container">
        <el-dialog :title="dialogState.title" :close-on-click-modal="false" v-model="dialogState.showSafetyGoodsAndEquipmentDialog" width="600px">
            <el-form
                :model="dialogState.safetyGoodsAndEquipmentForm"
                :rules="dialogState.safetyGoodsAndEquipmentRules"
                ref="safetyGoodsAndEquipmentRef"
                size="default"
                v-loading="dialogState.loading"
                element-loading-text="Loading..."
                label-width="120px">
                <el-row>
                    <el-col :span="24" class="mb20">
                        <el-form-item label="物资大类" prop="bigClassifyId">
                            <el-select v-model="dialogState.safetyGoodsAndEquipmentForm.bigClassifyId" @change="changeSmallClassify(null)" placeholder="物资大类" class="input-add">
                                <el-option
                                v-for="item in dialogState.goodsBigClassifyList"
                                :key="item.id"
                                :value="item.id"
                                :label="item.materialClassifyName"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
<!--                    <el-col :span="5">-->
<!--                        <el-button type="primary" @click="openAddGoods">添加物资</el-button>-->
<!--                    </el-col>-->
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="物资小类" prop="smallClassifyId">
                            <el-select class="input-add" v-model="dialogState.safetyGoodsAndEquipmentForm.smallClassifyId" placeholder="请先选择物资大类">
                                <el-option
                                    v-for="item in dialogState.goodsSmallClassifyList"
                                    :key="item.id"
                                    :value="item.id"
                                    :label="item.materialClassifyName"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
 
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="部门" prop="depId">
                            <el-cascader
                                :disabled="true"
                                :options="dialogState.departmentList"
                                :props="{ emitPath: false, checkStrictly: true, value: 'depId', label: 'depName' }"
                                placeholder="请选择部门"
                                clearable
                                class="input-add"
                                v-model="dialogState.safetyGoodsAndEquipmentForm.depId">
                            </el-cascader>
                        </el-form-item>
                    </el-col>
<!--                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">-->
<!--                        <el-form-item label="是否耗材" prop="consumable">-->
<!--                            <el-select class="input-add" v-model="dialogState.safetyGoodsAndEquipmentForm.consumable" placeholder="是否耗材" clearable filterable>-->
<!--                                <el-option-->
<!--                                    v-for="item in dialogState.consumableList"-->
<!--                                    :key="item.id"-->
<!--                                    :value="item.id"-->
<!--                                    :label="item.name"-->
<!--                                >-->
<!--                                </el-option>-->
<!--                            </el-select>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogState.showSafetyGoodsAndEquipmentDialog = !dialogState.showSafetyGoodsAndEquipmentDialog" size="default">取 消</el-button>
                    <el-button type="primary" @click="submitSafetyGoodsAndEquipment" v-throttle size="default">确 定</el-button>
                </span>
            </template>
        </el-dialog>
        <add-goods-dialog ref="addGoodsDialogRef" @refreshClassify="getAllSafetyEquipmentList"></add-goods-dialog>
    </div>
</template>
 
<script setup lang="ts">
import {computed, nextTick, reactive, ref, watch} from 'vue'
import {
    BigClassifyType,
    DataType, DepartmentType,
    isValidKey,
    SafetyGoodsAndEquipmentDialogStateType, safetyGoodsAndEquipmentFormType
} from "/@/views/facilityManagement/safetyGoodsAndEquipment/index";
import { goodsAndEquipmentApi } from "/@/api/facilityManagement/safetyGoodsAndEquipment";
import {ElMessage} from "element-plus";
import AddGoodsDialog from "./addGoodsDialog.vue";
import {useUserInfo} from "/@/stores/userInfo";
 
const safetyGoodsAndEquipmentRef = ref();
const addGoodsDialogRef = ref();
const userInfo = useUserInfo()
 
const dialogState = reactive<SafetyGoodsAndEquipmentDialogStateType>({
    safetyGoodsAndEquipmentForm: {
        id: null,
        bigClassifyId: null,
        smallClassifyId: null,
        depId: null,
        // consumable: null,
    },
    safetyGoodsAndEquipmentRules: {
        bigClassifyId: [{ required: true, message: '请选择物资大类', trigger: 'change' }],
        smallClassifyId: [{ required: true, message: '请选择物资小类', trigger: 'change' }],
        // depId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        // consumable: [{ required: true, message: '请选择是否耗材', trigger: 'change' }],
    },
    showSafetyGoodsAndEquipmentDialog: false,
    title: '',
    loading: false,
    depList: [],
    goodsBigClassifyList: [],
    goodsSmallClassifyList: [],
    departmentList: [],
    consumableList: [
        {id: 0, name: '是'},
        {id: 1, name: '否'},
    ],
});
 
watch(() => dialogState.safetyGoodsAndEquipmentForm.bigClassifyId ,(newVal, oldVal) => {
 
},);
 
// const openAddGoods = () => {
//     addGoodsDialogRef.value.openAddGoodsDialog(dialogState.goodsBigClassifyList)
// }
 
const openSafetyGoodsAndEquipmentDialog =
    (title: string, value: safetyGoodsAndEquipmentFormType, goodsBigClassifyList: BigClassifyType [], departmentList : DepartmentType []) => {
    dialogState.title = title;
    dialogState.showSafetyGoodsAndEquipmentDialog = true;
    dialogState.goodsBigClassifyList = goodsBigClassifyList;
    dialogState.departmentList = departmentList;
    dialogState.safetyGoodsAndEquipmentForm = {
        id: null,
        bigClassifyId: null,
        smallClassifyId: null,
        depId: userInfo.userInfos.depId,
        // consumable: null,
    };
    nextTick( () => {
        safetyGoodsAndEquipmentRef.value.clearValidate();
    })
    if(title === '新增') {
        dialogState.title = '新增'
    }else {
        dialogState.title = '编辑'
        changeSmallClassify(value.bigClassifyId)
        for(let key in dialogState.safetyGoodsAndEquipmentForm) {
            if(isValidKey(key, dialogState.safetyGoodsAndEquipmentForm)){
                dialogState.safetyGoodsAndEquipmentForm[key] = value[key];
            }
        }
    }
};
 
const changeSmallClassify = (id: number | null) => {
    dialogState.safetyGoodsAndEquipmentForm.smallClassifyId = null;
    dialogState.goodsSmallClassifyList = dialogState.goodsBigClassifyList[dialogState.goodsBigClassifyList.
    findIndex(item => item.id === (dialogState.safetyGoodsAndEquipmentForm.bigClassifyId ?? id))].childList as Array<BigClassifyType>;
}
 
const submitSafetyGoodsAndEquipment = () => {
    safetyGoodsAndEquipmentRef.value.validate(async (valid: boolean) => {
        if(valid){
            dialogState.loading = true
            if(dialogState.title === '新增') {
                let res = await goodsAndEquipmentApi().addGoodsEquipment(dialogState.safetyGoodsAndEquipmentForm)
                if(res.data.code === '200'){
                    dialogState.showSafetyGoodsAndEquipmentDialog = false
                    emit('refreshData')
                    ElMessage({
                        type: 'success',
                        message: '安全物资设备新增成功',
                        duration: 2000
                    });
                }else{
                    ElMessage({
                        type:'warning',
                        message:res.data.msg
                    })
                }
            }else{
                let res = await goodsAndEquipmentApi().updateGoodsEquipment(dialogState.safetyGoodsAndEquipmentForm)
                if(res.data.code === '200'){
                    dialogState.showSafetyGoodsAndEquipmentDialog = false
                    emit('refreshData')
                    ElMessage({
                        type: 'success',
                        message: '安全物资设备编辑成功',
                        duration: 2000
                    });
                }else{
                    ElMessage({
                        type:'warning',
                        message:res.data.msg
                    })
                }
            }
            dialogState.loading = false
        }else{
            ElMessage({
                type: 'warning',
                message: '请完善基本信息'
            })
        }
    })
};
 
const getAllSafetyEquipmentList = async () => {
    let res = await goodsAndEquipmentApi().getAllSafetyEquipment();
    if(res.data.code === '200'){
        dialogState.goodsBigClassifyList = res.data.data
    }else{
        ElMessage({
            message:res.data.msg,
            type:'warning'
        })
    }
};
 
const emit = defineEmits(['refreshData',])
 
defineExpose({
    openSafetyGoodsAndEquipmentDialog,
});
 
</script>
 
<style scoped>
.input-length{
    width: 100% !important;
}
</style>