<template>
  <div class="app-container">
    <router-link to="/question/create">
      <el-button
          type="primary"
          icon="el-icon-edit"
      >
        添加
      </el-button>
    </router-link>
    <el-table
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;margin-top:30px;"
    >

      <el-table-column
          width="180px"
          align="center"
          label="时间"
      >
        <template slot-scope="{row}">
          <span>{{ row.displayTime | parseTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
          width="180px"
          label="类型"
      >
        <template slot-scope="{row}">
          <span>{{ row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column
          min-width="300px"
          label="标题"
      >
        <template slot-scope="{row}">
          <router-link
              :to="'/question/edit/'+row._id"
              class="link-type"
          >
            <span>{{ row.title }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column
          align="center"
          label="操作"
          width="120"
      >
        <template slot-scope="{row}">
          <router-link :to="'/question/edit/'+row.id">
            <el-button
                type="primary"
                size="small"
                icon="el-icon-edit"
            >
             编辑
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
    />
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { getArticles } from '@/api/articles'
import { IArticleData } from '@/api/types'
import Pagination from '@/components/Pagination/index.vue'

@Component({
  name: 'QuestionList',
  components: {
    Pagination
  },
  filters: {
    parseTime: (timestamp: string) => {
      return new Date(timestamp).toISOString()
    }
  }
})

export default class extends Vue {
  private total = 0
  private list: IArticleData[] = []
  private listLoading = true
  private listQuery = {
    page: 1,
    limit: 20
  }

  created() {
    this.getList()
  }

  private async getList() {
    this.listLoading = true
    const { data } = await getArticles(this.listQuery)
    this.list = data.items
    this.total = data.total
    // Just to simulate the time of the request
    setTimeout(() => {
      this.listLoading = false
    }, 0.5 * 1000)
  }
}
</script>

<style lang="scss" scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}

</style>
