<template>
    <div class="top">
        <div class="top-content">
            <div class="title">寄售说明</div>
            <div class="name" v-for="(item, index) in list" :key="index">
                <span>{{ item.title }}</span>
            </div>
            <div class="border border1"></div>
            <div class="content">
                <div class="title">寄售价格(元）<span>最高定价20000.00</span></div>
                <!-- <van-field type="number" input-align="center" placeholder="请输入价格" v-model="price" /> -->
                <van-stepper
                    v-model="price"
                    :default-value="''"
                    :show-plus="false"
                    :show-minus="false"
                    :decimal-length="2"
                    allow-empty
                    max="20000"
                    min="0"
                    placeholder="请输入价格"
                />
                <div class="title">预计收入(元）</div>
                <div class="info-item">
                    <span>寄售定价</span>
                    <span class="val">{{ price1 }}</span>
                </div>
                <div class="info-item">
                    <span>版税({{ info.royalties || 0 }}%)</span>
                    <span class="val">{{ price2 }}</span>
                </div>
                <div class="info-item">
                    <span>技术服务费(4%) </span>
                    <span class="val">{{ price3 }}</span>
                </div>
                <div class="info-item">
                    <span>支付通道费(1%)</span>
                    <span class="val">{{ price4 }}</span>
                </div>
                <div class="info-item">
                    <span>最终收入</span>
                    <span class="val prim">{{ price5 }}</span>
                </div>
            </div>
            <div class="border"></div>
            <div class="input">
                <div class="text1">请输入交易密码</div>
                <div v-if="sets">
                    <van-password-input :gutter="10" :value="password" :focused="showKeyboard" @focus="keyboardEnter" />
                    <van-number-keyboard
                        v-model="password"
                        :show="showKeyboard"
                        @blur="keyBlur"
                        :gutter="10"
                        maxlength="6"
                        theme="custom"
                        close-button-text="完成"
                        random-key-order
                    />
                    <div class="text2-content">
                        <div class="text2" @click="goRouter">忘记密码？</div>
                    </div>
                </div>
                <div v-else class="text3" @click="$router.push('/tradingPassword')">您当前没有交易密码，点击去设置</div>
            </div>
        </div>
        <div class="bottom van-safe-area-bottom" ref="bottom">
            <van-notice-bar
                :color="$colors.prim"
                :background="$colors.prim"
                text="出售成功的数字藏品将会携带数字藏品的所有信息转让于购买方，包括但不限于：权益的使用、数字藏品的区块信息等"
            />
            <div class="bottom-content">
                <div class="btn1" @click="$router.go(-1)">取消</div>
                <div class="btn2" @click="submit">确认寄售</div>
            </div>
        </div>
        <div class="border"></div>
    </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
    name: 'Top',
    inject: ['changeScroll'],
    data() {
        return {
            price: '',
            showKeyboard: false,
            password: '',
            sets: false,
            list: [
                {
                    title: '1. 作品寄售为单个作品价格'
                },
                {
                    title: '2. 用户将会看到数字藏品的权益的使用情况'
                },
                {
                    title: '3. 交易成功后数字藏品的点赞数量将会被清除'
                },
                {
                    title: '4. 平台会收取作品出售价格的 5% 作为服务费'
                },
                {
                    title: '5. 因电子支付通道限制，单笔限额20000元'
                }
            ],
            info: {}
        };
    },
    computed: {
        ...mapState(['userInfo']),
        price1() {
            let price = Math.floor(this.price * 100) / 100;
            return price.toFixed(2);
        },
        price2() {
            let price = Math.floor(this.price * (this.info.royalties || 0)) / 100;
            return price.toFixed(2);
        },
        price3() {
            let price = Math.floor(this.price * 4) / 100;
            return price.toFixed(2);
        },
        price4() {
            let price = Math.floor(this.price * 1) / 100;
            return price.toFixed(2);
        },
        price5() {
            let price = Math.floor(this.price * (95 - (this.info.royalties || 0))) / 100;
            return price.toFixed(2);
        }
    },
    mounted() {
        this.passFn();
        this.$http.get('/asset/get/' + this.$route.query.id).then(res => {
            this.info = res;
        });
    },
    methods: {
        keyboardEnter() {
            this.changeScroll(1000);
            this.showKeyboard = true;
        },
        passFn() {
            this.$http
                .get('/user/tradeCodeStatus', {
                    userId: this.userInfo.id
                })
                .then(res => {
                    console.log(res);
                    this.sets = res.set;
                });
        },
        submit() {
            if (Number(this.price)) {
                this.$dialog
                    .confirm({
                        title: '寄售',
                        message: `商品将以<span style='color:#ff4f50;font-weight:bold;font-size:18px;margin:0 5px'>${this.price}</span>的价格上架`,
                        allowHtml: true
                    })
                    .then(() => {
                        this.$toast.loading({
                            message: '加载中...',
                            forbidClick: true
                        });
                        return new Promise((resolve, reject) => {
                            this.$http
                                .post('/user/verifyTradeCode', {
                                    tradeCode: this.password
                                })
                                .then(_ => resolve())
                                .catch(e => {
                                    reject({ error: '密码错误' });
                                });
                        });
                    })
                    .then(() => {
                        return this.$http.post('/asset/consignment', {
                            id: this.$route.query.id,
                            price: this.price
                        });
                    })
                    .catch(e => {
                        this.$toast(e.error || '出现错误，请稍后再试');
                        return Promise.reject('cancel');
                    })
                    .then(res => {
                        this.$toast.success('寄售成功');
                        setTimeout(() => {
                            this.$router.go(-1);
                        }, 1000);
                    })
                    .catch(e => {
                        if (e && e.error) {
                            this.$toast(e.error);
                        }
                    });
            } else {
                this.$toast('请输入价格');
            }
        },
        goRouter() {
            if (!this.showKeyboard) {
                this.$router.push('/tradingPassword');
            }
        },
        keyBlur() {
            setTimeout(() => {
                this.showKeyboard = false;
            }, 100);
        }
    }
};
</script>

<style lang="less" scoped>
/deep/ .van-notice-bar {
    background: rgba(255, 108, 0, 0.1) !important;
}
/deep/ .van-password-input {
    margin: 0 0;
    .van-password-input__security {
        border: 1px solid @text3;
        height: 42px;
        border-radius: 2px;
        li {
            background-color: transparent;
            &:not(:last-child) {
                border-right: 1px solid @text3;
            }
            i {
                // background-color: #fff;
            }
        }
    }
}
/deep/.van-number-keyboard__body {
    background-color: fade(#4a4a4a, 50%);
    .van-key {
        background-color: @bg;
        &:active {
            background-color: @bg3;
        }
    }
}

.input {
    padding: 12px 0 20px;
    .text1 {
        font-size: @font2;
        color: @text0;
        line-height: 24px;
        margin-bottom: 6px;
    }
}
.top-content {
    background-color: @bg;
    padding-top: 10px;
}
.top {
    background-color: @bg3;
    /deep/ .van-password-input {
        margin: 10px 0;
        padding: 0 25px;
        .van-password-input__security {
            border-width: 0;
            height: 42px;
            border-radius: 2px;
            li {
                background-color: transparent;
                &:not(:last-child) {
                    border-right: 1px solid @text3;
                }
                border: 1px solid @text3;
                i {
                    // background-color: #fff;
                }
            }
        }
    }
    /deep/ .van-cell {
        padding-top: 20px;
    }
    /deep/.van-cell__title {
        font-size: @font2;
        font-weight: bold;
        color: @text0;
        line-height: 24px;
    }
    // background-color: @bg3;;
    padding-bottom: 280px;
    .title {
        font-size: 14px;
        color: @text0;
        line-height: 30px;
        margin-top: 5px;
        padding-left: 16px;
    }
    .name {
        padding-left: 16px;
        span {
            font-size: 12px;
            font-weight: 400;
            color: @text3;
            line-height: 22px;
        }
    }
    .bottom {
        position: fixed;
        bottom: 0px;
        left: 0;
        right: 0;
        background-color: @bg;
        z-index: 20;
        border-top: 1px solid @tabBorder;

        .bottom-content {
            padding: 6px 16px;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .btn1 {
                width: 161px;
                height: 44px;
                border-radius: 22px;
                line-height: 44px;
                color: @text3;
                text-align: center;
                border: 1px solid @text3;
            }
            .btn2 {
                width: 162px;
                height: 44px;
                line-height: 44px;
                text-align: center;

                color: @btnText;
                background: linear-gradient(135deg, @prim 0%, @warn 100%);
                border-radius: 22px;
            }
            .van-button {
                flex-grow: 1;
                color: #333230 !important;
                max-width: 210px;
            }
            .text {
                font-size: @font2;
                color: @text3;
                line-height: 20px;
                word-break: keep-all;
            }
        }
    }
    .border {
        // width: 375px;
        height: 5px;
        background: @bg3;
        &.border1 {
            margin-top: 20px;
        }
    }
    .content {
        // width: 375px;
        background: @bg;

        .title {
            span {
                font-size: 12px;
                color: #ff4f50;
            }
        }

        .van-cell {
            padding: 0 16px 0px;
            margin: 10px 0 0;

            /deep/.van-cell__value {
                background-color: @bg3;
                padding: 8px;
            }
        }

        .info-item {
            padding: 3px 16px;
            .flex();
            justify-content: space-between;
            font-size: 12px;
            color: #939599;
            line-height: 24px;

            .val {
                border: 1px solid #f5f7fa;
                box-sizing: border-box;
                text-align: center;
                min-width: 72px;
                display: inline-block;

                &.prim {
                    color: @prim;
                }
            }
        }
    }
    .input {
        // margin-top: 20px;
        width: 100%;
        .text1 {
            font-size: @font2;
            color: @text0;
            line-height: 24px;
            margin-bottom: 6px;
            padding: 0 16px;
        }
        .text2 {
            font-size: @font2;
            text-align: right;
            font-weight: 400;
            color: @text3;
            line-height: 24px;
            margin-right: 15px;
        }
        .text3 {
            font-size: @font2;
            color: #ff4f50;
            line-height: 24px;
            margin-top: 25px;
            display: flex;
            justify-content: center;
        }
    }
}
.text2-content {
    .flex();
    justify-content: flex-end;
}
/deep/.van-stepper {
    padding: 10px 16px;
    width: 100%;
    box-sizing: border-box;
    .van-stepper__input {
        width: 100%;
        height: 40px;
        margin: 0 0;
    }
}

.van-notice-bar {
    font-size: 12px;
    height: 36px;
    line-height: 36px;
    background: fade(@prim, 10%) !important;
}
</style>
