<template>
    <div>
    <el-row class="marginBottom2">
      <el-col :span="18" class="box_border">
        <el-col class="lineHeight" :span="6">{{$t('traceMgr.auditor')}}&nbsp;&nbsp;&nbsp;&nbsp;{{auditor}}</el-col>
        <el-col class="lineHeight" :span="8">{{$t('traceMgr.checkTime')}}&nbsp;&nbsp;&nbsp;&nbsp;{{checkTime}}</el-col>
        <el-col class="lineHeight" :span="8">{{$t('traceMgr.feedbackTime')}}&nbsp;&nbsp;&nbsp;&nbsp;{{feedbackTime}}</el-col>
      </el-col>
        <el-col :span="6" class="textRight" style="margin-top: 10px">
            <btn-batch-upload class="marginRight" :show-file-list="true" :loading="loading" :btnDisplay="true" @uploadSuccess="batchUploadSuccess" @uploadFailed="uploadFailed" :uploadApi="batchUploadApi"></btn-batch-upload>
            <auth-button class="marginTop iconfont iconbaocun11" size="medium" type="primary" :label="$t('common.save')" @click="save" :loading="saveLoading"></auth-button>
        </el-col>
    </el-row>
    <data-table :handleFlag.sync="handleFlag" :showSelection="false" :toolMenuFlag="false" :showPage="false"
                :sourceData.sync="formTableData.sourceData" :menuFlag="true" operateBtnLayout="false"
                :columns="columns" :border="true" :stripe="true">
        <el-table-column align="center" :label="$t('common.operate')">
            <template slot-scope="scope">
                <btn-upload v-if="scope.row.fileId === '0'" :classType="true" size="small" :uploadApi="uploadApi" :rowData="scope.row" @uploadFailed="uploadFailed" @uploadSuccess="uploadSuccess"></btn-upload>
                <btn-upload v-if="scope.row.fileId !== '0'" :icon="'iconfont iconzhongxinshangchuan'" :fileID="scope.row.fileId" :classType="true" size="small" :uploadApi="uploadApi" :rowData="scope.row" @uploadFailed="uploadFailed" @uploadSuccess="uploadSuccess"></btn-upload>
                <btn-download v-if="scope.row.fileId !== '0'" :fileID="scope.row.fileId"></btn-download>
                <el-tooltip v-if="scope.row.fileId !== '0'" class="item" effect="dark" :content="$t('common.del')" placement="top">
                  <el-button @click="deleteFile(scope.row)" circle size="medium" class="iconfont iconshanchu12"></el-button>
                </el-tooltip>
            </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('traceMgr.regionFeedback') + '/' + $t('traceMgr.memo')">
            <template slot-scope="scope">
                <el-input type="textarea" v-model.trim="scope.row.factoringCommissionerFeedback" maxlength="254"></el-input>
            </template>
        </el-table-column>
    </data-table>
    </div>
</template>

<script>
import {
  DataTable,
  authButton,
  btnUpload,
  btnDownload,
  DialogEdit,
  speechCraftInput,
  btnBatchUpload,
  btnBatchDownload
} from 'newepoch-components'
import tracemgrApi from '@/api/tracemgrApi'
import businessFileDownloadDialog from '@/components/tracemgr/common/businessFileDownloadDialog'
import { UPLOAD_API, BATCH_UPLOAD_API } from '@/constant.js'
export default {
  components: {
    DataTable,
    authButton,
    tracemgrApi,
    DialogEdit,
    speechCraftInput,
    btnBatchUpload,
    btnBatchDownload,
    btnUpload,
    btnDownload,
    businessFileDownloadDialog
  },
  props: {
    formData: {
      type: Object,
      default () {
        return this.sourceData
      }
    },
    instanceId: {
      type: String
    },
    visible: {
      type: Boolean
    },
    assetsId: {
      type: String
    },
    taskId: {
      type: String
    }
  },
  mounted () {
    this.getOrderData()
  },
  computed: {
    api () {
      return tracemgrApi['problemList']
    }
  },
  data () {
    return {
      saveLoading: false,
      whetherSave: false,
      loading: false,
      batchUploadApi: BATCH_UPLOAD_API,
      uploadApi: UPLOAD_API,
      businessForm: {},
      businessVisible: false,
      auditor: '',
      checkTime: '',
      feedbackTime: '',
      content: '',
      handleFlag: true,
      saveData: [],
      saveTableData: [],
      formTableData: {
        sourceData: []
      },
      columns: [
        {
          prop: 'fileName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.fileName,
          showOverflowTooltip: true
        },
        {
          prop: 'checkResult',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].approval.opinions,
          showOverflowTooltip: true
        }
      ]
    }
  },
  watch: {
    'formTableData.sourceData': {
      deep: true,
      handler: function (newVal, oldVal) {
        if (newVal) {
          this.$emit('sourceData', this.formTableData.sourceData)
        }
      }
    },
    visible: {
      handler: function (newVal, oldVal) {
        if (newVal) {
          this.getOrderData()
        }
      }
    }
  },
  methods: {
    save: function () {
      this.saveLoading = true
      let saveData = []
      let $this = this
      if (this.formTableData.sourceData.length > 0) {
        this.formTableData.sourceData.forEach((item, index) => {
          let data = {}
          data['taskInfoId'] = $this.taskId
          data['fileCode'] = item.fileCode
          data['fileId'] = item.fileId
          data['factoringCommissionerFeedback'] = item.factoringCommissionerFeedback
          data['fileName'] = item.fileName
          saveData.push(data)
        })
        tracemgrApi['problemList'].save(saveData).then(res => {
          if (res.data) {
            this.saveLoading = false
            this.success(this.$i18n.messages[this.$i18n.locale].common.saveSuccess)
            this.whetherSave = true
            this.saveTableData = []
            for (let i = 0; i < this.formTableData.sourceData.length; i++) {
              this.saveTableData.push(JSON.parse(JSON.stringify(this.formTableData.sourceData[i])))
            }
            this.$emit('whetherSave', this.whetherSave)
            this.$emit('saveTableData', this.saveTableData)
          } else {
            this.error(this.$i18n.messages[this.$i18n.locale].common.saveError)
            this.saveLoading = false
          }
        })
      } else {
        this.saveLoading = false
        this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.noSaveData)
      }
    },
    uploadFailed: function (a, b, c) {
      this.error(this.$i18n.messages[this.$i18n.locale].traceMgr.fileEorro)
    },
    uploadSuccess (returnData, rowData) {
      // let data = {}
      // data['fielId'] = returnData.fileID
      // data['taskInfoId'] = this.taskId
      // data['fileCode'] = rowData.detailList.fileCode
      //
      // tracemgrApi['problemList'].upload(data).then(res => {
      //
      // })
      rowData.fileId = returnData.fileID
      // rowData.fileName = returnData.fileName
      this.success(returnData.fileName + this.$i18n.messages[this.$i18n.locale].traceMgr.fileSuccess)
    },
    batchUploadSuccess (returnData) {
      for (let i = 0; i < this.formTableData.sourceData.length; i++) {
        for (let j = 0; j < returnData.fileList.length; j++) {
          let fileCode = returnData.fileList[j].fileName
          if (fileCode.substring(0, fileCode.lastIndexOf('.')) === this.formTableData.sourceData[i].fileName) {
            this.formTableData.sourceData[i].fileId = returnData.fileList[j].keyId
            break
          }
        }
      }
      this.success(this.$i18n.messages[this.$i18n.locale].traceMgr.uploadMessage1 + returnData.fileName + this.$i18n.messages[this.$i18n.locale].traceMgr.uploadMessage10)
    },
    closeDownBatch () {
      this.businessVisible = !this.businessVisible
    },
    getOrderData () {
      tracemgrApi['problemList'].getList({ 'instanceId': this.assetsId, 'taskId': this.taskId }).then(res => {
        if (res.success) {
          this.auditor = res.data.examineUserName
          if (res.data.examineDate !== null && res.data.examineDate !== '' && res.data.examineDate !== undefined) {
            this.checkTime = this.dateFormat(res.data.examineDate)
          }
          if (res.data.feedbackDate !== null && res.data.feedbackDate !== '' && res.data.feedbackDate !== undefined) {
            this.feedbackTime = this.dateFormat(res.data.feedbackDate)
          }
          this.formTableData.sourceData = res.data.detailList
        }
      })
    },
    dateFormat (val) {
      let date = new Date(val)
      let y = date.getFullYear()
      let MM = date.getMonth() + 1
      MM = MM < 10 ? ('0' + MM) : MM
      let d = date.getDate()
      d = d < 10 ? ('0' + d) : d
      let h = date.getHours()
      h = h < 10 ? ('0' + h) : h
      let m = date.getMinutes()
      m = m < 10 ? ('0' + m) : m
      let s = date.getSeconds()
      s = s < 10 ? ('0' + s) : s
      return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
    },
    deleteFile: function (row) {
      row.fileId = '0'
      // tracemgrApi['fileList'].deleteFile({ 'fileId': fileId }).then(res => {
      //   if (res.data) {
      //     fileId = res.data.fileId
      //     this.success(this.$i18n.messages[this.$i18n.locale].common.delSuccess)
      //   } else {
      //     this.error(this.$i18n.messages[this.$i18n.locale].common.delFailure)
      //   }
      // }).catch(res => {
      //   this.error(this.$i18n.messages[this.$i18n.locale].common.delFailure)
      // })
    }
  }
}
</script>

<style scoped>
  @import '~@/styles/tracemgr/factoringCommissioner/taskTable/onlineAudit/taskDialog.scss'
</style>
