<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="form.single === 0 ? $t('ladingPlanManageList.confirmExceptionResult') : $t('ladingPlanManageList.cancelconfirmExceptionResult')"
                 :footerVisible="true" :saveFlag="false" :width="'600px'"
                 :api="api" :form="form" :editModel="editModel">
      <data-table :operateBtnLayout="operateBtnLayout" :columns="columns"
                  :menuFlag="false" :sourceData.sync="soruchData" :selectedRows.sync="selectedRows" :showPage="false">
        <template slot="assetCodes" slot-scope="scope">
          <expand-collapse :sourceData="soruchData" colProp="assetCodes" :valueProp="editModel === 'V' ? 'assetCode' : 'debtor'"
                           :rowIndex="scope.data.$index"></expand-collapse>
        </template>
        <el-table-column align="center" :label="$t('common.operate')" width="120px">
          <template slot-scope="scope">
            <auth-button size="medium" class="iconCircle" v-if="scope.row.keyId !== 1"
                         :title="$t('expressFileReceiveList.downloadFileInfo')"
                         :icon="'iconfont iconycxiazai1'"
                         :loading="downloadFileByTemplateCodeLoading"
                         @click="downloadFileByTemplateCode(scope.row, scope.$index)"></auth-button>
          </template>
        </el-table-column>
      </data-table>
      <!-- 操作 -->
    </dialog-edit>
  </div>
</template>

<script>
import { DialogEdit, DataTable, authButton, ExpandCollapse } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import sysApi from '@/api/sysApi'
import fileApi from '@/api/fileApi'
import { downFile } from '@/utils/devUtils'
export default {
  extends: new BaseEditPage(''),
  components: {
    DialogEdit,
    DataTable,
    authButton,
    ExpandCollapse
  },
  data () {
    return {
      downloadFileByTemplateCodeLoading: false,
      selectedRows: [],
      operateBtnLayout: '',
      soruchData: [],
      columns: [
        // 付款确认书
        {
          prop: 'assetCodes',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].expressFileReceiveList.assetCodeInfo,
          width: '250px',
          slot: true
        },
        // 处理方式
        {
          prop: 'processMode',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].expressFileReceiveList.importResultInfo,
          width: '100px'
        }
        // // 附加信息
        // {
        //   prop: 'extra',
        //   align: 'left',
        //   label: this.$i18n.messages[this.$i18n.locale].importResultInfo.fileDownload,
        //   width: '200px'
        // }
      ]
    }
  },
  mounted () {
  },
  computed: {
    api: function () {
      return {}
    }
  },
  methods: {
    downloadFileByTemplateCode (row, index) {
      if (this.editModel === 'V') {
        let back = { map: { api: {} } }// JSON.parse(JSON.parse(errDataList))
        back.map.api = { shourangsql005: [] }
        back.map.api.shourangsql005 = row.assetCodes
        return this.creaFile(back, 'shourangsql005')
      } else {
        let back = { map: { api: {} } }// JSON.parse(JSON.parse(errDataList))
        back.map.api = { shourangsql006: [] }
        back.map.api.shourangsql006 = row.assetCodes
        return this.creaFile(back, 'shourangsql006')
      }
    },
    // 导入数据错误导出事件
    creaFile (obj, template) {
      let $this = this
      sysApi.exportFileTemplate.createTemplate(obj, 'CREATE', template).then(res => {
        if (res.success) {
          let returnData = {}
          returnData.fileId = res.data.keyId
          returnData.fileName = res.data.fileName
          fileApi.file.download({ keyId: res.data.keyId }).then(res => {
            $this.loading = false
            downFile(res, this.reName, this.inlineName)
          }).catch(function () {
            $this.loading = false
          })
        } else {
          $this.loading = false
          $this.error('导出失败')
        }
      }).catch(function () {
        $this.loading = false
      })
    }
  },
  watch: {
    dialogVisible (n, o) {
      if (n) {
        if (this.$attrs.typeLeiXin === '0') {
          this.columns[0].label = this.$i18n.messages[this.$i18n.locale].expressFileReceiveList.projCompy
        } else {
          this.columns[0].label = this.$i18n.messages[this.$i18n.locale].expressFileReceiveList.assetCodeInfo
        }
        this.soruchData = []
        let assetCodesC = []
        let assetCodesS = []
        let assetCodesF = []
        this.form.data.forEach(it => {
          if (it.isOk) {
            assetCodesC.push({ ...it.tempData })
          } else {
            if (!it.tempData.isTable) {
              assetCodesF.push({ ...it.tempData })
            } else {
              assetCodesS.push({ ...it.tempData })
            }
          }
        })
        if (assetCodesC.length > 0) {
          this.soruchData.push({ assetCodes: assetCodesC, processMode: '成功', keyId: 1 })
        }
        if (assetCodesS.length > 0) {
          this.soruchData.push({ assetCodes: assetCodesS, processMode: '失败', keyId: 2 })
        }
        if (assetCodesF.length > 0) {
          this.soruchData.push({ assetCodes: assetCodesF, processMode: '忽略', keyId: 3 })
        }
      }
    }
  }
}
</script>
