<template>
    <view class="content">
        <block v-if="loading">
            <view class="loadingBox">
                <img class="loading" lazy-load src="http://teaclub.oss-cn-chengdu.aliyuncs.com/CloudShop/head_pic/3a96ff52efce6661adb41c54c29a87241b6e72f5gif" />
            </view>
        </block>
        <block v-else>
            <view style="height: 30rpx;"></view>
            <view class="orderBox">
                <view class="header" v-if="orderDetail.status==0">
                    <view class="title" :style="'color:'+template.color.color2">待付款</view>
                    <view class="statusBox">
                        <view class="status">
                            <icon type="daifukuan" size="30" :color="template.color.color5"></icon>
                            <view style="font-size: 24rpx;" :style="'color:'+template.color.color5">请在 {{ $day(orderDetail.create_time).add(15, 'm').format("YYYY-MM-DD HH:mm") }} 前完成支付</view>
                        </view>
                    </view>
                    <view class="button" v-if="$day().unix() < $day(orderDetail.create_time).add(15, 'm').unix()" :style="'background-color:'+template.color.color1" @click="paypalPrderTap(orderDetail.trade_no)">立即支付</view>
                </view>
                <view class="header" v-else>
                    <view class="title" :style="'color:'+template.color.color2">{{orderDetail.order_status=='OUT_MEAL'?"待取餐":"待出餐"}}</view>
                    <view class="statusBox">
                        <view class="status">
                            <icon type="dingdan" size="25" :color="template.color.color5"></icon>
                            <text :style="'color:'+template.color.color5">下单</text>
                        </view>
                        <view class="line"></view>
                        <view class="status">
                            <icon type="zhunbeizhong" size="25" :color="template.color.color5"></icon>
                            <text :style="'color:'+template.color.color5">准备</text>
                        </view>
                        <view class="line"></view>
                        <view class="status">
                            <icon type="ziqu" size="25" :color="orderDetail.order_status=='OUT_MEAL'?template.color.color5:template.color.color3"></icon>
                            <text :style="'color:'+(orderDetail.order_status=='OUT_MEAL'?template.color.color5:template.color.color3)">取餐</text>
                        </view>
                    </view>
                    <view class="codes" v-if="orderDetail.code">{{ orderDetail.code }}</view>
                    <!-- <view class="button" v-if="orderDetail.order_status=='OUT_MEAL'" :style="'background-color:'+template.color.color1" @click="qrcodeTap(true)">出示取餐码</view> -->
                </view>
                <!-- <view class="header" v-if="orderDetail.order_status==3">
                    <view class="title" :style="'color:'+template.color.color2">{{orderDetail.delivery_type==1?'已送达':'已取餐'}}</view>
                    <view class="statusBox">
                        <view class="status">
                            <icon type="wanchen" size="25" :color="template.color.color1"></icon>
                            <text style="padding-top: 6rpx;" :style="'color:'+template.color.color1">感谢惠顾，期待您的下次光临</text>
                        </view>
                    </view>
                </view>
                <view class="header" v-if="orderDetail.order_status==4">
                    <view class="title" :style="'color:'+template.color.color2">{{orderDetail.paypal_type?'已关闭':'已过期'}}</view>
                    <view class="statusBox">
                        <view class="status">
                            <icon type="yiguanbi" size="25" :color="template.color.color5"></icon>
                            <text style="padding-top: 6rpx;" :style="'color:'+template.color.color5">{{orderDetail.paypal_type?'订单已关闭，如有疑问请联系客服':'订单因超时未支付已过期'}}</text>
                        </view>
                    </view>
                </view> -->
                <view class="content">
                    <view class="storeBox">
                        <view class="store">
                            <view class="name">{{orderDetail.restaurant_name}}</view>
                            <view class="adds">{{orderDetail.restaurant_address}}</view>
                        </view>
                    </view>
                    <view class="lineBox">
                        <view class="left"></view>
                        <view class="right"></view>
                    </view>
                    <view class="goodsBox">
                        <view class="goods" v-for="(item, index) in orderGoods" :key="index">
                            <view class="infoBox">
                                <view class="title" :style="'color:'+template.color.color2">{{item.product_name}}</view>
                                <!-- <view v-if="item.details" class="label" :style="'color:'+template.color.color3">
                                    <text v-for="(va, i) in item.details" :key="i">{{va.sku_name}}</text>
                                </view> -->
                            </view>
                            <view class="number" :style="'color:'+template.color.color2">x{{item.number}}</view>
                            <view class="price" :style="'color:'+template.color.color2">¥{{$h.Add(item.product_price, item.spec_price)}}</view>
                        </view>
                        <view class="goods">
                            <view class="infoBox">
                                <view class="title" :style="'color:'+template.color.color3">商品合计<text>(共{{GoodNumber}}件商品)</text></view>
                            </view>
                            <view class="number"></view>
                            <view class="price" :style="'color:'+template.color.color3">¥{{orderTotal}}</view>
                        </view>
                        <view class="goods">
                            <view class="infoBox">
                                <view class="title" :style="'color:'+template.color.color3">优惠抵扣<text>(消费金)</text></view>
                            </view>
                            <view class="number"></view>
                            <view class="price" :style="'color:'+template.color.color3">-¥{{deduction}}</view>
                        </view>
                        <view class="total">
                            <view class="name" :style="'color:'+template.color.color2">合计</view>
                            <view class="price" :style="'color:'+template.color.color2">¥{{actuallypaid}}</view>
                        </view>
                    </view>
                    <view class="lineBox">
                        <view class="left"></view>
                        <view class="right"></view>
                    </view>
                    <view class="footer">
                        <view class="label">
                            <view class="title">订单编号</view>
                            <view class="nums">{{orderDetail.trade_no}}</view>
                        </view>
                        <view class="label">
                            <view class="title">下单时间</view>
                            <view class="nums">{{orderDetail.order_time || orderDetail.create_time}}</view>
                        </view>
                        <view class="label" v-if="orderDetail.order_time">
                            <view class="title">支付时间</view>
                            <view class="nums">{{orderDetail.order_time}}</view>
                        </view>
                        <view class="label" v-if="orderDetail.remark_user">
                            <view class="title">备注信息</view>
                            <view class="nums">{{orderDetail.remark_user}}</view>
                        </view>
                    </view>
                </view>
            </view>
            <view style="height: 50rpx;"></view>
            <view class="showContent" :style="qrcodeShow?'bottom:0;':''">
                <view class="bg" @click="qrcodeTap(false)"></view>
                <view class="showBox" style="min-height: 550rpx;">
                    <view class="pheader" :style="'color:'+template.color.color2">请向店员出示取餐码</view>
                    <view class="images">
                        <uqcode ref="uqcode" class="code-img" :size="150"/>
                    </view>
                    <view class="qrText" :style="'background-color:'+template.color.color4" @click="copyVerificationCode()">{{orderDetail.code}}</view>
                </view>
            </view>
        </block>
    </view>
</template>
<script>
import Icon from "@/pagesB/components/icon/icon.vue";
import uqcode from "@/components/uqrcode/uqrcode.vue";
import { post } from "@/request/api.js";
import { ToPayOpre } from "@/utils/reqTools.js";
let toPayOpre = new ToPayOpre();
export default {
    components: {
        Icon,
        uqcode
    },
    data() {
        return {
            loading: true,
            template: {
                color: {
                    color1: "#d3aa79",
                    color2: "#323232",
                    color3: "#999",
                    color5: "#d3aa79"
                }
            },
            shopData: uni.getStorageSync('__yipinShopInfo'),
            orderId: 0,
            orderDetail: '',
            paypalShow: false,
            qrcodeShow: false,
            userData: uni.getStorageSync('userinfo'),

            orderGoods: [],
            orderTotal: 0,
            GoodNumber: 0,
            Integral: 0,
            deduction: 0,
            actuallypaid: 0,
            pda: {}
        }
    },
    onShow() {},
    onLoad(e) {
        this.pda = e;
        this.getOrderDetail(e);
    },
    methods: {
        getOrderDetail(da) {
            post("local/orderDetail", da).then(res => {
                if (res.code == 0) {
                    this.loading = false;
					if(res.data.detail.error_code == 200){
						this.orderDetail = res.data.detail.data;
                        this.orderDetail.status = res.data.order.status;
					}
                    else{
						this.orderDetail = res.data.order;
                    }
                    let da = res.data.order.orderGoods
                    let totals = 0;
                    let number = 0;
                    da.forEach(itm => {
                        number += itm.number;
                        totals = this.$h.Add(totals, this.$h.Mul(this.$h.Add(itm.product_price, itm.spec_price), itm.number));
                    })
                    this.orderGoods = da;
                    this.orderTotal = totals;
                    this.GoodNumber = number;
                    this.getIntegral();
                    // if(this.orderDetail.order_status=='OUT_MEAL'){
                    //     setTimeout(() => {
                    //         this.$refs.uqcode.createCode(this.orderDetail.code)
                    //     }, 1000);
                    // }
                }
            })
        },
        getIntegral(){
            post("local/getIntegral",{type:1}).then(res=>{
                if (res.code == 0) {
                    let va = res.data.integral
                    this.Integral = this.$h.Mul(va,100);
                    this.deduction = this.$h.Mul(this.orderTotal,va).toFixed(2);
                    this.actuallypaid = this.$h.Sub(this.orderTotal,this.deduction);
                }
            })
        },

        getPhoneTap(number) {
            uni.makePhoneCall({
                phoneNumber: number
            });
        },
        paypalPrderTap(trade_no) {
            post("local/goOrderPay", {trade_no}).then(res => {
                if (res.code == 0 && res.data.data.prepayid) {
                    toPayOpre.toPay(res.data.data, (rea) => {
                      if (!rea) {
                        // 支付成功
                        this.getOrderDetail(this.pda);
                      } else {
                        // 支付失败
                      }
                    });
                }
            })
        },
        copyVerificationCode() {
            let data = this.orderDetail;
            uni.setClipboardData({
                data: data.code,
                success: function() {}
            });
        },
        qrcodeTap(va){
            this.qrcodeShow = va
        },
    }
}
</script>
<style lang="scss">
page {
    background-color: #e7e7e7;
}

.orderBox {
    width: 90%;
    margin: 0 auto;
    border-radius: 20rpx;
    overflow: hidden;
    position: relative;

    .lineBox {
        position: relative;
        width: 100%;
        height: 30rpx;
        margin-top: -16rpx;

        .left {
            position: absolute;
            left: -12rpx;
            width: 30rpx;
            height: 30rpx;
            background-color: #e7e7e7;
            border-radius: 100%;
        }

        .right {
            position: absolute;
            right: -12rpx;
            width: 30rpx;
            height: 30rpx;
            background-color: #e7e7e7;
            border-radius: 100%;
        }
    }

    .header {
        width: 100%;
        padding: 10rpx 0 30rpx;
        background-color: #fff;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.05);
        position: relative;

        .title {
            font-size: 30rpx;
            font-weight: bold;
            height: 100rpx;
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .statusBox {
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            width: 100%;
            height: 160rpx;

            .status {
                display: flex;
                align-items: center;
                flex-direction: column;
                justify-content: center;

                text {
                    font-size: 24rpx;
                }
            }

            .line {
                width: 80rpx;
                border-top: 1px dashed #e2e2e2;
                height: 1px;
                margin: 0 12rpx;
                margin-top: -30rpx;
            }
        }

        .button {
            padding: 8rpx 20rpx;
            font-size: 24rpx;
            color: #fff;
            border-radius: 100rpx;
            position: relative;

            button {
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                opacity: 0;
            }
        }
    }

    .content {
        width: 100%;
        height: 67%;
        background-color: #f7f7f7;

        .storeBox {
            width: 90%;
            margin: 0 auto;
            display: flex;
            align-items: center;
            border-bottom: 2rpx solid #e7e7e7;
            padding-bottom: 30rpx;
            padding-top: 30rpx;

            .store {
                width: 100%;
                display: flex;
                flex-direction: column;
                align-content: center;
                justify-content: center;

                .name {
                    color: #343434;
                    width: 100%;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    overflow: hidden;
                }

                .adds {
                    color: #999;
                    font-size: 26rpx;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                }
            }

            .buttonBox {
                width: 30%;
                display: flex;
                align-items: center;
                justify-content: flex-end;

                .button {
                    width: 80rpx;
                    height: 80rpx;
                    border-radius: 100%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin-left: 20rpx;
                }
            }
        }

        .goodsBox {
            margin-top: 12rpx;
            min-height: 250rpx;

            .goods {
                display: flex;
                width: 90%;
                margin: 0 auto;
                color: #343434;
                padding-bottom: 30rpx;

                .infoBox {
                    width: 60%;

                    .title {
                        width: 100%;
                        text-overflow: ellipsis;
                        white-space: nowrap;
                        overflow: hidden;

                        text {
                            font-size: 24rpx;
                            padding-left: 10rpx;
                        }
                    }

                    .label {
                        color: #999;
                        font-size: 26rpx;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        display: -webkit-box;
                        -webkit-line-clamp: 2;
                        -webkit-box-orient: vertical;
                    }
                }

                .number {
                    width: 15%;
                    display: flex;
                    justify-content: flex-end;
                    padding-right: 10rpx;
                }

                .price {
                    width: 25%;
                    display: flex;
                    justify-content: flex-end;
                }
            }

            .total {
                width: 90%;
                display: flex;
                margin: 0 auto;
                border-top: 1px dashed #e7e7e7;
                height: 90rpx;
                align-items: center;

                .name {
                    width: 50%;
                    display: flex;
                    justify-content: flex-start;
                    color: #343434;
                }

                .price {
                    width: 50%;
                    display: flex;
                    justify-content: flex-end;
                    color: #343434;
                }
            }
        }

        .footer {
            border-top: 2rpx solid #e7e7e7;
            width: 90%;
            margin: 0 auto;
            margin-top: -16rpx;
            padding-top: 30rpx;
            color: #999;
            font-size: 26rpx;
            line-height: 42rpx;
            padding-bottom: 30rpx;

            .label {
                display: flex;
                align-items: center;
                height: 70rpx;

                .title {
                    flex: 0 0 25%;
                }

                .nums {
                    flex: 0 0 75%;
                    display: flex;
                    justify-content: flex-end;
                }
            }
        }
    }
}

.showContent {
    position: fixed;
    bottom: -2000rpx;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
    transition: all .5s;

    .bg {
        position: absolute;
        z-index: 1001;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
    }

    .showBox {
        position: absolute;
        bottom: 0;
        width: 100%;
        min-height: 600rpx;
        z-index: 1002;
        background-color: #fff;
        border-top-left-radius: 20rpx;
        border-top-right-radius: 20rpx;
        box-shadow: 0 0 20rpx rgba(0, 0, 0, .08);

        .pheader {
            font-size: 32rpx;
            text-align: center;

            padding: 20rpx 0 10rpx;
        }

        .qrText {
            background-color: #CAB88F;
            width: 300rpx;
            margin: 0 auto;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 60rpx;
            border-radius: 6rpx;
            color: #fff;

            font-size: 30rpx;
        }

        .images {
            width: 400rpx;
            height: 350rpx;
            margin: 0 auto;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;

            image {
                width: 400rpx;
                height: 400rpx;
            }
        }

        .payTime {
            text-align: center;
            padding: 20rpx 0 0;

        }

        .paypalList {
            padding-top: 20rpx;
            padding-bottom: 80rpx;

            .paypal {
                display: flex;
                align-items: center;
                width: 90%;
                margin: 0 auto;
                position: relative;
                padding: 20rpx 0;

                .left {
                    flex: 0 0 18%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }

                .right {
                    flex: 0 0 75%;

                    .info {
                        font-size: 26rpx;
                    }
                }

                .icon {
                    flex: 0 0 7%;
                }
            }
        }
    }
}

.loadingBox {
    .loading {
        width: 100%;
        height: 100vw;
    }
}

.codes{
    color: #222;
    font-size: 26rpx;
}
</style>