<template>
  <div class="order">
    <div class="order-item" v-for="(item, index) in list" :key="index" hover-class="text-hover"
         @click="$linkTo('./orderInfo?id=' + item.id)">
      <div class="order-item__imgs">
        <scroll-view scroll-x>
          <image v-for="(goods, index) in item.merchandiseList" :key="index"
                 :src="JSON.parse(goods.merchandisePhoto)[0].url"/>
        </scroll-view>
        <div class="order-item__imgs--right">
          <div class="top">
            <span>{{ item.orderIntegral }}</span>
            <i>积分</i>
          </div>
          <p>共{{ item.total }}件</p>
        </div>
      </div>
      <div class="order-item__footer">
        <div style="display:flex;justify-content: space-between;">
          <span :style="{color: $dict.getColor('integralOrderStatus', item.orderStatus)}">{{$dict.getLabel('integralOrderStatus', item.orderStatus) }}</span>
          <div class="order-item__footer--btn" v-if="item.orderStatus === '0'" @click.stop="cancel(item.id)">取消订单</div>
          <div class="order-item__footer--btn" v-if="item.orderStatus == '3'" @click.stop="confirm(item.id)">确认兑换</div>
        </div>
        <p v-if="item.orderStatus == '3'" style="margin-top:8px;color:#f46;font-size:14px;">请确认兑换，如没有进行确认，系统将于72小时后自动确认，如有疑问，请联系工作人员</p>
      </div>
    </div>
    <AiEmpty v-if="!list.length"/>
    <!-- <div class="loading" style="margin-top:20px">
      <u-loadmore :status="loadingStatus" />
    </div> -->
  </div>
</template>

<script>

export default {
  name: "AppOrderList",
  appName: "超市订单",
  data() {
    return {
      list: [],
      current: 0,
      loadingStatus: 'loadmore'
    }
  },

  onLoad() {
    this.$dict.load(['integralOrderStatus']).then(() => {
      this.getList()
    })
    uni.$on('update', () => {
      this.reset()
    })
  },

  methods: {
    reset() {
      this.loadingStatus = 'loadmore'
      this.current = 0
      this.$loading()

      this.$nextTick(() => {
        this.getList()
      })
    },

    cancel(id) {
      this.$loading()
      this.$instance.post(`/app/appvillagerintegralshoporder/overOrderForWx?orderId=${id}`).then(res => {
        if (res.code === 0) {
          this.$toast('取消成功')
          this.reset()
        }
      })
    },

    confirm(id) {
      this.$loading()
      this.$instance.post(`/app/appvillagerintegralshoporder/finishOrder?orderId=${id}`).then(res => {
        if (res.code === 0) {
          this.$toast('确认成功')
          this.reset()
        }
      })
    },

    getTotal(arr) {
      let total = 0
      arr.forEach(item => {
        total = total + item.merchandiseNumber
      })

      return total
    },

    getList() {
      if (this.loadingStatus === 'loading' || this.loadingStatus === 'nomore') return

      this.loadingStatus = 'loading'
      this.$instance.post(`/app/appvillagerintegralshoporder/listForWx?current=${this.current + 1}&size=10`).then(res => {
        if (res.code === 0) {
          if (this.current === 0) {
            this.list = []
          }

          const data = res.data.records.map(item => {
            item.total = this.getTotal(item.merchandiseList)

            return item
          })
          this.list.push(...data)
          this.current = this.current + 1
          this.loadingStatus = 'loadmore'
          if (!res.data.records.length || res.data.records.length < 10) {
            this.loadingStatus = 'nomore'
          }
        } else {
          this.loadingStatus = 'loadmore'
        }
      }).catch(() => {
        this.loadingStatus = 'loadmore'
      })
    },

    toDetail(id) {
      this.$linkTo(`./newsDetail?id=${id}&areaId=${this.areaId}`)
    }
  },

  onReachBottom() {
    this.getList(this.areaId)
  }
}
</script>

<style lang="scss" scoped>
.order {
  padding: 30px 0;
}

.order-item {
  width: 686px;
  margin: 0 auto 32px;
  background: #FFFFFF;
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.02);
  border-radius: 8px;

  .order-item__imgs--right {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    width: 144px;
    height: 160px;
    background: rgba(255, 255, 255, 0.98);

    .top {
      display: flex;
      align-items: baseline;
      color: #4185F5;
      font-size: 40px;
      i {
        padding-left: 4px;
        font-size: 24px;
      }
    }

    p {
      color: #ADADAD;
      font-size: 26px;
    }
  }

  .order-item__footer {
    padding: 24px;
    span {
      color: #3078E1;
      font-size: 28px;
    }

    .order-item__footer--btn {
      width: 160px;
      height: 56px;
      line-height: 56px;
      text-align: center;
      color: #666666;
      font-size: 26px;
      background: #FFFFFF;
      border-radius: 32px;
      border: 1px solid #CCCCCC;
    }
  }

  .order-item__imgs {
    display: flex;
    align-items: center;
    flex: 1;
    height: 208px;
    padding: 0 0 024px;
    border-bottom: 1px solid #DDDDDD;

    scroll-view {
      flex: 1;
      height: 160px;
      font-size: 0;
      overflow: hidden;
      white-space: nowrap;
    }

    image {
      flex-shrink: 1;
      width: 160px;
      height: 160px;
      margin-right: 10px;
      border-radius: 8px;
      border: 1px solid #DDDDDD;
    }
  }
}
</style>
