<template>
  <v-card class="mx-auto pa-3" height="100%">
    <m-table
      :loading="loading"
      :disable-sort="true"
      :is-tools="false"
      :show-select="false"
      :total="total"
      :show-page="true"
      :page-info="pageInfo"
      :items="items"
      :headers="headers"
      @pageHandleChange="pageHandleChange"
    >
      <template v-slot:actions="{ item }">
        <v-btn text color="primary" @click.stop="handleLog(item)">查看日志</v-btn>
        <v-btn text color="primary" @click.stop="handleDownload(item)">下载日志</v-btn>
      </template>
    </m-table>
    <v-navigation-drawer
      v-model="drawer"
      absolute
      right
      temporary
      width="700"
      hide-overlay
    >
      <log-details v-if="drawer" :code="logCode"></log-details>
    </v-navigation-drawer>
  </v-card>
</template>

<script>
import { versionLogHistory, versionLogDownloadHasParam } from '@/api/system'
import MTable from '@/components/List/table'
import LogDetails from './dynamic/logDetails'
import { currentTime } from '@/utils/date.js'
import { downloadFile } from '@/utils/index.js'
export default {
  name: 'system-update-history',
  components: { MTable, LogDetails },
  data () {
    return {
      drawer: false,
      pageInfo: {
        size: 10,
        current: 1
      },
      total: 0,
      items: [],
      headers: [
        { text: '文件', value: 'fileName' },
        { text: '版本', value: 'versionNo' },
        { text: '时间', value: 'createDate' },
        { text: '操作', value: 'actions' }
      ],
      loading: false,
      logCode: ''
    }
  },
  created () {
    this.init()
  },
  methods: {
    async init () {
      this.loading = true
      try {
        const { data } = await versionLogHistory({ ...this.pageInfo })
        this.items = data.records
        this.total = data.total
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    },
    pageHandleChange (index) {
      this.pageInfo.current = index
      this.init()
    },
    async handleLog ({ versionNo }) {
      // console.log(logCode)
      this.logCode = versionNo
      this.drawer = true
    },
    async handleDownload (item) {
      const timestamp = new Date().getTime()
      const filename = `更新日志导出_${item.versionNo}_${currentTime(timestamp)}.txt`
      try {
        const { data } = await versionLogDownloadHasParam({ logCode: item.versionNo })
        downloadFile(data, filename)
      } catch (error) {
        this.$snackbar.error(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
