<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="$t('assetInfo.assetInfoTitle')" width="1480px" :autoSave="false" @onSaved="onSaved"
                 :form="form" :api="api" :editModel="editModel" :saveLabel="$t('common.submit')" :append-to-body="true" :loading="loading">
      <el-form :model="fileForm" :rules="formRules" ref="fileForm" :show-message="false">
        <!-- 必须文件清单 -->
        <div class="table-item">
          <vxe-grid
            border
            show-overflow
            highlight-cell
            ref="assetInfoGrid"
            class="vxeTable"
            element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(255, 255, 255, 1)"
            :columns="columns"
            cell-class-name="common-table-cell"
            header-cell-class-name="common-table-header"
            max-height="500"
            :data="fileForm.sourceData"
            :optimization="{scrollY: {gt: 10}}"
            @checkbox-change="selectionData"
            @checkbox-all="selectionData">
            <!-- 表格上方操作按钮 -->
            <template v-slot:top>
              <div class="headButton">
                <btn-batch-download v-if="form.creditorStampWay === 1" type="primary" size="medium" :classType="true" :downloadButtonMsg="$t('taskManage.todoTask.eleSealFileDownload')"
                                    :fileID="eleSealFileId" :icon="'iconfont iconxiazai1'" :beforeBtchDownload="beforeBtchDownload"></btn-batch-download>
                <auth-button type="danger" size="medium" icon="iconfont iconshanchu" :label="$t('common.del')" @click="delFile"></auth-button>
              </div>
            </template>
            <!-- 显示总数项 -->
            <template slot="bottom">
              <span>{{$t('common.viewTip1')}}{{startNum1}}{{$t('common.viewTip2')}}{{endNum1}}{{$t('common.viewTip3')}}{{endNum1}}{{$t('common.viewTip4')}}</span>
            </template>
          </vxe-grid>
        </div>
        <!-- 另需文件清单 -->
        <div class="table-top">
          <vxe-grid
            border
            show-overflow
            highlight-cell
            ref="assetOtherGrid"
            class="vxeTable"
            element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(255, 255, 255, 1)"
            :columns="fileColumns"
            cell-class-name="common-table-cell"
            header-cell-class-name="common-table-header"
            max-height="300"
            :data="fileForm.fileData"
            :optimization="{scrollY: {gt: 10}}"
            @checkbox-change="otherSelectionData"
            @checkbox-all="otherSelectionData">
            <!-- 表格上方操作按钮 -->
            <template v-slot:top>
              <div class="flex-parent">
                <div class="left-div"></div>
                <div class="flex-title">
                  <span>{{$t('assetInfo.otherTableTitle')}}</span>
                </div>
                <div class="flex-btn">
                  <auth-button type="primary" size="medium" icon="iconfont iconxinzeng" :label="$t('common.create')" @click="add"></auth-button>
                  <auth-button type="danger" size="medium" icon="iconfont iconshanchu" :label="$t('common.del')" @click="delOther"></auth-button>
                </div>
              </div>
            </template>
            <!-- 列表操作按钮 -->
            <template v-slot:operation="{ row, rowIndex }">
              <auth-button class="iconCircle btn-marginRight" @click="onClick(row, rowIndex)"
                           :icon="UtilJs.isEmpty(row.fileId) ? 'iconfont iconshangchuan1' : 'iconfont iconzhongxinshangchuan'"
                           :title="UtilJs.isEmpty(row.fileId) ? $t('common.upload') : $t('common.reUpload')"></auth-button>
              <btn-upload v-show="false" :ref="'upload' + rowIndex" :uploadApi="uploadApi" :classType="true" :fileType="row.fileType"
                          :rowData="row" @uploadSuccess="uploadSuccess"></btn-upload>
              <btn-download v-if="!UtilJs.isEmpty(row.fileId)" :fileID="row.fileId"></btn-download>
              <!-- 删除附件 -->
              <auth-button v-if="!UtilJs.isEmpty(row.fileId) && row.fileId !== '0'" class="iconCircleDel"
                           icon="iconfont iconshanchufujian" :title="$t('taskManage.todoTask.delFile')" @click="delAttachment(row)"></auth-button>
            </template>
            <!-- 显示总数项 -->
            <template slot="bottom">
              <span>{{$t('common.viewTip1')}}{{startNum2}}{{$t('common.viewTip2')}}{{endNum2}}{{$t('common.viewTip3')}}{{endNum2}}{{$t('common.viewTip4')}}</span>
            </template>
          </vxe-grid>
        </div>
      </el-form>
    </dialog-edit>
  </div>
</template>
<script>
import BaseEdit from '@/pages/baseEditPage.js'
import { DialogEdit, DataTable, authButton, SelectDictionary, btnUpload, btnDownload, btnBatchDownload } from 'newepoch-components'
import SelectFileName from '@/components/element/dataAduit/SelectFileName.vue'
import assetInfoApi from '@/api/dataAudit/assetInfoApi.js'
import { UPLOAD_API } from '@/constant.js'
import { regExp } from '@/validator/index.js'
import * as format from '@/filters/index'
import cn from '@/i18n/langs/cn.js'

const { common, assetInfo, complianceCheck, taskManage: { todoTask } } = cn
const STAMP_TYPE = {
  1: 'creditorStampWay',
  2: 'stampWayFactor',
  3: 'debtorStampWay',
  4: 'stampWayCoreEnterprise'
}
export default {
  name: 'auditHistoryEdit',
  extends: new BaseEdit(),
  components: {
    DialogEdit,
    DataTable,
    authButton,
    SelectDictionary,
    btnUpload,
    btnDownload,
    btnBatchDownload,
    SelectFileName
  },
  data () {
    return {
      loadCount: 0,
      startNum1: 0,
      endNum1: 0,
      startNum2: 0,
      endNum2: 0,
      loading: false,
      uploadApi: UPLOAD_API,
      eleSealFileId: [],
      formRules: {
        fileCode: [{ required: true, message: todoTask.selectFileMsg, trigger: 'change' }]
      },
      submitDto: {
        assetInfoId: '',
        assetPoolId: '',
        coreEnterpriseId: '',
        assetCode: '',
        addUserName: '',
        assetPoolMaterialGroupDtos: [],
        otherAssetPoolMaterialGroupDtos: []
      },
      selectParas: {
        assetInfoId: ''
      },
      selectData: [],
      selectOtherData: [],
      fileForm: {
        sourceData: [],
        fileData: []
      },
      compareOldData: [],
      selectedData: [],
      queryParas: {},
      columns: [
        // 复选框
        {
          type: 'checkbox',
          align: 'center',
          width: '50px'
        },
        // 文件名称
        {
          field: 'fileName',
          align: 'left',
          minWidth: '280px',
          headerAlign: 'center',
          showOverflow: 'title',
          title: assetInfo.file
        },
        // 需求份数
        {
          field: 'needCopies',
          align: 'right',
          headerAlign: 'center',
          title: assetInfo.neededNumber,
          minWidth: '90px'
        },
        // 交单标准份数
        {
          field: 'deliveryStandardCopies',
          align: 'right',
          headerAlign: 'center',
          minWidth: '110px',
          title: todoTask.deliveryStandardCopies
        },
        // 是否交单必须
        {
          field: 'ifsDeliveryMust',
          align: 'center',
          headerAlign: 'center',
          minWidth: '110px',
          title: todoTask.ifsDeliveryMust,
          formatter: ({ cellValue }) => {
            return format.formatDict(cellValue, 'ZDZD0024')
          }
        },
        // 有效份数
        {
          field: 'validCopies',
          align: 'right',
          headerAlign: 'center',
          title: assetInfo.effectiveNumbers,
          minWidth: '100px',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0" prop={'sourceData.' + rowIndex + '.validCopies'}
                  rules={ row.stampWay !== 1 ? { required: true, validator: this.effectiveNumberCheck, trigger: 'blur' } : { required: false } }>
                  {row.stampWay !== 1
                    ? <el-input v-model_number_trim={row.validCopies} size="medium" placeholder={common.pleaseInput}></el-input>
                    : <span>{'-'}</span>
                  }
                </el-form-item>
              ]
            }
          }
        },
        // 是否有效
        {
          field: 'ifsEffectiveElecChapter',
          align: 'center',
          title: assetInfo.effectiveOrNot,
          minWidth: '130px',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0">
                  { row.stampWay === 1
                    ? <select-dictionary v-model={row.ifsEffectiveElecChapter} dictTypeCode="effective_elec_chapter"></select-dictionary>
                    : <span>{'-'}</span>
                  }
                </el-form-item>
              ]
            }
          }
        },
        // 是否补寄扫描件
        {
          field: 'ifsNeedScanFile',
          align: 'center',
          title: assetInfo.scanSend,
          minWidth: '120px',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0">
                  { row.stampWay !== 1
                    ? <select-dictionary v-model={row.ifsNeedScanFile} dictTypeCode="ZDZD0024"></select-dictionary>
                    : <span>{'-'}</span>
                  }
                </el-form-item>
              ]
            }
          }
        },
        // 扫描件是否有效
        {
          field: 'ifsScanValid',
          align: 'center',
          title: assetInfo.effectiveScan,
          minWidth: '120px',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0">
                  { row.stampWay !== 1 && row.ifsNeedScanFile === '1'
                    ? <select-dictionary v-model={row.ifsScanValid} dictTypeCode="ZDZD0024"></select-dictionary>
                    : <span>{'-'}</span>
                  }
                </el-form-item>
              ]
            }
          }
        },
        // 已归档份数
        {
          field: 'archiveCopies',
          align: 'right',
          headerAlign: 'center',
          title: assetInfo.archiveFinishNumber,
          minWidth: '100px'
        },
        // 备注
        {
          field: 'comments',
          align: 'left',
          headerAlign: 'center',
          title: assetInfo.remark,
          minWidth: '230px',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0">
                  <el-input v-model_trim={row.comments} size="medium" maxlength="1000" placeholder={todoTask.auditOpinionMsg}></el-input>
                </el-form-item>
              ]
            }
          }
        }
      ],
      fileColumns: [
        // 复选框
        {
          type: 'checkbox',
          align: 'center',
          width: '50px'
        },
        // 文件名称
        {
          field: 'fileName',
          align: 'left',
          headerAlign: 'center',
          title: assetInfo.file,
          minWidth: '280px',
          showOverflow: 'title',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0" prop={'fileData.' + rowIndex + '.fileCode'}
                  rules={ row.status ? this.formRules.fileCode : { required: false }}>
                  { row.status
                    ? <select-file-name class="dymForm" v-model={row.fileCode} placeholder={common.pleaseSelect} queryParas={this.selectParas}
                      optionDisabled={this.optionDisabled} v-on:selectChanged={this.projFileChanged} rowIndex={rowIndex}></select-file-name>
                    : <span>{row.fileName}</span>
                  }
                </el-form-item>
              ]
            }
          }
        },
        // 需求份数
        {
          field: 'needCopies',
          align: 'right',
          headerAlign: 'center',
          title: assetInfo.neededNumber,
          minWidth: '150px',
          formatter: ({ cellValue }) => { return cellValue === '' ? '-' : cellValue }
        },
        // 有效份数
        {
          field: 'validCopies',
          align: 'right',
          headerAlign: 'center',
          title: assetInfo.effectiveNumbers,
          minWidth: '150px',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0" prop={'fileData.' + rowIndex + '.validCopies'}
                  rules={ this.UtilJs.isEmpty(row.fileSource)
                    ? { required: true, validator: this.effectiveNumberCheck, trigger: 'blur' }
                    : this.form[STAMP_TYPE[row.fileSource]] === 1
                      ? { required: false }
                      : { required: true, validator: this.effectiveNumberCheck, trigger: 'blur' } }>
                  {this.UtilJs.isEmpty(row.fileSource)
                    ? <el-input v-model_number_trim={row.validCopies} size="medium" placeholder={common.pleaseInput}></el-input>
                    : this.form[STAMP_TYPE[row.fileSource]] === 1
                      ? <span>{'-'}</span>
                      : <el-input v-model_number_trim={row.validCopies} size="medium" placeholder={common.pleaseInput}></el-input>
                  }
                </el-form-item>
              ]
            }
          }
        },
        // 是否有效
        {
          field: 'ifsEffectiveElecChapter',
          align: 'center',
          headerAlign: 'center',
          minWidth: '120px',
          title: todoTask.ifsElecChapterValid,
          slots: {
            default: ({ row, rowIndex }) => {
              let innerHtml
              if (this.UtilJs.isEmpty(row.fileSource)) {
                innerHtml = <select-dictionary v-model={row.ifsEffectiveElecChapter} dictTypeCode="effective_elec_chapter"></select-dictionary>
              } else {
                if (this.form[STAMP_TYPE[row.fileSource]] === 1) {
                  innerHtml = <select-dictionary v-model={row.ifsEffectiveElecChapter} dictTypeCode="effective_elec_chapter"></select-dictionary>
                } else {
                  innerHtml = <span>{'-'}</span>
                }
              }
              return [
                <el-form-item label-width="0">
                  {innerHtml}
                </el-form-item>
              ]
            }
          }
        },
        // 是否另需扫描件
        {
          field: 'ifsNeedScanFile',
          align: 'center',
          title: this.$i18n.messages[this.$i18n.locale].taskManage.todoTask.otherScan,
          minWidth: '150px',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0">
                  {this.UtilJs.isEmpty(row.fileSource)
                    ? <select-dictionary v-model={row.ifsNeedScanFile} dictTypeCode="ZDZD0024"></select-dictionary>
                    : this.form[STAMP_TYPE[row.fileSource]] === 1
                      ? <span>{'-'}</span>
                      : <select-dictionary v-model={row.ifsNeedScanFile} dictTypeCode="ZDZD0024"></select-dictionary>
                  }
                </el-form-item>
              ]
            }
          }
        },
        // 备注
        {
          field: 'comments',
          align: 'left',
          title: this.$i18n.messages[this.$i18n.locale].assetInfo.remark,
          minWidth: '300px',
          slots: {
            default: ({ row, rowIndex }) => {
              return [
                <el-form-item label-width="0">
                  <el-input v-model_trim={row.comments} size="medium" maxlength="1000" placeholder={todoTask.auditOpinionMsg}></el-input>
                </el-form-item>
              ]
            }
          }
        },
        {
          title: common.operate,
          minWidth: '120px',
          align: 'center',
          fixed: 'right',
          slots: { default: 'operation' }
        }
      ]
    }
  },
  computed: {
    api () {
      return assetInfoApi.assetInfo
    }
  },
  methods: {
    onSaved () {
      let _this = this
      _this.$refs['fileForm'].validate((valid) => {
        if (valid) {
          let result = _this.isRepeat()
          if (result.length > 0) {
            let fileMsg = '文件名称为：'
            result.forEach(res => {
              fileMsg = fileMsg + res.fileName + '，'
            })
            fileMsg = fileMsg.substring(0, fileMsg.length - 1)
            _this.warning(fileMsg + '不能重复新增！')
            return
          }
          let pdfCheckMsg = _this.checkFilePdf(_this.fileForm.fileData)
          if (pdfCheckMsg.length > 0) {
            _this.warning('文件名称为' + pdfCheckMsg + '没有上传附件！请上传相关PDF附件')
            return
          }
          // 比较数据是否发生改变
          _this.submitDto.assetPoolMaterialGroupDtos.forEach(save => {
            _this.compareOldData.forEach(old => {
              if (save.keyId === old.keyId && save.ifsDelete === 0) {
                if (save.stampWay === 1) {
                  if (save.ifsEffectiveElecChapter !== old.ifsEffectiveElecChapter) {
                    save.ifsModify = 1
                  }
                } else {
                  if (save.validCopies !== old.validCopies || save.ifsNeedScanFile !== old.ifsNeedScanFile || save.ifsScanValid !== old.ifsScanValid) {
                    save.ifsModify = 1
                  }
                }
              }
            })
          })
          console.log(_this.submitDto.assetPoolMaterialGroupDtos)
          _this.submitDto.otherAssetPoolMaterialGroupDtos = _this.fileForm.fileData
          _this.submitDto.coreEnterpriseId = _this.form.coreEnterpriseId
          _this.submitDto.assetCode = _this.form.assetCode
          _this.submitDto.addUserName = _this.$store.getters.nikeName
          _this.submitDto.assetInfoId = _this.form.keyId
          _this.loading = true
          _this.api.submit(_this.submitDto).then(res => {
            if (res && res.data) {
              _this.success(complianceCheck.submitSucMsg)
              _this.dialogVisible = false
              _this.$emit('submit-success')
            } else {
              _this.error(complianceCheck.submitFailMsg)
            }
            _this.loading = false
          }).catch(() => {
            _this.loading = false
          })
        } else {
          _this.error(todoTask.submitMsg)
        }
      })
    },
    checkFilePdf (data) {
      let msg = ''
      data.forEach(it => {
        if (it.fileSource === 2) {
          if (this.UtilJs.isEmpty(it.fileId)) {
            msg = msg + it.fileName + '，'
          }
        }
      })
      if (msg.length > 0) {
        msg = msg.substring(0, msg.length - 1)
      }
      return msg
    },
    // 数组判重
    isRepeat () {
      let _this = this
      let arr = []
      _this.fileForm.sourceData.forEach(audit => {
        arr.push({
          fileCode: audit.fileCode,
          fileName: audit.fileName
        })
      })
      _this.fileForm.fileData.forEach(file => {
        arr.push({
          fileCode: file.fileCode,
          fileName: file.fileName
        })
      })
      let hash = {}
      let res = []
      arr.forEach(it => {
        if (hash[it.fileCode]) {
          res.push(it)
        }
        // 不存在该元素，则赋值为true，可以赋任意值，相应的修改if判断条件即可
        hash[it.fileCode] = true
      })
      return res
    },
    // 有效份数校验
    effectiveNumberCheck (rule, value, callback) {
      if (regExp.sequence.test(value)) {
        callback()
      } else {
        callback(new Error(todoTask.effectiveNumberMsg))
      }
    },
    beforeBtchDownload () {
      let _this = this
      _this.eleSealFileId.splice(0, _this.eleSealFileId.length)
      _this.fileForm.sourceData.forEach(it => {
        if (it.fileSource === 1) {
          if (it.fileId) {
            _this.eleSealFileId.push(it.fileId)
          }
        }
      })
      return _this.eleSealFileId
    },
    projFileChanged (val, name, data, index) {
      let _this = this
      if (this.UtilJs.isEmpty(val)) {
        _this.fileForm.fileData[index].fileName = ''
        _this.fileForm.fileData[index].needCopies = ''
        _this.fileForm.fileData[index].necessaryType = ''
        _this.fileForm.fileData[index].deliveryStandardCopies = ''
        _this.fileForm.fileData[index].ifsDeliveryMust = ''
        _this.fileForm.fileData[index].fileBussType = ''
        _this.fileForm.fileData[index].fileSource = ''
        _this.fileForm.fileData[index].ifsScanningDeliveryStandard = ''
        _this.fileForm.fileData[index].fileOrder = ''
        _this.fileForm.fileData[index].fileType = null
        _this.$set(_this.fileForm.fileData[index], 'materialGroupId', '')
        _this.$set(_this.fileForm.fileData[index], 'materialGroupDetailId', '')
        _this.$set(_this.fileForm.fileData[index], 'materialFileId', '')
        _this.fileForm.fileData[index].ifsEffectiveElecChapter = ''
      } else {
        const { fileSource, fileName, needCopies, necessaryType, deliveryStandardCopies, ifsDeliveryMust,
          fileBussType, ifsScanningDeliveryStandard, fileOrder, materialGroupId, materialGroupDetailId, materialFileId } = data
        if (fileSource === 2) {
          _this.warning('该文件来源是保理商，请上传附件，附件格式只能是pdf！')
          _this.fileForm.fileData[index].fileType = '.pdf'
        } else {
          _this.fileForm.fileData[index].fileType = null
        }
        _this.fileForm.fileData[index].fileName = fileName
        _this.fileForm.fileData[index].needCopies = needCopies
        _this.fileForm.fileData[index].necessaryType = necessaryType
        _this.fileForm.fileData[index].deliveryStandardCopies = deliveryStandardCopies
        _this.fileForm.fileData[index].ifsDeliveryMust = ifsDeliveryMust
        _this.fileForm.fileData[index].fileBussType = fileBussType
        _this.fileForm.fileData[index].fileSource = fileSource
        _this.fileForm.fileData[index].ifsScanningDeliveryStandard = ifsScanningDeliveryStandard
        _this.fileForm.fileData[index].fileOrder = fileOrder
        _this.$set(_this.fileForm.fileData[index], 'materialGroupId', materialGroupId)
        _this.$set(_this.fileForm.fileData[index], 'materialGroupDetailId', materialGroupDetailId)
        _this.$set(_this.fileForm.fileData[index], 'materialFileId', materialFileId)
        if (_this.form[STAMP_TYPE[_this.fileForm.fileData[index].fileSource]] === 1) {
          _this.fileForm.fileData[index].ifsEffectiveElecChapter = '0'
        } else {
          _this.fileForm.fileData[index].ifsEffectiveElecChapter = ''
        }
      }
      _this.fileForm.fileData[index].validCopies = 0
      _this.fileForm.fileData[index].ifsNeedScanFile = ''
    },
    optionDisabled () {
      let _this = this
      return function (item) {
        if (_this.selectedData.indexOf(item.fileCode) !== -1) {
          return true
        } else {
          return false
        }
      }
    },
    onClick (data, index) {
      if (this.UtilJs.isEmpty(data.fileCode)) {
        this.warning(complianceCheck.uploadMsg)
      } else {
        this.$refs['upload' + index].upload()
      }
    },
    uploadSuccess (returnData, rowData) {
      rowData.fileId = returnData.fileID
      this.success(common.uploadSuccess)
    },
    loadSourceData (assetId) {
      let _this = this
      _this.api.getInfo({ assetId: assetId }).then(res => {
        if (res && res.data) {
          if (res.data.assetPoolMaterialGroupDtos.length > 0) {
            _this.startNum1 = 1
            _this.endNum1 = res.data.assetPoolMaterialGroupDtos.length
          } else {
            _this.startNum1 = 0
            _this.endNum1 = 0
          }
          _this.fileForm.sourceData = res.data.assetPoolMaterialGroupDtos
          _this.fileForm.fileData = res.data.otherAssetPoolMaterialGroupDtos
          _this.fileForm.sourceData.forEach(it => {
            // 供应商盖章方式
            _this.$set(it, 'creditorStampWay', _this.form.creditorStampWay)
            // 项目公司盖章方式
            _this.$set(it, 'debtorStampWay', _this.form.debtorStampWay)
            // 删除标识
            _this.$set(it, 'ifsDelete', 0)
            // 字典ID转换为字符串
            // 是否有效
            it.ifsEffectiveElecChapter === undefined ? _this.$set(it, 'ifsEffectiveElecChapter', '') : it.ifsEffectiveElecChapter = it.ifsEffectiveElecChapter + ''
            // 是否补寄扫描件
            it.ifsNeedScanFile === undefined ? _this.$set(it, 'ifsNeedScanFile', '') : it.ifsNeedScanFile = it.ifsNeedScanFile + ''
            // 扫描件是否有效
            it.ifsScanValid === undefined ? _this.$set(it, 'ifsScanValid', '') : it.ifsScanValid = it.ifsScanValid + ''
            // 备注
            if (it.comments === undefined) {
              _this.$set(it, 'comments', '')
            }
          })
          // 保存查出来的 审核列表
          _this.compareOldData = JSON.parse(JSON.stringify(_this.fileForm.sourceData))
          _this.fileForm.fileData.forEach(item => {
            // 文件名称是否可编辑
            _this.$set(item, 'status', false)
            // 删除标识
            _this.$set(item, 'ifsDelete', 0)
          })
          _this.selectedData = []
          res.data.assetPoolMaterialGroupDtos.forEach(it => {
            _this.selectedData.push(it.fileCode)
          })
          _this.submitDto.assetPoolId = res.data.assetPoolId
          _this.submitDto.assetPoolMaterialGroupDtos = res.data.assetPoolMaterialGroupDtos
          _this.submitDto.otherAssetPoolMaterialGroupDtos = res.data.otherAssetPoolMaterialGroupDtos
        }
      })
    },
    // 必须文件删除
    delFile () {
      let _this = this
      if (_this.selectData.length === 0) {
        _this.warning(common.delSelectedTip)
        return
      }
      _this.$confirm(common.confirmTip, common.tip, {
        type: 'warning'
      }).then(() => {
        _this.selectData.forEach(it => {
          _this.fileForm.sourceData.forEach(item => {
            if (it.keyId === item.keyId) {
              item.ifsDelete = 1
            }
          })
        })
        _this.submitDto.assetPoolMaterialGroupDtos = _this.fileForm.sourceData
        _this.fileForm.sourceData = _this.fileForm.sourceData.filter(it => {
          return it.ifsDelete === 0
        })
        if (_this.fileForm.sourceData.length === 0) {
          _this.startNum1 = 0
          _this.endNum1 = 0
        } else {
          _this.startNum1 = 1
          _this.endNum1 = _this.fileForm.sourceData.length
        }
        _this.success(common.delSuccess)
      }).catch(() => {})
    },
    // 删除附件
    delAttachment (row) {
      let _this = this
      _this.$confirm(todoTask.delFileTip, common.tip, {
        type: 'warning'
      }).then(() => {
        row.fileId = ''
      }).catch(() => {})
    },
    // 新增
    add () {
      let _this = this
      let addObj = {}
      let filedMap = _this.fileColumns.map(it => { return it.field }).filter(item => item)
      filedMap.forEach(it => { addObj[it] = '' })
      _this.fileForm.fileData.push(Object.assign(addObj, {
        fileId: null,
        fileCode: '',
        necessaryType: '',
        deliveryStandardCopies: '',
        ifsDeliveryMust: '',
        fileBussType: '',
        fileSource: '',
        ifsScanningDeliveryStandard: '',
        fileOrder: '',
        fileType: null,
        ifsModify: 1,
        ifsOther: 1,
        ifsDelete: 0,
        date: new Date().valueOf(),
        status: true
      }))
      _this.startNum2 = 1
      _this.endNum2 = _this.fileForm.fileData.length
    },
    // 另需文件删除
    delOther () {
      let _this = this
      if (_this.selectOtherData.length === 0) {
        _this.warning(common.delSelectedTip)
        return
      }
      _this.$confirm(common.confirmTip, common.tip, {
        type: 'warning'
      }).then(() => {
        let arr = _this.filterData(_this.fileForm.fileData, _this.selectOtherData)
        _this.submitDto.otherAssetPoolMaterialGroupDtos = arr
        _this.fileForm.fileData = arr.filter(it => {
          return it.ifsDelete === 0
        })
        if (_this.fileForm.fileData.length === 0) {
          _this.startNum2 = 0
          _this.endNum2 = 0
        } else {
          _this.startNum2 = 1
          _this.endNum2 = _this.fileForm.fileData.length
        }
        _this.success(common.delSuccess)
      }).catch(() => {})
    },
    filterData (data, rows) {
      let arr1 = []
      let arr2 = []
      rows.forEach(item => {
        if (item.keyId) {
          arr1.push(item.keyId)
        } else {
          arr2.push(item.date)
        }
      })
      data.forEach(item => {
        if (item.keyId) {
          if (arr1.indexOf(item.keyId) !== -1) {
            item.ifsDelete = 1
          }
        } else {
          if (arr2.indexOf(item.date) !== -1) {
            item.ifsDelete = 1
          }
        }
      })
      return data
    },
    selectionData ({ records }) {
      this.selectData = records
    },
    otherSelectionData ({ records }) {
      this.selectOtherData = records
    }
  },
  watch: {
    visible (val) {
      let _this = this
      if (val) {
        _this.selectParas.assetInfoId = _this.form.keyId
        _this.loadSourceData(_this.form.keyId)
      } else {
        _this.compareOldData = []
        _this.fileForm.sourceData = []
        _this.fileForm.fileData = []
        _this.submitDto = {
          assetInfoId: '',
          coreEnterpriseId: '',
          assetCode: '',
          addUserName: '',
          assetPoolMaterialGroupDtos: [],
          otherAssetPoolMaterialGroupDtos: []
        }
        _this.startNum1 = 0
        _this.endNum1 = 0
        _this.startNum2 = 0
        _this.endNum2 = 0
      }
    }
  }
}
</script>
<style scoped lang="scss" src="@/styles/dataAudit/assetInfo/assetInfoEdit.scss">
</style>
