<template>
    <div class="login">
        <van-form v-if="step === 0" @submit="next">
            <div class="tabs">修改手机号</div>
            <div class="transaction_tips">
                <div class="transaction_tips_phone">{{phone}}</div>
                <div class="transaction_tips_con">修改手机号需先验证当前手机号</div>
            </div>
            <div class="login_information">
                <img
                    :src="require('@assets/verification_code.png')"
                    alt=""
                    class="login_information_img login_information_img_con"
                />
                <van-field
                    v-model="form.code"
                    :maxlength="4"
                    type="tel"
                    placeholder="请输入验证码"
                    :rules="[{ required: true, message: '请输入验证码' }]"
                    class="login_information_con"
                >
                    <template #button>
                        <van-button
                            @click="sendBefPhone"
                            class="send_verification"
                            size="small"
                            plain
                            :disabled="isSend"
                            type="primary"
                        >
                            {{ isSend ? `${sendNum}S后重新发送` : '发送验证码' }}
                        </van-button>
                    </template>
                </van-field>
            </div>
            <van-divider class="login_divider" />
            <div>
                <van-button
                    native-type="submit"
                    class="sure"
                    color="#28B6FF"
                    >确认修改</van-button
                >
            </div>
        </van-form>
        <van-form v-else ref="form" @submit="submit">
            <div class="tabs">绑定新号码</div>
            <div class="transaction_tips_cons">修改手机号即同时修改登录账户</div>
            <div class="login_information">
                <img :src="require('@assets/login_icon_zhanghao.png')" alt="" class="login_information_img" />
                <van-field
                    v-model="form.phone"
                    :maxlength="11"
                    type="tel"
                    placeholder="请输入手机号"
                    class="login_information_con"
                    :rules="[
                        { required: true, message: '请输入手机号码' },
                        {
                            pattern: phonePattern,
                            message: '手机号码格式错误'
                        }
                    ]"
                />
            </div>
            <van-divider class="login_divider" />
            <div class="login_information">
                <img
                    :src="require('@assets/verification_code.png')"
                    alt=""
                    class="login_information_img login_information_img_con"
                />
                <van-field
                    v-model="form.code"
                    :maxlength="4"
                    type="tel"
                    placeholder="请输入验证码"
                    :rules="[{ required: true, message: '请输入验证码' }]"
                    class="login_information_con"
                >
                    <template #button>
                        <van-button
                            @click="sendPhone"
                            class="send_verification"
                            size="small"
                            plain
                            :disabled="isSend"
                            type="primary"
                        >
                            {{ isSend ? `${sendNum}S后重新发送` : '发送验证码' }}
                        </van-button>
                    </template>
                </van-field>
            </div>
            <van-divider class="login_divider" />
            <div>
                <van-button
                    native-type="submit"
                    color="#28B6FF"
                    class="sure suretwo"
                    >确定修改</van-button
                >
            </div>
        </van-form>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import phone from '../../mixins/phone';
import { Toast } from 'vant';
export default {
    mixins: [phone],
    data() {
        return {
            step: 0,
            form: {
                phone: '',
                code: ''
            }
        };
    },
    computed: {
        ...mapState(['userInfo']),
        phone() {
            let phone = this.userInfo.phone || '';
            return phone.substr(0, 3) + '****' + phone.substr(7, 6);
        }
    },
    methods: {
        sendBefPhone() {
            this.sendMsg(this.userInfo.phone);
        },
        sendPhone() {
            if (this.form.phone == '') {
                Toast('手机号码格式错误');
            } else {
                this.sendMsg(this.form.phone);
            }
        },
        submit() {
            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });
            this.verifyMsg(this.form.phone, this.form.code)
                .then(() => {
                    return this.$http.post('/user/bindPhone?phone=' + this.form.phone);
                })
                .then(() => {
                    this.$toast.success('修改成功');
                    setTimeout(() => {
                        this.$router.back();
                    }, 1500);
                })
                .catch(e => {
                    if (e) {
                        this.$toast(e.error);
                    }
                });
        },
        next() {
            this.verifyMsg(this.userInfo.phone, this.form.code).then(res => {
                this.setTime(0);
                this.step = 1;
            });
        }
    }
};
</script>

<style lang="less" scoped>
/deep/ .van-field__control::placeholder {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #5f646f;
}
/deep/ .van-divider::before {
    height: 1px;
    background: #5f646f;
    opacity: 0.2;
}
/deep/ .van-field__control {
    color: #ffffff;
    background: @bg;
}
.login {
    padding: 60px 48px 0px;
    background: @bg;
}
.tabs {
    font-size: 28px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #ffffff;
    line-height: 34px;
}
.transaction_tips_cons{
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #939599;
    line-height: 17px;
    margin-top: 6px;
    margin-bottom: 30px;
}
.transaction_tips {
    margin-top: 50px;
    margin-bottom: 30px;
    .transaction_tips_phone {
        font-size: 20px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #ffffff;
        line-height: 28px;
        margin-bottom: 2px;
    }
    .transaction_tips_con {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #939599;
        line-height: 24px;
    }
}
.login_information {
    display: flex;
    // margin-left: 45px;
    .login_information_img {
        width: 24px;
        height: 24px;
        margin-right: 8px;
    }
    .login_information_img_con {
        padding-top: 5px;
    }
    .van-cell {
        padding: 0;
    }
    .login_information_con {
        width: 250px;
        // height: 24px;
        background: @bg;
        overflow: visible;
    }
    .send_verification {
        background-color: @bg;
        border: none;
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #28b6ff;
        margin-right: -20px;
    }
}
.login_divider {
    padding: 7px 0px 30px 29px;
    box-sizing: border-box;
    // background: #5F646F;
    // opacity: 0.2;
}
.sure {
    width: 100%;
    margin-top: 30px;
    height: 40px;
    border-radius: 8px;
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
}
.suretwo{
    margin-top: 100px;
}
</style>
