<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight">
        <el-col class="top-btn">
          <el-button type="primary" size="mini" @click="toBack()">返回</el-button>
        </el-col>
        <el-col :span="24" class="one"> <span>平台活动商品添加</span> </el-col>
        <el-col :span="6" class="two">
          <el-input placeholder="请输入商品名称" v-model="goods">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="24" class="four">
          <data-table :fields="fields" :opera="opera" @query="search" :data="list" :total="total" @add="toAdd"> </data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('goodsJoinAct');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
const { mapActions: goods } = createNamespacedHelpers('goods');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    const that = this;
    return {
      // 列表
      opera: [{ label: '选择规格', method: 'add' }],
      fields: [
        { label: '商品名称', model: 'name', showTip: false },
        // { label: '店铺名称', model: 'shop.name' },
        { label: '浏览量', model: 'view_num' },
        { label: '来源', model: 'source' },
        { label: '发货时间', model: 'send_time' },
        {
          label: '商品状态',
          model: 'status',
          format: (i) => {
            let data = that.statusList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
      ],
      list: [],
      total: 0,
      // 商品搜索
      goods: '',
      // 商品状态
      statusList: [],
    };
  },
  async created() {
    await this.searchOther();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...goods({ gQuery: 'query' }),
    ...mapActions(['query', 'fetch', 'create', 'update', 'delete']),
    // 查询
    async search({ skip = 0, limit = this.$limit, ...info } = {}) {
      if (this.goods) info.name = this.goods;
      let res = await this.gQuery({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, 'list', res.data);
        this.$set(this, 'total', res.total);
      }
    },
    // 新增
    async toAdd({ data }) {
      let query = { platform_act: this.id, shop: data.shop, goods: data._id, platform_act_type: this.type };
      if (this.type == '4') query.goods_type = this.goods_type;
      this.$router.push({ path: './goodsSpec', query: query });
    },
    // 返回
    toBack() {
      window.history.go('-1');
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 商品状态
      res = await this.dictQuery({ code: 'goods_status' });
      if (this.$checkRes(res)) this.$set(this, `statusList`, res.data);
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
    type() {
      return this.$route.query.type;
    },
    goods_type() {
      return this.$route.query.goods_type;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .top-btn {
    margin: 20px 0;
  }
  .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;
    text-align: right;
  }
}
.el-col {
  margin: 10px 0;
}
</style>
