<template>
  <div class="uploadFile">
    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" :before-close="handleClose" :append-to-body="true"
      :close-on-click-modal="false" label-width="300px">
      <el-form :model="form" status-icon :rules="rules" ref="form" label-width="120px" class="demo-ruleForm">

        <el-form-item label="流程：" prop="processId">
          <el-select v-model="form.processId" placeholder="请选择流程" style="width: 100%;">
            <el-option v-for="item in pathOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件：" prop="delivery">
          <myUpload :file-list="form.systemFileList" @on-change="onchangeFile" @on-remove="onRemove" style="height: 185px;" :autoUpload="true"></myUpload>
        </el-form-item>
        <el-form-item label="最终文件：" prop="ifFinal" style="width: 20px;">
          <el-switch v-model="form.ifFinal" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </el-form-item>
        <el-form-item label="说明：">
          <el-input v-model="form.description" type="textarea" placeholder="请输入说明内容"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose" size="small">取 消</el-button>
        <el-button @click="submit" size="small" type="primary">直接上传</el-button>
        <el-button v-if="!form.ifFinal && !form.id" @click="examine" size="small" type="primary">提交审核</el-button>
      </span>
    </el-dialog>

    <examine ref="examine" @save="handleExamineFile"></examine>
  </div>
</template>

<script>
import { optionsData } from '../mixins/index2'
import examine from '@/views/components/dialog/examine.vue'

export default {
  mixins: [optionsData],
  components: {
    examine,
  },
  props: ['processId'],
  data() {
    return {
      dialogVisible: false,
      title: '',
      form: {
        systemFileList: [],
        ifFinal: false,
      },
      rules: {
        processId: [{ required: true, message: '请选择流程', trigger: 'change' },],
      },

    };
  },
  watch: {},
  computed: {},
  created() { },
  mounted() { },
  methods: {
    // 提交审核任务
    handleExamineFile(val) {
      let patentDigProjectFilesDTO = {
        ...this.form,
        projectTaskDTO: val,
      }
      this.$api.addPDProjectFilesTask(patentDigProjectFilesDTO).then(res => {
        if (res.code == 200) {
          this.$emit('isSuccess', '新增成功')
          this.$refs.examine.handleCloseTask()
          this.handleClose()
        }
      })
    },
    //提交审核
    examine() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 判断文件是否都上传完毕
          this.$commonJS.allUploadFile(this.form)
          this.$refs.examine.open()
        }
      })
    },

    //打开弹窗
    open(row, id) {
      if (row && row.id) {
        this.form = JSON.parse(JSON.stringify(row))
        if (!this.form.systemFileList) {
          this.$set(this.form, 'systemFileList', [])
        }
        this.title = '编辑文件'
      } else {
        if (this.processId) {
          this.$set(this.form, 'processId', this.processId)
        }
        this.title = '上传文件'
      }
      this.form.projectId = id//项目id
      this.dialogVisible = true
    },
    //关闭弹窗
    handleClose() {
      this.$refs.form.resetFields()
      this.form = {
        systemFileList: [],
      }
      this.dialogVisible = false
    },
    //提交数据
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 判断文件是否都上传完毕
          this.$commonJS.allUploadFile(this.form)
          if (!this.form.id) {
            this.$api.addPatentDigProjectFiles(this.form).then(res => {
              if (res.code == 200) {
                this.$message.success('新增成功')
                this.$emit('isSuccess', '新增成功')
                this.handleClose()
              }
            })
          }
        } else {
          this.$message.error('信息未输入完整')
        }
      });
    },
    // 上传的文件监听
    onchangeFile(file, fileList) {
      if (file.guid) {
        let index = this.form.systemFileList.findIndex(item => {
          return item.uid == file.uid
        })
        if (index != -1) {
          this.form.systemFileList.splice(index, 1, file)
        }
      } else {
        this.form.systemFileList.push(file.raw)
      }

    },
    // 删除上传的文件
    onRemove(file, fileList) {
      let index = this.form.systemFileList.findIndex(item => {
        return item.uid == file.uid
      })
      if (index != -1) {
        this.form.systemFileList.splice(index, 1)
      }
    },

  },

};
</script>
<style lang="scss">
.uploadFile {
  .el-dialog__body {
    padding: 0px 20px;
  }
}
</style>
<style lang="scss" scoped></style>