<template>
  <div>
    <el-form ref="form" label-width="80px" :inline="true" class="form screen">
      <el-form-item label="关键词：" class="el-form-item">
        <el-input v-model="keyword"></el-input>
      </el-form-item>
      <el-button
        style="float: right"
        type="primary"
        @click="$router.push({name: 'newsDetails', params: {id: 'add'}})">
        添加
      </el-button>
    </el-form>
    <div class="view">
      <el-table
        v-loading="loading"
        :data="news"
        style="width: 100%"
        class="creamtable table"
        :highlight-current-row="false"
        >
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="封面图片" width="290">
          <img slot-scope="{row: {cover}}" :src="cover" style="max-width: 100%">
        </el-table-column>
        <el-table-column label="新闻标题" prop="title"></el-table-column>
        <el-table-column label="标签" prop="label"></el-table-column>
        <el-table-column label="更新时间" prop="date">
          <p slot-scope="{row: {date}}">{{new Date(Number(date)).format('yyyy-MM-dd')}}</p>
        </el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>

        <el-table-column label="操作" width="160">
          <template slot-scope="{row: {id}}">
            <router-link :to="{name: 'newsDetails', params: {id}}" class="link edit">修改</router-link>
            <a @click="removeItem(id)" class="link delete">删除</a>
          </template>
        </el-table-column>
      </el-table>

      <div class="pag-layout" v-if="pag.total">
        <el-pagination
          :total="pag.total"
          :current-page="pag.page"
          :page-size="pag.limit"
          @current-change="handleCurrentChange"
          layout="total, prev, pager, next, jumper"
          class="pag">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  props: ['screen'],
  data () {
    return {
      keyword: '',
      news: [],
      pag: {
        page: 1,
        limit: 10
      },
      loading: true
    }
  },
  methods: {
    async getData () {
      let params = {
        ...this.pag,
        keyword: this.keyword
      }
      --params.page
      let result = await this.$http.get('/news', {params: params})
      this.news = result.content.data
      this.pag.total = result.content.total
      this.pag.limit = result.content.average
    },
    async removeItem (id) {
      if (confirm('删除后将无法恢复，您确定要删除此数据吗？')) {
        try {
          await this.$http.delete('/news/' + id)
          this.$message('成功删除')
          this.refresh()
        } catch (e) {
          this.$error(e.msg)
        }
      }
    },
    async refresh () {
      this.loading = true
      await this.getData()
      this.loading = false
    },
    handleCurrentChange (page) {
      this.pag.page = page
      this.refresh()
    }
  },
  watch: {
    keyword () {
      this.refresh()
    }
  },
  activated () {
    this.refresh()
  },
  mounted () {
    this.refresh()
  }
}
</script>

<style scoped>
@import url('./style.css');
</style>
