Newer
Older
xc-metering-front / src / views / system / installation / editDialog.vue
lyg on 31 Jan 2024 6 KB 文档管理修改
<!-- 安装位置编辑弹窗 -->
<script lang="ts" setup name="EditInstallation">
import type { FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { IRuleFrom } from './installation-interface'
import { getPostList } from '@/api/system/post'
import { addLocation, updateLocation } from '@/api/system/installation'
import type { IPostList } from '@/views/system/post/post-interface'
import { getDeptList, getDeptTree } from '@/api/system/dept'
import { judgeTree, toTreeList } from '@/utils/structure'
const emits = defineEmits(['refresh'])
const dataFormRef = ref()
const dialogFormVisible = ref(false) // 对话框是否显示
const dialogStatus = ref('') // 对话框类型:create,update
const isEditMode = ref(true)
const areaForm = ref<IRuleFrom>({
  installLocation: '', // 安装位置
  subSystemId: '', // 分系统id
  positionId: '', // 岗位id
  id: '',
}) // 表单
const textMap: { [key: string]: string } = {
  update: '编辑',
  create: '新增',
  detail: '详情',
} // 表头显示标题
const rules: FormRules = {
  installLocation: [{ required: true, message: '安装位置不能为空', trigger: ['blur', 'change'] }],
  subSystemId: [{ required: true, message: '所在单位/部门/分系统不能为空', trigger: ['blur', 'change'] }],
  // positionId: [{ required: true, message: '岗位不能为空', trigger: ['blur', 'change'] }],
} // 前端校验规则

// 重置表单
const resetForm = () => {
  areaForm.value = {
    installLocation: '', // 安装位置
    subSystemId: '', // 分系统id
    positionId: '', // 岗位id
    id: '',
  }
}

// 初始化对话框
const initDialog = (dialogStatusValue: string, row: IRuleFrom) => {
  dialogStatus.value = dialogStatusValue
  dialogFormVisible.value = true
  if (dialogStatus.value === 'create') { // 如果是新增,清除验证
    resetForm()
    nextTick(() => {
      dataFormRef.value.clearValidate()
    })
  }
  else if (dialogStatus.value === 'update' || dialogStatus.value === 'detail') { // 如果是修改,将row中数据填写到输入框中
    areaForm.value = {
      id: row.id, // 编号
      installLocation: row.installLocation, // 编号
      subSystemId: row.subSystemId, // 编号
      positionId: row.positionId, // 编号
    }
  }
}
defineExpose({
  initDialog,
})

// 新增数据
const createData = () => {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      addLocation(areaForm.value).then((response) => {
        if (response.code === 200) {
          ElMessage({
            message: '添加成功',
            type: 'success',
          })
          resetForm()
          nextTick(() => {
            dataFormRef.value.clearValidate()
          })
          emits('refresh')
          dialogFormVisible.value = false
        }
      })
    }
  })
}

// 修改数据
const updateData = () => {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      updateLocation(areaForm.value).then((response) => {
        if (response.code === 200) {
          ElMessage({
            message: '修改成功',
            type: 'success',
          })
          dataFormRef.value.clearValidate()
          emits('refresh')
          dialogFormVisible.value = false
        }
      })
    }
  })
}

// 保存数据
const saveData = () => {
  if (dialogStatus.value === 'update') {
    updateData()
  }
  else if (dialogStatus.value === 'create') {
    createData()
  }
}
const cancel = () => {
  dialogFormVisible.value = false
}
// 获取岗位列表
const postList = ref<IPostList[]>([])
const fetchPost = () => {
  getPostList({}).then((res) => {
    postList.value = res.data
  })
}
fetchPost()
const deptList = ref<any[]>([])
const fetchDict = () => {
  getDeptList({}).then((res) => {
    const data = res.data.list.filter((item: any) => item.fullName !== '顶级').map((item: any) => ({ ...item, label: item.fullName, value: item.id }))
    deptList.value = toTreeList(data)
  })
}
fetchDict()
</script>

<template>
  <el-dialog v-model="dialogFormVisible" :title="textMap[dialogStatus]" append-to-body>
    <el-form ref="dataFormRef" :rules="rules" :model="areaForm" label-position="right" label-width="80px" :disabled="dialogStatus === 'detail'">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="所在组织" prop="subSystemId">
            <!-- <dept-select v-model="areaForm.subSystemId" :need-top="false" placeholder="所在单位/部门/分系统" style="width: 100%;" />
             -->
            <el-tree-select
              v-model="areaForm.subSystemId"
              :data="deptList"
              check-strictly
              :render-after-expand="false"
              placeholder="所在单位/部门/分系统"
              style="width: 100%;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="岗位名称" prop="positionId">
            <el-select v-model="areaForm.positionId" filterable placeholder="岗位名称" clearable style="width: 100%;">
              <el-option v-for="item in postList" :key="item.id" :value="item.id" :label="item.positionName" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="安装位置" prop="installLocation">
            <el-input v-model.trim="areaForm.installLocation" type="text" placeholder="请输入安装位置" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div v-if="dialogStatus !== 'detail'">
          <el-button type="primary" @click="saveData">
            保存
          </el-button>
          <el-button @click="cancel">
            取消
          </el-button>
        </div>
        <div v-else>
          <el-button type="primary" @click="cancel">
            确认
          </el-button>
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.el-dialog {
  width: 700px;
}

.el-select {
  width: 100%;
}
</style>