<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="postForm" label-position="right" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="岗位名称" prop="name">
            <el-input v-model.trim="postForm.name" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="岗位类别" prop="type">
            <el-select v-model="postForm.type" placeholder="请选择">
              <el-option
                v-for="item in postTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="岗位职责" prop="duty">
            <el-input v-model.trim="postForm.duty" type="text" placeholder="非必填"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getPostType, addPost, updatePost } from '@/api/system/post'

export default {
  name: 'EditPost',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      isEditMode: true,
      postForm: {
        id: '', // id
        name: '', // 岗位名称
        type: '', // 岗位类别
        duty: '' // 岗位职责
      }, // 表单
      postTypeList: [], // 岗位树列表数据
      textMap: {
        update: '编辑',
        create: '新增'
      }, // 表头显示标题
      btnLoading: false, // 保存按钮的加载中状态
      rules: {
        name: [{ required: true, message: '岗位名称不能为空', trigger: ['blur', 'change'] }],
        type: [{ required: true, message: '岗位类别不能为空', trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  created() {
    this.fetchPostType()
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      this.btnLoading = false
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.postForm = {
          id: row.id, // id
          name: row.name, // 岗位名称
          type: row.type, // 岗位类别
          duty: row.duty // 岗位职责
        }
        this.isEditMode = true
      }
    },
    // 获取岗位类型
    fetchPostType() {
      getPostType().then(response => {
        this.postTypeList = response.data
      })
    },
    // 重置表单
    resetForm() {
      this.postForm = {
        id: '', // id
        name: '', // 岗位名称
        type: '', // 岗位类别
        duty: '' // 岗位职责
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.postForm)
        if (valid) {
          this.btnLoading = true
          addPost(this.postForm).then(response => {
            if (response.code === 200) {
              this.$message.success('新增成功')
              this.resetForm()
              this.btnLoading = false
              this.$nextTick(() => {
                this.$refs['dataForm'].clearValidate()
              })
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          }).catch(_ => { // 异常情况，loading置为false
            this.btnLoading = false
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          updatePost(this.postForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$refs['dataForm'].clearValidate()
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          }).catch(_ => { // 异常情况，loading置为false
            this.btnLoading = false
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-dialog{
    width:700px
  }
  .el-select{
    width: 100%;
  }
</style>
