<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="岗位名称" prop="postName">
        <el-input v-model="form.postName" placeholder="请输入岗位名称" />
      </el-form-item>
      <el-form-item label="岗位编号" prop="postCode">
        <el-input v-model="form.postCode" placeholder="请输入岗位编号" />
      </el-form-item>
      <el-form-item label="岗位顺序" prop="sort">
        <el-input-number
          v-model="form.sort"
          controls-position="right"
          :min="0" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio-group v-model="form.status">
            <el-radio label="10">正常</el-radio>
            <el-radio label="20">停用</el-radio>
          </el-radio-group>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          placeholder="请输入内容"
          type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import postApi from '@/api/system/post'

  export default {
    name: 'PostEdit',
    data() {
      return {
        form: { status: '10' },
        rules: {
          postName: [
            { required: true, message: '岗位名称不能为空', trigger: 'blur' },
          ],
          postCode: [
            { required: true, message: '岗位编码不能为空', trigger: 'blur' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              const { msg } = await postApi.doEdit(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              const { msg } = await postApi.doAdd(this.form)
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
