<template>
  <div class="app-container">
    <div class="filter-container">
      <router-link :to="{ path: '/banner/create' }">
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-edit">
          新建banner
        </el-button>
      </router-link>
      <!--<el-checkbox v-model="showReviewer" class="filter-item" style="margin-left:15px;" @change="tableKey=tableKey+1">
        reviewer
      </el-checkbox>-->
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="日期">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" width="80px">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status == '1' ? "正常" : "隐藏" }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" label="位置" width="100px">
        <template slot-scope="{row}">
          <router-link :to="'/banner/edit/'+row.id" class="link-type">
            <el-tag :type="row.posiiton | positionFilter">
              {{ row.position | positionFilterTxt }}
            </el-tag>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column min-width="300px" label="排序" width="80px">
        <template slot-scope="{row}">
          <el-tag :type="info">
            {{ row.sort }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="300px" label="跳转路径">
        <template slot-scope="{row}">
          <span>{{ row.redirect }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Actions" width="190" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/banner/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">
              修改
            </el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" style="margin-left: 10px;" @click="handleDelete(scope)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { fetchList, deleteBanner } from '@/api/banner'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import waves from '@/directive/waves'

export default {
  name: 'ArticleList',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        draft: 'info',
        2: 'danger'
      }
      return statusMap[status]
    },
    positionFilter(status) {
      const statusMap = {
        'HOME': 'success',
        draft: 'info',
        2: 'danger'
      }
      return statusMap[status]
    },
    positionFilterTxt(status) {
      const statusMap = {
        HOME: '首页',
        ACTIVITY: '活动页',
        VIDEO: '短视频页',
        GUIDE: '开店指导'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.count
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.count
        this.listLoading = false
      })
    },
    handleDelete({ $index, row }) {
      console.log(row.id)
      this.$confirm('您确定要隐藏吗', '警告', {
        confirmButtonText: '是的',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteBanner(row.id)
          this.list.splice($index, 1)
          this.$message({
            type: 'success',
            message: '隐藏成功'
          })
        })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
.filter-container{
  margin-bottom: 20px;
}
.filter-item {
  margin-right: 10px;
}

</style>
