<template>
  <!-- 处理挖掘任务2（文件分配任务弹窗） -->
  <div class="handleTask2">
    <el-dialog ref="dialog" :title="title" :visible.sync="dialogVisible" width="900px" :before-close="handleClose"
      :close-on-click-modal="false">

      <el-form :model="form" status-icon :rules="rules" ref="form" label-position="top" label-width="120px"
        class="demo-form">
        <el-form-item label="附件（已保存）：">
          <el-table class="elTable" :data="tableData" border style="width: 100%"
            header-row-class-name="custom-table-header">
            <el-table-column prop="name" label="文件名称" align="center"></el-table-column>
            <el-table-column prop="time" label="文件上传时间" align="center"> </el-table-column>
            <el-table-column prop="fileType" label="文件类型" align="center"> </el-table-column>
            <el-table-column prop="description" label="说明" align="center"> </el-table-column>
            <el-table-column label="操作" align="center" width="140">
              <el-button @click="deleteFile" size="small" type="text" style="color: red;">删除</el-button>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="附件（未保存）：" prop="systemFileList">
          <myUpload :file-list="form.systemFileList" @on-change="onchangeFile" @on-remove="onRemove"
            style="height: 180px;" :autoUpload="true"></myUpload>
        </el-form-item>
        <el-form-item label="说明：">
          <el-input type="textarea" :rows="2" placeholder="请输入说明内容" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>




      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose" size="small">取 消</el-button>
        <el-button @click="handleSave" size="small" type="primary">保 存</el-button>
        <el-button @click="submit" size="small" type="primary">提交审核</el-button>

      </span>
    </el-dialog>

    <examine ref="examine" @taskForm="handleTaskForm"></examine>
  </div>
</template>

<script>
import examine from '@/views/components/dialog/examine.vue'
export default {
  components: {
    examine,
  },
  props: {},
  data() {
    return {
      title: '',
      tableData: [],
      form: {
        systemFileList: [],
      },
      rules: {
        // systemFileList: [{ required: true, message: '请选择附件', trigger: 'blur' },],
      },
      dialogVisible: false,
      // auditTask: false,
      // 备注
      textarea: '',
      // 是否是处理任务？
      isLook: false,
      row: null,
    };
  },
  watch: {},
  computed: {},
  created() { },
  mounted() { },
  methods: {

    // 上传的文件监听
    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)
      }
    },
    //打开弹窗
    open(row, val) {
      // let row = JSON.parse(JSON.stringify(row))
      // if(this.form.id){
      //     this.title = '编辑任务'
      // }else{
      //     this.title = '创建任务'
      // }
      this.row = row
      this.isLook = val
      this.title = '任务名称：' + row.name
      // this.getTableList()
      this.dialogVisible = true
    },
    // 查询已保存的项目
    getTableList() {
      let params = {
        id: this.row.id
      }
      this.$api.getTableList(params).then(res => {
        if (res.code == 200) {
          this.tableData = res.data
        }
      })
    },
    // 删除文件
    deleteFile(row) {
      this.deleteFiles([row.id])
    },
    deleteFiles(ids) {
      this.$api.dele(ids).then(res => {
        if (res.code == 200) {
          this.getTableList()
          this.$message.success('删除已保存的文件成功')
        }
      })
    },
    //关闭弹窗
    handleClose() {
      this.dialogVisible = false
    },
    // 保存
    handleSave() {
      // 判断文件是否都上传完毕
      if (this.form.systemFileList && this.form.systemFileList.length > 0) {
        this.form.fileGuids = []
        for (let i = 0; i < this.form.systemFileList.length; i++) {
          if (this.form.systemFileList[i].guid) {
            this.form.fileGuids.push(this.form.systemFileList[i].guid)
          } else {
            this.$message.error('文件未全部上传，请耐心等待')
            return false
          }
        }
      } else {
        return false
      }
      this.form.id=this.row.id
      this.$api.add(this.form).then(res => {
        if (res.code == 200) {
          this.$message.success('保存文件成功')
          this.handleClose()
        }
      })
    },
    //提交审核
    submit() {
      this.$refs.form.validate((valid) => { 
        if (valid) {
          this.$refs.examine.open(this.form,6)
        }
      })
    },
    // 审核弹窗发送的值
    handleTaskForm(val) {
      this.$emit('isSuccess', '新增成功')
      this.handleClose()
    },
  },

};
</script>
<style lang="scss">
.handleTask2 {
  .el-dialog__body {
    padding: 10px 20px;
  }
}
</style>
<style lang="scss" scoped>
.handleTask2 {
  .handleTask2Dialog {
    background: red;
  }

  .upload-demo {
    float: right;
    margin-bottom: 10px;
  }

  .explain {
    margin-top: 10px;

    span {
      font-size: 16px;
    }
  }
}
</style>