<template>
<view id="order-detail">
    <view class="border-box">
        <view class="order-item" v-if="order.group_item_count == 1">
            <view class="title mx-1px-bottom">
                <view class="name">
                    订单状态
                </view>
                <view class="type">
                    {{typeList[order.status]}}
                </view>
            </view>
            <view class="goods-item">
                <view class="item-middle">
                    <view class="middle-item mx-1px-bottom" :data-id="item.item_meta.detail_id" v-for="(item, index) in order.items" :key="index" @tap="jumpDetail">
                        <image :src="item.item_meta.image"></image>
                        <view class="text">
                            <view class="names">
                               {{item.item_name}}
                            </view>
                            <view class="model">
                                {{item.item_meta.specs_text}}
                            </view>
                        </view>
                        <view class="money-box">
                            <view>
                                {{item.quantity}}件
                            </view>
                            <view>
                                {{item.redeem_point}} 积分
                            </view>
                            <!--<view class="retreat-box" data-no="{{item.refund_no}}" bindtap="jumpRetreat" wx:if="{{(item.total != 0 && order.can_refund) || (item.total != 0 && item.is_refunded)}}">-->
                                <!--申请售后-->
                            <!--</view>-->
                           <!-- <view wx:if="{{(item.total != 0 && order.can_refund) || (item.total != 0 && item.is_refunded)}}">
                                <view class="retreat" data-id="{{item.id}}" data-no="{{order.order_no}}" wx:if="{{order.can_refund && !item.is_refunded}}" @tap.stop="applyRetreat" >申请售后</view>
                                <view class="retreat" data-no="{{btn.refund_no}}" wx:for="{{item.refund_btn}}" wx:for-item="btn" @tap.stop="jumpRetreat">{{btn.refund_status_text}}</view>
                            </view>-->
                        </view>
                    </view>
                </view>
                <view class="item-bottom">
                    <view class="all-money">
                        {{order.count}}件, 共计 {{order.redeem_point}}积分
                    </view>
                </view>
            </view>
        </view>
        <view class="order-item" v-if="order.group_item_count > 1">
            <view class="title mx-1px-bottom">
                <view class="name">
                    订单状态
                </view>
                <view class="type">
                    {{typeList[order.status]}}
                </view>
            </view>
            <view class="goods-item">
                <view class="item-middle" v-for="(item, index) in order.group_order_item" :key="index">
                    <view class="express-info mx-1px-bottom" v-if="item.shipping">
                        <view class="left">
                            <view>
                                {{item.shipping_title}}
                            </view>
                        </view>
                        <view class="right">
                            <view>
                                物流公司 : {{item.shipping.name}}
                            </view>
                            <view>
                                物流编号 : {{item.shipping.tracking}}
                            </view>
                        </view>
                    </view>
                    <view class="express-info mx-1px-bottom" v-else>
                        等待商家发货
                    </view>
                    <view class="middle-item middle-item__active mx-1px-bottom" :data-id="good.item_meta.detail_id" v-for="(item, index) in item.item" :key="index" wx:for-item="good" @tap="jumpDetail">
                        <image :src="good.item_meta.image"></image>
                        <view class="text">
                            <view class="names">
                                {{good.item_name}}
                            </view>
                            <view class="model">
                                {{good.item_meta.specs_text}}
                            </view>
                        </view>
                        <view class="money-box">
                            <view>
                                {{good.quantity}}件
                            </view>
                            <view>
                                {{good.redeem_point}} 积分
                            </view>
                            <!--<view class="retreat-box" data-no="{{good.refund_no}}" bindtap="jumpRetreat" wx:if="{{(good.total != 0 && order.can_refund) || (good.total != 0 && good.is_refunded)}}">
                            申请售后
                            </view>-->
                           <!-- <view wx:if="{{(good.total != 0 && order.can_refund) || (good.total != 0 && good.is_refunded)}}">
                                <view class="retreat" data-id="{{good.id}}" data-no="{{order.order_no}}" wx:if="{{order.can_refund && !good.is_refunded}}" @tap.stop="applyRetreat" >申请售后</view>
                                <view class="retreat" data-no="{{btn.refund_no}}" wx:for="{{good.refund_btn}}" wx:for-item="btn" @tap.stop="jumpRetreat">{{btn.refund_status_text}}</view>
                            </view>-->
                            <!--<view class="retreat-box" data-id="{{good.id}}" data-no="{{order.order_no}}" @tap.stop="applyRetreat" wx:if="{{(order.status==2 || order.status == 3 || order.status == 4) && (good.total != 0 && !good.is_refunded)}}">
                                申请售后
                            </view>
                            <view class="retreat-box" data-no="{{good.refund_no}}" bindtap="jumpRetreat" wx:if="{{(order.status==2 || order.status == 3 || order.status == 4 || order.status == 5) && (good.is_refunded)}}">
                                {{refundStatus[good.refund_status]}}
                            </view>-->
                        </view>
                    </view>
                </view>
                <view class="item-bottom item-bottom__active">
                    <div class="all">商品合计：</div>
                    <view class="all-money">
                        {{order.count}}件, 共计 {{order.count}} 积分

                    </view>
                </view>
            </view>
        </view>
        <view class="order-item">
            <view class="title mx-1px-bottom">
                订单信息
            </view>
            <view class="order-info">
                <view class="info-item">
                    <view class="name">
                        订单编号 :
                    </view>
                    <view class="text">{{order.order_no}}</view>

                </view>
                <view class="info-item">
                    <view class="name">
                        订单来源 :
                    </view>
                    <view class="text">{{order.from}}</view>
                </view>
                <view class="info-item">
                    <view class="name">
                        订单时间 :
                    </view>
                    <view class="text">{{order.submit_time}}</view>
                </view>
            </view>
        </view>
        <view class="order-item">
            <view class="title mx-1px-bottom">
                收货人信息
            </view>
            <view class="order-info">
                <view class="info-item">
                    <view class="name">
                        地址 :
                    </view>
                    <view class="text">{{order.address_name}} {{order.address}}</view>
                </view>
                <view class="info-item">
                    <view class="name">
                        姓名 :
                    </view>
                    <view class="text">{{order.accept_name}}</view>
                </view>
                <view class="info-item">
                    <view class="name">
                        电话 :
                    </view>
                    <view class="text">{{order.mobile}}</view>

                </view>
            </view>
        </view>
        <view class="order-item">
            <view class="title mx-1px-bottom">
                备注
            </view>
            <view class="order-info">
                {{order.note || order.message || '暂无备注'}}
            </view>
        </view>
        <view class="order-detail-info">
            <view class="item">
                <view class="name">
                    商品积分：
                </view>
                <view class="num">
                    {{order.adjustment_point}}积分
                </view>
            </view>
            <view class="item">
                <view class="name big">
                    实付积分：
                </view>
                <view class="num big-money">
                    {{order.items[0].redeem_point}} 积分
                </view>
            </view>
        </view>
    </view>
    <view class="button-type point-type">
        <view class="more btn" :data-id="order.items[0].item_meta.detail_id" @tap="jumpDetail">
            再次购买
        </view>
        <view class="pointindex btn" @tap="jumpPoint">
            商城首页
        </view>
    </view>
    <!--<view class="button-type">
        <view class="type-submit" wx:if="{{order.status==1}}">
            <view class="cancel" bindtap="cancel">
                取消订单
            </view>
            <view class="submit" data-no="{{order.order_no}}" bindtap="pay">
                立即付款
            </view>
        </view>

        <view class="sbumit-cancel" wx:if="{{order.status==3}}" bindtap="receive">
            确认收货
        </view>
    </view>-->
</view>


</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      order: {},
      typeList: {
        0: '临时订单',
        1: '待付款',
        2: '付款成功',
        3: '已发货',
        4: '已完成',
        5: '已完成',
        6: '已取消',
        7: '已退款',
        8: '已作废',
        9: '已删除',
        31: '部分已发货'
      },
      refundStatus: ['待审核', '审核通过', '拒绝申请', '已完成', '已关闭', '等待买家退货', '买家已退货', '等待商城发货'],
      order_no: ''
    };
  },

  onLoad(e) {
    console.log(e, '00000000');
    this.setData({
      order_no: e.no
    });
  },

  onShow() {
    wx.showLoading({
      title: "加载中",
      mask: true
    });
    this.queryOrderDetail(this.order_no); // let app =getApp();
    // app.isBirthday().then(()=>{
    //     if(this.$cookieStorage.get("birthday_gift")){
    //         var giftData=this.$cookieStorage.get("birthday_gift").data;
    //         new app.ToastPannel().__page.showText(giftData);
    //     }
    // });
  },

  components: {},
  props: {},
  methods: {
    applyRetreat(e) {
      wx.navigateTo({
        url: '/pages/afterSales/apply/apply?no=' + e.currentTarget.dataset.no + '&id=' + e.currentTarget.dataset.id
      });
    },

    jumpRetreat(e) {
      var refund_no = e.currentTarget.dataset.no;
      wx.navigateTo({
        url: '/pages/afterSales/detail/detail?no=' + refund_no
      });
    },

    pay(e) {
      var order_no = e.currentTarget.dataset.no;
      wx.navigateTo({
        url: '/pages/store/payment/payment?order_no=' + order_no
      });
    },

    jumpDetail(e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/pointStore/detail/detail?id=' + id
      });
    },

    jumpPoint() {
      wx.navigateTo({
        url: '/pages/pointStore/index/index'
      });
    },

    cancel() {
      wx.showModal({
        title: '',
        content: '确定取消该订单',
        success: res => {
          if (res.confirm) {
            this.cancelOrder(this.order_no);
          }
        }
      });
    },

    receive() {
      wx.showModal({
        title: '',
        content: '确定已收货?',
        success: res => {
          if (res.confirm) {
            this.receiveOrder(this.order_no);
          }
        }
      });
    },

    // 获取订单详情
    queryOrderDetail(orderNo) {
      var token = this.$cookieStorage.get('user_token');
      this.$http.get({
        api: 'api/order/' + orderNo,
        header: {
          Authorization: token
        }
      }).then(res => {
        if (res.statusCode = 200) {
          res = res.data;
          this.setData({
            order: res.data
          });
        } else {
          wx.showModal({
            title: '',
            content: '请求失败，请稍后重试'
          });
        }

        wx.hideLoading();
      }).catch(rej => {
        wx.showModal({
          title: '',
          content: '请求失败，请稍后重试'
        });
        wx.hideLoading();
      });
    },

    // 取消订单
    cancelOrder(orderNo) {
      var token = this.$cookieStorage.get('user_token');
      this.$http.post({
        api: 'api/shopping/order/cancel',
        header: {
          Authorization: token
        },
        data: {
          order_no: orderNo
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;
          wx.showModal({
            title: '',
            content: res.message,
            showCancel: false,
            success: res => {
              if (res.confirm) {
                this.queryOrderDetail(orderNo);
              }
            }
          });
        } else {
          wx.showModal({
            title: '',
            content: '取消订单失败, 请检查您的网络状态',
            showCancel: false
          });
        }
      }).catch(rej => {
        if (rej.statusCode == 404) {
          wx.showModal({
            title: '',
            content: '接口不存在',
            showCancel: false
          });
        } else {
          wx.showModal({
            title: '',
            content: '取消订单失败, 请检查您的网络状态',
            showCancel: false
          });
        }
      });
    },

    // 确认收货
    receiveOrder(orderNo) {
      var token = this.$cookieStorage.get('user_token');
      this.$http.post({
        api: 'api/shopping/order/received',
        header: {
          Authorization: token
        },
        data: {
          order_no: orderNo
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;
          wx.showModal({
            title: '',
            content: res.message,
            showCancel: false,
            success: res => {
              if (res.confirm) {
                this.queryOrderDetail(orderNo);
              }
            }
          });
        } else {
          wx.showModal({
            title: '',
            content: '取消订单失败, 请检查您的网络状态',
            showCancel: false
          });
        }
      }).catch(rej => {
        wx.showModal({
          title: '',
          content: '取消订单失败, 请检查您的网络状态',
          showCancel: false
        });
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "orderdetail";
</style>