<template>
  <view class="NumberWeight">
	  <!-- 订单分类 -->
	  <scroll-view class="nav" scroll-x="true" :scroll-into-view='selectCategoryId'	scroll-with-animation="true">
	    <view :class="['nav-item' ,selectNavId === item.id?'nav-item-selected':'']" v-for="(item,index) in navList"
	      :key="index" @click="selectNav(item.id)">
		  {{item.name}}
		  {{item.id ==='apply' && count.apply >0 ? '('+count.apply + ')':''}}
		  {{item.id ==='deliver' && count.deliver >0 ?'('+count.deliver + ')':''}}
		  {{item.id ==='delivered' && count.delivered >0 ?'('+count.delivered + ')':''}}
		  {{item.id ==='signed' && count.signed >0 ?'('+count.signed + ')':''}}
		  {{item.id ==='process' && count.process >0 ?'('+count.process + ')':''}}
		  {{item.id ==='completed' && count.completed >0 ?'('+count.completed + ')':''}}
		</view>
	  </scroll-view>
	  
    <!-- 有订单列表 -->
    <view class="" v-if="lists.length > 0">
      <!-- 订单列表 -->
      <view class="order-list" v-for="(item,index) in lists" :key="index">
        <MallRefundsTemplate :item="item" />
        <view class="actions">
          <view @click="goDetail(item.refund_id)" class="nowPay">查看详情</view>
        </view>
      </view>
    </view>
    <!-- 没有订单列表 -->
    <no-list v-if="lists.length === 0" name='no-order' txt="暂无数据~" />
	
	<!-- <u-toast ref="uToast" /> -->
	<u-toast ref="uToast" />
  </view>
</template>

<script>
  import {
    MallRefundsTemplate
  } from '@/components/mall-refunds-template/mall-refunds-template'
  import { mallRefunds } from '@/apis/interfaces/numberWeight'
  export default {
    components: {
      MallRefundsTemplate
    },
    data() {
      return {
        lists: [],
		page:1,
		total:0,
		navList: [{
		    name: '待审核',
		    id: 'apply'
		  },{
		    name: '待返货',
		    id: 'deliver'
		  },{
		    name: '待签收',
		    id: 'delivered'
		  },{
		    name: '已签收',
		    id: 'signed'
		  },
		  {
		    name: '待退权证',
		    id: 'process'
		  },
		  {
		    name: '完成退货',
		    id: 'completed'
		  }
		],
		selectNavId: 'apply',
		count:{}
      };
    },
	onLoad() {
		this.getList()
	},
	onShow(){
		console.log(uni.getStorageSync('refresh'),'getStorageSync')
		if(uni.getStorageSync('refresh')){
			this.reset()
		}
	},
	onUnload() {
		uni.setStorageSync('refresh',false)
	},
	onReachBottom() {
		if(this.total>this.lists.length){
			this.page = this.page + 1
			this.getList()
		}else{
			this.$refs.uToast.show({
			  title: '吼吼吼~我是有底的~',
			  type: 'error',icon:false,
			  duration: 3000
			})
		}
	},
    methods: {
		reset(){
			this.page =1
			this.total = 0
			this.lists = []
			this.getList()
			uni.setStorageSync('refresh',false)
		},
		// 选择订单
		selectNav(id) {
		  if (this.selectNavId !== id) {
		    this.selectNavId = id
			this.reset()
		  }
		},
		// 获取订单列表
		getList(){
			let data = {
				pageSize:20,
				page:this.page,
				state:this.selectNavId
			}
			mallRefunds(data).then(res=>{
				console.log(res)
				this.count = res.count
				this.lists = this.lists.concat(res.lists.data)
				this.total = res.lists.page.total
			}).catch(err=>{
				this.$refs.uToast.show({
				  title: err.message,
				  type: 'error',icon:false,
				  duration: 3000
				})
			})
		},
		// 取消提货单
		nowCancel(index,no){
			console.log(index,no)
			uni.showModal({
			    title: '哎呦，提醒你',
			    content: '是否确认要取消订单啊，取消后请去我的权证中查看',
			    success:  (res) =>{
			        if (res.confirm) {
			            mallShipmentsCancel(no).then(res=>{
			            	console.log(res)
							this.$refs.uToast.show({
							  title:res,
							  type: 'error',icon:false,
							  duration: 3000
							})
			            	this.lists.splice(index,1)
			            	this.total = this.total - 1
			            }).catch(err=>{
			            	this.$refs.uToast.show({
			            	  title: err.message,
			            	  type: 'error',icon:false,
			            	  duration: 3000
			            	})
			            })
			        } else if (res.cancel) {
						this.$refs.uToast.show({
						  title:'放弃了~',
						  type: 'error',icon:false,
						  duration: 3000
						})
			        }
			    }
			});
		
		},
		// 签收提货单
		nowSign(index,no){
			console.log(index,no)
			uni.showModal({
			    title: '哎呦，提醒你',
			    content: '是否确认已经收到商品了呀',
			    success:  (res) =>{
			        if (res.confirm) {
						console.log(no)
			            mallShipmentsSign(no).then(res=>{
			            	console.log(res)
							this.$refs.uToast.show({
							  title:res,
							  type: 'error',icon:false,
							  duration: 3000
							})
			            	this.lists.splice(index,1)
			            	this.total = this.total - 1
			            }).catch(err=>{
			            	this.$refs.uToast.show({
			            	  title: err.message,
			            	  type: 'error',icon:false,
			            	  duration: 3000
			            	})
			            })
			        
					} else if (res.cancel) {
						this.$refs.uToast.show({
						  title:'放弃了~',
						  type: 'error',icon:false,
						  duration: 3000
						})
			        }
			    }
			});
		},
		
		// 查看详情
		goDetail(no){
			this.$router.push({
			  name: 'MallRefundsInfo',
			  params:{
				  no:no
			  }
			})
		}
    }
  }
</script>

<style lang="scss" scoped>
  page {
    width: 100%;
    height: 100%;
  }

  .NumberWeight {
    width: 100%;
    min-height: 100vh;
    box-sizing: border-box;
    background-color: #F7F7F7;

    // 订单nav
    .nav {
	  display: flex;
	  flex-direction: row;
	  align-items: center;
	  justify-content: flex-start;
	  box-sizing: border-box;
	  white-space: nowrap;
      font-size: $title-size*0.95;
      padding: 0 30rpx;
      background-color: #fff;
      color: #666;
      border-top: solid 20rpx #f7f7f7;
	  position: sticky;
	  top: 0rpx;
	  z-index: 1;

      .nav-item {
	    display: inline-block;
        border-bottom: solid 4rpx #fff;
        padding: 30rpx 10rpx;
		margin-right: 10rpx;
      }

      .nav-item-selected {
        border-bottom: solid 4rpx $main-color;
        color: $main-color;
      }
    }

    // 订单列表
    .order-list {
      background-color: #fff;
      border-radius: 20rpx;
      min-height: 300rpx;
      margin: 30rpx 20rpx 0 20rpx;
      padding:30rpx 30rpx 20rpx 30rpx ;
	  // border-top: solid 4rpx #cacaca;

      // 操作信息
      .actions {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: flex-end;
        box-sizing: border-box;
        flex-wrap: wrap;
        flex: 1;
        font-size: 28rpx;
        color: #fff;
		border-top: solid 1rpx #EFF4F2;
        .nowPay {
          padding: 4rpx 20rpx;
          border-radius: 40rpx;
          margin-left: 20rpx;
          margin-top: 20rpx;
          // background-color: $main-color;
		  color: #999;
		  border:solid 1rpx #cacaca;
        }

        .cancelOrder {
          padding: 10rpx 30rpx;
          border-radius: 40rpx;
          margin-left: 20rpx;
          margin-top: 20rpx;
          background-color: #DD524D;
        }

        .logistics {
          background-color: $main-color;
          padding: 10rpx 30rpx;
          border-radius: 40rpx;
          margin-left: 20rpx;
          margin-top: 20rpx;
        }

        .sign {
          background-color: #DD524D;
          padding: 10rpx 30rpx;
          border-radius: 40rpx;
          margin-left: 20rpx;
          margin-top: 20rpx;
        }

        .evaluate {
          background-color: $main-color;
          padding: 10rpx 30rpx;
          border-radius: 40rpx;
          margin-left: 20rpx;
          margin-top: 20rpx;
        }
      }
    }
  }
</style>
