<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">
                <el-option v-for="item in goodsList" :key="item._id" :label="item.name" :value="item._id"> </el-option>
              </el-select>
            </template>
            <template #leader>
              <el-select v-model="searchInfo.leader" filterable clearable remote :remote-method="leaderSearch" placeholder="请选择团长名称" :loading="loading">
                <el-option v-for="item in leaderList" :key="item._id" :label="item.name" :value="item._id"> </el-option>
              </el-select>
            </template> -->
            <template #status>
              <el-option v-for="i in statusList" :key="i.value" :label="i.label" :value="i.value"></el-option>
            </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: group } = createNamespacedHelpers('group'); // 团表
const { mapActions: dictData } = createNamespacedHelpers('dictData'); // 字典
const { mapActions: goods } = createNamespacedHelpers('goods'); // 商品
const { mapActions: users } = createNamespacedHelpers('users'); // 团长
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: 'leader.name' },
        { label: '人数限制', model: 'person_limit' },
        { label: '开始时间', model: 'start_time' },
        { label: '结束时间', model: 'end_time' },
        {
          label: '团状态',
          model: 'status',
          format: (i) => {
            let data = this.statusList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
      ],
      opera: [
        { label: '修改', method: 'edit' },
        // { label: '删除', method: 'delete', confirm: true, type: 'danger' },
      ],
      btnList: [{ label: '添加', method: 'add' }],
      searchFields: [
        // { label: '商品名称', model: 'goods', custom: true },
        // { label: '团长', model: 'leader', custom: true },
        { label: '状态', model: 'status', type: 'select' },
      ],
      searchInfo: {},
      list: [],
      total: 0,
      // 团状态
      statusList: [],
      id: '',
      // 商品列表
      goodsList: [],
      // 团长列表
      leaderList: [],
    };
  },
  created() {
    this.searchOthers();
    this.search();
  },
  methods: {
    ...goods({ goodsQuery: 'query' }),
    ...users({ userQuery: 'query' }),
    ...dictData({ getDict: 'query' }),
    ...group(['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 leaderSearch(value) {
      this.loading = true;
      let res = await this.userQuery({ name: value, is_leader: '0' });
      if (this.$checkRes(res)) this.$set(this, 'leaderList', 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: 'group_status' });
      if (this.$checkRes(res)) this.$set(this, `statusList`, 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;
  }
}
</style>
