<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-tab-pane name="6" label="取消订单"> </el-tab-pane>
              <!-- 待付款 -->
              <el-col :span="24" v-if="activeName == '1'">
                <card-1 :statusList="statusList" @toDetail="toDetail" @toSales="toSales"></card-1>
              </el-col>
              <el-col :span="24" v-else-if="activeName != '1'">
                <el-col :span="24" class="one">
                  <search-1 :form="searchForm" @onSubmit="toSearch" @querySearch="querySearch" @toReset="toClose" :shopList="shopList"> </search-1>
                </el-col>
                <data-table
                  ref="dataTable"
                  :fields="fields"
                  :opera="opera"
                  @query="search"
                  :data="list"
                  :total="total"
                  @detail="toDetails"
                  @sales="toSaless"
                  @order="toOrder"
                >
                  <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>
        <order v-if="view === 'order'" :id="order_id" @toBack="toBack"></order>
        <orderDetaile v-else-if="view === 'orderDetaile'" :id="order_id" @toBack="toBack"></orderDetaile>
        <salseOrder v-else-if="view === 'salseOrder'" :id="sales_id" @toBack="toBack"></salseOrder>
        <salseOrderDetail v-else-if="view === 'salseOrderDetail'" :id="sales_id" :status="status" @toBack="toBack"></salseOrderDetail>
      </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 } = createNamespacedHelpers('orderDetail');
const { mapActions: shopNotice } = createNamespacedHelpers('shopNotice');

export default {
  name: 'index',
  props: {},
  components: {
    // 未付款---列表
    card1: () => import('@/components/orderParts/card/card-1.vue'),
    // 未付款---详情
    order: () => import('@/components/orderParts/detail/order.vue'),
    // 待发货，待收货，已收货---详情
    orderDetaile: () => import('@/components/orderParts/detail/orderDetaile.vue'),
    // 未付款----申请售后
    salseOrder: () => import('@/components/orderParts/detail/salseOrder.vue'),
    // 待发货，待收货，已收货----申请售后
    salseOrderDetail: () => import('@/components/orderParts/detail/salseOrderDetail.vue'),
    search1: () => import('@/components/orderParts/search/search-1.vue'),
  },
  data: function () {
    return {
      loadings: true,
      view: 'list',
      activeName: '2',
      // 类型列表
      statusList: [],
      // 店铺列表
      shopList: [],
      order_id: '',
      sales_id: '',
      // 状态
      status: '',
      // 查询
      searchForm: {},
      list: [],
      total: 0,
      opera: [
        { label: '详情', method: 'detail' },
        { label: '售后', method: 'sales', type: 'danger', display: (i) => i.status == '1' || i.status == '2' || i.status == '2-' || i.status == '3' },
        { label: '提醒发货', method: 'order', type: 'warning', display: (i) => i.status == '1' },
      ],
      fields: [
        { label: '订单号', model: 'no', showTip: false },
        { label: '下单时间', model: 'buy_time' },
        { label: '顾客', model: 'customer.name' },
        { label: '收货人', model: 'address', showTip: false, format: (i) => this.getAddress(i) },
        { label: '支付金额', model: 'real_pay' },
        { label: '商品数量', model: 'buy_num_total' },
        { 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' }),
    ...shopNotice({ shopRts: 'rts' }),
    ...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 == '2') info.status = '1';
      else if (this.activeName == '1') info.status = '0';
      else if (this.activeName == '3') info.status = '2-';
      else if (this.activeName == '4') info.status = '2';
      else if (this.activeName == '5') info.status = '3';
      else if (this.activeName == '6') 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;
    },
    toOrder({ data }) {
      let info = { source_id: data._id };
      this.$confirm('是否确认提醒发货', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        let res = await this.shopRts(info);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `提醒发货成功` });
          this.search();
        }
      });
    },
    // 未付款---详情
    toDetail(val) {
      this.$set(this, `order_id`, val);
      this.$set(this, `view`, 'order');
    },
    // 未付款----申请售后
    toSales(val) {
      this.$set(this, `sales_id`, val);
      this.$set(this, `view`, 'salseOrder');
    },
    // 待发货，待收货，已收货---详情
    toDetails({ data }) {
      this.$set(this, `order_id`, data._id);
      this.$set(this, `view`, 'orderDetaile');
    },
    // 待发货，待收货，已收货-----申请售后
    toSaless({ data }) {
      this.$set(this, `sales_id`, data._id);
      this.$set(this, `status`, data.status);
      this.$set(this, `view`, 'salseOrderDetail');
    },
    handleClick(tab) {
      this.loadings = true;
      this.search();
    },
    toBack() {
      this.view = 'list';
      this.search(this.searchQuery);
    },
    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 searchOther() {
      let res;
      // 类型
      res = await this.dictQuery({ code: 'order_process' });
      if (this.$checkRes(res)) this.$set(this, `statusList`, res.data);
      res = await this.shopQuery();
      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;
  }
}
.el-col {
  margin: 10px 0;
}
</style>
