<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="fileId"
                          :rules="[
                          {required: true, message:'附件名称不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.fileId" placeholder="请选择附件名称" style="width:100%;" clearable>
                <el-option
                  v-for="item in fileDictList"
                  :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="requiredStage"
                          :rules="[
                 ]">
              <el-select v-model="inputForm.requiredStage" placeholder="请选择必填阶段" style="width:100%;" clearable>
                <el-option
                  v-for="item in $dictUtils.getDictList('program_required_stage')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </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 ProgramTypeFileDictService from '@/api/program/ProgramTypeFileDictService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          fileId: '',
          requiredStage: '',
          attachmentProjectType: '',
          attachmentProjectSort: ''
        },
        fileDictList: []
      }
    },
    programTypeFileDictService: null,
    created () {
      this.programTypeFileDictService = new ProgramTypeFileDictService()
    },
    methods: {
      init (method, id, attachmentProjectType, attachmentProjectSort, fileId) {
        this.method = method
        this.getFileList(attachmentProjectType, attachmentProjectSort, fileId)
        this.inputForm = {
          fileId: '',
          requiredStage: '',
          attachmentProjectType: '',
          attachmentProjectSort: ''
        }
        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(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.attachmentProjectType = attachmentProjectType
          this.inputForm.attachmentProjectSort = attachmentProjectSort
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.programTypeFileDictService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              if (this.inputForm.requiredStage === '0') {
                this.inputForm.requiredStage = ''
              }
              this.inputForm.attachmentProjectType = attachmentProjectType
              this.inputForm.attachmentProjectSort = attachmentProjectSort
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (this.commonJS.isEmpty(this.inputForm.requiredStage)) {
              this.inputForm.requiredStage = '0'
            }
            this.programTypeFileDictService.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
      },
      getFileList (attachmentProjectType, attachmentProjectSort, fileId) {
        this.programTypeFileDictService.getFileList({'attachmentProjectType': attachmentProjectType, 'attachmentProjectSort': attachmentProjectSort, 'fileId': fileId}).then((data) => {
          this.fileDictList = data
        })
      }
    }
  }
</script>
