<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" :typeList="typeList" @onSubmit="search" @toReset="toClose"> </search-1>
        </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"
            @exam="toExam"
          >
          </data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('afterSale');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
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: 'exam' }],
      fields: [
        { label: '顾客', model: 'customer.name' },
        { label: '商品名称', model: 'goods.goods.name' },
        {
          label: '售后类型',
          model: 'type',
          format: (i) => {
            let data = that.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 '暂无';
          },
        },
        { label: '申请时间', model: 'apply_time' },
        { label: '结束时间', model: 'end_time' },
      ],
      // 多选值
      selected: [],
      typeList: [],
      statusList: [],
    };
  },
  async created() {
    await this.search();
    await this.searchOther();
  },
  methods: {
    ...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, shop: _.get(this.shop, '_id') });
      if (this.$checkRes(res)) {
        this.$set(this, 'list', res.data);
        this.$set(this, 'total', res.total);
      }
    },
    toExam({ data }) {
      this.$router.push({ path: '/selfShop/sales/detail', query: { id: data.id } });
    },
    toClose() {
      this.searchForm = {};
      this.search();
    },
    // 多选
    handleSelect(data) {
      this.$emit('handleSelect');
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 类型
      res = await this.dictQuery({ code: 'afterSale_type' });
      if (this.$checkRes(res)) {
        this.$set(this, `typeList`, res.data);
      }
      // 售后状态
      res = await this.dictQuery({ code: 'afterSale_status' });
      if (this.$checkRes(res)) {
        this.$set(this, `statusList`, 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>
.one {
  margin: 0 0 10px 0;
}
.two {
  margin: 0 0 10px 0;
}
</style>
