<!-- 岗位管理编辑弹窗 -->
<script lang="ts" setup name="EditPost">
import type { FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { addPost, updatePost } from '@/api/system/post'
const emits = defineEmits(['refresh'])
const dataFormRef = ref()
const dialogFormVisible = ref(false) // 对话框是否显示
const dialogStatus = ref('') // 对话框类型：create,update
const areaForm = ref({
  id: '',
  remarks: '', // 备注
  positionName: '', // 岗位名称
  subSystemId: '', // 分系统id
}) // 表单
const textMap: { [key: string]: string } = {
  update: '编辑',
  create: '新增',
  detail: '详情',
} // 表头显示标题
const rules: FormRules = {
  positionName: [{ required: true, message: '岗位名称不能为空', trigger: ['blur', 'change'] }],
  subSystemId: [{ required: true, message: '所在组织不能为空', trigger: ['blur', 'change'] }],
} // 前端校验规则

// 重置表单
const resetForm = () => {
  areaForm.value = {
    id: '',
    remarks: '', // 备注
    positionName: '', // 岗位名称
    subSystemId: '', // 分系统id
  }
}

// 初始化对话框
const initDialog = (dialogStatusValue: string, row: any) => {
  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, // 编号
      remarks: row.remarks,
      positionName: row.positionName,
      subSystemId: row.subSystemId,
    }
  }
}
defineExpose({
  initDialog,
})

// 新增数据
const createData = () => {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      addPost(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) {
      updatePost(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
}
</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-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="岗位名称" prop="positionName">
            <el-input v-model.trim="areaForm.positionName" type="text" placeholder="岗位名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model.trim="areaForm.remarks" type="text" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="saveData">
          保存
        </el-button>
        <el-button @click="cancel">
          取消
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

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

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

