<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>
    <e-dialog :dialog="dialog" @toClose="toClose">
      <template v-slot:info>
        <el-table :data="set" border size="mini">
          <el-table-column label="店铺名称" prop="shop_name" align="center">
            <template #default="{ row }">
              <p>{{ row.shop_name }}</p>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" prop="goods_name" align="center">
            <template #default="{ row }">
              <p>{{ row.goods_name }}</p>
            </template>
          </el-table-column>
          <el-table-column label="规格名称" prop="spec_name" align="center">
            <template #default="{ row }">
              <p>{{ row.spec_name }}</p>
            </template>
          </el-table-column>
          <el-table-column label="组合套装数量" prop="set_num" align="center">
            <template #default="{ row }">
              <p>{{ row.set_num }}</p>
            </template>
          </el-table-column>
          <el-table-column label="套装销售金额" prop="set_money" align="center">
            <template #default="{ row }">
              <p>{{ row.set_money }}</p>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </e-dialog>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('goodsSet');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
export default {
  name: 'index',
  props: {},
  components: {
    search1: () => import('./parts/search-1.vue'),
    detail: () => import('./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: 'name' },
        // { label: '店铺名称', model: 'shop', format: (i) => i.join(',') },
        // { label: '商品', model: 'goods', format: (i) => i.join(',') },
        // { label: '规格', model: 'spec', format: (i) => i.join(',') },
        { label: '套装销售金额', model: 'sell_money' },
        { label: '运费', model: 'freight' },
        {
          label: '是否开启',
          model: 'is_use',
          format: (i) => {
            let data = that.isuseList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
      ],
      list: [],
      total: 0,
      // 查询
      searchForm: {},
      // 是否开启
      isuseList: [],
      id: '',
      // 查询条件
      searchQuery: {},
      set: [],
      // 弹框
      dialog: { title: '信息管理', show: false, type: '1' },
    };
  },
  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.$set(this, `set`, data.set);
      this.dialog = { title: '信息管理', show: true, type: '1' };
    },
    // 关闭
    toClose() {
      this.set = [];
      this.searchForm = {};
      this.dialog = { title: '信息管理', show: false, type: '1' };
      this.search();
    },
    // 删除
    async toDel({ data }) {
      let res = await this.delete(data._id);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `刪除信息成功` });
        this.search(this.searchQuery);
      }
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 是否使用
      res = await this.dictQuery({ code: 'use' });
      if (this.$checkRes(res)) this.$set(this, `isuseList`, 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;
  }
}
.el-col {
  margin: 10px 0;
}
</style>
