<template>
  <div id="feedback">
    <div>
      <span style="font-weight: bold;font-size: 14px">文件生成日期：</span>
      <el-date-picker
        v-model="dataTime"
        type="date"
        placeholder="选择日期"
        size="small"
      />
      <span style="font-weight: bold;font-size: 14px">上报对象：</span>
      <el-select v-model="uploadObjectId" size="small" @change="uploadObjectChange">
        <el-option
          v-for="item in uploadList"
          :key="item.id"
          :label="item.uploadObjectName"
          :value="item.id"
        />
      </el-select>
      <span style="font-weight: bold;font-size: 14px">文件类型：</span>
      <el-select v-model="uploadFileType" clearable size="small">
        <el-option
          v-for="item in FileTypeList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <span style="font-weight: bold;font-size: 14px">文件获取成功：</span>
      <el-select v-model="fileStatus" clearable size="small">
        <el-option
          v-for="item in fileStatusList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" icon="el-icon-search" size="small" @click="seachLogInfo">查询</el-button>
    </div>
    <div style="padding-top: 15px">
      <vxe-table
        ref="feedbackTable"
        align="center"
        :loading="loading"
        auto-resize
        border
        resizable
        highlight-current-row
        show-overflow
        :data="feedbackTable"
      >
        <vxe-table-column title="反馈文件日志">
          <vxe-table-column field="uploadObjectNo" title="上报对象" :formatter="formateUploadObjectNo" />
          <vxe-table-column field="fileName" title="反馈文件名称" width="350" />
          <vxe-table-column field="fileTypeEnum" title="反馈文件类型" :formatter="formatefileType" />
          <vxe-table-column field="uploadTime" title="获取时间" :formatter="dateFormat" />
          <vxe-table-column
            field="feedbackFileStatusEnum.message"
            title="获取文件状态"
            :filters="this.feedbackFileStatusEnum"
            :filter-multiple="false"
          />
          <vxe-table-column field="uploadFailureReason" title="失败原因" />
        </vxe-table-column>
      </vxe-table>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Index',
  data() {
    return {
      loading: false,
      dataTime: new Date(),
      uploadObjectId: '',
      uploadFileType: '',
      fileStatus: '',
      uploadList: [],
      FileTypeList: [],
      fileStatusList: [{ value: 'E1', label: '是' }, { value: 'E2', label: '否' }],
      feedbackFileStatusEnum: [],
      uploadFileTypeList: [],
      feedbackTable: []
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      Promise.all([this.getFeedbackFileTypeEnum(), this.getFileTypeEnum(), this.getUploadObject(), this.getFileType()]).then((res) => {
        this.feedbackFileStatusEnum = res[0]
        this.fileTypeEnum = res[1]
        this.uploadList = res[2]
        this.uploadFileTypeList = res[3]
      })
    },
    getFeedbackFileTypeEnum() {
      const a = this.$axios
      return new Promise(function(resolve, reject) {
        a.get('/feedbackFileLog/getFeedbackFileStatusEnum').then(res => {
          resolve(res.data)
        }).catch((error) => {
          this.$message.error('获取反馈文件类型枚举出错' + error)
        })
      })
    },
    getFileTypeEnum() {
      const a = this.$axios
      return new Promise(function(resolve, reject) {
        a.get('/uploadFileLog/getFileTypeEnum').then(res => {
          resolve(res.data)
        }).catch((error) => {
          this.$message.error('获取上报文件类型枚举出错' + error)
        })
      })
    },
    getUploadObject() {
      const a = this.$axios
      return new Promise(function(resolve, reject) {
        a.get('/uploadObject/').then(res => {
          resolve(res.data)
        }).catch(error => {
          this.$message.error('获取通道对象信息出错' + error)
        })
      })
    },
    getFileType() {
      const a = this.$axios
      return new Promise(function(resolve, reject) {
        a.get('/uploadObject/getFileType').then(res => {
          resolve(res.data)
        }).catch((error) => {
          this.$message.error('获取上报文件类型出错' + error)
        })
      })
    },
    uploadObjectChange() {
      this.$axios.get('/uploadObject/getByuploadObjectId/' + this.uploadObjectId).then((res) => {
        var list = res.data[0].uploadFileType.split(',')
        const data1 = []
        for (let n = 0; n < list.length; n++) {
          for (let j = 0; j < this.uploadFileTypeList.length; j++) {
            if (list[n] == this.uploadFileTypeList[j]['value']) {
              data1.push({ value: this.uploadFileTypeList[j]['value'], label: this.uploadFileTypeList[j]['label'] })
            }
          }
        }
        this.FileTypeList = data1
      })
    },
    seachLogInfo() {
      this.loading = true
      var data = new Date()
      data = this.dataTime
      var dataTime = data.getTime()
      const nowTimeDate = new Date(dataTime)
      nowTimeDate.setHours(0, 0, 0, 0)
      if (this.uploadObjectId === '' || this.uploadObjectId === null) {
        this.$message.error('请选择上报对象')
        this.loading = false
      } else {
        var uploadObjectNo = ''
        const item = this.uploadList.find(item => item.id === this.uploadObjectId)
        uploadObjectNo = item.objectNo
        var uploadFileType = this.uploadFileType
        var fileStatus = this.fileStatus
        if (this.uploadFileType === '' || this.uploadFileType === null) {
          uploadFileType = 'ALL'
        }
        if (this.fileStatus === '' || this.fileStatus === null) {
          fileStatus = 'ALL'
        }
        this.$axios.get('/feedbackFileLog/' + uploadObjectNo + '/' + nowTimeDate.getTime() + '/' + uploadFileType + '/' + fileStatus).then((res) => {
          // console.log(res.data)
          this.feedbackTable = res.data
          this.loading = false
        }).catch(res => {
          this.loading = false
        })
      }
    },
    // 转换文件类型协议
    formatefileType({ cellValue }) {
      const item = this.fileTypeEnum.find(item => item.name === cellValue)
      return item ? item.message : ''
    },
    // 转换上报对象
    formateUploadObjectNo({ cellValue }) {
      for (var i = 0; i < this.uploadList.length; i++) {
        if (this.uploadList[i].objectNo == cellValue) {
          return this.uploadList[i].uploadObjectName
        }
      }
    },
    // 时间转换
    dateFormat({ cellValue, row, column }) {
      if (cellValue !== null) {
        const date = new Date(cellValue) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
        const Y = date.getFullYear() + '-'
        const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        const D = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate() + ' '
        const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
        const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return Y + M + D + h + m + s
      }
    }
  }
}
</script>

<style scoped>
  #feedback{
    padding-left: 20px;
    padding-top: 20px;
  }
</style>
