<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight">
        <el-col :span="24" class="one"> <span>首页广告图标设置</span> </el-col>
        <el-col :span="24" class="two">
          <search-1 :form="searchForm" :typeList="typeList" @onSubmit="search" @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
            :select="true"
            :selected="selected"
            @handleSelect="handleSelect"
            :fields="fields"
            :opera="opera"
            @query="search"
            :data="list"
            :total="total"
            @edit="toEdit"
            @del="toDel"
          >
          </data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('indexModule');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
export default {
  name: 'index',
  props: {},
  components: {
    search1: () => import('./parts/search-1.vue'),
  },
  data: function () {
    const that = this;
    return {
      // 列表
      opera: [
        { label: '修改', method: 'edit' },
        { label: '删除', method: 'del', confirm: true, type: 'danger' },
      ],
      fields: [
        { label: '名称', model: 'name' },
        {
          label: '跳转类型',
          model: 'to_type',
          format: (i) => {
            let data = that.typeList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
        { label: '跳转到', model: 'to' },
        {
          label: '是否正在使用',
          model: 'status',
          format: (i) => {
            let data = that.statusList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
      ],
      list: [],
      total: 0,
      // 查询
      searchForm: {},
      // 多选值
      selected: [],
      // 类型列表
      typeList: [],
      // 是否使用
      statusList: [],
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...mapActions(['query', 'fetch', 'create', 'update', 'delete']),
    // 查询
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchForm);
      let res = await this.query({ skip, limit, ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, 'list', res.data);
        this.$set(this, 'total', res.total);
      }
    },
    // 新增
    toAdd() {
      this.$router.push({ path: '/system/indexModule/detail', query: { status: '0' }  });
    },
    // 修改
    async toEdit({ data }) {
      this.$router.push({ path: '/system/indexModule/detail', query: { id: data.id } });
    },
    async toDel({ data }) {
      let res = await this.delete(data._id);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `刪除信息成功` });
        this.search();
      }
    },
    toClose() {
      this.searchForm = {};
      this.search();
    },
    // 多选
    handleSelect(data) {
      this.$set(this, `selected`, data);
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 类型
      res = await this.dictQuery({ code: 'to_type' });
      if (this.$checkRes(res)) {
        this.$set(this, `typeList`, res.data);
      }
      res = await this.dictQuery({ code: 'status' });
      if (this.$checkRes(res)) {
        this.$set(this, `statusList`, 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>
