<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
	  draggable
      width="500px"
      @close="close"
      @keyup.enter.native="doSubmit"
	  v-model="visible">
      <el-form size="large" :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="type"
                          :rules="[
                          {required: true, message:'项目类型不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.type" placeholder="请填写项目类型"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="排序" prop="sort"
                          :rules="[
                          {required: true, message:'排序不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="备注" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" maxlength="200" v-model="inputForm.remarks" placeholder="请填写备注"  show-word-limit   ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
		<template #footer>
			<span  class="dialog-footer">
			  <el-button  @click="close()" icon="el-icon-circle-close">关闭</el-button>
			  <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
			</span>
		</template>

    </el-dialog>
  </div>
</template>

<script>
  import ProgramTypeDictService from '@/api/program/ProgramTypeDictService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          type: '',
          sort: '',
          remarks: '',
          parentId: ''
        }
      }
    },
    programTypeDictService: null,
    created () {
      this.programTypeDictService = new ProgramTypeDictService()
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          type: '',
          sort: '',
          remarks: '',
          parentId: ''
        }
        if (method === 'add') {
          this.title = `新建项目类型`
          this.inputForm.parentId = '0'
        } 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(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.programTypeDictService.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.programTypeDictService.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>
