<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'||method==='viewChild'?'readonly':''"  :disabled="method==='view'||method==='viewChild'"
               label-width="120px" @submit.native.prevent>
        <el-row  :gutter="15">
          <el-col v-if="method === 'addChild'||method === 'editChild'||method === 'viewChild'" :span="21">
            <el-form-item label="附件类型" prop="parentId"
                          :rules="[
                          {required: true, message:'附件类型不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.parentId" style="width:100%" placeholder="请选择附件类型">
                <el-option
                  v-for="item in fileTypes"
                  :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="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="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" v-if="method === 'addChild'||method === 'editChild'||method === 'viewChild'">
            <el-form-item label="文件类型" prop="type"
                          :rules="[
                          {required: true, message:'文件类型不能为空', trigger:'blur'},{required: true, message:'文件类型不能为空', trigger:'change'}
                 ]">
              <el-select v-model="inputForm.type" collapse-tags style="width:100%" multiple placeholder="请选择文件类型">
                <el-checkbox style="margin-left: 20px;" v-model="checked" @change='selectAll'>全选</el-checkbox>
                <el-option
                  v-for="item in $dictUtils.getDictList('file_type')"
                  :key="item.label"
                  :label="item.label"
                  :value="item.label">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="21" v-if="method === 'addChild'||method === 'editChild'||method === 'viewChild'">
            <el-form-item label="文件大小(MB)" prop="size"
                          :rules="[
                          {required: true, message:'文件大小不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.size" 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'&&method != 'viewChild'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
			</span>
		</template>

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

<script>
  import ProgramFileDictService from '@/api/program/ProgramFileDictService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          name: '',
          size: '',
          type: [],
          sort: '',
          remarks: '',
          parentId: ''
        },
        fileTypes: [],
        check: false
      }
    },
    programFileDictService: null,
    created () {
      this.programFileDictService = new ProgramFileDictService()
    },
    methods: {
      init (method, id) {
        this.method = method
        this.getAllFileType()
        this.inputForm = {
          name: '',
          size: '',
          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
        } else if (method === 'editChild') {
          this.title = '修改附件结构'
          this.inputForm.id = id
        } else if (method === 'viewChild') {
          this.title = '查看附件结构'
          this.inputForm.id = id
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view' || method === 'editChild' || method === 'viewChild') { // 修改或者查看
            this.loading = true
            this.programFileDictService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.inputForm.type = this.inputForm.type.split(',')
              this.loading = false
            })
          }
          if (method === 'addChild') {
            this.inputForm.parentId = id
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (!this.commonJS.isEmpty(this.inputForm.type)) {
              this.inputForm.type = this.inputForm.type.join(',')
            } else {
              this.inputForm.type = ''
            }
            this.programFileDictService.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.$refs.inputForm.parentId = ''
        this.checked = false
        this.visible = false
      },
      getAllFileType () {
        this.programFileDictService.getAllFileType().then((data) => {
          this.fileTypes = data
        })
      },
      selectAll () {
        this.inputForm.type = []
        if (this.checked) {
          this.$dictUtils.getDictList('file_type').forEach((item) => {
            this.inputForm.type.push(item.label)
          })
        } else {
          this.inputForm.type = []
        }
      }
    }
  }
</script>
