<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="add">
          <el-button type="primary" size="mini" @click="add">添加</el-button>
        </el-col>
        <el-col :span="24" class="list">
          <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: dock } = createNamespacedHelpers('dock');
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: 'room_id', filter: 'input' },
        { label: '展会标题', prop: 'title', filter: 'input' },
        { label: '负责人', prop: 'adminuser' },
        { label: '联系电话', prop: 'phone' },
        { label: '开始时间', prop: 'start_time' },
        { label: '结束时间', prop: 'end_time' },
        {
          label: '状态',
          prop: 'status',
          format: item => {
            return item === '0' ? '准备中' : item === '1' ? '开始' : '结束';
          },
        },
      ],
      list: [],
      total: 0,
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...dock(['query', '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: '/live/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: '/live/detail' });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
};
</script>

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