Form.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <Dialog v-model="dialogVisible" title="添加楼栋">
  3. <el-form ref="formRef" :model="dataForm" :rules="dataRule" :label-width="labelWidth">
  4. <div style="margin-top: 10px">
  5. <el-row>
  6. <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
  7. <el-form-item label="楼栋名称" prop="buildName" style="margin: 0 0; padding: 0 0">
  8. <el-input
  9. v-model="dataForm.buildName"
  10. style="margin: 0 0; padding: 0 0"
  11. :maxlength="20"
  12. />
  13. </el-form-item>
  14. </el-col>
  15. <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
  16. <el-form-item label="显示顺序" prop="sort">
  17. <el-input :maxlength="5" v-model="dataForm.sort" />
  18. </el-form-item>
  19. </el-col>
  20. </el-row>
  21. <el-row>
  22. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  23. <el-form-item label="楼层高度" prop="floorCount" style="margin: 0 0; padding: 0 0">
  24. <el-input
  25. v-model="dataForm.floorCount"
  26. style="margin: 0 0; padding: 0 0"
  27. :maxlength="6"
  28. />
  29. </el-form-item>
  30. </el-col>
  31. </el-row>
  32. <el-divider style="margin-top: 10px" />
  33. <el-row>
  34. <el-col :span="6">
  35. <el-button @click="addFloor" :icon="CirclePlus" style="margin-bottom: 20px"
  36. >添加楼层</el-button
  37. >
  38. </el-col>
  39. <el-col :span="16">
  40. <el-text />
  41. </el-col>
  42. </el-row>
  43. <el-scrollbar max-height="50vh">
  44. <div
  45. v-loading="loadingBed"
  46. v-for="(item, index) in dataForm.floorList"
  47. :key="index"
  48. >
  49. <el-row v-show="!item.isDelete">
  50. <el-col :xs="23" :sm="23" :md="23" :lg="11" :xl="11">
  51. <el-form-item label="楼层名称" prop="floorName">
  52. <el-input :maxlength="6" v-model="item.floorName" />
  53. </el-form-item>
  54. </el-col>
  55. <el-col :xs="23" :sm="23" :md="23" :lg="11" :xl="11">
  56. <el-form-item label="显示顺序" prop="sort">
  57. <el-input :maxlength="5" v-model="item.sort" />
  58. </el-form-item>
  59. </el-col>
  60. <el-col :span="2">
  61. <div
  62. @click="deleteFloor(index)"
  63. style="
  64. height: 30px;
  65. display: flex;
  66. align-items: center;
  67. justify-content: center;
  68. width: 3vw;
  69. "
  70. >
  71. <el-icon :size="16" color="#ff0000">
  72. <Remove />
  73. </el-icon>
  74. </div>
  75. </el-col>
  76. </el-row>
  77. </div>
  78. </el-scrollbar>
  79. </div>
  80. </el-form>
  81. <template #footer>
  82. <el-button @click="handleClosed" :type="isDetail ? 'danger' : ''">{{
  83. isDetail ? '删除' : '关闭'
  84. }}</el-button>
  85. <el-button v-loading="formLoading" type="primary" @click="submitForm">确定</el-button>
  86. </template>
  87. </Dialog>
  88. </template>
  89. <script lang="ts" setup>
  90. import { computed, ref } from 'vue'
  91. import { elderlyBakAdd, getElderlyBakInfo } from '@/api/elderly/bak/check-out'
  92. import { FormRules } from 'element-plus'
  93. import { useMediaQuery } from '@vueuse/core'
  94. import { CirclePlus, Remove, OfficeBuilding } from '@element-plus/icons-vue'
  95. import {
  96. addBuild,
  97. getBuildListId,
  98. deleteBuildListId,
  99. editBuild,
  100. checkBuildFloorById
  101. } from '@/api/system/badManage'
  102. const message = useMessage() // 消息弹窗
  103. const { t } = useI18n() // 国际化
  104. const floorNum = ref(1)
  105. const dialogVisible = ref(false) // 弹窗
  106. const formRef = ref() // 表单 Ref
  107. const isDetail = ref(false) // 是否详情打开
  108. const loadingBed = ref(false) // 是否详情打开
  109. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  110. let dataForm = ref({
  111. // 表单字段
  112. id: undefined,
  113. buildName: undefined,
  114. sort: undefined,
  115. floorCount: undefined,
  116. floorList: [
  117. {
  118. floorName: '1层',
  119. sort: 1
  120. }
  121. ],
  122. tenantId: undefined,
  123. })
  124. // 表单规则
  125. const dataRule = reactive<FormRules>({
  126. buildName: [{ required: true, message: '楼栋名称不能为空', trigger: 'blur' }],
  127. floorCount: [{ required: true, message: '楼层高度不正确', trigger: 'blur' }]
  128. })
  129. // 计算窗口大小
  130. const currentWidth = useMediaQuery('(max-width: 800px)')
  131. // 计算文字大小
  132. const labelWidth = computed(() => {
  133. return currentWidth.value ? '80px' : '80px'
  134. })
  135. const selectElderRef = ref() // 选择老人ref
  136. /** 打开弹窗 */
  137. const open = async (tId, id?: any, detail: boolean = false) => {
  138. resetForm()
  139. dialogVisible.value = true
  140. dataForm.value.id = id || undefined
  141. isDetail.value = detail
  142. dataForm.value.tenantId = tId
  143. if (id) {
  144. try {
  145. const res = await getBuildListId(id)
  146. dataForm.value = res
  147. } catch (err) {}
  148. }
  149. }
  150. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  151. /** 提交表单 */
  152. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  153. const submitForm = async () => {
  154. try {
  155. if (!/^\d+$/.test(dataForm.value.sort)) {
  156. // 验证输入是否只包含数字
  157. message.error('显示顺序只能输入数字')
  158. return
  159. }
  160. if (!/^\d+$/.test(dataForm.value.floorCount)) {
  161. // 验证输入是否只包含数字
  162. message.error('楼层高度只能输入数字')
  163. return
  164. }
  165. for (const validElement of dataForm.value.floorList) {
  166. if (validElement.floorName == '') {
  167. message.error('请输入楼层号')
  168. return
  169. }
  170. if (validElement.sort != '') {
  171. if (!/^\d+$/.test(validElement.sort)) {
  172. // 验证输入是否只包含数字
  173. message.error('显示顺序只能输入数字')
  174. return
  175. }
  176. }
  177. }
  178. } catch (err) {}
  179. // 校验表单
  180. if (!formRef.value) return
  181. const valid = await formRef.value.validate()
  182. if (!valid) return
  183. // 提交请求
  184. formLoading.value = true
  185. try {
  186. let res
  187. if (isDetail.value) {
  188. res = await editBuild(dataForm.value)
  189. } else {
  190. res = await addBuild(dataForm.value)
  191. }
  192. if (res) {
  193. message.success(t('common.updateSuccess'))
  194. dialogVisible.value = false
  195. // 发送操作成功的事件
  196. emit('success')
  197. }
  198. } catch (e) {
  199. message.error(e)
  200. } finally {
  201. formLoading.value = false
  202. }
  203. }
  204. /** 重置表单 */
  205. const resetForm = () => {
  206. dataForm.value = {
  207. floorList: [
  208. {
  209. floorName: '1层',
  210. sort: 1
  211. }
  212. ],
  213. id: undefined,
  214. buildName: undefined,
  215. sort: undefined,
  216. floorCount: undefined,
  217. tenantId: undefined
  218. }
  219. isDetail.value = false
  220. floorNum.value = 1
  221. formRef.value?.resetFields()
  222. }
  223. // 关闭表单
  224. const handleClosed = async () => {
  225. if (isDetail.value) {
  226. try {
  227. // 删除的二次确认
  228. await message.confirm('确定要删除该楼栋?')
  229. // 删除
  230. const res = await deleteBuildListId(dataForm.value.id)
  231. if (res) {
  232. resetForm()
  233. message.success(t('删除成功'))
  234. dialogVisible.value = false
  235. // 发送操作成功的事件
  236. emit('success')
  237. }
  238. } catch {}
  239. } else {
  240. resetForm()
  241. dialogVisible.value = false
  242. }
  243. }
  244. // 获取老人
  245. const getItems = () => {
  246. nextTick(() => {
  247. selectElderRef.value.open()
  248. })
  249. }
  250. const addFloor = () => {
  251. floorNum.value++
  252. dataForm.value.floorList.push({ sort: '', floorName: '' ,isDelete:false})
  253. }
  254. const deleteFloor = (index) => {
  255. if (dataForm.value.floorList.length <= 1) {
  256. message.warning('最少需要一个楼层')
  257. return
  258. } else {
  259. loadingBed.value=true
  260. //编辑的
  261. if(dataForm.value.floorList[index].id!=undefined && dataForm.value.floorList[index].id!==null){
  262. //根据ID查询床位是否被使用
  263. checkBuildFloorById(dataForm.value.floorList[index].id).then((res)=>{
  264. console.log('AAA',res)
  265. if(!res){
  266. dataForm.value.floorList[index].isDelete = true
  267. }else {
  268. message.error('楼层中有床位已被入住,无法删除!')
  269. }
  270. loadingBed.value=false
  271. }).catch((e)=>{ loadingBed.value=false})
  272. }else {//新增的直接删
  273. dataForm.value.floorList.splice(index, 1)
  274. loadingBed.value=false
  275. }
  276. }
  277. }
  278. const successAddBuild = () => {}
  279. function clickBuildIcon(item) {
  280. console.log(item)
  281. }
  282. function clickBuildItem(item) {
  283. //console.log(item.name)
  284. }
  285. </script>
  286. <style lang="scss" scoped></style>