<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" @set="toSet" @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: questionnaire } = createNamespacedHelpers('questionnaire');
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'index',
  props: {},
  components: {
    dataTable,
  },
  data: function() {
    return {
      opera: [
        {
          label: '设置问卷',
          method: 'set',
        },
        {
          label: '编辑',
          method: 'edit',
        },
        {
          label: '删除',
          method: 'delete',
        },
      ],
      fields: [
        { label: '名称', prop: 'title' },
        { label: '信息简介', prop: 'brief' },
        { label: '创建时间', prop: 'create_time' },
      ],
      list: [],
      total: 0,
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...questionnaire(['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);
      }
    },
    // 设置问卷
    toSet({ data }) {
      this.$router.push({ path: '/questions/setQuestion', query: { id: data.id } });
    },
    // 添加
    add() {
      this.$router.push({ path: '/questions/detail' });
    },
    // 修改
    toEdit({ data }) {
      this.$router.push({ path: '/questions/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();
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
};
</script>

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