<config>
{
    "navigationBarTitleText": "充值",
    "navigationBarBackgroundColor": "#00BA64",
    "navigationBarTextStyle": "white",
    "backgroundColorTop":'#00BA64',
    "backgroundColorBottom":'#FFFFFF',
}
</config>
<template>
    <div style="padding-bottom:100px">
        <div class="head">
            <div class="label">{{ title }}</div>
            <div class="value">¥{{ money }}</div>
            <div class="btn-detail" @click="detail">查看明细</div>
        </div>
        <div class="title" style="margin-top:20px;">充值金额</div>
        <div class="recharge-options">
            <div
                class="option"
                v-for="item in options"
                :key="item"
                :class="{ active: amount === item }"
                @click="amount = item"
            >
                {{ item }}
            </div>
        </div>
        <div class="recharge-options" style="margin-top:0">
            <div class="option" style="background:white">自定义金额</div>
            <div class="option"><input type="number" v-model="amount" />元</div>
        </div>
        <!-- <div class="pay-method">
            <div class="method" :class="{ active: payMethod === 'ALIPAY' }" @click="payMethod = 'ALIPAY'">
                <div class="info">
                    <div class="name">支付宝</div>
                    <div class="desc">使用支付宝支付，方便快捷</div>
                </div>
                <div class="cb"></div>
            </div>
        </div> -->
        <div class="list-btn">
            <div class="btn" @click="pay">立即充值</div>
        </div>
    </div>
</template>
<script>
import { mapState } from 'vuex';
export default {
    name: 'recharge',
    metaInfo: {
        title: '充值'
    },
    data() {
        return {
            options: [100, 200, 500, 1000, 2000, 5000],
            amount: 100,
            payMethod: 'WEIXIN'
        };
    },
    created() {
        this.$store.dispatch('updateRoomInfo');
    },
    computed: {
        type() {
            return this.$mp.query.type;
        },
        ...mapState(['roomInfo', 'userInfo', 'ammeterInfo']),
        title() {
            switch (this.type) {
                case 'ROOM':
                    return '总预存金额';
                case 'PERSON':
                    return '我的余额';
                case 'AMMETER':
                    return '电费充值';
                default:
                    return '';
            }
        },

        money() {
            switch (this.type) {
                case 'ROOM':
                    return (this.roomInfo ? this.roomInfo.balance : 0).toFixed(2);
                case 'PERSON':
                    return (this.roomInfo.money || 0).toFixed(2);
                case 'AMMETER':
                    return (this.ammeterInfo.rest || 0).toFixed(2);
                default:
                    return '';
            }
        }
    },
    methods: {
        pay() {
            // console.log(this.type);
            wx.showLoading({
                title: '加载中'
            });
            this.$http
                .get('/api/mp/recharge', {
                    amount: this.amount,
                    type: this.type,
                    payMethod: this.payMethod
                })
                .then(res => {
                    // console.log(res);
                    res.package = res.packageValue;
                    wx.requestPayment({
                        ...res,
                        success: () => {
                            wx.hideLoading();
                            wx.showToast({
                                title: '支付成功'
                            });
                            setTimeout(() => {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }, 1000);
                        },
                        fail: e => {
                            console.log(e);
                            wx.hideLoading();
                            wx.showToast({
                                icon: 'none',
                                title: '支付失败'
                            });
                        }
                    });
                })
                .catch(e => {
                    wx.hideLoading();
                    wx.showToast({
                        icon: 'none',
                        title: e.error
                    });
                });
        },
        detail() {
            if (this.type === 'PERSON') {
                // 个人
                this.navigateTo('/pages/personDetail?type=PERSON');
            } else if (this.type === 'ROOM') {
                // 房间
                this.navigateTo('/pages/feeDetail?type=ROOM');
            } else {
                // 电费
                wx.navigateBack({
                    delta: 1
                });
            }
        }
    },
    watch: {
        amount(val) {
            console.log(val);
            // this.amount = val.replace(/[.+]/g, "");
        }
    }
};
</script>
<style lang="less" scoped>
.head {
    height: 80px;
    padding: 20px 40px;
    background: #00ba64;
    display: flex;
    flex-direction: column;
    justify-content: center;
    color: white;
    position: relative;
    .label {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: normal;
        color: #f5f7fa;
        line-height: 20px;
    }
    .value {
        font-size: 24px;
        font-weight: 500;
        line-height: 33px;
    }
    .btn-detail {
        width: 92px;
        height: 36px;
        background: #ffffff;
        font-size: 13px;
        border-radius: 18px;
        color: #00ba64;
        position: absolute;
        right: 20px;
        top: 0;
        bottom: 0;
        margin: auto;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
.title {
    margin-left: 15px;
    margin-right: 15px;
    font-size: 14px;
    font-weight: bold;
    color: black;
    line-height: 20px;
    &.method {
        padding-top: 15px;
        border-top: 1px solid #f2f4f5;
    }
}
.list-btn {
    .bottom();
    position: fixed;
    width: 100%;
    bottom: 0;
    background: #ffffff;
    padding-left: 43px;
    z-index: 999;
    .btn {
        width: 290px;
        height: 48px;
        line-height: 48px;
        text-align: center;
        background: #00ba64;
        border-radius: 24px;
        color: #fff;
        font-size: 16px;
    }
}
// .btn-round-large {
//     width: 290px;
//     height: 48px;
//     background: #00ba64;
//     border-radius: 24px;
//     margin: 0 auto;
//     line-height: 48px;
//     text-align: center;
//     color: #fff;
//     font-size: 16px;
// }
.recharge-options {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    padding: 0 15px;
    font-size: 0;
    margin-top: 10px;
    .option {
        width: calc((100vw - 45px) / 2);
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #f2f4f5;
        border-radius: 4px;
        font-size: 15px;
        color: black;
        margin-bottom: 15px;
        &.active {
            color: #00ba64;
            background: rgba(1, 168, 91, 0.12);
        }
        input {
            font-size: 15px;
            width: calc((100vw - 45px) / 2 - 45px);
            text-align: center;
        }
    }
}
.pay-method {
    width: calc(100vw - 30px);
    margin: auto;
    border-bottom: 1px solid #f2f4f5;
    .method {
        height: 80px;
        display: flex;
        align-items: center;
        .icon {
            width: 50px;
            height: 50px;
        }
        .info {
            margin-left: 8px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            flex-grow: 1;
            .name {
                font-size: 14px;
                color: #1a1a1a;
                line-height: 20px;
            }
            .desc {
                color: #666666;
                font-size: 13px;
                line-height: 18px;
                margin-top: 6px;
            }
        }
        .cb {
            width: 24px;
            height: 24px;
            border-radius: 12px;
            background: #f2f4f5;
            margin-right: 6px;
        }
        &.active .cb {
            background: white;
            border: 6px solid #f7931d;
        }
    }
}
</style>
