<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 #goods>
              <el-select
                v-model="searchInfo.goods"
                filterable
                clearable
                remote
                :remote-method="querySearch"
                placeholder="请选择商品名称"
                :loading="loading"
                size="small"
                style="width: 100%"
              >
                <el-option v-for="item in goodsList" :key="item._id" :label="item.name" :value="item._id"> </el-option>
              </el-select>
            </template>
            <template #spec>
              <el-select
                v-model="searchInfo.spec"
                filterable
                clearable
                remote
                :remote-method="specSearch"
                placeholder="请选择规格名称"
                :loading="loading"
                size="small"
                style="width: 100%"
              >
                <el-option v-for="item in specList" :key="item._id" :label="item.name" :value="item._id"> </el-option>
              </el-select>
            </template>
          </data-search>
          <data-btn :fields="btnList" @add="toAdd"></data-btn>
          <data-table
            ref="dataTable"
            :fields="fields"
            :opera="opera"
            :data="list"
            :total="total"
            @query="search"
            @edit="toEdit"
            @delete="toDelete"
          ></data-table>
        </span>
        <detail v-if="view === 'info'" :id="id" @toBack="toBack"></detail>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: goodsConfig } = createNamespacedHelpers('goodsConfig');
const { mapActions: goods } = createNamespacedHelpers('goods'); // 商品
const { mapActions: goodsSpec } = createNamespacedHelpers('goodsSpec'); // 商品规格
const { mapActions: dictData } = createNamespacedHelpers('dictData');

export default {
  name: 'index',
  props: {},
  components: {
    detail: () => import('./detail.vue'),
  },
  data: function () {
    return {
      loadings: true,
      loading: false,
      view: 'list',
      fields: [
        { label: '商品名称', model: 'goods.name', showTip: false },
        { label: '规格名称', model: 'spec.name' },
        { label: '团购价', model: 'price' },
        { label: '团长提成金额', model: 'leader_get' },
        { label: '运费', model: 'freight' },
        {
          label: '购买限制',
          model: 'buy_limit',
          format: (i) => {
            let data = this.buy_limitList.find((f) => f.value == i);
            if (data) return data.label;
            else return '';
          },
        },
        { label: '购买数量界限', model: 'limit_num' },
      ],
      opera: [
        { label: '修改', method: 'edit' },
        { label: '删除', method: 'delete', confirm: true, type: 'danger' },
      ],
      btnList: [{ label: '添加', method: 'add' }],
      searchFields: [
        { label: '商品名称', model: 'goods', custom: true },
        { label: '规格名称', model: 'spec', custom: true },
      ],
      searchInfo: {},
      list: [],
      total: 0,
      id: '',
      // 商品列表
      goodsList: [],
      // 规格列表
      specList: [],
      // 购买限制
      buy_limitList: [],
    };
  },
  created() {
    this.searchOthers();

    this.search();
  },
  methods: {
    ...dictData({ getDict: 'query' }),
    ...goods({ goodsQuery: 'query' }),
    ...goodsSpec({ specQuery: 'query' }),
    ...goodsConfig(['query', 'delete', 'fetch', 'update', 'create']),
    // 查询
    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.query(query);
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
      this.loadings = false;
    },
    // 远程查询
    async querySearch(value) {
      this.loading = true;
      let res = await this.goodsQuery({ name: value });
      if (this.$checkRes(res)) this.$set(this, 'goodsList', res.data);
      this.loading = false;
    },
    async specSearch(value) {
      this.loading = true;
      let res = await this.specQuery({ name: value });
      if (this.$checkRes(res)) this.$set(this, 'specList', res.data);
      this.loading = 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');
    },
    // 删除
    async toDelete({ data }) {
      let res = await this.delete(data._id);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `刪除信息成功` });
        this.search();
      }
    },

    // 执行返回
    toBack() {
      this.view = 'list';
    },
    // 查询其他信息
    async searchOthers() {
      // 购买限制
      let res = await this.getDict({ code: 'buy_limit' });
      if (this.$checkRes(res)) this.$set(this, 'buy_limitList', 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: 0 0 10px 0;
  }
  .thr {
    margin: 0 0 10px 0;
  }
}
.el-col {
  margin: 10px 0;
}
</style>
