<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="pay">立即支付</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 {};
            }
        }
    },
    mixins: [room],
    data() {
        return {
            show: false,
            payType: '',
            qty: 1
        };
    },
    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;
        },
        pay() {
            if (!this.payType) {
                this.$toast('请选择购买方式');
                return;
            }
            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });

            this.$http
                .post('/ticketOrder/create', {
                    type: this.info.type,
                    qty: this.qty
                })
                .then(res => {
                    return this.$http.post('/ticketOrder/balancePay', {
                        orderId: res.id
                    });
                })
                .then(res => {
                    this.$toast.success('购买成功');
                    this.show = false;
                })
                .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>
