<!-- 素材分析、复核弹窗-lrj -->
<template>
    <div>
      <dialog-edit width="1200px"
                   :visible.sync="dialogVisible"
                   :autoSave="false"
                   :autoClose="true"
                   :editModel="editModel"
                   :form="form"
                   :title="title"
                   :footerBtnShow="false"
                   @close="close">
        <div class="titleLine">{{$t('reviewMaterialIndex.materialInfo')}}<span class="line"></span></div>
        <dym-form :componentList="componentList"
                  :formValue.sync="detailForm"
                  v-if="loadCount"></dym-form>
        <!--下载附件-->
        <el-form-item :label="$t('common.attachment')" label-width="140px">
          <btn-batch-download size="medium"
                              type="primary"
                              v-if="fileArray.length > 0"
                              :fileID="fileID"
                              :classType="true"
                              :downloadButtonMsg="$t('feedbackMaterialIndex.downAnnex')"></btn-batch-download>
        </el-form-item>
        <repositoryListNew  v-if="sourceDataFlag"
                            :pageType="editModel"
                            :sourceData="sourceData"
                            @callbackData="callbackData"
                            ref="repositoryListNew"></repositoryListNew>
        <template slot="footer">
            <!--查重-->
            <auth-button :label="$t('feedbackMaterialIndex.check')"
                         type="primary"
                         size="medium"
                         @click="fCnki"
                         v-if="radio==='2' && editModel !== 'V'"></auth-button>
            <!--提交下一条-->
            <auth-button :label="sumbitNext"
                         type="primary"
                         v-if="nextAnalyze && editModel !== 'V'"
                         size="medium"
                         @click="commitNext('0',commitStatus)"></auth-button>
            <!--提交-->
            <auth-button :label="$t('common.submit')"
                         type="primary"
                         size="medium"
                         v-if="editModel !== 'V'"
                         @click="commit('0',commitStatus)"></auth-button>
            <!--暂存-->
            <auth-button :label="$t('feedbackMaterialIndex.temporaryStorage')"
                         type="primary"
                         size="medium"
                         v-if="editModel !== 'V'"
                         @click="commit('1',commitStatus)"></auth-button>
            <!--关闭-->
            <auth-button :label="$t('common.close')"
                         size="medium"
                         @click="close"></auth-button>
        </template>
      </dialog-edit>
      <!--查重弹窗-->
      <cnki :cnkiData="cnkiData" @cnkiSave="cnkiSave" :editModel="editModel"></cnki>
    </div>
</template>

<script>
import BaseEdit from '@/pages/baseEditPage'
import { DialogEdit, authButton, dymForm, btnBatchDownload } from 'newepoch-components'
import repositoryListNew from '@/components/element/assetaudit/repositoryListNew'
import ReviewMateriallInput from '@/components/element/assetaudit/ReviewMateriallInput'
import feedbackMaterialApi from '@/api/assetaudit/feedbackMaterial/feedbackMaterialApi'
import * as format from '@/filters/index'
import mixin from '@/pages/mixin'
import cnki from '@/components/element/assetaudit/cnki'
export default {
  extends: new BaseEdit(),
  mixins: [mixin],
  name: 'analysisAndReviewDialog',
  components: { DialogEdit, authButton, dymForm, ReviewMateriallInput, btnBatchDownload, cnki, repositoryListNew },
  props: {
    commitStatus: { // 01：分析,02：复核
      type: [String],
      default () {
        return ''
      }
    },
    autoSave: {
      type: [Boolean],
      default: false
    },
    form: {
      type: [Array, Object],
      default () {
        return []
      }
    },
    editModel: {
      type: [String],
      default () {
        return 'E'
      }
    }
  },
  data () {
    return {
      nextAnalyze: false, // 下一条显隐
      title: '', // 弹窗标题
      sourceData: [],
      submitData: {}, // 提交表单数据
      tableData: [], // 已有知识库 自定义分析
      listVisible: false, // table显示隐藏
      loadCount: 0,
      sourceDataFlag: false, // 是否显示分析组件
      radio: '', // 单选选中数据
      fileArray: [], // 附件
      commitState: 'false', // 是否已经提交
      fileName: '', // 文件名称
      // applicableProjectName: '', // 适用产品名
      cnkiData: { // 查重的数据
        dialogVisible: false,
        fromData: {}, // 表单数据
        showSave: true
      },
      detailForm: {}, // 表单展示数据
      componentList: [
        // 产品类型
        {
          label: this.$t('feedbackMaterialIndex.productType'),
          value: 'businessName',
          prop: 'businessName',
          labelWidth: '140px',
          cols: [8, 8, 8, 8],
          componentName: ReviewMateriallInput
        },
        // 素材来源
        {
          label: this.$t('feedbackMaterialIndex.materialSourceType'),
          disabled: true,
          value: 'materialSourceTypeName',
          prop: 'materialSourceTypeName',
          labelWidth: '140px',
          cols: [8, 8, 8, 8],
          componentName: ReviewMateriallInput
        },
        // 产品名称
        {
          label: this.$t('feedbackMaterialIndex.productName_issue'),
          disabled: true,
          value: 'productName_issue',
          prop: 'productName',
          labelWidth: '140px',
          cols: [8, 8, 8, 8],
          componentName: ReviewMateriallInput
        },
        // 付款确认书编号
        {
          label: this.$t('feedbackMaterialIndex.payCode'),
          disabled: true,
          value: 'payCode',
          prop: 'payCode',
          labelWidth: '140px',
          cols: [8, 8, 8, 8],
          componentName: ReviewMateriallInput
        },
        // 项目公司名称
        {
          label: this.$t('feedbackMaterialIndex.xmg'),
          disabled: true,
          value: 'xmgsName',
          prop: 'xmgsName',
          labelWidth: '140px',
          cols: [8, 8, 8, 8],
          componentName: ReviewMateriallInput
        },
        // 供应商名称
        {
          label: this.$t('feedbackMaterialIndex.gys'),
          disabled: true,
          value: 'gysName',
          prop: 'gysName',
          labelWidth: '140px',
          cols: [8, 8, 8, 8],
          componentName: ReviewMateriallInput
        },
        {
          // 第三方机构反馈意见
          label: this.$t('feedbackMaterialIndex.institutionOpinions'),
          disabled: true,
          value: 'institutionOpinions',
          prop: 'institutionOpinions',
          labelWidth: '140px',
          cols: [24, 24, 24, 24],
          componentName: 'el-input',
          autosize: { minRows: 4 },
          type: 'textarea'
        },
        {
          // 合规反馈
          label: this.$t('feedbackMaterialIndex.hgOpinions'),
          disabled: true,
          value: 'hgOpinions',
          prop: 'hgOpinions',
          labelWidth: '140px',
          cols: [24, 24, 24, 24],
          componentName: 'el-input',
          autosize: { minRows: 4 },
          type: 'textarea'
        },
        // 合规对接人
        {
          label: this.$t('feedbackMaterialIndex.compliancePerson'),
          disabled: true,
          value: 'dockPersonName',
          prop: 'dockPersonName',
          labelWidth: '140px',
          cols: [8, 8, 8, 8],
          componentName: ReviewMateriallInput
        },
        // 分析人
        {
          label: this.$t('reviewMaterialIndex.analysisPerson'),
          disabled: true,
          value: 'analysisPersonName',
          prop: 'analysisPersonName',
          labelWidth: '140px',
          cols: [8, 8, 8, 8],
          componentName: ReviewMateriallInput
        }
      ],
      sumbitNext: this.$t('common.analysisTips') // 下一条按钮提示
    }
  },
  mounted () {
    this.getInfo()
    this.handleFilter()
    this.getModel()
  },
  computed: {
    api () {
      return feedbackMaterialApi.feedbackmaterial
    },
    tApi () {
      return feedbackMaterialApi.audit
    },
    nApi () {
      return feedbackMaterialApi.nextmsg
    },
    fileID () {
      return this.fileArray
    }
  },
  methods: {
    // 判断页面模式
    getModel () {
      if (this.editModel === 'A') {
        this.title = this.$t('feedbackMaterialIndex.materialFx')
      } else if (this.editModel === 'V') {
        this.title = this.$t('feedbackMaterialIndex.materialCk')
      } else if (this.editModel === 'R') {
        this.title = this.$t('feedbackMaterialIndex.materialFh')
      }
    },
    // 画面关闭
    close () {
      this.dialogVisible = false
    },
    getRadio (data) {
      this.radio = data
    },
    // 详情
    getInfo () {
      this.api.get(this.form.keyId).then(res => {
        if (res.data.analysisStatus === '0') { // 提交下一条按钮提示判断
          this.sumbitNext = this.$t('common.analysisTips')
        } else {
          this.sumbitNext = this.$t('common.reviewTips')
        }
        if (res.data.showButton === '0') {
          this.nextAnalyze = true
        } else {
          this.nextAnalyze = false
        }
        this.tableData = [] // 初始化清空分析数据
        this.fileArray = [] // 初始化下载文件的数据
        let data = JSON.parse(JSON.stringify(res.data))
        data.businessName = format.formatDict(data.businessType, 'ZD0087') // 产品类型
        data.materialSourceTypeName = format.formatDict(data.materialSourceType, 'MATERIAL_SOURCE') // 素材来源
        if (data.issue) { // 产品名称-期数
          data.productName_issue = data.productName + data.issue + '期'
        } else {
          data.productName_issue = data.productName
        }
        this.detailForm = data // 赋值基础数据
        this.fileArray = data.accessoryExtDtoList.map(res => { return res.fileId }) // 附件
        if (data.analysisStatus === '1' && data.reviewStatus !== '0') { // 已分析、已复核/无需复核取knowledgeMgrList值，并且添加analyseWay
          data.knowledgeMgrList.map((res, index) => { // 添加analyseWay值
            data.knowledgeMgrList[index].analyseWay = data.materialAnalyzeList[index].analyseWay
          })
          this.sourceData = JSON.parse(JSON.stringify(data.knowledgeMgrList)) // 分析/复核数据
        } else {
          let tempData = JSON.parse(JSON.stringify(data.materialAnalyzeList))
          tempData.map((res, index) => { // 中间值知识库的DTO用knowledgeMgrList中的替换
            data.knowledgeMgrList.map(item => {
              if (item.keyId === res.keyId) {
                tempData[index] = item
                tempData[index].analyseWay = data.materialAnalyzeList[index].analyseWay // 赋值analyseWay
              }
            })
          })
          this.sourceData = tempData
        }
        this.sourceDataFlag = true
      })
    },
    // 插件返回数据
    callbackData (val) {
      this.parameters = val
    },
    // 提交
    commit (commitType, commitStatus, next, callback) {
      this.$refs.repositoryListNew.callbackDataFun()
      let data = JSON.parse(JSON.stringify(this.parameters))
      if (!data) {
        return
      }
      if (data.length > 0) {
        this.detailForm.materialAnalyzeDtoList = data
        this.detailForm.knowledgeMgrDtoList = data
        this.detailForm.status = commitStatus
        this.detailForm.voltooideStatus = commitType
        if (data.analyseWay === '02' && data.fileType === '01' && data.fileName.length > 1) {
          this.warning(this.$t('knowledgeBaseIndex.oneSelect'))
          return
        }
        if (data.analyseWay === '02' && data.fileType === '02' && data.fileName.length < 2) {
          this.warning(this.$t('knowledgeBaseIndex.moreSelect'))
          return
        }
        this.$LoadingShow()
        this.tApi.putData(this.detailForm).then(res => {
          if (res.code && res.code === '0' && res.data === true) {
            this.success(commitType === '0' ? this.$t('common.submittedSuccess') : this.$t('common.temporarynSuccess')) // 提交成功、暂存成功
            this.$LoadingClose()
            this.$emit('operateBtnClick', true)
            this.commitState = true
            callback && callback(res)
            this.dialogVisible = !!next
          } else {
            this.$emit('operateBtnClick', true) // 操作失败
            this.error(this.$t('common.submitFailure'))
          }
        })
      } else {
        this.warning(this.$t('feedbackMaterialIndex.SelectKnowledgeBase'))
      }
    },
    // 获取下一条的详情（同详情）
    getNextInfo () {
      this.sourceDataFlag = false
      feedbackMaterialApi.getNextData.setReviewNo({ status: this.commitStatus }).then(res => {
        if (res.data.showButton === '0') {
          this.nextAnalyze = true
        } else {
          this.nextAnalyze = false
        }
        this.tableData = [] // 初始化清空分析数据
        this.fileArray = [] // 初始化下载文件的数据
        let data = JSON.parse(JSON.stringify(res.data))
        data.businessName = format.formatDict(data.businessType, 'ZD0087') // 产品类型
        data.materialSourceTypeName = format.formatDict(data.materialSourceType, 'MATERIAL_SOURCE') // 素材来源
        if (data.issue) { // 产品名称-期数
          data.productName_issue = data.productName + data.issue + '期'
        } else {
          data.productName_issue = data.productName
        }
        this.detailForm = data // 赋值基础数据
        this.fileArray = data.accessoryExtDtoList.map(res => { return res.fileId }) // 附件
        if (data.analysisStatus === '1' && data.reviewStatus !== '0') { // 已分析、已复核/无需复核取knowledgeMgrList值，并且添加analyseWay
          data.knowledgeMgrList.map((res, index) => { // 添加analyseWay值
            data.knowledgeMgrList[index].analyseWay = data.materialAnalyzeList[index].analyseWay
          })
          this.sourceData = JSON.parse(JSON.stringify(data.knowledgeMgrList))
        } else {
          let tempData = JSON.parse(JSON.stringify(data.materialAnalyzeList))
          tempData.map((res, index) => { // 中间值知识库的DTO用knowledgeMgrList中的替换
            data.knowledgeMgrList.map(item => {
              if (item.keyId === res.keyId) {
                tempData[index] = item
                tempData[index].analyseWay = data.materialAnalyzeList[index].analyseWay // 赋值analyseWay
              }
            })
          })
          this.sourceData = tempData
        }
        this.sourceDataFlag = true
      })
    },
    // 提交分析下一条
    commitNext (commitType, commitStatus) {
      this.listVisible = false
      this.commit(commitType, commitStatus, true, res => {
        if (res.data) {
          this.getNextInfo()
        } else {
          this.warning(this.$t('common.analysisFailure'))
        }
      })
    },
    // 点击查重按钮
    fCnki () {
      let data = this.$refs.repositoryListNew.submitData()
      if (data) {
        this.cnkiData.fromData = {
          ...data,
          fileTypeStr: format.formatDict(data.fileType, 'HG_FILE_TYPE'),
          fileId: data.fileName.join(','),
          fileNameStr: data.fileNameStr.join(',')
        }
        this.cnkiData.dialogVisible = true
      }
    },
    // 查重返回数据
    cnkiSave (data) {
      this.tableData = [{
        analyseWay: '01',
        fileType: data.fileType,
        fileName: data.fileId.split(','),
        problemType: data.problemType,
        problemPoint: data.problemPoint,
        solution: data.solution,
        keyId: data.keyId,
        applicableProject: data.applicableProject
      }]
    }
  }
}
</script>
