<template>
    <el-dialog center title="赠送" :visible.sync="show" width="680px" :before-close="handleClose">
        <div class="page" v-loading="loading">
            <div class="top-tips">通过手机号码检索用户信息，<span class="span1">赠送后该加密作品将会被转移</span></div>
            <div class="user">
                <el-input
                    v-model="phone"
                    placeholder="请输入手机号码进行检索"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    type="tel"
                    @change="getUser"
                ></el-input>

                <div class="user-content">
                    <div class="empty" v-if="info.length === 0">检索用户区域</div>
                    <div v-if="info.length != 0" class="content">
                        <img class="img" :src="info.avatar" alt="" />
                        <div class="init">
                            <div>
                                <div class="text1">{{ info.realName || '保密' }}</div>
                                <div class="text2">
                                    手机号码 <span>{{ info.phone }}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- <div class="info">
                <div class="price">
                    <span class="text1">应付金额</span>
                    <img src="../assets/icon_jiage.png" alt="" />
                    <span class="text2">{{ orderInfo.price }}</span>
                </div>
                <div class="flex1"></div>
                <div class="order">
                    <span class="text1">订单编号</span>
                    <span class="text2">{{ orderInfo.id }}</span>
                    <el-button type="info" @click="copy" size="mini" plain>复制</el-button>
                </div>
            </div> -->
            <el-form :model="form" :rules="rules" ref="rules" label-width="85px" class="demo-ruleForm">
                <div class="title">请输入交易密码，验证信息</div>
                <div v-if="sets">
                    <el-form-item prop="password">
                        <el-input
                            style="width: 300px; margin: 20px 0 0 100px"
                            type="password"
                            v-model="form.password"
                            placeholder="请输入纯数字交易密码"
                            autocomplete="off"
                        ></el-input>
                    </el-form-item>
                    <div @click="all('/transaction')" class="password">
                        <el-link class="sets" type="danger"> 忘记密码？</el-link>
                    </div>
                </div>
                <div v-else>
                    <el-link type="danger" @click="all('/transaction')" class="set"
                        >您当前没有交易密码，点击去设置</el-link
                    >
                </div>
            </el-form>

            <div class="payMethods">
                <div class="title">支付GAS</div>
                <div class="pay-list">
                    <div class="pay-info">
                        <el-radio-group v-model="payMethods">
                            <el-radio 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>
            <div class="price">
                应付金额<span class="span">{{ gas }}</span>
            </div>
            <div v-if="status == 'NOT_PAID'">
                <div class="qrcode">
                    <vue-qrcode :value="payUrl" :options="{ width: 140, margin: 1 }" class="code"></vue-qrcode>
                </div>
                <h3 class="qrcode-text">
                    二维码有效时常为5分钟，<span style="color: #939599" v-if="minters < 10">0</span>{{ minters }}分<span
                        style="color: #939599"
                        v-if="seconds < 10"
                        >0</span
                    >{{ seconds }} 请尽快支付
                </h3>
            </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 :disabled="btn" type="primary" class="pay" @click="submit">立即支付</el-button>
        </span>
    </el-dialog>
</template>

<script>
import vueQrcode from '@chenfengyuan/vue-qrcode';
import { mapState } from 'vuex';
import resolveUrl from 'resolve-url';
import phone from '../mixins/phone';
export default {
    components: {
        vueQrcode
    },
    data() {
        return {
            payMethods: 'WEIXIN',
            show: false,
            info: [],
            payTimeout: undefined,
            checked: true,
            id: 103,
            idOrder: 0,
            form: { password: '' },
            status: '',
            gas: 0,
            rules: {
                password: { min: 6, max: 6, message: '密码长度在 6 到 6 个数字', trigger: 'blur' }
            },
            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: false,
            orderInfo: {},
            minters: 0,
            seconds: 0,
            allTime: 5 * 60 - 1,
            closeTime: '',
            phone: '',
            sets: false
        };
    },
    mounted() {
        this.$http.get('/sysConfig/get/gas_fee').then(res => {
            this.gas = res.value;
        });
        if (this.isLogin) {
            this.passFn();
        }
    },
    computed: {
        btn() {
            if (this.phone && this.form.password) {
                return false;
            } else {
                return true;
            }
        },
        ...mapState(['userInfo']),
        payUrl() {
            return resolveUrl(
                this.$baseUrl,
                'https://nfttest.9space.vip/wx/redirect?redirectUrl=https://nfttest.9space.vip/payOrder/gift/weixin_pc?id=' +
                    this.idOrder
            );
        }
    },
    methods: {
        passFn() {
            this.$http
                .get('/user/tradeCodeStatus', {
                    // set: true
                    userId: this.userInfo.id
                })
                .then(res => {
                    // console.log(res);
                    this.sets = res.set;
                });
        },
        handleClose() {
            this.show = false;
            if (this.payTimeout) {
                clearInterval(this.payTimeout);
            }
        },
        init() {
            this.status = 'Unpaid';
        },
        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);
            }
        },
        init2() {
            this.$confirm('赠送成功，加密作品将会被转移', '赠送成功', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                customClass: 'myClass',
                center: true
            }).then(() => {});
        },
        payWatch() {
            //监控支付状态
            this.payTimeout = setInterval(() => {
                this.$http.get('/asset/get/' + this.$route.query.id).then(res => {
                    console.log(res);
                    if (res.status === 'GIFTED') {
                        clearInterval(this.payTimeout);
                        // this.status = res.status;
                        this.show = false;
                        setTimeout(() => {
                            this.init2();
                        }, 1000);
                    }
                });
            }, 1000);
        },
        submit() {
            // if (this.payMethods === 'DCEP') {
            //     this.wait();
            // }
            this.$http
                .post('/user/verifyTradeCode', {
                    tradeCode: this.form.password
                })
                .then(() => {
                    this.$http
                        .post('/asset/gift', {
                            assetId: this.$route.query.id,
                            toUserId: this.id
                        })
                        .then(res => {
                            this.initTime(res.id, res.createdAt);
                            this.status = res.status;
                            this.idOrder = res.id;
                            this.payWatch();
                            // console.log(res);
                        })
                        .catch(e => {
                            if (e) {
                                this.$message.error(e.error);
                            }
                        });
                })
                .catch(e => {
                    if (e) {
                        this.$message('交易密码错误');
                    }
                });
        },
        copy() {
            this.$copyText(this.orderInfo.id).then(
                e => {
                    this.$message.success('复制成功');
                },
                e => {
                    this.$message.warning('复制失败');
                }
            );
        },
        getUser() {
            this.$http
                .post('/user/searchByPhone', {
                    phone: this.phone
                })
                .then(res => {
                    this.info = res;
                    this.id = res.id;
                    if (this.info.length === 0) {
                        this.empty = true;
                    }
                })
                .catch(e => {
                    if (e) {
                        this.$message.error(e.error);
                    }
                });
        }
    }
};
</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-checkbox__inner {
    // width: 24px;
    // height: 24px;
    border-radius: 50%;
    // background-image: linear-gradient(135deg, #fdfb60 20%, #ff8f3e 100%);
}
/deep/ .el-checkbox__label {
    margin-right: 20px;
}
/deep/ .el-form-item__error {
    padding-left: 100px;
}
.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;
}
// .qrcode-text {
//     font-size: 16px;
//     font-weight: 400;
//     color: #939599;
//     line-height: 24px;
//     text-align: center;
// }
.sets {
    /deep/ .el-link--inner {
        color: #ff4f50;
    }
}
.set {
    font-size: 14px;
    line-height: 24px;
    margin-top: 50px;
    margin-left: 225px;
    /deep/ .el-link--inner {
        color: #ff4f50;
    }
}
.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;
        }
    }

    .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;
        }
    }
}
/deep/.el-loading-mask {
    background-color: #ffffffee;
}
/deep/ .el-dialog__header {
    padding: 18px 0;
}
/deep/ .el-dialog__title {
    font-weight: bold;
}
.title {
    font-size: 16px;
    font-weight: 400;
    color: #000000;
    line-height: 24px;
}
.password {
    text-align: right;
}
.price {
    font-size: 16px;
    font-weight: 400;
    color: #000000;
    line-height: 24px;
    text-align: center;
}
.span {
    font-size: 40px;
    font-family: 'OSP';
    font-weight: normal;
    color: #ff4f50 !important;
    line-height: 36px;
    margin-left: 3px;
}
/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 {
        .line(@bg:#E0FFF8,@radius:4px);
        border-width: 0;
        .el-radio__label {
            color: #000;
        }
    }
}
.payMethods {
    padding: 30px 0;
    .title {
        font-size: 16px;
        color: #000000;
        line-height: 24px;
        padding-bottom: 20px;
    }
}
/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));
    }
}

.top-tips {
    text-align: center;
    font-size: 14px;
    color: #939599;
    line-height: 28px;
}
.span1 {
    color: #ff4f50 !important;
}
.user {
    padding: 20px 0;

    .el-input {
        border-radius: 8px;
    }

    .user-content {
        background: #f5f7fa;
        border-radius: 8px;
        height: 142px;
        margin-top: 10px;
        position: relative;

        .empty {
            font-size: 14px;
            color: rgba(147, 149, 153, 0.5);
            line-height: 24px;
            text-align: center;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        .content {
            // padding: 0px 16px;
            display: flex;
            align-items: center;
            padding-top: 40px;
            border-radius: 12px;
            .img {
                width: 70px;
                height: 70px;
                border-radius: 50%;
                margin: 0 12px 0 20px;
            }
            .init {
                display: flex;
                align-items: center;
                justify-content: space-between;
                width: 100%;
                .text1 {
                    font-size: 16px;
                    font-weight: bold;
                    color: #000;
                    line-height: 24px;
                }
                .text2 {
                    font-size: 14px;
                    font-weight: 400;
                    color: #939599;
                    line-height: 24px;
                }
                span {
                    color: #000;
                }
                .btn {
                    width: 70px;
                    height: 26px;
                    border-radius: 16px;
                    border: 1px solid;
                    // border-image: linear-gradient(135deg, rgba(253, 251, 96, 1), rgba(255, 143, 62, 1)) 1 1;
                    line-height: 26px;
                    font-size: 14px;
                    color: #fdfb60;
                    text-align: center;
                }
            }
        }
    }
}
</style>
