<template>
  <view class="container">
    <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ use: false }" :up="upOption"
      @up="upCallback">

      <!-- tab栏 -->
      <u-tabs :list="tabList" :is-scroll="false" :current="curTab" active-color="#786cff" :duration="0.2"
        @change="onChangeTab" />

      <!-- 列表数据 -->
      <view class="widget-list b-f">
        <view class="widget__detail" v-for="(item, index) in list.data" :key="index">
          <view class="detail__row dis-flex flex-x-between">
            <view class="detail__left f-24">订单号：{{ item.order.order_no }}</view>
            <view class="detail__right f-24 c-violet">
              {{ item.order.state_text }}
            </view>
          </view>
          <view class="detail__row m-top10 dis-flex flex-x-between">
            <view class="detail__left dis-flex flex-y-center">
              <avatar-image class="user-avatar" :url="item.user.avatar_url" :width="100" :borderWidth="4"
                :borderColor="`#fff`" />
              <view class="user-info dis-flex flex-dir-column flex-x-center">
                <view class="user-nickName f-28">{{ item.user.nick_name }}</view>
                <view class="user-time f-24 c-80">消费金额：￥{{ item.order_price }}</view>
              </view>
            </view>
            <view class="detail__right dis-flex flex-dir-column flex-x-center flex-y-center">
              <view class="detail__money t-r col-m">
                <text class="f-26">+ </text>
                <text class="f-28">{{ item.my_money }}</text>
              </view>
              <view class="detail__time f-22 c-80">{{ item.create_time }}</view>
            </view>
          </view>
        </view>
      </view>
    </mescroll-body>
  </view>
</template>

<script>
  import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
  import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
  import AvatarImage from '@/components/avatar-image'
  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
  import * as Api from '@/api/dealer/order'
  import SettingModel from '@/common/model/dealer/Setting'

  const pageSize = 15

  export default {
    components: {
      MescrollBody,
      AvatarImage
    },
    mixins: [MescrollMixin],
    data() {
      return {
        // 选项卡列表
        tabList: [],
        // 当前选项
        curTab: 0,
        // 列表数据
        list: getEmptyPaginateObj(),
        // 上拉加载配置
        upOption: {
          // 首次自动执行
          auto: true,
          // 每页数据的数量; 默认10
          page: { size: pageSize },
          // 数量要大于12条才显示无更多数据
          noMoreSize: 12,
          // 空布局
          empty: {
            tip: '亲，暂无相关数据'
          }
        }
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.getSetting()
    },

    methods: {

      // 获取分销设置
      getSetting() {
        const app = this
        SettingModel.data()
          .then(setting => {
            const words = setting.words.order
            app.setPageTitle(words.title)
            app.setTabList(words.words)
          })
      },

      // 设置页面标题
      setPageTitle(title) {
        uni.setNavigationBarTitle({
          title: title.value
        })
      },

      // 设置选项卡数据
      setTabList(words) {
        const app = this
        app.tabList = [
          { value: -1, name: words.all.value },
          { value: 0, name: words.unsettled.value },
          { value: 1, name: words.settled.value }
        ]
      },

      /**
       * 上拉加载的回调 (页面初始化时也会执行一次)
       * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
       * @param {Object} page
       */
      upCallback(page) {
        const app = this
        // 设置列表数据
        app.getList(page.num)
          .then(list => {
            const curPageLen = list.data.length
            const totalSize = list.data.total
            app.mescroll.endBySize(curPageLen, totalSize)
          })
          .catch(() => app.mescroll.endErr())
      },

      // 获取提现列表
      getList(pageNo = 1) {
        const app = this
        return new Promise((resolve, reject) => {
          Api.list({ settled: app.getTabValue(), page: pageNo })
            .then(result => {
              // 合并新数据
              const newList = result.data.list
              app.list.data = getMoreListData(newList, app.list, pageNo)
              resolve(newList)
            })
        })
      },

      // 获取当前标签项的值
      getTabValue() {
        const app = this
        if (app.tabList.length) {
          return app.tabList[app.curTab].value
        }
        return -1
      },

      // 切换标签项
      onChangeTab(index) {
        const app = this
        // 设置当前选中的标签
        app.curTab = index
        // 刷新订单列表
        app.onRefreshList()
      },

      // 刷新列表数据
      onRefreshList() {
        this.list = getEmptyPaginateObj()
        setTimeout(() => {
          this.mescroll.resetUpScroll()
        }, 120)
      }

    }
  }
</script>

<style>
  page {
    background: #fff;
  }
</style>
<style lang="scss" scoped>
  .c-violet {
    color: #786cff;
  }

  .c-80 {
    color: #808080;
  }

  // 订单列表
  .widget-list {
    padding: 10rpx 20rpx 40rpx 20rpx;
    box-sizing: border-box;
  }

  .widget__detail {
    padding: 20rpx 15rpx;
    box-sizing: border-box;
    font-size: 28rpx;
    border-bottom: 1rpx solid #e7e7e7;

    .user-avatar {
      margin-right: 16rpx;
    }

    .user-info {
      height: 100%;

      .user-nickName {
        margin-bottom: 8rpx;
      }
    }

    .detail__money {
      width: 100%;
      margin-bottom: 8rpx;
    }
  }
</style>
