<template>
  <div class="app-container">
    <div style="margin-top:10px">
      <el-input
        v-model="listQuery.search"
        placeholder="搜索"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleSearch"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-refresh-left"
        @click="resetFilter"
      >刷新重置</el-button>
    </div>
    <div style="margin-top:10px">
    <el-button type="primary" @click="handleAdd" icon="el-icon-plus">新增</el-button>
    </div>
<el-table
      :data="tableData.results"
      style="width: 100%;margin-top:10px;"
      border
      stripe
      fit
      v-loading="listLoading"
      highlight-current-row
      max-height="600"
    >
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="left" label="标题">
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>
      <el-table-column align="left" label="来源">
        <template slot-scope="scope">{{ scope.row.ifrom }}</template>
      </el-table-column>
      <el-table-column align="left" label="外链">
        <template slot-scope="scope">
          {{ scope.row.elink }}
          <el-tag v-if="!scope.row.elink">原创</el-tag>
          </template>
      </el-table-column>
      <el-table-column align="left" label="新建时间">
        <template slot-scope="scope">{{ scope.row.create_time }}</template>
      </el-table-column>
      <el-table-column align="left" label="更新时间">
        <template slot-scope="scope">{{ scope.row.update_time }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作" >
        <template slot-scope="scope">
          <el-button
          v-show="scope.row.is_top"
            type="danger"
            size="small"
            @click="handleTop(scope)"
            icon=""
            :disabled="!checkPermission(['article_top'])"
          >取消置顶</el-button>
          <el-button
          v-show="!scope.row.is_top"
            type="primary"
            size="small"
            @click="handleTop(scope)"
            icon="el-icon-upload2"
            :disabled="!checkPermission(['article_top'])"
          >置顶</el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleEdit(scope)"
            icon="el-icon-edit"
            :disabled="!checkPermission(['article_update'])"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope)"
            icon="el-icon-delete"
            :disabled="!checkPermission(['article_delete'])"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableData.count>0"
      :total="tableData.count"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getArticleList, deleteArticle, topArticle } from "@/api/cms";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination";

const listQuery = {
  page: 1,
  limit: 20,
  search:''
};
export default {
  components: { Pagination },
  data() {
    return {
      listQuery: Object.assign({}, listQuery),
      tableData: {
          count:0,
          results:[],
      },
      listLoading: true,
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList(query = this.listQuery) {
      this.listLoading = true;
      getArticleList(query).then(response => {
        if(response.data.results){
            this.tableData = response.data;
        }
        this.listLoading = false;
      });
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        limit: 20,
        search:''
      };
      this.getList();
    },
    handleSearch() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleAdd() {
      this.$router.push({path:"/cms/news/create"})
    },
    handleEdit(scope) {
    this.$router.push({path:"/cms/news/update",query:{id:scope.row.id}})
    },
    handleDelete(scope) {
      this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteArticle(scope.row.id).then(response => {
            this.$message({
                type: 'success',
                message: '删除成功!'
              });
          this.getList()
      });
          
        }).catch(() => {      
        });
    },
    handleTop(scope) {
        topArticle(scope.row.id).then(response => {
          if(response.code>=200){
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.getList()
          }

          
      });
    }
  }
};
</script>
