<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="500px"
      @close="close"
      @keyup.enter.native="doSubmit"
      :visible.sync="visible">
      <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
               label-width="100px" @submit.native.prevent>
        <el-row  :gutter="15">
          <el-col :span="21">
            <el-form-item label="职务分类" prop="name"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写职务分类"     ></el-input>
            </el-form-item>
          </el-col>


        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="close()" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import PositionCategoryService from '@/api/position/PositionCategoryService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          name: ''
        }
      }
    },
    positionCategoryService: null,
    created () {
      this.positionCategoryService = new PositionCategoryService()
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          name: ''
        }
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建职务分类`
        } else if (method === 'edit') {
          this.title = '修改职务分类'
        } else if (method === 'view') {
          this.title = '查看职务分类'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.positionCategoryService.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.positionCategoryService.save(this.inputForm).then(({data}) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.inputForm.resetFields()
        this.visible = false
      }
    }
  }
</script>
