<template>
  <div class="myOrderList">
    <div class="fixed-top">
      <u-tabs :list="tabList" :is-scroll="false" :current="currentTabs" height="90" font-size="28" bg-color="#fff" inactive-color="#8891A1" 
          active-color="#1D2229 " :bar-style="barStyle" @change="changeTab" ></u-tabs>
    </div>
    <div class="list-content" v-if="list.length">
      <div class="item" v-for="(item, index) in list" :key="index">
        <div class="time-flex">
          <p>{{item.createTime.substring(0, 16)}}</p>
          <div :class="`status`+item.status">{{ $dict.getLabel('integralSGOStatus', item.status) }}</div>
        </div>
        <div class="flex">
          <img :src="item.goodsPicUrl" alt="">
          <div class="type" :class="`type`+item.goodsTypeText">{{ $dict.getLabel('integralSGTypeText', item.goodsTypeText) }}</div>
          <div class="flex-right">
            <p>{{item.goodsTitle}}</p>
            <div class="num-flex">
              <h3>{{item.usedIntegral}}积分<span v-if="item.goodsType == 2">+{{item.payMoney}}元</span></h3>
              <div>x {{item.quantity}}</div>
            </div>
          </div>
        </div>
        <p class="tips" v-if="item.goodsType == 2"><span>兑换成功后，点击「去购买」前往京东低价购买</span></p>
         <p class="tips" v-else>免费兑换商品，可到固定的兑换点进行「核销兑换」</p>
        <p class="remark" v-if="item.remarks">备注：{{item.remarks}}</p>
        <div class="flex-btn" v-if="item.goodsType == 2 && item.status != 2">
          <p></p>
          <div class="btn confirm" @click="openJd(item)">去购买</div>
        </div>
        <div class="flex-btn" v-if="item.goodsType != 2">
          <p>核销码:<span>{{item.verificationCode}}</span></p>
          <div class="btn" v-if="item.status == 0" @click="cancel(item)">取消订单</div>
        </div>
      </div>
    </div>
    <AiEmpty v-else></AiEmpty>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: 'myOrderList',
  appName: '我的订单',
  data() {
    return {
      tabList: [{name: '全部'}, {name: '待核销'}, {name: '已完成'}, {name: '已取消'}],
      currentTabs: 0,
      barStyle: {
        'width': '20px',
        'height': '3px',
        'border-radius': '2px',
        'bottom': '3px',
        'background': '#2D7DFF'
      },
      list: [],
      current: 1
    }
  },
  computed: {
    ...mapState(['user']),
  },
  onLoad() {
    this.$dict.load(['integralSGOStatus', 'integralSGTypeText']).then(() => {
      this.getList()
    })
  },
  methods: {
    changeTab(index) {
      this.currentTabs = index
      this.getListInit()
    },
    getListInit() {
      this.current = 1
      this.getList()
    },
    getList() {
      this.$instance.post(`/app/appintegralsupermarketorder/listForXCX`, null, {
        params: {
          current: this.current,
          status: this.currentTabs == 0 ? '' : this.currentTabs - 1,
          createUserId: this.user.id
        }
      }).then(res => {
        if (res.code === 0) {
          res.data.records.map((item) => {
            item.goodsTypeText = item.goodsType == 0 ? 0 : 1
          })
          this.list = this.current == 1 ? res.data.records : [...this.list, ...res.data.records]
        }
      })
    },
    openJd(item) {
      if(item.goodsType == 1) {
        uni.navigateTo({url: `./jdH5?goodsJdUrl=${item.goodsJdUrl}`})
      }else {
        uni.navigateToMiniProgram({
          appId: item.goodsJdAppid,
          path: item.goodsJdUrl
        })
      }
    },
    cancel(item) {
      uni.showModal({
        title: '确认取消此订单？',
        content: '取消订单后，积分将退回至积分余额',
        confirmColor: "#2D7DFF",
        cancelColor: "#2D7DFF",
        cancelText: "我在想想",
        confirmText: "确认取消", 
        success: (res) => {
          if (res.confirm) {
            this.cancelOrder(item)
          }
        }
      });
    },
    cancelOrder(item) {
      this.$instance.post(`/app/appintegralsupermarketorder/cancelForXCX?id=${item.id}`).then(res => {
        if (res.code === 0) {
          this.getListInit()
          uni.$emit('updateGoodsList')
        }
      })
    },
  },
  onReachBottom() {
    this.current = this.current + 1
    this.getList()
  },
}
</script>

<style lang="scss" scoped>
.myOrderList {
  min-height: 100%;
  .fixed-top {
    background-color: #fff;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 2;
  }
  .list-content {
    padding: 112px 24px 24px;
    background-color: #F3F6F9;
    .item {
      width: 100%;
      background: #FFF;
      border-radius: 16px;
      margin-bottom: 32px;
      .time-flex {
        padding: 0 32px;
        line-height: 64px;
        font-family: PingFangSC-Regular;
        font-size: 24px;
        color: #666;
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #ddd;
        div {
          color: #2D7DFF;
        }
        .status2 {
          color: #999;
        }
      }
      .flex {
        padding: 32px 32px 0;
        display: flex;
        position: relative;
        margin-bottom: 20px;
        img {
          width: 166px;
          height: 166px;
          border-radius: 16px;
          margin-right: 20px;
        }
        .type {
          position: absolute;
          left: 32px;
          bottom: 0;
          border-bottom-left-radius: 16px;
          border-bottom-right-radius: 16px;
          width: 166px;
          text-align: center;
          line-height: 34px;
          font-family: PingFangSC-Regular;
          font-size: 18px;
          color: #FFF;
          p {
            scale: 0.9;
          }
        }
        .type2 {
          background-color: #E64E39;
        }
        .type1 {
          background-color: #E64E39;
        }
        .type0 {
          background-color: #FF6900;
        }
        .flex-right {
          width: calc(100% - 186px);
          p {
            word-break: break-all;
            font-family: PingFangSC-Regular;
            font-size: 26px;
            color: #222;
            line-height: 34px;
            text-overflow: -o-ellipsis-lastline;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;					
            -webkit-box-orient: vertical;
            margin-bottom: 50px;
          }
          .num-flex {
            display: flex;
            h3 {
              width: calc(100% - 100px);
              font-family: PingFangSC-SNaNpxibold;
              font-weight: 600;
              font-size: 34px;
              color: #FF6900;
              line-height: 48px;
              span {
                display: inline-block;
                font-size: 34px;
                color: #4181FF;
                margin-left: 8px;
              }
            }
            div {
              width: 100px;
              text-align: right;
              font-family: PingFangSC-SNaNpxibold;
              font-weight: 600;
              font-size: 24px;
              color: #222;
              line-height: 48px;
            }
          }
          
        }
      }
      .tips {
        display: inline-block;
        padding: 12px;
        line-height: 36px;
        background: #F5FCF5;
        border-radius: 16px;
        font-family: PingFangSC-Regular;
        font-size: 24px;
        color: #3BBC37;
        margin: 0 32px 16px 32px;
        span {
          display: inline-block;
          scale: 0.9;
        }
      }
      .remark {
        font-family: PingFangSC-Regular;
        font-size: 24px;
        color: #666;
        line-height: 34px;
        word-break: break-all;
        margin: 0 32px 24px 32px;
      }
      .flex-btn {
        padding: 0 32px 32px;
        display: flex;
        justify-content: space-between;
        p {
          height: 56px;
          line-height: 56px;
          font-family: PingFangSC-Regular;
          font-size: 24px;
          color: #666;
          span {
            display: inline-block;
            font-family: PingFangSC-SNaNpxibold;
            font-weight: 600;
            font-size: 28px;
            color: #2D7DFF;
            margin-left: 8px;
          }
        }
        .btn {
          width: 136px;
          text-align: center;
          line-height: 54px;
          border: 1px solid #CCC;
          border-radius: 44px;
          font-family: PingFangSC-Regular;
          font-size: 22px;
          color: #666;
        }
        .confirm {
          border: 1px solid #2D7DFF;
          background-color: #2D7DFF;
          color: #fff;
        }
      }
    }
  }

  
}
</style>
