<template>
  <div id="index">
    <data-table :fields="fields" :opera="opera" :data="list" :total="total" @query="search" @file="toFile" @edit="toEdit" @delete="toDelete">
      <template #selfbtn>
        <el-button type="primary" size="mini" @click="toAdd">添加</el-button>
      </template>
      <template #options="{ item }">
        <template v-if="item.prop === 'column_name'">
          <el-option v-for="(i, index) in typeList" :key="`columns-name-${index}`" :label="i" :value="i"></el-option>
        </template>
      </template>
    </data-table>
  </div>
</template>

<script>
const { onlineType, progressType } = require('@common/dict/index');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: online } = createNamespacedHelpers('online');
export default {
  name: 'onlineIndex',
  props: {},
  components: {},
  data: function () {
    return {
      list: [],
      total: 0,
      opera: [
        {
          label: '修改',
          method: 'edit',
        },
        {
          label: '查看播放视频',
          method: 'file',
        },
        {
          label: '删除',
          method: 'delete',
          type: 'danger',
        },
      ],
      fields: [
        { label: '栏目名称', prop: 'column_name', filter: 'select' },
        { label: '标题', prop: 'title', filter: true },
        { label: '举办方', prop: 'organizers' },
        { label: '联系人', prop: 'contacts' },
        { label: '联系电话', prop: 'phone' },
        { label: '举办开始时间', prop: 'start_time' },
        { label: '举办结束时间', prop: 'end_time' },
        {
          label: '进行状态',
          prop: 'status',
          format: (i) => {
            const r = progressType.find((f) => f.value === i);
            if (r) return r.label;
            return '未知状态';
          },
        },
      ],
      typeList: onlineType,
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...online(['query', 'delete']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const res = await this.query({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    toAdd() {
      this.$router.push('/adminCenter/online/detail');
    },
    toEdit({ data }) {
      this.$router.push({ path: '/adminCenter/online/detail', query: { id: data._id } });
    },
    toFile({ data }) {
      this.$router.push({ path: '/adminCenter/online/file', query: { id: data._id } });
    },
    async toDelete({ data }) {
      const res = await this.delete(data._id);
      if (this.$checkRes(res, '删除成功', '删除失败')) {
        this.search();
      }
    },
  },
  computed: {
    ...mapState(['user', 'menuParams']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped></style>
