<template>
    <data-table
            :border="true"
            :stripe="true"
            :api="api"
            @selectRowChange="selectRowChange"
            :loadCount.sync="loadCount"
            :columns="columns"
            :loadList="loadList"
            :loadingFlag="loadingFlag"
            @getTableData="transData"
            :queryParas="queryParas"
            :operateBtnLayout="''"
            :sourceData.sync="sourceData"
            :showSelection="true"
            :showPage="false">

        <template slot="operate" slot-scope="scope">
          <btn-upload :fileType="fileType" :classType="true" :fileID="scope.data.row.fileId" :icon="(scope.data.row.fileId !== '' && scope.data.row.fileId !== 0)?
        'iconfont iconzhongxinshangchuan' : ''" size="small" :rowData="scope.data.row" :uploadApi="uploadApi"  @uploadFailed="uploadFailed" @uploadSuccess="uploadSuccess"></btn-upload>
          <btn-download v-if="(scope.data.row.fileId && scope.data.row.fileId !== 0)" :fileID="scope.data.row.fileId"></btn-download>
            <btn-download v-if="scope.data.row.archiveId"
                          :circleIcon="'iconfont iconxiazaiwenjian'"
                          :fileID="scope.data.row.archiveId"
                          :title="$t('traceMgr.downloadArchive')">
            </btn-download>
          <el-tooltip v-if="(scope.data.row.fileId && scope.data.row.fileId !== 0)" class="item" effect="dark" :content="$t('common.del')" placement="top">
            <el-button @click="deleteFile(scope.data.row)" circle size="medium" class="iconfont iconshanchu12"></el-button>
          </el-tooltip>
        </template>
        <template slot="factoringCommissionerFeedback" slot-scope="scope">
            <el-input type="textarea"
                      :rows="1"
                      maxlength="253"
                      :placeholder="$t('common.MultilineInput')"
                      v-model="scope.data.row.factoringCommissionerFeedback"></el-input>
        </template>

    </data-table>
</template>

<script>

import { DataTable, btnDownload, btnUpload } from 'newepoch-components'
import tracemgrApi from '@/api/tracemgrApi'
import BasePage from '@/pages/basePage'
import * as format from '@/filters/index'
import { UPLOAD_API } from '@/constant.js'

export default {
  extends: new BasePage(),
  name: 'fileList',
  components: {
    DataTable,
    tracemgrApi,
    format,
    UPLOAD_API,
    btnDownload,
    btnUpload
  },
  data () {
    return {
      selectData: [],
      queryParas: {
        bpmId: this.bpmId,
        assetCode: this.assetCode,
        assetInfoId: this.assetInfoId,
        supplierId: this.supplierId
      },
      fileSuccess: this.$i18n.messages[this.$i18n.locale].traceMgr.fileSuccess,
      uploadFail: this.$i18n.messages[this.$i18n.locale].traceMgr.uploadFail,
      uploadApi: UPLOAD_API,
      loadList: 'getList',
      loadCount: 0,
      fileType: '.pdf,.png,.gif,.bmp,.jpg,.jpeg,.rar,.zip',
      tableData: {
        sourceData: []
      },
      columns: [
        // 任务类型
        {
          prop: 'taskType',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.taskType,
          width: '110px',
          format: function (row) {
            return row.taskType === 0 ? '' : format.formatDict(row.taskType + '', 'tracemgr_supplementary_task_type')
          }
        },
        // 补寄类型
        {
          prop: 'replacementFileType',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.supplementaryType,
          width: '110px',
          format: function (row) {
            return row.replacementFileType === 0 ? '' : format.formatDict(row.replacementFileType + '', 'tracemgr_supplementary_type')
          }
        },
        // 文件名称
        {
          prop: 'fileName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.fileName,
          width: '200px'
        },
        // 审核意见
        {
          prop: 'auditOpinion',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.auditOpinion,
          width: '200px'
        },
        // 补寄份数
        {
          prop: 'replacementCount',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.quantityOfSupplement,
          width: '100px',
          format: function (row) {
            return row.replacementCount < 0 ? 0 : row.replacementCount
          }
        },
        // 截至时间
        {
          prop: 'deadline',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].common.deadline,
          width: '180px'
        },
        // 操作
        {
          prop: 'operate',
          align: 'center',
          showOverflowTooltip: true,
          label: this.$i18n.messages[this.$i18n.locale].common.operate,
          width: '180px',
          slot: true
        },
        // 区域专员反馈
        {
          prop: 'factoringCommissionerFeedback',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.regionFeedback,
          width: '200px',
          slot: true
        },
        // 审核人员
        {
          prop: 'auditors',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.auditor,
          width: '110px'
        },
        // 处理状态
        {
          prop: 'status',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].traceMgr.processingStatus,
          width: '80px',
          format: function (row) {
            return row.status === 0 ? '' : format.formatDict(row.status + '', 'tracemgr_supplementary_process_status')
          }
        }
      ]
    }
  },
  props: {
    loadingFlag: {
      type: Boolean
    },
    sourceData: {
      type: Array
    },
    initData: {
      type: Boolean
    },
    supplierId: {
      type: String
    },
    bpmId: {
      type: String
    },
    assetCode: {
      type: String
    },
    instanceId: {
      type: String
    },
    assetInfoId: {
      type: String
    }
  },
  computed: {
    api () {
      return tracemgrApi['supplemenFileInPool']
    }
  },
  mounted: function () {
    // this.handleFilter()
  },
  watch: {
    initData (newValue) {
      if (newValue) {
        this.$nextTick(function () {
          // this.handleFilter()
        })
      }
    }
  },
  methods: {
    selectRowChange (selectedData) {
      // 任务状态  1:待处理  2:待盖电子章  3:待单证审核  4:补寄完成
      // 已完成的条目不允许选择，不允许编辑和上传文件
      if (selectedData.status === 4) {
        return this.error('当前记录已完成，不能执行该操作！')
      }
      this.$emit('selectRowChange', selectedData)
      // this.selectData = selectedData
    },
    transData (data) {
      this.tableData.sourceData = data.detailListDTOList
      this.$emit('submitData', data)
      for (var i = 0; i < data.detailListDTOList.length; i++) {
        if (data.detailListDTOList[i].replacementFileType === 1) {
          // 更新入池需补异常的状态
          let params = {
            assetInfoId: this.assetInfoId,
            abnormalType: 3,
            dealPersonType: 2,
            dealStatus: 2
          }
          tracemgrApi['fcTaskTablePage'].updateAbnormalOrderStatus(params).then(res => {
          })
          break
        }
      }

      // 更新资料补寄异常的状态
      let params = {
        assetInfoId: this.assetInfoId,
        abnormalType: 4,
        dealPersonType: 2,
        dealStatus: 2
      }
      tracemgrApi['fcTaskTablePage'].updateAbnormalOrderStatus(params).then(res => {
      })
    },
    uploadSuccess (returnData, rowData) {
      if (rowData.status === 4) {
        return this.error('当前记录已完成，不能执行该操作')
      }
      rowData.fileId = returnData.fileID
      this.success(returnData.fileName + this.fileSuccess)
    },
    uploadFailed (returnData) {
      this.error(returnData.fileName + this.uploadFail)
    },
    deleteFile: function (row) {
      row.fileId = 0
    }
  }
}
</script>
