<template>
  <div class="app-container">
    <el-row>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="add()">{{ $t('ec.table.add') }}</el-button>
    </el-row>
    <el-table
      class="table"
      size="medium"
      :data="dataList"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="name"
        :label="$t('ec.reports.tableName')"
      />
      <el-table-column
        prop="updateDate"
        :label="$t('ec.table.updateDate')"
      >
        <template slot-scope="scope">
          {{ $moment(scope.row.updateDate).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('ec.table.actions')">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="pre(scope.row)"
          >{{ $t('ec.reports.preview') }}</el-button>
          <el-divider direction="vertical" />
          <el-button
            size="mini"
            type="text"
            @click="design(scope.row)"
          >{{ $t('ec.reports.design') }}</el-button>
          <el-divider direction="vertical" />
          <el-button
            size="mini"
            type="text"
            @click="del(scope.row)"
          >{{ $t('ec.table.del') }}</el-button>
          <el-divider direction="vertical" />
          <el-button
            size="mini"
            type="text"
            @click="download(scope.row)"
          >{{ $t('ec.table.export') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  name: 'Reports',
  data() {
    return {
      dataList: []
    }
  },
  created() {
    this.refreshList()
  },
  activated() {
    this.refreshList()
  },
  methods: {
    // 获取数据列表
    refreshList() {
      this.loading = true
      this.$http({
        url: 'reports/list',
        method: 'get'
      }).then(({ data }) => {
        this.loading = false
        this.dataList = data.reports
      })
    },
    add() {
      // this.$router.push({ path: '/iframe', query: { name: 'ureport-designer', title: '新建报表', src: `${process.env.VUE_APP_REPORT_URL}/ureport/designer` }})
      this.$router.push({ path: '/iframe', query: { name: 'add-report' }})
    },
    pre(report) {
      this.$router.push({ path: `/iframe`, query: { name: 'preview-report', _u: `file:${report.name}` }})
    },
    design(report) {
      this.$router.push({ path: `/iframe`, query: { name: 'design-report', _u: `file:${report.name}` }})
    },
    // 删除
    del(report) {
      this.$confirm(this.$t('ec.message.delConfirm'), this.$t('ec.message.prompt'), {
        confirmButtonText: this.$t('ec.message.confirm'),
        cancelButtonText: this.$t('ec.message.cancel'),
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$http({
          url: 'reports/delete',
          method: 'delete',
          params: { 'id': `file:${report.name}` }
        }).then(({ data }) => {
          if (data && data.success) {
            this.$message.success(data.msg)
            this.refreshList()
          }
          this.loading = false
        })
      })
    },
    download(report) {
      window.open(`${process.env.VUE_APP_REPORT_URL}/ureport/word?_u=file:${report.name}`)
    }

  }
}
</script>
