<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="top">
          <el-button type="primary" size="mini" @click="add">添加</el-button>
        </el-col>
        <el-col :span="24" class="down">
          <data-table :fields="fields" :opera="opera" :data="list" :total="total" @query="search" @edit="toEdit" @delete="toDelete"></data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import dataTable from '@common/src/components/frame/filter-page-table.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: roadShow } = createNamespacedHelpers('roadShow');
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'index',
  props: {},
  components: { dataTable },
  data: function() {
    return {
      opera: [
        {
          label: '编辑',
          method: 'edit',
        },
        {
          label: '删除',
          method: 'delete',
        },
      ],
      fields: [
        { label: '信息标题', prop: 'title', filter: 'input' },
        { label: '信息来源', prop: 'origin' },
        { label: '发布时间', prop: 'publish_time' },
      ],
      list: [],
      total: 0,
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...roadShow(['query', 'fetch', 'create', 'update', 'delete']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      let res = await this.query({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 修改
    toEdit({ data }) {
      this.$router.push({ path: '/roadshow/detail', query: { id: data.id } });
    },
    // 删除
    async toDelete({ data }) {
      let res = await this.delete(data.id);
      if (this.$checkRes(res)) {
        this.$message({
          message: '信息删除成功',
          type: 'success',
        });
        this.search();
      }
    },
    // 添加数据
    add() {
      this.$router.push({ path: '/roadshow/detail' });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
};
</script>

<style lang="less" scoped>
.main {
  .top {
    text-align: right;
    margin: 0 0 10px 0;
  }
}
</style>
