<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="four">
            <el-tabs v-model="activeName" @tab-click="handleClick" type="border-card">
              <el-tab-pane name="1" label="待付款"> </el-tab-pane>
              <el-tab-pane name="2" label="待发货"> </el-tab-pane>
              <el-tab-pane name="3" label="待收货"> </el-tab-pane>
              <el-tab-pane name="4" label="已收货"> </el-tab-pane>
              <el-tab-pane name="5" label="取消订单"> </el-tab-pane>
              <!-- 待发货 -->
              <el-col :span="24" v-if="activeName == '2'">
                <card-2 :statusList="statusList" @toDetails="toDetail_t" @toSaless="toSaless_t"></card-2>
              </el-col>
              <el-col :span="24" v-else-if="activeName != '2'">
                <el-col :span="24" class="one">
                  <search-1
                    :form="searchForm"
                    :shopList="shopList"
                    :goodsList="goodsList"
                    @onSubmit="toSearch"
                    @querySearch="querySearch"
                    @toReset="toClose"
                    @goodsSearch="goodsSearch"
                  >
                  </search-1>
                </el-col>
                <data-table ref="dataTable" :fields="fields" :opera="opera" @query="search" :data="list" :total="total" @detail="toDetails" @sales="toSaless">
                  <template #is_afterSale="{ row }">
                    <span :style="{ color: row.is_afterSale === true ? 'red' : '' }">
                      {{ row.is_afterSale === true ? '该订单有商品申请售后' : '未申请售后' }}
                    </span>
                  </template>
                </data-table>
              </el-col>
            </el-tabs>
          </el-col>
        </span>
        <group_order v-if="view === 'card_detail'" :id="order_id" @toBack="toBack"></group_order>
        <group_sales v-else-if="view === 'card_sales'" :id="sales_id" :status="status" @toBack="toBack"></group_sales>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions: dictData } = createNamespacedHelpers('dictData');
const { mapActions: shop } = createNamespacedHelpers('shop');
const { mapActions: goods } = createNamespacedHelpers('goods');
const { mapActions } = createNamespacedHelpers('groupOrder');
export default {
  name: 'index',
  props: {},
  components: {
    card2: () => import('@/components/orderParts/card/card-4.vue'),
    group_order: () => import('@/components/orderParts/detail/group_order.vue'),
    group_sales: () => import('@/components/orderParts/detail/group_sales.vue'),
    search1: () => import('@/components/orderParts/search/search-3.vue'),
  },
  data: function () {
    return {
      loadings: true,
      view: 'list',
      activeName: '2',
      // 类型列表
      statusList: [],
      // 店铺列表
      shopList: [],
      // 商品列表
      goodsList: [],
      // 规格列表
      // specList: [],
      order_id: '',
      sales_id: '',
      status: '',
      searchForm: {},
      list: [],
      total: 0,
      opera: [
        { label: '详情', method: 'detail' },
        { label: '售后', method: 'sales', type: 'danger', display: (i) => this.activeName == '1' || this.activeName == '3' || this.activeName == '4' },
      ],
      fields: [
        { label: '订单号', model: 'no', showTip: false },
        { label: '下单时间', model: 'buy_time' },
        { label: '顾客', model: 'customer' },
        { label: '收货人', model: 'address', showTip: false, format: (i) => this.getAddress(i) },
        { label: '商品名称', model: 'goods' },
        { label: '规格名称', model: 'spec' },
        { label: '支付金额', model: 'pay' },
        { label: '购买数量', model: 'num' },
        { label: '是否售后', model: 'is_afterSale', format: (i) => (i === true ? '该订单有商品申请售后' : '未申请售后'), custom: true },
      ],
      // 查询条件
      searchQuery: {},
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...shop({ shopQuery: 'query' }),
    ...goods({ goodsQuery: '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, ...info } = {}) {
      let condition = _.cloneDeep(this.searchForm);
      if (condition.buy_time) {
        condition[`buy_time@start`] = _.head(condition.buy_time);
        condition[`buy_time@end`] = _.last(condition.buy_time);
        delete condition.buy_time;
      }
      if (this.activeName == '1') info.status = '0';
      else if (this.activeName == '2') info.status = '1';
      else if (this.activeName == '3') info.status = '2';
      else if (this.activeName == '4') info.status = '3';
      else if (this.activeName == '5') info.status = '-1';
      let query = { skip, limit, ...info };
      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;
    },
    // 待收货，已收货
    toDetails({ data }) {
      this.$set(this, `order_id`, data._id);
      this.$set(this, `view`, 'card_detail');
    },
    toSaless({ data }) {
      this.$set(this, `sales_id`, data._id);
      this.$set(this, `status`, data.status);
      this.$set(this, `view`, 'card_sales');
    },
    // 待发货
    toDetail_t(data) {
      this.$set(this, `order_id`, data);
      this.$set(this, `view`, 'card_detail');
    },
    toSaless_t(data) {
      this.$set(this, `sales_id`, data.id);
      this.$set(this, `status`, data.status);
      this.$set(this, `view`, 'card_sales');
    },
    handleClick(tab) {
      this.loadings = true;
      this.search();
    },
    toBack() {
      this.view = 'list';
    },
    getAddress(i) {
      let name = i.name + ',' + i.phone;
      return name;
    },
    // 店铺名称
    getShopName(i) {
      let shopname = i.map((e) => e.shop_name);
      return shopname.join(',');
    },

    // 重置
    toClose() {
      this.searchForm = {};
      this.search();
    },
    // 店铺名称远程查询
    async querySearch(value) {
      let res = await this.shopQuery({ name: value });
      if (this.$checkRes(res)) this.$set(this, 'shopList', res.data);
    },
    //商品名称远程查询
    async goodsSearch(value) {
      let res = await this.goodsQuery({ name: value });
      if (this.$checkRes(res)) this.$set(this, 'goodsList', res.data);
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 类型
      res = await this.dictQuery({ code: 'order_process' });
      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>
.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>
