edit.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div class="system-edit-dic-container">
  3. <el-dialog :title="(ruleForm.id !== 0 ? '修改' : '添加') + '小区'" v-model="dialogVisible" width="550px">
  4. <el-form :model="ruleForm" ref="formRef" :rules="rules" size="default" label-width="110px">
  5. <!-- <el-form-item label="所属组织" prop="organizationId">
  6. <el-tree-select
  7. v-model="ruleForm.organizationId"
  8. :data="orgList"
  9. :props="{
  10. label: 'name',
  11. children: 'children'
  12. }"
  13. node-key="id"
  14. :clearable="true"
  15. check-strictly
  16. style="width: 100%;"
  17. :render-after-expand="true"
  18. />
  19. </el-form-item> -->
  20. <el-form-item label="小区名称" prop="name">
  21. <el-input v-model="ruleForm.name" placeholder="请输入小区名称" />
  22. </el-form-item>
  23. <el-form-item label="状态" prop="status">
  24. <el-radio v-model="ruleForm.status" :label="1">启用</el-radio>
  25. <el-radio v-model="ruleForm.status" :label="0">禁用</el-radio>
  26. </el-form-item>
  27. </el-form>
  28. <template #footer>
  29. <span class="dialog-footer">
  30. <el-button @click="onCancel" size="default">取 消</el-button>
  31. <el-button type="primary" @click="onSubmit" size="default">{{ ruleForm.id !== 0 ? '修 改' : '添 加' }}</el-button>
  32. </span>
  33. </template>
  34. </el-dialog>
  35. </div>
  36. </template>
  37. <script lang="ts">
  38. import { reactive, toRefs, defineComponent, ref, unref, nextTick } from 'vue';
  39. import api from '/@/api/heatingDistrict';
  40. import heatApi from '/@/api/heatStation';
  41. import { ElMessage } from 'element-plus';
  42. interface RuleFormState {
  43. id?: number;
  44. name: string;
  45. organizationId: string;
  46. }
  47. export default defineComponent({
  48. name: 'edit',
  49. setup(prop, { emit }) {
  50. const formRef = ref<HTMLElement | null>(null);
  51. const state = reactive({
  52. dialogVisible: false,
  53. ruleForm: {
  54. id: 0,
  55. name: '',
  56. organizationId: '',
  57. status: 1
  58. },
  59. rules: {
  60. name: [{ required: true, message: '小区名称不能为空', trigger: ['blur', 'change'] }],
  61. organizationId: [{ required: true, message: '所属组织不能为空', trigger: ['blur', 'change'] }]
  62. },
  63. orgList: [],
  64. heatList: []
  65. })
  66. // 打开弹窗
  67. const openDialog = (row: RuleFormState | null, organizationId: any) => {
  68. resetForm()
  69. queryTree()
  70. if (organizationId) {
  71. state.ruleForm.organizationId = organizationId
  72. }
  73. if (row) {
  74. (state.ruleForm as any).id = row.id
  75. getDetail()
  76. }
  77. state.dialogVisible = true
  78. }
  79. const resetForm = () => {
  80. state.ruleForm = {
  81. id: 0,
  82. name: '',
  83. organizationId: '',
  84. status: 1
  85. }
  86. }
  87. // 关闭弹窗
  88. const closeDialog = () => {
  89. state.dialogVisible = false;
  90. (formRef.value as any).clearValidate()
  91. }
  92. // 取消
  93. const onCancel = () => {
  94. closeDialog()
  95. }
  96. const queryTree = () => {
  97. heatApi.heatStation.getList({
  98. name: '',
  99. code: '',
  100. status: -1
  101. })
  102. .then((res: any) => {
  103. state.heatList = res || [];
  104. });
  105. };
  106. const getDetail = () => {
  107. api.regionalManage.detail(state.ruleForm.id)
  108. .then((res: any) => {
  109. state.ruleForm = {
  110. ...res
  111. }
  112. })
  113. }
  114. // 新增
  115. const onSubmit = () => {
  116. const formWrap = unref(formRef) as any
  117. if (!formWrap) return;
  118. formWrap.validate((valid: boolean) => {
  119. if (valid) {
  120. let params = { ...state.ruleForm }
  121. if (params.id) {
  122. //修改
  123. api.regionalManage.edit(params).then(() => {
  124. ElMessage.success('小区修改成功')
  125. closeDialog() // 关闭弹窗
  126. emit('queryList')
  127. })
  128. } else {
  129. //添加
  130. api.regionalManage.add(params).then(() => {
  131. ElMessage.success('小区添加成功')
  132. closeDialog() // 关闭弹窗
  133. emit('queryList')
  134. })
  135. }
  136. }
  137. })
  138. }
  139. return {
  140. openDialog,
  141. closeDialog,
  142. onCancel,
  143. onSubmit,
  144. formRef,
  145. ...toRefs(state)
  146. }
  147. }
  148. })
  149. </script>