<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="parentId">
              <SelectTree
                ref="areaTree"
                :props="{
                      value: 'id',             // ID字段名
                      label: 'name',         // 显示名称
                      children: 'children'    // 子级字段名
                    }"
                url="/proofread/type/treeData?type=2"
                :value="inputForm.parentId"
                :clearable="true"
                :accordion="true"
                @getValue="(value) => {inputForm.parentId=value}"/>
            </el-form-item>
          </el-col>
          <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-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 ProofreadTypeService from '@/api/sys/ProofreadTypeService'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          name: '',
          parentId: '',
          type: ''
        }
      }
    },
    proofreadTypeService: null,
    created () {
      this.proofreadTypeService = new ProofreadTypeService()
    },
    components: {
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          name: '',
          parentId: '',
          type: ''
        }
        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 = '查看自检类型'
        } else if (method === 'addChild') {
          this.title = '添加下级结构'
          this.inputForm.parentId = id
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$refs.inputForm.resetFields()
            this.proofreadTypeService.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.inputForm.type = '2'
            this.proofreadTypeService.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>
