<template>
  <div id="index">
    <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-show="view === 'list'">
          <el-col :span="24" class="one"> <span>活动标题</span> </el-col>
          <el-col :span="24" class="two">
            <search-1 :form="searchForm" @onSubmit="toSearch" @toReset="toClose"></search-1>
          </el-col>
          <el-col :span="24" class="thr">
            <el-button type="primary" size="mini" @click="toAdd()">新增</el-button>
          </el-col>
          <el-col :span="24" class="four">
            <data-table
              ref="dataTable"
              :fields="fields"
              :opera="opera"
              @query="search"
              :data="list"
              :total="total"
              @edit="toEdit"
              @del="toDel"
              @manage="toManage"
            >
            </data-table>
          </el-col>
        </span>
        <detail v-if="view === 'info'" :id="id" @toBack="toBack"></detail>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('platformAct');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
export default {
  name: 'index',
  props: {},
  components: {
    search1: () => import('./search/search-1.vue'),
    detail: () => import('./parts/detail.vue'),
  },
  data: function () {
    const that = this;
    return {
      loadings: true,
      view: 'list',
      // 列表
      opera: [
        { label: '修改', method: 'edit' },
        { label: '商品管理', method: 'manage' },
        { label: '删除', method: 'del', confirm: true, type: 'danger' },
      ],
      fields: [
        { label: '活动标题', model: 'act_time.title' },
        {
          label: '活动类型',
          model: 'type',
          format: (i) => {
            let data = that.act_typeList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
        { label: '活动时间', model: 'act_time.value' },
        {
          label: '是否开启',
          model: 'is_use',
          format: (i) => {
            let data = that.isuseList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
        {
          label: '是否在首页显示',
          model: 'show_index',
          format: (i) => {
            let data = that.isuseList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
        { label: '排序', model: 'sort' },
      ],
      list: [],
      total: 0,
      // 查询
      searchForm: {},
      // 是否开启
      isuseList: [],
      id: '',
      // type: '',
      act_typeList: [],
      // 查询条件
      searchQuery: {},
    };
  },
  created() {
    this.searchOther();
    this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...mapActions(['query', 'fetch', 'create', 'update', 'delete']),
    toSearch() {
      this.$refs.dataTable.resetPage();
      let res = this.$refs.dataTable.getPageConfig();
      this.search(res);
    },
    // 查询
    async search({ skip = 0, limit = this.$limit, ...others } = {}) {
      const condition = _.cloneDeep(this.searchForm);
      let query = { skip, limit, ...others };
      if (Object.keys(condition).length > 0) query = { ...query, ...condition };
      let res = await this.query(query);
      if (this.$checkRes(res)) {
        this.$set(this, 'list', res.data);
        this.$set(this, 'total', res.total);
        this.$set(this, `searchQuery`, query);
      }
      this.loadings = false;
    },
    // 新增
    toAdd() {
      let id = '';
      this.$set(this, `id`, id);
      this.$set(this, `view`, 'info');
    },
    // 修改
    async toEdit({ data }) {
      this.$set(this, `id`, data.id);
      this.$set(this, `view`, 'info');
    },
    toBack() {
      this.view = 'list';
      this.search(this.searchQuery);
    },
    // 商品管理
    async toManage({ data }) {
      this.$router.push({ path: './act/parts/goods', query: { id: data.id, type: data.type } });
    },
    // 删除
    async toDel({ data }) {
      let res = await this.delete(data._id);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `刪除信息成功` });
        this.search(this.searchQuery);
      }
    },
    // 重置
    toClose() {
      this.searchForm = {};
      this.search();
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 是否使用
      res = await this.dictQuery({ code: 'use' });
      if (this.$checkRes(res)) this.$set(this, `isuseList`, res.data);
      // 活动类型
      res = await this.dictQuery({ code: 'act_type' });
      if (this.$checkRes(res)) this.$set(this, `act_typeList`, res.data);
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</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: 0 0 10px 0;
  }
  .thr {
    margin: 0 0 10px 0;
  }
}
</style>
