<template>
<view id="order-detail">
    <view class="border-box">
        <view class="closeTime mx-1px-bottom" v-if="order.status===1">
            <i class="iconfont icon-shijian"></i> <span>剩余付款时间：</span>
            <countdown :value="order.will_closed_at" detail-class="text-color"></countdown>
        </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">
                    <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.units_total_yuan}}
                            </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 v-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" v-if="order.can_refund && !item.is_refunded" @tap.stop="applyRetreat">申请售后</view>
                                <view class="retreat" :data-no="btn.refund_no" v-for="(btn, index) in item.refund_btn" :key="index"  @tap.stop="jumpRetreat">{{btn.refund_status_text}}</view>
                            </view>
                        </view>
                    </view>
                </view>
                <view class="item-bottom">
                    <view class="all-money" v-if="order.type == 5">
                        {{order.count}}件, 共计 {{order.redeem_point}} 积分
                    </view>
					<view class="all-money" v-else>
					    {{order.count}}件, 共计 ￥{{order.items_total_yuan}}
					</view>
                </view>
            </view>
        </view>
        <view class="order-item" v-if="order && order.multi_groupon_users&&order.multi_groupon_users.length != 0" @tap="jump">
            <view class="title mx-1px-bottom">
                <view class="name">
                    拼团详情
                </view>
                <view class="type">
                    {{muStatus[order.multi_groupon_status]}}
                </view>
            </view>
            <view class="member-info">
                <view class="avatar" v-for="(item, index) in order.multi_groupon_users" :key="index">
                    <image :src="item.meta.avatar"></image>
                    <view class="colonel" v-if="item.is_leader == 1">团长</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>
                            <div class="info" :data-no="item.shipping.tracking" :data-name="item.shipping.name" @tap="jumpExpress">
                                查看物流详情 >
                            </div>
                        </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="(good, index) in item.item" :key="index"  @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.units_total_yuan}}
                            </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 v-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" v-if="order.can_refund && !good.is_refunded" @tap.stop="applyRetreat">申请售后</view>
                                <view class="retreat" :data-no="btn.refund_no" v-for="(btn, index) in good.refund_btn" :key="index"  @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.items_total_yuan}}
                    </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.payment_text}}</view>
                </view>
                <view class="info-item">
                    <view class="name">
                        下单时间 :
                    </view>
                    <view class="text">{{order.submit_time}}</view>
                </view>
                <view class="info-item" v-if="order.status==6">
                    <view class="name">
                        取消原因 :
                    </view>
                    <view class="text">{{order.cancel_reason}}</view>
                </view>
                <view class="info-item" v-if="order.status==6">
                    <view class="name">
                        取消时间 :
                    </view>
                    <view class="text">{{order.updated_at}}</view>
                </view>
            </view>
        </view>
        <view class="order-item" v-if="order && order.pick_self_id>0">
            <view class="title mx-1px-bottom">
                收货人信息
            </view>
            <view class="order-info">
                <view class="info-item">
                    <view class="name">
                        配送方式 :
                    </view>
                    <view class="text">门店自提</view>
                    <view class="pick-code" @tap="jumpCode">提货凭证</view>
                </view>
                <view class="info-item">
                    <view class="name">
                        提货人 :
                    </view>
                    <view class="text">{{order.pick_self.nick_name || order.pick_self.mobile}}</view>
                </view>
                <view class="info-item">
                    <view class="name">
                        地址 :
                    </view>
                    <view class="text">{{order.pick_self.store.address}}</view>
                </view>
                <view class="info-item">
                    <view class="name">
                        电话 :
                    </view>
                    <view class="text">{{order.pick_self.store.phone}}</view>
                </view>
            </view>
        </view>
        <view class="order-item" v-else>
            <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" v-if="order && order.invoices">
            <view class="title mx-1px-bottom">
                发票信息
            </view>
            <view class="order-info">
                {{order.invoices[0]?order.invoices[0].content:"暂无发票信息"}}
            </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-item" v-if="(order.status==3 ||order.status==4 ||order.status==5) && order.group_item_count == 1">
            <view class="title mx-1px-bottom">
                运单信息
            </view>
            <view class="order-info" v-if="order && order.shippings">
                <view class="info-item">
                    <view class="name">
                        物流公司 :
                    </view>
                    <view class="text">{{order.shippings[0]?order.shippings[0].name:"暂无运单信息"}}</view>
                </view>
                <view class="info-item" >
                    <view class="name">
                        运单编号 :
                    </view>
                    <view class="text">{{order.shippings[0]?order.shippings[0].tracking:"暂无运单信息"}}
                        <span class="express-order" @tap="jumpExpress" :data-name="order.shippings[0].name" :data-no="order.shippings[0].tracking" v-if="order.shippings[0] && order.shippings[0].tracking">查看物流</span>
                    </view>
                </view>
            </view>
        </view>
        <view class="order-detail-info">
            <view class="item">
                <view class="name">
                    商品件数：
                </view>
                <view class="num">
                    {{order.count}}件
                </view>
            </view>
            <view class="item">
                <view class="name">
                    商品金额：
                </view>
                <view class="num">
                    ￥{{order.items_total_yuan}}
                </view>
            </view>
            <view class="item">
                <view class="name">
                    积分抵扣：
                </view>
				<view class="num" v-if="order.type == 5">{{order.redeem_point}}积分</view>
                <view class="num" v-else>
                    {{order.adjustment_point}}积分
                </view>
            </view>
            <view class="item">
                <view class="name">
                    优惠抵扣：
                </view>
                <view class="num">
                    {{order.adjustments_total_yuan}}
                </view>
            </view>
            <view class="item">
                <view class="name">
                    运费：
                </view>
                <view class="num">
                    {{order.payable_freight}}
                </view>
            </view>
            <view class="item">
                <view class="name big">
                    实付金额：
                </view>
                <view class="num big-money" v-if="order.type == 5">
                    ￥ 0
                </view>
				<view class="num big-money" v-else>
				    ￥{{order.total_yuan}}
				</view>
            </view>
        </view>
    </view>
    <!-- #ifdef MP-WEIXIN -->
	<view class="order-share" @tap="jumpShare" v-if="order.status == 2 || order.status == 3 || order.status == 4 || order.status == 5 || order.status == 31">
	    <view class="text" :style="'background: ' + config.mainColor">订单分享</view>
	</view>
    <!-- #endif -->
   
	<view class="button-type">
        <view class="type-submit" v-if="order.status==1">
            <view class="cancel" @tap="cancel">
                取消订单
            </view>
            <view class="submit" :style="'background: ' + config.mainColor" :data-no="order.order_no" @tap="pay">
                立即付款
            </view>
        </view>

        <view class="sbumit-cancel" v-if="order.status==3" @tap="receive">
            确认收货
        </view>
    </view>
	<!-- 回到首页的按钮展示 -->
	<view class="home" @tap="jumpHome" :style="'background: ' + config.mainColor">
	    <i class="iconfont icon--shouye"></i> 
	    <view>
	        首页
			
	    </view>
	</view>
</view>


</template>
<script>

import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
import countdown from '@/components/Countdown/Countdown';

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

  onLoad(e) {
    // 第三方平台配置颜色
    var bgConfig = this.$cookieStorage.get('globalConfig') || '';
    this.setData({
      config: bgConfig
    });
    this.setData({
      norder_no: e.no
    });
  },

  onShow() {
    wx.showLoading({
      title: "加载中",
      mask: true
    });
    this.queryOrderDetail(this.norder_no);
  },

  components: {
    countdown
  },
  props: {},
  methods: {
    jumpExpress(e) {
      var no = e.currentTarget.dataset.no;
      var name = e.currentTarget.dataset.name;
      wx.navigateTo({
        url: '/pages/order/express/express?no=' + no + '&name=' + name
      });
    },

    jumpShare() {
      wx.navigateTo({
        url: '/pages/order/orderShare/orderShare?order_no=' + this.order.order_no + '&is_order=true'
      });
    },

    jump() {
      wx.navigateTo({
        url: '/pages/store/collage/collage?multi_groupon_item_id=' + this.order.multi_groupon_users[0].multi_groupon_items_id
      });
    },
	jumpHome() {
	  wx.switchTab({
	    url: '/pages/index/index/index'
	  });
	},

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

    jumpCode() {
      wx.navigateTo({
        url: '/pages/store/selfCode/selfCode?order_no=' + this.order.order_no
      });
    },

    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/store/detail/detail?id=' + id
      });
    },

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

    receive() {
      wx.showModal({
        title: '',
        content: '确定已收货?',
        success: res => {
          if (res.confirm) {
            this.receiveOrder(this.norder_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.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 "detail";
</style>