<!-- 选择已有模板 -->
<template>
  <div>
    <dialog-edit :saveFlag="saveFlag" :appendToBody="appendToBody" :visible.sync="dialogVisible" :editModel="editModel" :form="form" :title="$t('effectDealFeedBackMain.descriptionTemp')">
      <data-table ref="mydata"
                  :columns="columns"
                  v-bind="{maxHeight:400}"
                  :queryParas.sync="queryParas"
                  :loadingFlag="loadingFlag"
                  @sizeChange="pageChange"
                  @currentChange="pageChange">
        <el-table-column :label="$t('common.downLoad')" align="center" width="80x" fixed="right">
          <template slot-scope="scope">
            <btn-download :disabled="scope.row.templateFileId === ''" size="small" :title="$t('common.downLoad')" :fileID="scope.row.templateFileId"></btn-download>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.operate')" align="center" width="80px" fixed="right">
          <template slot-scope="scope">
            <auth-button size="small" class="iconCircle marginLeft"
                        :title="$t('effectDealFeedBackMain.select')" icon="iconfont iconxuanze" @click="select(scope.row)"></auth-button>
          </template>
        </el-table-column>
      </data-table>
    </dialog-edit>
  </div>
</template>

<script>
import effectDealFeedBackApi from '@/api/assetoutfeedback/effectDealFeedBack/effectDealFeedBackApi'
import { DialogEdit, DataTable, authButton, btnDownload } from 'newepoch-components'
import BasePage from '@/pages/baseEditPage'
export default {
  extends: new BasePage(),
  components: { DialogEdit, DataTable, authButton, btnDownload },
  props: {
    form: {
      type: Object,
      default () {
        return {}
      }
    },
    editModel: {
      type: String,
      default () {
        return ''
      }
    },
    id: {
      type: String,
      default () {
        return ''
      }
    },
    code: {
      type: String,
      default () {
        return ''
      }
    }
  },
  computed: {
    api () {
      return effectDealFeedBackApi.getProductFile
    },
    fileNameApi () {
      return effectDealFeedBackApi.getProtFileName
    },
    fileCodeapi () {
      return effectDealFeedBackApi.getProFileCode
    },
    effectForm: {
      get: function () {
        return this.form
      },
      set: function (value) {
        this.$emit('update:form', value)
      }
    }
  },
  data () {
    return {
      tablePayChange: [],
      loadingFlag: false,
      pageData: [],
      queryParas: {
        pageIndex: 1,
        pageSize: 10,
        bussNode: 'situationDescription'
      },
      loadCount: 0,
      saveFlag: false,
      appendToBody: true,
      columns: [
        {
          // 情况说明模板
          prop: 'fileName',
          align: 'left',
          width: '120px',
          label: this.$i18n.messages[this.$i18n.locale].effectDealFeedBackMain.scenTemplate
        }
      ]
    }
  },
  mounted () {
    if (this.editModel === 'A') {
      Object.assign(this.queryParas, { specialPlanId: this.form.specialPlanId })
    } else {
      Object.assign(this.queryParas, { specialPlanId: this.form.notImplementAssetInfoDto.specialPlanId })
    }
    this.situationDescription()
  },
  methods: {
    situationDescription () {
      this.getSituFileCode()
    },
    getFileName (fileCode) {
      let fileName = new Promise((resolve, reject) => {
        this.fileNameApi.getList({ fileCode: fileCode }).then(resq => {
          if (resq.code === '0' && resq.data && resq.data.length > 0) {
            // obj.fileName = resq.data[0].fileName
            resolve(resq.data[0].fileName)
          }
        })
      })
      return fileName
    },
    getTemplateFileId (fileCode) {
      let templateFileId = new Promise((resolve, reject) => {
        this.fileCodeapi.getSitufile({ fileCode: fileCode }).then(resq => {
          if (resq.code === '0' && resq.data && resq.data.length > 0) {
            resolve(resq.data[0].templateFileId)
          } else {
            resolve('')
          }
        })
      })
      return templateFileId
    },
    async getList (datas) {
      for (let index in datas) {
        let ele = datas[index]
        let obj = {}
        obj.fileCode = ele.fileCode
        let fileName = await this.getFileName(ele.fileCode)
        let templateFileId = await this.getTemplateFileId(ele.fileCode)
        obj = {
          fileName: fileName,
          templateFileId: templateFileId
        }
        this.pageData.push(obj)
      }
      this.handleCurrentChange(this.pageData)
    },
    // 获取情况模板数据
    getSituFileCode () {
      this.$nextTick(() => {
        this.loadingFlag = true
        this.api.getList(this.queryParas).then(res => {
          if (res.code === '0' && res.data && res.data.length > 0) {
            this.getList(res.data)
          }
        })
      })
    },
    // 改变当前页
    handleCurrentChange (form) {
      if (form) {
        this.tablePayChange = form.slice(
          (this.queryParas.pageIndex - 1) * this.queryParas.pageSize,
          this.queryParas.pageIndex * this.queryParas.pageSize
        )
      }
      this.$nextTick(() => {
        this.$refs.mydata.buildTableData(this.tablePayChange, form.length)
        this.loadingFlag = false
      })
    },
    // 翻页
    pageChange () {
      this.handleCurrentChange(this.pageData)
    },
    // 选择
    select (row) {
      if (row.templateFileId === '') {
        this.warning('没有可选择的文件模板')
        return
      }
      let param = [{
        fileId: row.templateFileId,
        fileName: row.fileName
      }]
      this.success('情况说明模板选择成功')
      if (this.code === 'LS') {
        this.form.notImplementAssetInfoDto.questionList[this.effectForm.index].replyRelationPostDtoList.filter(res => res.id === this.id)[0].uploadFilePostDtoList = param
      } else if (this.editModel === 'IS' && this.code === 'XGF') {
        this.form.relationHandlingQuestionDtoList[this.effectForm.index].replyRelationPostDtoList.filter(res => res.id === this.id)[0].uploadFilePostDtoList = param
      } else if (this.editModel === 'IS' && this.code === 'SH') {
        this.form.notHandleQuestionDtoList[this.effectForm.cindex].checkButtonPostDtoList[0].replyRelationPostDtoList.filter(res => res.id === this.id)[0].uploadFilePostDtoList = param
      } else if (this.rowData.code === 'SLS') {
        this.form.notImplementAssetInfoDto.questionList[this.effectForm.index].relationHandlingDtoList[this.effectForm.aindex].replyRelationPostDtoList.filter(res => res.id === this.id)[0].uploadFilePostDtoList = param
      }
      this.form.situation = true
      this.dialogVisible = false
    }
  }
}
</script>

<style>

</style>
