<template>
  <view class="dt-page">
    <!-- <view class="scroll-tab-wrap">
      <DtScrollTab 
        :config="configTab"
        :dataList="tabList" 
        :current.sync="curTab" 
        @change="onTabChange"/>
    </view> -->
    <!-- <swiper :style="calcHeight" duration="300"
      :current="curTab" 
      @change="onSwiperChange">
      <swiper-item v-for="(tItem, tIdx) in tabList" :key="tIdx"> -->
        <!-- <scroll-view @scrolltolower="onReachLower" scroll-y="true" class="scoll-h"> -->
          <!-- <block v-if="curTab==tIdx"> -->
            <view v-if="emptyType==0" class="content-wrap">
              <view v-for="(item,idx) in dataList" :key="idx" class="order-item-wrap">
                <DtOrderItem 
                  :dataDetail="item"
                  @timer="onTimer(idx)"
                  @tapbtn="onTapBtn"
                  @order="onOrderTap" />
                <DtNoMore v-if="isNoMore" />
              </view>
            </view>
            <DtEmpty :type="emptyType" />
          <!-- </block> -->
        <!-- </scroll-view>
      </swiper-item>
    </swiper> -->
  </view>
</template>

<script>
import DtScrollTab from '../comps/dt_scroll_tab.vue'
import DtOrderItem from '../comps/dt_order_item.vue'
import DtEmpty from '../comps/dt_empty.vue'

export default {
  components:{
    DtScrollTab,
    DtOrderItem,
    DtEmpty
  },
  data(){
    return {
      // configTab:{
      //   width:'20vw',
      //   isActiveLine:true
      // },
      // curTab:0,
      // tabList:['全部','待付款','待发货','待收货','已取消','已完成','退货退款'],
      // winHeight: "", //窗口高度,
      dataList:[],
      memberId:'',
    }
  },
  computed:{
    calcHeight () {
      return "height:"+this.winHeight+"px;";
    },
    tabValues(){
      return [
        this.orderState.all,
        this.orderState.waitForPay,
        this.orderState.waitForDelive,
        this.orderState.waitForReceive,
        this.orderState.cancel,
        this.orderState.finished,
        // this.orderState.
      ]
    },
    orderState(){
      return this.$global.orderState
    },
    refundState(){
      return [
        this.orderState.refunded,     // 已退款
        this.orderState.failRefund,   // 退款失败
        this.orderState.refunding,    // 退款中
      ]
    },
  },
  methods:{
    
    // onSwiperChange(e){
    //   console.log(113,e)
    //   this.curTab = e.detail.current
    //   this.isNoMore = false
    //   this.emptyType = -1
    //   this.pageIndex = 0;
    //   this.dataList.length = 0
    //   this.queryDataList(this.tabValues[this.curTab])
    // },
    // onTabChange(idx){
    //   console.log(23,this.tabValues[idx])
    // },
    onOrderTap(item){
      uni.navigateTo({
        url:'/pagesM/pages/order_detail?orderSns='+item.sn
      })
    },
    // 过期
    onTimer(idx){
      let dataList = this.dataList.slice(0)
      let curItem = dataList[idx]
      let state = this.orderState.cancel
      curItem.status = state
      curItem.statusTxt = this.$global.orderStateName[state]
      dataList[idx] = curItem
      this.dataList = dataList
    },
    onTapBtn(flag,item){
      console.log(118,flag,item)
      switch(flag){
        case 'cancel':
          this.$dialog.confirm({
            content:'确认取消该订单吗？',
            success: async (res) => {
              if (res.confirm) {
                this.cancelOrder(item)
              }
            }
          })
          
          break;
        case 'pay':
          this.payOrder(item)
          break;
        case 'refund':
          this.refund(item)
          break;
        case 'cancelRefund':
          this.cancelRefund(item)
          break;
        case 'receive':
          this.receive(item)
          break;
        case 'appraise':
          uni.navigateTo({
            url:'/pagesM/pages/order_appraise?orderId='+item.orderId
          })
          break;
        case 'delete':
          this.$dialog.confirm({
            content:'确认删除该订单吗？',
            success: async (res) => {
              if (res.confirm) {
                this.delOrder(item)
              }
            }
          })
          break;
        default:break;
      }
    },
    async cancelOrder(item){
      let resp = await this.$api.cancelOrder({
        sn:item.sn,
        detail:'订单取消',
        remark:'B2C微信小程序用户取消订单',
      })
      this.$util.refreshPage(['pages/mine'])
      this.isPull = true
      this.queryDataList()
    },
    async payOrder(item){
      let sns = [item.sn]
      uni.navigateTo({ url: "/pagesM/pages/order_pay?orderSns=" + JSON.stringify(sns)+"&payFlag="+this.$global.payFlag.order});
    },
    async refund(item){
      
    },
    async cancelRefund(item){
      // let resp = await this.$api.
    },
    async receiveOrder(item){
      let resp = await this.$api.orderChange({
        memberId:this.memberId,
        orderId:item.id,
        execType:2
      })
      this.$dialog.success('确认收货')
    },
    async delOrder(item){
      let resp = await this.$api.delOrder({
        orderId:item.id
      })
      // this.$dialog.success('删除成功')
      this.$util.refreshPage(['pages/mine'])
      this.isPull = true
      this.queryDataList()
    },
    onReachLower(ev){
      this.onReachBottomPage()
    },
    // 查询数据
    async queryDataList(){
      if(this.isPull){
        this.emptyType = -1
        this.isNoMore = false
        this.pageIndex = 0
        this.dataList.length = 0
      }
      let resp = await this.$api.getAftersaless({
        _isShowLoading:true,
        memberId:this.memberId,
        pageNo:this.pageIndex,
        pageSize:this.pageSize
      })
      let list = this.getDataList(resp)
      list.map((item)=>{
        item.aftersalesItems.map((itm)=>{
          itm.specificationsDesc = itm.specifications.join('+')
          return itm
        })
        return item
      })
      this.dataList = this.dataList.concat(list)
    },
    onLoadPage(options){
	  wx.hideShareMenu();
      if(this.isLoad){
        this.memberId = this.$auth.getMemberId()
        // 初始化高度
        // let res = uni.getSystemInfoSync();
        // let clientHeight = res.windowHeight;
        // let clientWidth = res.windowWidth;
        // let upxR = 750/clientWidth;
        // let calc = clientHeight*upxR-110;
        // let height = uni.upx2px(calc);
        // this.winHeight=height;
      }
      
      this.queryDataList()
    },
  }
}
</script>

<style lang="scss" scoped>
.dt-page{
  min-height:100vh;
  background-color:#f2f2f2;
  .scroll-tab-wrap{
    margin-bottom:20upx;
  }
  .scoll-h {
    height: 100%;
    // .conttent-wrap {
    // }
  }

  .content-wrap{
    .order-item-wrap{
      margin-bottom:20upx;
    }
  }
}
</style>