<!-- 批量审核 -->
<template>
  <div class="traceAuditBatchPop">
    <externalTransition v-if="editModel === 'IS' && effectForm.notHandleQuestionDtoList && effectForm.notHandleQuestionDtoList.length > 0" :title="$t('effectDealFeedBackMain.pendIssue')">
    </externalTransition>
    <data-table v-if="effectForm.notHandleQuestionDtoList && effectForm.notHandleQuestionDtoList.length > 0" :showPage="showPage" :sourceData.sync="effectForm.notHandleQuestionDtoList" :columns="batchAuditColumns" v-bind="{spanMethod:e => objectSpanMethod(effectForm.notHandleQuestionDtoList,e)}" :row-class-name="rowClassName">
      <!-- 律所最新一次反馈意见 -->
      <template #lawFirmNewestFeedback="scope">
        <span-plus :index="scope.data.$index" :text="scope.data.row.lawFirmNewestFeedback"></span-plus>
      </template>
      <!-- 合规对相关方最新一次回复 -->
      <template #practicableSuggestion="scope">
        <span-plus :index="scope.data.$index" :text="scope.data.row.practicableSuggestion"></span-plus>
      </template>
      <!-- 相关方最新一次回复给合规 -->
      <template #relationresolution="scope">
        <span-plus :index="scope.data.$index" :text="scope.data.row.relationresolution"></span-plus>
      </template>
      <!-- 是否疑难问题 -->
      <template slot="difficultQustionFlag" slot-scope="scope">
        <el-checkbox :index="scope.data.$index"  v-model="scope.data.row.difficultQustionFlag" true-label="2" false-label="1"  @change="difficultQustionFlagChange(scope.data.row, scope.data.$index, scope.data.row.difficultQustionFlag)"></el-checkbox>
      </template>
      <!-- 是否关联问题 -->
      <template slot="relationQustionFlag" slot-scope="scope">
        <el-checkbox :disabled="scope.data.row.relationCommonDealFlag=== '2'" :index="scope.data.$index" v-model="scope.data.row.relationQustionFlag" true-label="2" false-label="1" @change="relationQustionFlagChange(scope.data.row, scope.data.$index, scope.data.row.relationQustionFlag)"></el-checkbox>
        <auth-button :disabled="scope.data.row.relationQustionFlag === '1'" size="small" class="iconCircle trace-button" :title="$t('common.select')" icon="iconfont iconxuanze" @click="relationQustClick(scope.data.row,scope.data.$index)"></auth-button>
      </template>
      <template #fileList="scope">
        <div v-if="scope.data.row.relationDealStatus === '2'">
          <btn-batch-download :disabled="scope.data.row.fileList.length === 0" :fileID="scope.data.row.fileList.length > 0 ? scope.data.row.fileList.map(item=>{return item.fileId}) : scope.data.row.fileList" :classType="false" :downloadFileName="scope.data.row.fileList.length > 1 ?$t('effectDealFeedBackMain.xgfFileList') + '.zip':null"></btn-batch-download>
          <el-popover
            class="box-column-margin-left"
            placement="right"
            width="200"
            trigger="hover">
            <div v-for="(item,index) in scope.data.row.fileList" :key="index">
              <el-link type="primary" @click="preView(getFile(item.fileName),item.fileId)">{{item.fileName}}</el-link>
            </div>
            <auth-button :disabled="scope.data.row.fileList.length === 0" slot="reference" size="small" class="iconCircle" :title="$t('common.preview')" icon="iconfont iconview"></auth-button>
          </el-popover>
        </div>
      </template>
      <!-- 操作 -->
      <el-table-column :label="$t('common.operate')" align="center" width="70px">
        <template slot-scope="scope">
          <!-- 审核 -->
          <auth-button v-if="scope.row.relationDealStatus === '2' && scope.row.status === $t('effectDealFeedBackMain.yesApproved')" size="small" class="iconCircle marginLeft" :disabled="scope.row.relationQustionFlag === '2'"
                      :title="$t('effectDealFeedBackMain.auditAgain')" icon="iconfont iconzhongxinshenhe" @click="examine(scope.row, scope.$index)"></auth-button>
          <auth-button v-if="scope.row.relationDealStatus === '2' && scope.row.status === $t('effectDealFeedBackMain.noApproved')" size="small" class="iconCircle marginLeft" :disabled="scope.row.relationQustionFlag === '2'"
                      :title="$t('common.audit')" icon="iconfont iconreview" @click="examine(scope.row, scope.$index)"></auth-button>
          <!-- 终止 -->
          <auth-button type="danger" v-if="scope.row.relationDealStatus === '1' && scope.row.status === $t('effectDealFeedBackMain.noTerminated')" size="small" class="iconCircle marginLeft pssc-css"
                      :title="$t('effectDealFeedBackMain.terminateTask')"
                      :disabled="scope.row.relationQustionFlag === '2'"
                      icon="iconfont iconjieshu" @click="terminateTask(scope.row, scope.$index)"></auth-button>
          <auth-button v-if="scope.row.relationDealStatus === '1' && scope.row.status === $t('effectDealFeedBackMain.yesTerminated')" size="small" class="iconCircle marginLeft"
                      :title="$t('effectDealFeedBackMain.startTask')"
                      :disabled="scope.row.relationQustionFlag === '2'"
                      icon="iconfont iconstart" @click="terminateTask(scope.row, scope.$index)"></auth-button>
        </template>
      </el-table-column>
        <template slot="headButton">
            <div align="right">
              <!--查看问题履历-->
              <auth-button type="primary" icon="iconfont iconzuopintoupiao" :size="'medium'" :label="$t('effectDealFeedBackMain.viewProResume')" @click="viewMatterAnte"></auth-button>
            </div>
        </template>
    </data-table>
  <relation-qust-pop v-if="relationVisible" :visible.sync="relationVisible" :form="relationForm" :editModel="editModel" @qustData="selectSaCoPro" :code="'SH'"></relation-qust-pop>
  <relation-his-pop v-if="relationHisVisible" :visible.sync="relationHisVisible" :form="relationHisForm"></relation-his-pop>
  <audit-relation-pop v-if="auditRelationVisible" :visible.sync="auditRelationVisible" :form.sync="effectForm" :effApi="proApi" :editModel="editModel" @determin="determin" :changeForm="changeForm"></audit-relation-pop>
  <pre-view ref="preView" :oosUrl="config.VUE_APP_OSS_URL" :fileUrl="config.VUE_APP_BASE_URL" :dialogAttrs="dialogAttrs"></pre-view>
  </div>
</template>

<script>
import { DataTable, authButton, btnBatchDownload, preView, SpanPlus } from 'newepoch-components'
import externalTransition from '@/components/element/assetoutfeedback/externalTransition'
import relationQustPop from '@/pages/assetoutfeedback/effectDealFeedBack/relationQustPop'
import relationHisPop from '@/pages/assetoutfeedback/effectDealFeedBack/relationHisPop'
import auditRelationPop from '@/pages/assetoutfeedback/effectDealFeedBack/auditRelationPop'
export default {
  components: { DataTable, authButton, relationQustPop, preView, relationHisPop, auditRelationPop, btnBatchDownload, externalTransition, SpanPlus },
  props: {
    editModel: {
      type: String,
      default () {
        return ''
      }
    },
    form: {
      type: Object,
      default () {
        return {}
      }
    },
    effApi: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  computed: {
    effectForm: {
      get: function () {
        return this.form
      },
      set: function (value) {
        this.$emit('update:form', value)
      }
    },
    proApi () {
      return this.effApi
    }
  },
  data () {
    return {
      config: process.env,
      changeForm: {},
      obj: {
        replyOutPostDto: {
          chapterFlag: '1',
          replyLawContent: '',
          uploadFilePostDtoList: []
        },
        replyRelationPostDtoList: []
      },
      implementVisible: false,
      relationHisForm: {},
      relationForm: {},
      auditRelationForm: {},
      auditRelationVisible: false,
      relationHisVisible: false,
      relationVisible: false,
      batchAuditTable: [],
      batchAuditColumns: [
        {
          // 问题序号
          prop: 'qusetionNumber',
          align: 'center',
          width: '40px',
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.feedbackQustionNo
        }, {
          // 律所第一次反馈意见
          prop: 'lawFirmNewestFeedback',
          slot: true,
          align: 'left',
          width: '120px',
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.lawFirmNewestFeedback
        }, {
          // 合规相关方最新一次回复
          prop: 'practicableSuggestion',
          slot: true,
          align: 'left',
          width: '120px',
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.practicableSuggestion
        }, {
          // 相关方
          prop: 'relation',
          align: 'center',
          width: '60px',
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.relation
        }, {
          // 相关方最新一次回复给合规
          prop: 'relationresolution',
          slot: true,
          align: 'left',
          width: '120px',
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.relationresolutions
        }, {
          // 相关方提供附件下载
          prop: 'fileList',
          align: 'center',
          width: '60px',
          slot: true,
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.fileIdList
        }, {
          // 是否疑难问题
          prop: 'difficultQustionFlag',
          align: 'center',
          width: '60px',
          slot: true,
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.difficultQustionFlag
        }, {
          // 是否关联问题
          prop: 'relationQustionFlag',
          align: 'center',
          width: '60px',
          slot: true,
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.relationQustionFlag
        }, {
          // 状态
          prop: 'status',
          align: 'center',
          width: '50px',
          label: this.$i18n.messages[this.$i18n.locale].documentSealingTaskManage.status
        }
      ],
      showPage: false,
      dialogAttrs: {
        'append-to-body': true,
        'center': true,
        'title': this.$t('common.preview')
      }
    }
  },
  mounted () {
  },
  methods: {
    // 选择疑难问题
    difficultQustionFlagChange (row, index, flag) {
      this.effectForm.notHandleQuestionDtoList.filter(res => res.qstDetailKeyId === row.qstDetailKeyId).forEach(rese => { rese.difficultQustionFlag = flag })
    },
    // 选择关联问题checkBox
    relationQustionFlagChange (row, index, flag) {
      this.effectForm.notHandleQuestionDtoList.filter(res => res.qstDetailKeyId === row.qstDetailKeyId).forEach(rese => {
        if (rese.relationQustionFlag === '1' && rese.checkButtonPostDtoList) {
          rese.checkButtonPostDtoList[0].relationQustionNo = ''
        }
        rese.relationQustionFlag = flag
        // 改变审核状态
        if (rese.relationQustionFlag === '2') {
          if (rese.relationDealStatus === '2') {
            rese.status = this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.noApproved
          } else {
            rese.status = this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.noTerminated
          }
          if (this.editModel === 'A') {
            if (this.effectForm.notImplementAssetInfoDto.questionList.length > 0) {
              // console.log(this.effectForm.notImplementAssetInfoDto.questionList)
              this.effectForm.notImplementAssetInfoDto.questionList.filter(ques => ques.qstDetailKeyId === row.qstDetailKeyId).forEach(resqus => {
                resqus.checked = ['1']
                resqus.replyOutPostDto = {
                  chapterFlag: '1',
                  replyLawContent: '',
                  uploadFilePostDtoList: []
                }
                resqus.replyRelationPostDtoList = []
              })
            }
          } else {
            // this.effectForm.checkedAudit = ['1']
            // 单笔落实审核数据清空
            if (rese.checkButtonPostDtoList) {
              rese.checkButtonPostDtoList[0].checkedAudit = ['1']
              rese.checkButtonPostDtoList[0].replyOutPostDto = {
                chapterFlag: '1',
                replyLawContent: '',
                uploadFilePostDtoList: []
              }
              rese.checkButtonPostDtoList[0].replyRelationPostDtoList = []
            }
          }
        }
      })
    },
    // 序号
    rowClassName ({ row, rowIndex }) {
      row.rowIndex = rowIndex
    },
    // 选择问题弹窗
    relationQustClick (row, index) {
      let relationCheckBox = this.effectForm.notHandleQuestionDtoList.filter(res => res.qstDetailKeyId === row.qstDetailKeyId)
      let arr = []
      relationCheckBox.forEach(res => { arr.push(res.rowIndex) })
      Object.assign(row, { 'index': arr })
      Object.assign(row, { 'feedbackAssetKey': this.effectForm.feedbackAssetKey })
      this.relationForm = row
      this.relationForm.keyIdList = this.form.keyIdList
      this.relationVisible = true
    },
    // 查看相关履历
    viewMatterAnte () {
      this.relationHisForm = this.effectForm
      this.relationHisVisible = true
    },
    // 选择关联问题
    selectSaCoPro (val, index) {
      // this.dataChanged(index)
      this.obj = {
        practicableResult: '1',
        replyOutPostDto: {
          chapterFlag: '1',
          replyLawContent: '',
          uploadFilePostDtoList: []
        },
        replyRelationPostDtoList: []
      }
      val.relationQustionNo = val.relationQuestionType === '0' ? val.commonQuestionNo : val.feedbackQustionNo
      let newVal = { relationQustionNo: val.relationQustionNo, relationQuestionType: val.relationQuestionType }

      index.forEach(res => {
        // 批量审核
        if (this.editModel === 'A') {
          let i = 0
          let j = res + 1 - this.effectForm.notImplementAssetInfoDto.questionList.length
          while (i < j) {
            this.effectForm.notImplementAssetInfoDto.questionList.push({})
            i++
          }
          Object.assign(this.effectForm.notImplementAssetInfoDto.questionList[Number(res)], this.obj)
          // Object.assign(this.effectForm.notImplementAssetInfoDto.questionList[Number(res)], val)
          Object.assign(this.effectForm.notImplementAssetInfoDto.questionList[Number(res)], newVal)
        } else {
          if (!this.effectForm.notHandleQuestionDtoList[Number(res)].checkButtonPostDtoList) {
            this.effectForm.notHandleQuestionDtoList[Number(res)].checkButtonPostDtoList = []
            this.effectForm.notHandleQuestionDtoList[Number(res)].checkButtonPostDtoList.push(this.obj)
          } else {
            this.effectForm.notHandleQuestionDtoList[Number(res)].checkButtonPostDtoList[0] = this.obj
          }
          // Object.assign(this.effectForm.notHandleQuestionDtoList[Number(Number(res))], val)
          Object.assign(this.effectForm.notHandleQuestionDtoList[Number(Number(res))], newVal)
        }
      })
    },
    // 入力数据初始化
    dataChanged (index) {
      // this.effectForm.checkedAudit = ['1']
      this.obj = {
        practicableResult: '1',
        replyOutPostDto: {
          chapterFlag: '1',
          replyLawContent: '',
          uploadFilePostDtoList: []
        },
        replyRelationPostDtoList: []
      }
      // 批量审核
      if (this.editModel === 'A') {
        let i = 0
        let j = index + 1 - this.effectForm.notImplementAssetInfoDto.questionList.length
        while (i < j) {
          this.effectForm.notImplementAssetInfoDto.questionList.push({})
          i++
        }
        if (this.effectForm.notImplementAssetInfoDto.questionList[index].replyOutPostDto) {
          this.changeForm = JSON.parse(JSON.stringify(this.effectForm.notImplementAssetInfoDto.questionList[index]))
        }
        this.effectForm.index = index
        if (!this.effectForm.notImplementAssetInfoDto.questionList[index].replyOutPostDto) {
          Object.assign(this.effectForm.notImplementAssetInfoDto.questionList[index], this.obj)
          Object.assign(this.effectForm.notImplementAssetInfoDto.questionList[index], { 'qstDetailKeyId': this.effectForm.notHandleQuestionDtoList[index].qstDetailKeyId })
        }
        this.effectForm.checked = this.effectForm.notImplementAssetInfoDto.questionList[index].checked ? this.effectForm.notImplementAssetInfoDto.questionList[index].checked : ['1']
        this.effectForm.checked = this.effectForm.checked.length === 0 ? ['1'] : this.effectForm.checked
        // 单笔落实
      } else {
        if (!this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList) {
          this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList = []
          this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList.push(this.obj)
          this.changeForm = JSON.parse(JSON.stringify(this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList[0]))
        } else {
          if (this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList[0].replyOutPostDto) {
            this.changeForm = JSON.parse(JSON.stringify(this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList[0]))
          }
          if (!this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList[0].replyOutPostDto) {
            this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList[0] = this.obj
          }
        }
        this.effectForm.cindex = index
        this.effectForm.checkedAudit = this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList[0].checkedAudit ? this.effectForm.notHandleQuestionDtoList[index].checkButtonPostDtoList[0].checkedAudit : ['1']
        this.effectForm.checkedAudit = this.effectForm.checkedAudit.length === 0 ? ['1'] : this.effectForm.checkedAudit
      }
    },
    // 终止任务
    terminateTask (row, index) {
      if (row.status === this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.noTerminated) {
        this.$confirm('确认终止任务?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.dataChanged(index)
          row.status = this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.yesTerminated
        }).catch(() => {
        })
      } else {
        row.status = this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.noTerminated
      }
    },
    // 预览
    preView (type, id) {
      // console.log(type, id)
      this.$refs.preView.show(type, id)
    },
    // 获取文件类型
    getFile (e) {
      let name = ''
      let test = e.indexOf('.')
      if (test !== -1) {
        e.slice(test + 1)
      }
      return name
    },
    // 审核
    examine (row, index) {
      this.dataChanged(index)
      this.auditRelationVisible = true
    },
    // 审核状态变化
    determin (flag) {
      if (flag) {
        // 批量审核
        if (this.editModel === 'A') {
          this.effectForm.notHandleQuestionDtoList[this.effectForm.index].status = this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.yesApproved
        } else {
          this.effectForm.notHandleQuestionDtoList[this.effectForm.cindex].status = this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.yesApproved
        }
      }
    },
    // 行合并
    objectSpanMethod (item, { row, column, rowIndex, columnIndex }) {
      let spanCount = 1
      if ((columnIndex > -1 && columnIndex < 1) || (columnIndex > 5 && columnIndex < 8)) {
        if (rowIndex === 0 || item[rowIndex - 1].qstDetailKeyId !== row.qstDetailKeyId) {
          item.reduce((last, current) => {
            if (last.qstDetailKeyId === row.qstDetailKeyId && last.qstDetailKeyId === current.qstDetailKeyId) {
              spanCount++
            }
            return current
          })
          return {
            rowspan: spanCount,
            colspan: 1
          }
        }
        return {
          rowspan: 0,
          colspan: 0
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  @import '~@/styles/assetoutfeedback/common.scss'
</style>
