<template>
  <div id="card-1">
    <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"
            :statusList="statusList"
            @onSubmit="search"
            @toReset="toClose"
            @querySearch="querySearch"
            :shopList="shopList"
            :discount_typeList="discount_typeList"
          ></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, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('coupon');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
const { mapActions: shop } = createNamespacedHelpers('shop');

export default {
  name: 'card-1',
  props: {},
  components: { search1: () => import('./parts/search-1.vue') },
  data: function () {
    const that = this;
    return {
      // 查询
      searchForm: {},
      list: [],
      total: 0,
      opera: [
        { label: '修改', method: 'edit' },
        { label: '删除', method: 'del', confirm: true, type: 'danger' },
      ],
      fields: [
        {
          label: '发行方',
          model: 'issue',
          format: (i) => {
            let data = that.issueList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
        // { label: '商铺', model: 'shop.name' },
        { label: '优惠券名称', model: 'name' },
        { label: '优惠券数量', model: 'num' },
        {
          label: '减免方式',
          model: 'discount_type',
          format: (i) => {
            let data = that.discount_typeList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
        {
          label: '是否使用',
          model: 'status',
          format: (i) => {
            let data = that.statusList.find((f) => f.value == i);
            if (data) return data.label;
            else return '暂无';
          },
        },
      ],
      // 多选值
      selected: [],
      // 发行方
      issueList: [],
      // 是否使用
      statusList: [],
      // 商铺
      shopList: [],
      // 减免方式
      discount_typeList: [],
    };
  },
  async created() {
    await this.search();
    await this.searchOther();
  },
  methods: {
    ...shop({ shopQuery: 'query' }),
    ...dictData({ dictQuery: 'query' }),
    ...mapActions(['query', 'fetch', 'create', 'update', 'delete']),
    // 查询
    async search({ skip = 0, limit = 10, ...info } = {}) {
      let 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/coupon/detail' });
    },
    // 修改
    async toEdit({ data }) {
      this.$router.push({ path: '/system/coupon/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.$emit('handleSelect');
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 发行方
      res = await this.dictQuery({ code: 'coupon_issue', value: '0' });
      if (this.$checkRes(res)) {
        this.$set(this, `issueList`, res.data);
      }
      // 使用状态
      res = await this.dictQuery({ code: 'use' });
      if (this.$checkRes(res)) {
        this.$set(this, `statusList`, res.data);
      }

      // 减免方式
      res = await this.dictQuery({ code: 'coupon_discount_type' });
      if (this.$checkRes(res)) {
        this.$set(this, `discount_typeList`, res.data);
      }
    },
    // 店铺名称远程查询
    async querySearch(value) {
      let res = await this.shopQuery({ name: value });
      if (this.$checkRes(res)) {
        this.$set(this, 'shopList', 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>
