<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"
            @on-preview="onPreview" 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" @taskForm="handleTaskForm" @examineFile="handleExamineFile"></examine>
  </div>
</template>

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

export default {
  mixins: [optionsData, handleJs],
  components: {
    examine,
  },
  props: ['pathObj'],
  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.getList()
          this.$refs.examine.close()
          this.handleClose()
        }
      })
    },
    //提交审核
    examine() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var allUpload = this.validFile()
          if (allUpload) {
            this.$message.warning('文件未全部上传，请耐心等待')
            return false
          }
          this.form.fileGuids = this.form.systemFileList.map(item => {
            return item.guid
          })
          this.$refs.examine.open(null, 7)
        }
      })
    },
    // 审核弹窗发送的值
    handleTaskForm(val) {
      this.$emit('isSuccess', '新增成功')
      this.handleClose()
    },
    //打开弹窗
    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.pathObj) {
          this.$set(this.form, 'processId', this.pathObj.pathId)
        }
        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) {
          // 判断文件是否都上传完毕
          var allUpload = this.validFile()
          if (allUpload) {
            this.$message.warning('文件未全部上传，请耐心等待')
            return false
          }
          this.form.fileGuids = this.form.systemFileList.map(item => {
            return item.guid
          })
          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)
      }
    },
    // 点击文件
    onPreview(file) { },
  },

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