<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="150px" @submit.native.prevent>
        <el-row  :gutter="15">

          <el-col :span="21">
            <el-form-item label="分享类型" prop="klgsType"
                          :rules="[
                          {required: true, message:'分享类型不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.klgsType" placeholder="请填写分享类型"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="分析类型key值" prop="klgsKey"
                          :rules="[
                          {required: true, message:'分享类型key值不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.klgsKey" placeholder="请填写分析类型key值"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="排序" prop="klgsSort"
                          :rules="[
                          {required: true, message:'排序不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.klgsSort" oninput="value=value.replace(/[^\d]/g,'')" 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 KnowledgeShareTypeService from '@/api/sys/KnowledgeShareTypeService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          klgsType: '',
          klgsKey: '',
          klgsSort: ''
        }
      }
    },
    knowledgeShareTypeService: null,
    created () {
      this.knowledgeShareTypeService = new KnowledgeShareTypeService()
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          klgsType: '',
          klgsKey: '',
          klgsSort: ''
        }
        if (method === 'add') {
          this.title = `新建知识分享类型`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改知识分享类型'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看知识分享类型'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$refs.inputForm.resetFields()
            this.knowledgeShareTypeService.findById(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.knowledgeShareTypeService.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>
