// 资料审核列表
<template>
  <div>
    <data-table operateBtnLayout
                :columns="columns"
                :sourceData.sync="sourceData"
                :showPage="false"
                :spanMethod="spanMethod">
      <!-- 线上退回次数 -->
      <template slot="headButton">
        <div class="head_button_right">
          <span>{{'线上退回次数' + returnTimes + '次'}}</span>
        </div>
      </template>
      <!-- 操作栏 -->
      <el-table-column align="center" :label="$t('factoringBusinessInfo.review_record')" width="100px">
        <!-- 详情 -->
        <template slot-scope="scope">
          <auth-button size="small"
                    type="primary"
                    :label="$t('common.detail')"
                    @click="showDetail(scope.row,scope.$index)"
                    :menuCode="menuId"
                    perms="detail">
        </auth-button>
        </template>
      </el-table-column>
    </data-table>
    <reviewDetailRecord :visible.sync="visible" editModel="V" :form="form" :father="father" :indexRow="indexRow" ></reviewDetailRecord>
  </div>
</template>
<script>
import { DataTable, authButton } from 'newepoch-components'
import reviewDetailRecord from '../reviewDetailRecord'
import taskManageApi from '@/api/dataAudit/taskManageApi'
import autoApi from '@/api/autoApi'
export default {
  components: {
    DataTable,
    authButton,
    reviewDetailRecord
  },
  props: {
    orderList: {
      type: Object,
      default () {
        return {}
      }
    },
    orderInfo: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    const { factoringBusinessInfo } = this.$i18n.messages[this.$i18n.locale]
    return {
      father: [],
      queryParas: {
        assetInfoId: null
      },
      queryInfo: {
        assetsId: null
      },
      form: {},
      api: {},
      menuId: '',
      visible: false,
      sourceData: [],
      columns: [
        // 审核流程
        {
          prop: 'taskType',
          align: 'left',
          width: '110px',
          label: factoringBusinessInfo.review_process
        },
        // 审核轮数
        {
          prop: 'auditRound',
          align: 'center',
          width: '110px',
          label: factoringBusinessInfo.review_times
        },
        // 审核人员
        {
          prop: 'auditorName',
          align: 'center',
          width: '110px',
          label: factoringBusinessInfo.review_persons
        },
        // 返回次数
        {
          prop: 'effectiveNumber',
          align: 'center',
          width: '110px',
          label: factoringBusinessInfo.feedback_times
        }
      ],
      sortIndex: [3, 0, 0, 2, 0],
      addColumn: '',
      arr: [],
      arrLine: [],
      returnTimes: 0,
      indexRow: ''
    }
  },
  mounted () {
    this.queryParas.assetInfoId = this.orderList.assetInfoId
    this.queryInfo.assetsId = this.orderInfo.assetsId
  },
  methods: {
    // 合并行
    spanMethod ({ row, column, rowIndex, columnIndex }) {
      let $this = this
      let spanCount = 1
      if (columnIndex === 0) {
        if (rowIndex === 0 || $this.sourceData[rowIndex - 1].taskType !== row.taskType) {
          $this.sourceData.reduce((last, current) => {
            if (last.taskType === row.taskType && last.taskType === current.taskType) {
              spanCount++
            }
            return current
          })
          return {
            rowspan: spanCount,
            colspan: 1
          }
        }
        return {
          rowspan: 0,
          colspan: 0
        }
      }
    },
    // 资料审核详情
    showDetail (row, index) {
      this.father = row.dialogValue
      this.indexRow = row.taskType.replace(/[^0-9]/ig, '')
      this.visible = true
    },

    // 线下审核
    handleFilter () {
      taskManageApi.outlineAudit.dataReviewList(this.queryParas).then(res => {
        if (res && res.data) {
          let arrayAudit = res.data.itemCompanyAuditDetailsDtoList
          arrayAudit.forEach((value, index) => {
            value.roundDetailDtos.forEach(item => {
              let dialogArr = []
              let dialogValue = []
              item.offlineDetailDtos.forEach(content => {
                dialogArr.push({
                  fileName: content.fileName,
                  auditOpinions: content.auditOpinions
                })
              })
              dialogValue.push({
                auditorName: item.auditorName,
                auditTime: item.auditTime,
                isBack: '否',
                auditRound: item.auditRound,
                sourceData: dialogArr
              })
              this.arr.push({
                taskType: '项目公司资料线下审核' + '（第' + (index + 1) + '次审核）',
                auditRound: '线下第' + (item.auditRound) + '轮',
                auditorName: item.auditorName,
                effectiveNumber: '0',
                status: '2',
                dialogValue: dialogValue
              })
            })
          })
          this.sourceData = this.arrLine.concat(this.arr)
        }
      })
    },

    // 线上审核
    handerData () {
      this.sourceData = []
      this.arrLine = []
      this.arr = []
      this.returnTimes = 0
      autoApi.commitApi.commitData(this.queryInfo).then(res => {
        let arrayCommit = res.data.timeInfoList
        arrayCommit.forEach((value, index) => {
          this.returnTimes += value.returnTimes
          let dialogValue = []
          value.detailList.forEach(item => {
            let dialogArrLine = []
            item.auditResultList.forEach(content => {
              dialogArrLine.push({
                fileName: content.fileName,
                auditOpinions: content.auditResult
              })
            })
            dialogValue.push({
              auditorName: item.auditUserName,
              auditTime: item.auditDate,
              isBack: item.returnFlag,
              auditRound: item.auditRound,
              sourceData: dialogArrLine,
              status: '1'
            })
          })
          this.arrLine.push({
            taskType: res.data.processName,
            auditRound: '线上第' + (value.auditTime) + '轮',
            auditorName: value.auditUserName,
            effectiveNumber: value.returnTimes,
            status: '1',
            dialogValue: dialogValue
          })
        })
        this.handleFilter()
      })
    },
    clearArr () {
      this.sourceData.length = 0
    }
  },
  watch: {
    'orderList.assetInfoId': {
      handler: function (val) {
        this.queryParas.assetInfoId = val
      },
      immediate: true
    },
    'orderInfo.assetsId': {
      handler: function (val) {
        this.queryInfo.assetsId = val
      },
      immediate: true
    }
  }
}
</script>
