<template>
  <div id="goods">
    <el-row>
      <el-col
        :span="24"
        class="main animate__animated animate__backInRight"
        v-loading="loadings"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
      >
        <span v-if="view === 'list'">
          <el-col :span="24" class="one"> <span>系统消息管理</span> </el-col>
          <data-search :fields="searchFields" v-model="searchInfo" @query="search">
            <template #source>
              <el-option v-for="i in sourceList" :key="i.label" :label="i.label" :value="i.value"></el-option>
            </template>
          </data-search>
          <data-btn :fields="btnList" @add="toAdd"></data-btn>
          <el-col :span="24" class="two">
            <data-table ref="dataTable" :fields="fields" :opera="opera" :data="list" :total="total" @query="search" @view="toView"></data-table>
          </el-col>
        </span>
        <notice v-if="view === 'info'" :id="id" @toBack="toBack"></notice>
        <detail v-if="view === 'infos'" :id="id" @goBack="toBack"></detail>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: msgList } = createNamespacedHelpers('msgList');
const { mapActions: notice } = createNamespacedHelpers('notice');
const { mapActions: dictData } = createNamespacedHelpers('dictData'); // 字典

export default {
  name: 'index',
  props: {},
  components: {
    notice: () => import('./notice.vue'),
    detail: () => import('./detail.vue'),
  },
  data: function () {
    return {
      loadings: true,
      loading: false,
      view: 'list',
      fields: [
        { label: '发送时间', model: 'time' },
        { label: '来源id', model: '_id' },
        {
          label: '信息来源',
          model: 'source',
          format: (i) => {
            let data = this.sourceList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
      ],
      btnList: [{ label: '添加', method: 'add' }],
      opera: [{ label: '查看', method: 'view' }],
      searchFields: [{ label: '信息来源', model: 'source', type: 'select' }],
      searchInfo: {},
      list: [],
      total: 0,
      id: '',
      // 信息来源
      sourceList: [],
    };
  },
  created() {
    this.searchOthers();
    this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...notice(['query', 'delete', 'fetch', 'update', 'create']),
    ...msgList({ msgQuery: 'query' }),
    // 查询
    async search({ skip = 0, limit = this.$limit, ...others } = {}) {
      let query = { skip, limit, ...others };
      if (Object.keys(this.searchInfo).length > 0) query = { ...query, ...this.searchInfo };
      const res = await this.msgQuery(query);
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
      this.loadings = false;
    },
    // 新增
    toAdd() {
      let id = '';
      this.$set(this, `id`, id);
      this.$set(this, `view`, 'infos');
    },
    // 查看
    async toView({ data }) {
      this.$set(this, `id`, data._id);
      this.$set(this, `view`, 'info');
    },
    // 执行返回
    toBack() {
      this.view = 'list';
    },
    // 查询其他信息
    async searchOthers() {
      let res;
      // 信息来源
      res = await this.dictQuery({ code: 'notice_source' });
      if (this.$checkRes(res)) this.$set(this, `sourceList`, res.data);
    },
  },
  computed: {
    ...mapState(['user']),
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    margin: 0 0 10px 0;

    span:nth-child(1) {
      font-size: 20px;
      font-weight: 700;
      margin-right: 10px;
    }
  }
  .two {
    margin: 20px 0 10px 0;
  }
  .thr {
    margin: 0 0 10px 0;
  }
}
.el-col {
  margin: 10px 0;
}
</style>
