<template>
    <div>
        <el-dialog center title="支付订单" :visible.sync="show" width="680px" :before-close="handleClose">
            <div class="border"></div>
            <div class="page" v-loading="loading">
                <div v-if="list[0]">
                    <div class="title1">兑换券</div>
                    <div class="coupon" @click="choose(list[0])">
                        <div class="box">
                            <img class="img" src="../assets/icon_youhuiquan_01@3x.png" alt="" />
                            <div class="coupon-top">
                                <div class="text1">{{ list[0].name }}</div>
                                <div class="text2" v-if="list[0].limited">仅限规定商品可用</div>
                            </div>
                        </div>
                        <div class="box1">
                            <div>
                                <span class="text3"
                                    >兑换券编码<span class="text4"> {{ list[0].id }}</span></span
                                >
                                <div class="text5">有效期至:{{ list[0].expiration }}</div>
                            </div>
                            <img
                                v-if="chooseId === list[0].id"
                                class="icon"
                                src="../assets/icon_gouxuan_pre.png"
                                alt=""
                            />
                            <img v-else class="icon" src="../assets/icon_gouxuan_huise.png" alt="" />
                        </div>
                    </div>
                </div>
                <div v-if="listAll.length !== 0" class="all" @click="all">查看更多</div>
                <div class="border"></div>
                <div class="info">
                    <div class="price">
                        <span class="text1">应付金额</span>
                        <img src="../assets/icon_jiage.png" alt="" />
                        <span class="text2">{{ money }}</span>
                        <span class="text3">(含gas费¥{{ gas }})</span>
                    </div>
                    <div class="flex1"></div>
                    <div class="order">
                        <span class="text1">订单编号</span>
                        <span class="text2">{{ info.id }}</span>
                        <el-button type="info" @click="copy" size="mini" plain>复制</el-button>
                    </div>
                </div>
                <div class="border"></div>
                <div class="payMethods">
                    <div class="title">选择支付方式</div>
                    <div class="pay-list">
                        <div class="pay-info">
                            <el-radio-group v-model="payMethods">
                                <el-radio
                                    :disabled="flag"
                                    v-for="item in payList"
                                    border
                                    :label="item.type"
                                    :key="item.type"
                                >
                                    <img :src="item.icon" alt="" />
                                    <span> {{ item.name }}</span>
                                </el-radio>
                            </el-radio-group>
                        </div>
                    </div>

                    <div v-if="status === 'NOT_PAID'">
                        <div class="qrcode">
                            <vue-qrcode
                                v-if="payMethods === 'WEIXIN'"
                                :value="payUrl"
                                :options="{ width: 140, margin: 1 }"
                                class="code"
                            ></vue-qrcode>
                            <vue-qrcode
                                v-if="payMethods === 'ALIPAY'"
                                :value="payUrl2"
                                :options="{ width: 140, margin: 1 }"
                                class="code"
                            ></vue-qrcode>
                        </div>
                        <h3 class="qrcode-text">
                            二维码有效时常为5分钟，<span v-if="minters < 10">0</span>{{ minters }}分<span
                                v-if="seconds < 10"
                                >0</span
                            >{{ seconds }} 请尽快支付
                        </h3>
                    </div>
                </div>

                <div class="tips">
                    <div class="text1">购买须知</div>
                    <div class="text2">
                        1.数字盲盒按照随机概率进行抽取<br />
                        2.数字盲盒及其数字内容为虚拟商品<span style="color: #ff4f50">支付完成后，不可退换</span><br />
                        3.数字盲盒及其数字内容仅作为消费娱乐（或其他约定场景）使用<br />
                        4.切勿当成投资和投机品进行盲目消费
                    </div>
                </div>
            </div>

            <span v-if="status != 'NOT_PAID'" slot="footer" class="dialog-footer">
                <el-button type="primary" class="pay" @click="submit">确认</el-button>
            </span>
        </el-dialog>
        <el-dialog center title="兑换券" :visible.sync="show2" width="680px">
            <div class="border"></div>
            <div style="padding: 30px 0 20px 0" class="page">
                <div v-for="(item, index) in listAll" :key="index" @click="IdFn(item)">
                    <div class="coupon" @click="choose(item)">
                        <div class="box">
                            <img class="img" src="../assets/icon_youhuiquan_01@3x.png" alt="" />
                            <div class="coupon-top">
                                <div class="text1">{{ item.name }}</div>
                                <div class="text2" v-if="item.limited">仅限规定商品可用</div>
                            </div>
                        </div>
                        <div class="box1">
                            <div>
                                <span class="text3"
                                    >兑换券编码<span class="text4"> {{ item.id }}</span></span
                                >
                                <div class="text5">有效期至:{{ item.expiration }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- <el-empty v-if="empty" description="暂无兑换券哦~" /> -->
        </el-dialog>
    </div>
</template>

<script>
import vueQrcode from '@chenfengyuan/vue-qrcode';
import coupon from '../mixins/coupon';
import resolveUrl from 'resolve-url';
// import pageableTable from '../mixins/pageableTable';
import { mapState } from 'vuex';
import { add } from 'mathjs';
export default {
    mixins: [coupon],
    props: {
        statusAll: {
            type: Object
        }
    },
    // mixins: [pageableTable],
    data() {
        return {
            show: false,
            show2: false,
            payTimeout: undefined,
            chooseId: '',
            org: '',
            empty: false,
            payMethods: 'WEIXIN',
            flag: false,
            minters: 0,
            seconds: 0,
            allTime: 5 * 60 - 1,
            closeTime: '',
            list: [],
            listAll: [],
            payList: [
                {
                    icon: require('../assets/weixin.png'),
                    name: '微信',
                    type: 'WEIXIN'
                },
                {
                    icon: require('../assets/alipay.png'),
                    name: '支付宝',
                    type: 'ALIPAY'
                }

                // {
                //     icon: require('../assets/dcep.png'),
                //     name: 'DCEP支付',
                //     type: 'DCEP'
                // }
            ],
            loading: true,
            info: {},
            gas: 0,
            id: 0,
            status: '',
            payUrl: '',
            payUrl2: ''
        };
    },
    computed: {
        // 微信
        // payUrl() {
        // return resolveUrl(
        //     this.$baseUrl,
        //     'https://nfttest.9space.vip/wx/redirect?redirectUrl=https://nfttest.9space.vip/payOrder/weixin?id=' +
        //         this.id
        // );
        // },
        money() {
            let money = 0;
            if (this.info.price && !this.couponInfo) {
                money = this.accAdd(this.info.price, money);
            }
            if (this.gas && (!this.couponInfo || this.couponInfo.needGas)) {
                money = this.accAdd(money, this.gas);
            }

            return money;
        },
        ...mapState(['couponInfo', 'userInfo'])
    },
    mounted() {
        if (this.isLogin) {
            this.add();
        }
    },
    methods: {
        initTime(dingdanId, initTime) {
            let oldTime = new Date(initTime).valueOf();
            let newTime = new Date().valueOf();
            let matchedTime = undefined;
            let matchedTime1 = undefined;
            if (oldTime) {
                matchedTime = newTime - oldTime;
                matchedTime1 = this.allTime - parseInt(matchedTime / 1000);
                if (matchedTime1 > 0) {
                    this.timeout(matchedTime1);
                } else {
                    this.show = false;
                }
            } else {
                if (dingdanId) {
                    this.timeout(this.allTime);
                }
            }
        },
        timeout(time) {
            let _this = this;
            _this.minters = parseInt(time / 60) > 0 ? parseInt(time / 60) : 0;
            _this.seconds = parseInt(time - _this.minters * 60);
            if (_this.minters > 0) {
                _this.closeTime = setInterval(() => {
                    _this.seconds--;
                    if (_this.seconds <= 0) {
                        _this.minters--;

                        if (_this.minters == -1) {
                            this.show = false;
                            _this.minters = 0;
                            _this.seconds = 0;
                            clearInterval(_this.closeTime);
                        } else {
                            _this.seconds = 60;
                        }
                    }
                }, 1000);
            } else {
                _this.closeTime = setInterval(() => {
                    _this.seconds--;
                    if (_this.seconds <= 0) {
                        _this.seconds = 0;
                        clearInterval(_this.closeTime);
                    }
                }, 1000);
            }
        },

        add() {
            this.$http
                .post(
                    '/userCoupon/all',
                    {
                        query: {
                            userId: this.userInfo.id
                        }
                    },
                    {
                        body: 'json'
                    }
                )
                .then(res => {
                    this.list = res.content.filter(item => {
                        return !item.used && this.checkTime(item) && this.checkUse(item, this.$route.query.id);
                    });
                    this.listAll = res.content.filter(item => {
                        return !item.used && this.checkTime(item) && this.checkUse(item, this.$route.query.id);
                    });
                    // console.log(this.listAll);
                });
        },
        IdFn(e) {
            this.list[0] = e;
            this.show2 = false;
        },
        all() {
            this.show2 = true;
        },
        choose(info) {
            // console.log(info);
            this.chooseId = info.id;
            this.$store.commit('setCouponInfo', info);
        },
        handleClose() {
            this.show = false;
            this.flag = false;
            if (this.payTimeout) {
                clearInterval(this.payTimeout);
            }
        },
        payWatch() {
            //监控支付状态
            this.payTimeout = setInterval(() => {
                this.$http.get('/order/get/' + this.id).then(res => {
                    // console.log(res);
                    if (res.status === 'FINISH') {
                        clearInterval(this.payTimeout);
                        this.show = false;
                        this.flag = false;
                        setTimeout(() => {
                            this.init2();
                        }, 1000);
                    }
                });
            }, 1000);
        },
        init2() {
            this.$confirm('您购买成功的商品在我的藏品中展示', '支付成功', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                customClass: 'myClass',
                center: true
            }).then(() => {});
        },
        submit() {
            // console.log(this.$baseUrl);
            // if (this.payMethods === 'DCEP') {
            //     this.wait();
            // }
            this.flag = true;
            let url = '/order/create?collectionId=' + this.$route.query.id + '&qty=1';
            if (this.couponInfo) {
                url += '&couponId=' + this.couponInfo.id;
            }
            this.$http
                .post(url)
                .then(res => {
                    if (this.money) {
                        // 没有兑换券
                        this.initTime(res.id, res.createdAt);
                        this.status = res.status;
                        this.id = res.id;
                        this.payWatch();
                    } else {
                        // 兑换券
                        this.$message.success('支付成功');
                        setTimeout(() => {
                            this.$router.replace('/collectionorder');
                        }, 1000);
                    }
                    if (this.payMethods === 'WEIXIN') {
                        this.$http
                            .get('/payOrder/weixin', {
                                id: res.id
                            })
                            .then(res => {
                                this.payUrl = res.wx_h5_pay_url;
                            });
                    }
                    if (this.payMethods === 'ALIPAY') {
                        this.$http
                            .get('/payOrder/alipay_qr', {
                                id: res.id
                            })
                            .then(res => {
                                this.payUrl2 = res;
                            });
                    }
                })
                .catch(e => {
                    if (e) {
                        this.$message.error(e.error);
                    }
                });
        },
        init(info) {
            this.status = 'Unpaid';
            this.checkLogin()
                .then(() => {
                    this.show = true;
                    this.loading = true;
                    this.info = info;
                    return this.$http.get('/sysConfig/get/gas_fee');
                })
                .then(res => {
                    this.gas = res.value;
                    this.loading = false;
                })
                .catch(e => {
                    if (e) {
                        this.$message.warning(e.error);
                    }
                });
        },
        copy() {
            this.$copyText(this.info.id).then(
                e => {
                    this.$message.success('复制成功');
                },
                e => {
                    this.$message.warning('复制失败');
                }
            );
        }
    },
    components: {
        vueQrcode
    }
};
</script>
<style lang="less">
.myClass {
    width: 340px;
    .el-button {
        background: linear-gradient(133deg, @prim 0%, @warn 100%);
        border-radius: 4px;
        border: 0;
    }
    .el-button:nth-last-child(2) {
        background: #c4c7cc;
        color: #ffffff;
        font-size: 13px;
    }
}
</style>
<style lang="less" scoped>
/deep/ .el-dialog__body {
    padding: 4px 20px 0;
}
/deep/ .el-radio__inner {
    background-color: #fff !important;
}
.border {
    height: 1px;
    background: #f2f3f5;
    border-radius: 1px;
}
/deep/ .el-empty {
    width: 100px;
    height: 100px;
    margin: 0 auto;
    padding: 50px 0 100px 0px;
}
.title1 {
    font-size: 16px;
    font-weight: 400;
    color: #000000;
    line-height: 24px;
    margin: 31px 0 20px;
}
.info {
    .flex();
    height: 94px;
    .price {
        .flex();
        align-items: flex-end;
        .text1 {
            font-size: 16px;
            color: #000000;
            line-height: 24px;
        }
        img {
            width: 10px;
            display: block;
            margin: 0 0 4px 10px;
        }
        .text2 {
            font-size: 40px;
            font-family: OSP;
            font-weight: normal;
            color: #ff4f50;
            line-height: 36px;
        }
        .text3 {
            font-size: 13px;
            color: #000000;
            line-height: 24px;
            margin-left: 15px;
        }
    }

    .order {
        .text1 {
            font-size: 13px;
            color: #939599;
            line-height: 24px;
        }

        .text2 {
            font-size: 13px;
            color: #0e0e0e;
            line-height: 22px;
            padding: 0 6px 0 30px;
        }
        .el-button {
            height: 20px;
            line-height: 20px;
            padding: 0 10px;
        }
    }
}
.all {
    cursor: pointer;
    margin-bottom: 30px;
}
/deep/.el-loading-mask {
    background-color: #ffffffee;
}
/deep/ .el-dialog {
    border-radius: 8px;
}
/deep/ .el-dialog__header {
    padding: 18px 0;
}
/deep/ .el-dialog__title {
    font-weight: bold;
    font-size: 18px;
}
.list {
    padding: 24px 0 50px;
}
.coupon {
    border-radius: 8px;
    display: flex;
    margin-bottom: 16px;
    .box {
        display: flex;
        align-items: center;
        justify-content: center;
        .img {
            width: 189px;
            height: 120px;
        }
        .coupon-top {
            padding: 16px 0;
            position: absolute;
            z-index: 2;
            .text1 {
                text-align: center;
                font-size: 24px;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: bold;
                color: #1a1a1a;
                line-height: 36px;
            }
            .text2 {
                font-size: 12px;
                font-weight: 400;
                color: #1a1a1a;
                line-height: 22px;
                text-align: center;
            }
        }
    }
    .box1 {
        background: #e5e5e5;
        border-radius: 0px 8px 8px 0px;
        width: 100%;
        display: flex;
        justify-content: space-between;
        padding: 16px 20px;
        .text3 {
            font-size: 14px;
            font-weight: 400;
            color: #939599;
            line-height: 24px;
        }
        .text4 {
            font-size: 14px;
            font-weight: 400;
            color: #000000;
            line-height: 24px;
            text-align: bottom;
        }
        .text5 {
            font-size: 11px;
            font-weight: 400;
            color: #939599;
            line-height: 16px;
            margin-top: 45px;
        }
    }
    .icon {
        width: 24px;
        height: 24px;
        margin-top: 33px;
    }
}
/deep/.el-radio {
    .flex();
    display: inline-flex;
    flex-direction: row-reverse;
    justify-content: space-between;
    width: 190px;
    height: 42px;
    color: #939599;
    padding: 0;
    box-sizing: border-box;
    .el-radio__label {
        .flex();
        img {
            width: 24px;
            height: 24px;
        }
        span {
            font-size: 16px;
            font-weight: bold;
            padding: 0 10px;
        }
        margin-left: 24px;
    }
    .el-radio__input {
        margin-right: 24px;
        width: 20px;
        flex-shrink: 0;

        .el-radio__inner {
            width: 20px;
            height: 20px;
            &::after {
                content: '\e6da';
                font-family: 'element-icons';
                width: 14px;
                height: 14px;
                font-size: 14px;
                color: #fff;
                font-weight: bold;
                background-color: transparent;
            }
        }

        &.is-checked {
            .el-radio__inner {
                background-image: linear-gradient(135deg, @prim 20%, @warn 100%);
                border-width: 0;
            }
        }
    }
    &.is-checked {
        background: rgba(253, 251, 96, 0.12);
        border-radius: 4px;
        border: 1px solid #ff4f50;
        // .line(@bg:#E0FFF8,@radius:4px);
        // border-width: 0;
        .el-radio__label {
            color: #000;
        }
    }
}
.title {
    font-size: 16px;
    color: #000000;
    line-height: 24px;
    padding-bottom: 20px;
}
.payMethods {
    padding: 30px 0;
}
/deep/.el-radio.is-bordered + .el-radio.is-bordered {
    margin-left: 0;
}

.tips {
    padding-top: 20px;
    .text1 {
        font-size: 16px;
        color: #939599;
        line-height: 24px;
    }
    .text2 {
        font-size: 14px;
        color: #939599;
        line-height: 28px;
        margin-top: 10px;
    }
}
.pay {
    background-image: linear-gradient(135deg, @prim, @warn);
    border-width: 0;
    width: 200px;
    color: #000;
    &:hover {
        background-image: linear-gradient(135deg, darken(@prim, 20), darken(@warn, 20));
    }
}
.qrcode {
    width: 160px;
    height: 160px;
    background: #f5f7fa;
    border-radius: 11px;
    margin: 30px auto 10px;
    padding: 10px;
    box-sizing: border-box;
    .code {
        border-radius: 11px;
        overflow: hidden;
    }
}
.qrcode-text {
    font-size: 16px;
    color: #939599;
    line-height: 24px;
    margin: 0 auto;
    text-align: center;
}
</style>
