<template>
  <view class="order">
    <headersVue @headerJump="topgi" headerTitle="订单列表" />
    <view class="order-list-filter-wrapper">
      <view class="order-search_customs">
        <searchVue @search="search"></searchVue>
      </view>
      <view class="order-nbeirong_customs">
        <tabsVue
          :category="num"
          :curruntActive="indexs"
          @tabs="tabsHandleClick"
        />
      </view>
    </view>

    <view class="order-list">
      <template v-if="list.length > 0">
        <template v-for="(item, index) in list">
          <orderListCardVue
            class="order-list__card"
            :key="index + 'index'"
            @handleSendOutGoods="handleSendOutGoods"
            :item="item"
            :index="index"
          />
        </template>
      </template>
      <template v-else>
        <emptyDataVue />
      </template>
    </view>

    <view
      class=""
      v-if="tihsi == 0 && list.length > 0"
      style="margin: 60rpx 0 30rpx 0; color: #999999; font-size: 26rpx"
    >
      —— 已经到最底部了哦 ——
    </view>
  </view>
</template>

<script>
import {
  getOrderStatusNumber,
  getOrderList,
  getAfterSaleList,
} from "@/commit/order.js";
import emptyDataVue from "../components/emptyData/emptyData.vue";
import tabsVue from "../components/tabs/tabs/tabs.vue";
import orderListCardVue from "./components/orderList/orderListCard.vue";
import searchVue from "./components/search/search.vue";
import headersVue from "../components/headers/headers.vue";
import { toQueryString } from "@/utils/index.js";
export default {
  components: {
    emptyDataVue,
    tabsVue,
    orderListCardVue,
    searchVue,
    headersVue,
  },
  data() {
    return {
      page: 1,
      limit: 10,
      tihsi: "",
      is_panudn: true,
      //uid: 11, //	用户ID 查询用户订单列表需要传
      mer_id: "", //商户ID    查询商户订单列表的时候需要传
      status: "", //0：待发货；1：待收货；2：待评价；3：已完成；4已申请退款；-1：已退款）5未支付WADDW
      indexs: 0,
      // sty: true,
      list: [],
      keywords: "",
      num: [
        {
          name: "全部",
          status: null,
          totalNum: 0,
          show: true,
        },
        {
          name: "待付款",
          status: 1,
          totalNum: 0,
          show: true,
        },
        {
          name: "待发货",
          status: 2,
          totalNum: 0,
          show: true,
        },
        {
          name: "待收货",
          status: 3,
          totalNum: 0,
          show: true,
        },
        {
          name: "待评价", //售后中
          status: 4,
          totalNum: 0,
          show: true,
        },
        {
          name: "已完成",
          status: 5,
          totalNum: 0,
          show: true,
        },
        {
          name: "已退款",
          status: 6,
          totalNum: 0,
          show: true,
        },
        {
          name: "已删除",
          status: 7,
          totalNum: 0,
          show: true,
        },
      ],
    };
  },
  onLoad(e) {
    let selectmerchant = uni.getStorageSync("selectmerchant");
    this.mer_id = selectmerchant.merId;
    
    let tabIndex = e.index == null ? 0 : e.index;
    this.indexs = tabIndex;
    if (e.index !=  e.status) {
      this.status = tabIndex;
    } else {
      this.status = e.status; //进入状态
    }
    this.orderList();
    this.getOrderStatusNumberData();
  },

  onPullDownRefresh() {
    uni.showLoading({
      title: "加载中",
    });
    this.page = 1;
    this.is_panudn = true;
    this.list = [];
    this.orderList();
  },
  onReachBottom() {
    if (this.is_panudn == true) {
      this.page++;
      this.orderList();
    } else {
      return;
    }
  },
  methods: {
    async getOrderStatusNumberData() {
      try {
        const res = await getOrderStatusNumber({
          order_sn: "",
          order_type: "-1",
          distribution_mode: "-1",
          keywords: "",
          status: "",
          date: "",
          page: 1,
          limit: 10,
          type: 1,
          username: "",
          pay_type: -1,
          ylj_status: "",
          paid: "",
        });
        if (res.data.status == 200) {
          this.num[0].totalNum = res.data.data.all || 0; //全部
          this.num[1].totalNum = res.data.data.unpaid || 0; //待付款
          this.num[2].totalNum = res.data.data.unshipped || 0; //待发货
          this.num[3].totalNum = res.data.data.untake || 0; //待收货
          this.num[4].totalNum = res.data.data.unevaluate || 0; //待评价
          this.num[5].totalNum = res.data.data.complete || 0; //已完成
          this.num[6].totalNum = res.data.data.refund || 0; //已退款
          this.num[7].totalNum = res.data.data.del || 0; //已删除
        }
      } catch (error) {
      } finally {
        uni.hideLoading();
      }
    },
    async search(val) {
      this.keywords = val;
      await this.orderList();
    },
    getStatus(val) {
      if (val == undefined || val === "all" || val === null) {
        return "";
      } else {
        return val;
      }
    },

    handleSendOutGoods(v, type) {
      console.log(v, "v");
      let urlParams = {
        order_id: v.order_id,
        status: v.status,
        indexs: this.indexs,
        goodsCustomType: type,
        formType: "oldInfo",
      };
      let url = toQueryString(urlParams);
      let redirectToUrl = "";
      if (type === "detail") {
        redirectToUrl = `./details?${url}&detailInfo=${encodeURIComponent(JSON.stringify(v.orderProduct))}`;
      } else if (type === "logistics") {
          redirectToUrl = `./logisticsInformationDetail?${url}`;                           
      } else if (type === "sendGoods") {
        //发货
        redirectToUrl = `./sendoutgoods?${url}`;
      } else if (type === "delivered") {
        //立即发货
      } else if (type === "deliverGoods") {
        //送貨
      } else if (type === "pickup") {
        //立即自提
      }
      if (!redirectToUrl) {
        return;
      }
      uni.redirectTo({
        url: redirectToUrl,
      });
      // const newUrl = {
      //   sendGoods: `./sendoutgoods?${url}`, //立即发货
      //   delivered: `./sendoutgoods?${url}`, //已送达
      //   logistics: `./logisticsInformationDetail?${url}`, //查看物流
      //   deliverGoods: `./sendoutgoods?${url}`, //送貨
      //   pickup: `./sendoutgoods?${url}`, //立即自提
      //   detail: `./details?${url}`,
      // };
      // let redirectToUrl = newUrl[type] || newUrl["logistics"];
      // console.log(redirectToUrl, "redirectToUrl");
      // console.log(type, "type");
      // uni.redirectTo({
      //   url: redirectToUrl,
      // });
    },
    // 正向订单列表
    orderList() {
      uni.showLoading({
        title: "加载中",
      });

      getOrderList({
        // mer_id: this.mer_id,
        status: this.status,
        order_sn: "",
        order_type: -1,
        distribution_mode: -1,
        keywords: this.keywords,
        status: this.getStatus(this.status),
        date: "",
        page: this.page,
        limit: this.limit,
        type: 1,
        username: "",
        pay_type: -1,
        ylj_status: "",
        paid: "",
      }).then(
        (res) => {
          uni.stopPullDownRefresh();
          if (res.data.status == 200) {
            // this.tihsi = res.data.data.count || 0;
            this.tihsi = res.data.data.list.lenght || 0;
            if (res.data.data.count > 0) {
              if (this.page == 1) {
                this.list = res.data.data.list;
              } else {
                this.list = this.list.concat(res.data.data.list);
              }
            } else {
              if (this.page == 1) {
                this.list = [];
              }
              this.is_panudn = false;
            }
            uni.hideLoading();
          }
        },
        (fail) => {
          console.log(fail);
          uni.hideLoading();
        }
      );
    },
    details(e) {
      uni.redirectTo({
        // + "&status=" + this.status + "&indexs=" + this.indexs
        url:
          "./details?order_id=" +
          e +
          "&status=" +
          this.status +
          "&indexs=" +
          this.indexs,
      });
    },
    tabsHandleClick({ index, item }) {
      this.ysne(index, item.status);
    },
    ysne(index, status) {
      // console.log(index, status, "---tabsHandleClick")
      this.status = index == 0 ? null : status;
      this.indexs = index;
      this.page = 1;
       this.list = [];
      this.is_panudn = true;
       uni.pageScrollTo({ scrollTop: 0, duration: 0 });
      this.orderList();
    },
    topgi() {
      uni.switchTab({
        url: "../../pages/ordermanagement/index",
      });
    },
  },
};
</script>

<style>
page {
  background-color: #f7f8fc;
  /* padding-top: 90rpx; */
}
</style>
<style lang="scss" scoped>
.order {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  .order-nbeirong {
    width: 686rpx;
    height: 30rpx;
    margin: 204rpx 0 38rpx 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  .order-list-filter-wrapper {
    margin: 90rpx 0rpx 0rpx;
    width: 100%;
    position: fixed;
    top: 0rpx;
    z-index: 99;
    .order-nbeirong_customs {
      width: 100%;
      min-height: 30rpx;

      margin: 0rpx 0 26rpx 0;
    }
    .order-search_customs {
      width: 100%;
      min-height: 30rpx;
    }
  }
  .order-list {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 340rpx;
    .order-list__card {
      width: calc(100% - 40rpx);
      margin: 0 20rpx 26rpx;
    }
  }
}
</style>
