<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="[
                          {required: true, message:'职位不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写职位"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="职位编码" prop="no"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.no" placeholder="请填写职位编码"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="所属部门" prop="officeId" :rules="[
                          {required: true, message:'部门不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.officeId" placeholder="请选择"  style="width: 100%;">
                <el-option
                  v-for="item in this.officeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="编制人数" prop="staffNum"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.staffNum" placeholder="请填写编制人数"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="对应职务" prop="positionId">
              <el-select v-model="inputForm.positionId" placeholder="请选择"  style="width: 100%;">
                <el-option
                  v-for="item in this.positionList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="任职资格" prop="qualification"
                          :rules="[
                 ]">
              <el-input type="textarea" maxlength="1000" v-model="inputForm.qualification" placeholder="请填写任职资格"  show-word-limit   ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="责任描述" prop="obligation"
                          :rules="[
                 ]">
              <el-input type="textarea" maxlength="1000" v-model="inputForm.obligation" placeholder="请填写责任描述"  show-word-limit   ></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 JobPositionService from '@/api/position/JobPositionService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          name: '',
          no: '',
          officeId: '',
          staffNum: '',
          positionId: '',
          qualification: '',
          obligation: '',
          useFlag: ''
        },
        officeList: [],
        positionList: []
      }
    },
    jobPositionService: null,
    created () {
      this.jobPositionService = new JobPositionService()
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          name: '',
          no: '',
          officeId: '',
          staffNum: '',
          positionId: '',
          qualification: '',
          obligation: '',
          useFlag: ''
        }
        this.inputForm.id = id
        this.jobPositionService.queryAllOffice().then(({data}) => {
          this.officeList = data
        })
        this.jobPositionService.queryAllPosition().then(({data}) => {
          this.positionList = data
        })
        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.jobPositionService.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.jobPositionService.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>
