<template>
    <van-popup v-model:show="show" safe-area-inset-bottom closeable position="bottom">
        <div class="popup-content">
            <div class="title">购买详情</div>
            <div class="info">
                <van-image :src="ticketInfo.img" width="102" height="48" fit="cover" />
                <div class="info-content">
                    <div class="text1">{{ ticketInfo.label }}</div>
                    <div class="text2">
                        <div class="text2-l">
                            <span>¥</span>
                            <span>{{ info.price }}</span>
                        </div>
                        <van-stepper size="mini" :min="1" v-model="qty" input-width="40px" />
                    </div>
                </div>
            </div>

            <div class="title">购买方式</div>
            <div class="pay-list">
                <pay-method-pick v-model="payType"></pay-method-pick>

                <div class="tips">虚拟商品购买成功后不支持退换，有效果期30天</div>
            </div>
            <div class="btn">
                <div class="price">
                    <span>¥</span>
                    <span>{{ money }}</span>
                </div>
                <van-button type="primary" @click="submit">立即支付</van-button>
            </div>
        </div>
    </van-popup>
</template>

<script>
import PayMethodPick from './PayMethodPick.vue';
import room from '../mixins/room.js';
export default {
    components: { PayMethodPick },
    props: {
        info: {
            type: Object,
            default: () => {
                return {};
            }
        },
        buyType: {
            type: Boolean,
            default: false
        }
    },
    mixins: [room],
    data() {
        return {
            show: false,
            payType: '',
            qty: 1,
            timer: null,
            timerNum: 0,
            orderId: 0
        };
    },
    computed: {
        money() {
            let price = this.info.price || 0;
            price = this.accMul(price, this.qty);
            return price.toFixed(2);
        },
        ticketInfo() {
            let info = [...this.requireTicketOptions].find(item => {
                return item.value === this.info.type;
            });
            return info ? info : {};
        }
    },
    methods: {
        init() {
            this.show = true;
        },
        getOrder(next = false) {
            this.$http.get('/ticketOrder/get/' + this.orderId).then(res => {
                if (this.timer) {
                    clearTimeout(this.timer);
                }
                if (this.timerNum >= 20) {
                    this.$toast.clear();
                    this.$dialog
                        .confirm({
                            title: '提示',
                            message: '订单是否已经支付',
                            confirmButtonText: '已经支付',
                            cancelButtonText: '未支付'
                        })
                        .then(() => {
                            this.$dialog
                                .alert({
                                    title: '正在排队中...',
                                    message: '您的通行证订单正在排队，通行证后续会直接出现在您的账号中哦～'
                                })
                                .then(res => {
                                    this.show = false;
                                    if (this.buyType) {
                                        this.$router.go(-1);
                                    }
                                });
                        })
                        .catch(e => {
                            this.$dialog
                                .confirm({
                                    title: '提示',
                                    message: '订单未支付是否重新支付？',
                                    confirmButtonText: '重新支付',
                                    cancelButtonText: '取消订单'
                                })
                                .then(() => {
                                    this.timerNum = 0;
                                    this.pay();
                                })
                                .catch(() => {
                                    this.show = false;
                                });
                        });
                } else if (res.status === 'FINISH' || res.status === 'CANCELLED') {
                    this.$toast.clear();
                    this.$dialog
                        .alert({
                            title: '提示',
                            message: res.status === 'CANCELLED' ? '订单支付失败,点击重新下单' : '通行证购买成功'
                        })
                        .then(() => {
                            if (res.status === 'CANCELLED') {
                                this.show = false;
                            } else {
                                this.show = false;
                                if (this.buyType) {
                                    this.$router.go(-1);
                                }
                            }
                            // on close
                        });
                } else if (next) {
                    this.timerNum += 1;
                    this.timer = setTimeout(() => {
                        this.getOrder(next);
                    }, 1500);
                }
            });
        },
        submit() {
            if (!this.payType) {
                this.$toast('请选择购买方式');
                return;
            }

            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });
            this.createOrder().then(id => {
                this.pay(true);
            });
        },
        createOrder() {
            return this.$http
                .post('/ticketOrder/create', {
                    type: this.info.type,
                    qty: this.qty
                })
                .then(res => {
                    this.orderId = res.id;
                    return Promise.resolve(res.id);
                });
        },
        pay() {
            this.$http
                .get(`/order/pay/${this.orderId}/${this.payType}`)
                .then(res => {
                    if (this.payType === 'ALIPAY') {
                        this.getOrder(true);
                        setTimeout(() => {
                            document.location.href = res;
                        }, 500);
                    }
                })
                .catch(e => {
                    this.$toast(e.error);
                });
        }
    }
};
</script>

<style lang="less" scoped>
.pay-list {
    padding: 10px 16px;
}
.title {
    font-size: 14px;
    font-weight: bold;
    color: #ffffff;
    line-height: 24px;
    padding: 16px 16px 0;
}

.btn {
    padding: 50px 18px 9px;
    .flex();
    justify-content: space-between;
    .van-button {
        width: 160px;
    }
    .price {
        span {
            &:first-child {
                font-size: 12px;
                font-weight: bold;
                color: #ffffff;
                line-height: 20px;
            }
            &:nth-child(2) {
                font-size: 28px;
                font-weight: bold;
                color: #ffffff;
                line-height: 28px;
            }
        }
    }
}
.info {
    .flex();
    padding: 16px;
    align-items: stretch;
    .info-content {
        flex-grow: 1;
        margin-left: 7px;
        .flex-col();
        justify-content: space-between;
        .text1 {
            font-size: 14px;
            color: #ffffff;
            line-height: 24px;
        }
        .text2 {
            .flex();
            align-items: flex-end;
            .text2-l {
                flex-grow: 1;
                span {
                    font-size: 12px;
                    color: #ffffff;
                    line-height: 17px;
                    &:nth-child(2) {
                        font-size: 14px;
                    }
                }
            }
        }
    }
}

.tips {
    font-size: 12px;
    color: #6a6d83;
    line-height: 17px;
    padding: 6px 0;
}

/deep/.van-stepper {
    .van-stepper__minus,
    .van-stepper__plus {
        width: 20px;
        height: 20px;
        border-radius: 4px;
    }

    .van-stepper__input {
        height: 20px;
        margin: 0 6px;
        border-radius: 4px;
    }
}
</style>
