<template>
  <div class="app-container">
    <div class="filter-container">
      标题:
      <el-input v-model="listQuery.title" class="filter-item" placeholder="请输入" @change="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加图文素材</el-button>
    </div>
    <el-table v-loading="listLoading" row-key="id" :data="list" border fit highlight-current-row>
      <el-table-column label="标题" prop="title" align="center" />
      <el-table-column label="最后更新时间" prop="updateTime" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button size="mini" type="primary" @click="handleUpdate(row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { fetchList, deleteArticle } from '@/api/wechat/article'
import Pagination from '@/components/Pagination'

export default {
  name: 'WeChatArticleList',
  components: { Pagination },
  props: {
    // type 类型： 1采美，2呵呵商城
    listType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        // type 类型： 1采美，2呵呵商城
        type: '0',
        title: '',
        pageNum: 1,
        pageSize: 20
      },
      linkPath: ''
    }
  },
  watch: {
    $route(route) {
      this.getList()
    }
  },
  created() {
    this.listQuery.type = this.listType
    if (this.listQuery.type === 1) {
      this.linkPath = 'caimei'
    } else {
      this.linkPath = 'hehe'
    }
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.results
        this.total = response.data.totalRecord
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleCreate() {
      this.$router.push({ path: `/wechat/${this.linkPath}/article/add` })
    },
    handleUpdate(row) {
      this.$router.push({ path: `/wechat/${this.linkPath}/article/edit`, query: { id: row.id }})
    },
    handleDelete(row) {
      this.$confirm('是否要删除该图文素材', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteArticle(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
.filter-item{
  width: 160px;
  margin-right: 20px;
}
</style>
