Newer
Older
laserPTZFrontV2.0 / src / views / station / edit.vue
wangxitong on 22 May 2023 4 KB first commit
<script lang="ts" setup name="edit">
import type { Ref } from 'vue'
import { nextTick, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import type { IStationInfo, IStationTypeInfo } from './interface'
import { addStation, updateStation } from '@/api/ptz/station'

// ----------------------- 以下是字段定义 emits props ---------------------
const emits = defineEmits(['closeRefresh'])

// 对话框类型:create,update
const dialogStatus = ref('create')
const dialogVisible = ref(false)
// 显示标题
const textMap: { [key: string]: string } = {
  update: '编辑',
  create: '新增',
}
// 表单数据对象
const formData: Ref<IStationInfo> = ref({
  id: '',
  stationName: '',
  deptId: '',
})

// 保存按钮加载状态
const btnLoading = ref(false)

// ---------------表单提交--------------------------------
// 表单对象
const dataFormRef = ref<FormInstance>()
// 校验规则
const rules = reactive<FormRules>({
  stationName: [{ required: true, message: '场站名称不可为空', trigger: ['blur', 'change'] }],
  deptId: [{ required: true, message: '权属为必选', trigger: ['blur', 'change'] }],
})

function submitForm() {
  if (dataFormRef) {
    dataFormRef.value?.validate((valid: boolean) => {
      if (valid) {
        if (dialogStatus.value === 'create') {
          createData()
        }
        else if (dialogStatus.value === 'update') {
          updateData()
        }
      }
    })
  }
}
// 新增数据
function createData() {
  btnLoading.value = true
  addStation(formData.value).then((res) => {
    resetForm()
    nextTick(() => {
      console.log('clearValidate')
      dataFormRef.value?.clearValidate(['stationName', 'deptId'])
      ElMessage.success('新增成功')
      closeRefresh()
      btnLoading.value = false
    }).catch((_) => {
      btnLoading.value = false
    })
  })
}

// 更新数据
function updateData() {
  updateStation(formData.value).then((res) => {
    ElMessage.success('修改成功')
    btnLoading.value = false
    closeRefresh()
  }).catch((_) => { // 异常情况,loading置为false
    btnLoading.value = false
  })
}

// 重置表单
function resetForm() {
  formData.value = {
    id: '',
    stationName: '',
    deptId: '',
  }
  console.log('resetForm')
  nextTick(() => {
    dataFormRef.value?.clearValidate()
  })
}

// ----------初始化、关闭对话框相关-----------------
function initDialog(dialogstatus: string, row: IStationInfo) {
  dialogStatus.value = dialogstatus
  dialogVisible.value = true
  btnLoading.value = false
  // 获取权属
  if (dialogstatus === 'create') {
    resetForm()
  }
  else if (dialogstatus === 'update') {
    nextTick(() => {
      dataFormRef.value?.clearValidate()
    })
    formData.value = {
      id: row.id,
      stationName: row.stationName,
      deptId: row.deptId,
    }
  }
}

// 关闭并刷新
function closeRefresh() {
  dialogVisible.value = false
  resetForm()
  emits('closeRefresh')
}
// 关闭弹窗
function dialogClose() {
  dialogVisible.value = false
  resetForm()
}
// ----------------------- 以下是暴露的方法内容 ----------------------------
defineExpose({ initDialog })
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="textMap[dialogStatus]"
    width="500"
    :before-close="dialogClose"
    append-to-body
    :open-delay="0"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef" :model="formData" :rules="rules" label-position="left" label-width="80px" class="form-container"
      size="default" @submit.prevent
    >
      <el-row :gutter="10">
        <el-col :span="24" class="grid-cell">
          <el-form-item label="场站名称" prop="stationName" class="required">
            <el-input v-model="formData.stationName" type="text" placeholder="必填" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24" class="grid-cell">
          <el-form-item label="权属" prop="deptId" class="required">
            <dept-select v-model="formData.deptId" :dept-show="false" :clearable="false" placeholder="请选择权属" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button :loading="btnLoading" type="primary" @click="submitForm">
          保存
        </el-button>
        <el-button @click="dialogClose">
          取消
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.form-container {
  width: 100%;

  .full-width-input {
    width: 100%;
  }
}
</style>