<template>
    <div class="content">
        <div class="header">
            <u-navbar
                :background="{backgroundColor: '#ffcb57'}"
                :border-bottom="false"
                title="设置登录密码"
                title-color="#472d21"
            ></u-navbar>
        </div>
        <view class="wrap">
            <u-card :show-head="false" padding="0" margin="30rpx 30rpx 15rpx 30rpx">
                <view slot="body">
                    <u-cell-group>
                        <u-cell-item title="手机号码" :value="phone" :arrow="false"></u-cell-item>
                        <u-cell-item title="验证码" :arrow="false">
                            <view class="slMsg">
                                <u-input
                                    v-model="code"
                                    type="number"
                                    height="48"
                                    input-align="right"
                                    maxlength="6"
                                    :clearable="false"
                                    placeholder="请输入验证码"
                                    :custom-style="{marginRight: '12rpx'}"
                                />
                                <u-button
                                    size="mini"
                                    slot="right"
                                    type="success"
                                    @click="getCode"
                                >{{codeText}}</u-button>
                                <u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
                            </view>
                        </u-cell-item>
                        <u-cell-item title="新密码" :arrow="false">
                            <u-input
                                v-model="psw"
                                type="password"
                                :clearable="false"
                                height="48"
                                input-align="right"
                                placeholder="请设置新密码"
                            />
                        </u-cell-item>
                        <u-cell-item title="确认密码" :arrow="false">
                            <u-input
                                v-model="pswconfirm"
                                type="password"
                                :clearable="false"
                                height="48"
                                input-align="right"
                                placeholder="再次输入新密码"
                            />
                        </u-cell-item>
                    </u-cell-group>
                </view>
            </u-card>
            <view class="mc-btn">
                <u-button
                    :custom-style="{margin: '16rpx 30rpx',backgroundColor: '#ffcb57',color: '#472d21'}"
                    hover-class="defaultBtnhover"
                    @click="changePsw"
                >确认修改</u-button>
            </view>
        </view>
        <u-toast ref="uToast" />
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import request from '../../../common/service';
export default {
    data() {
        return {
            code: '',
            codeText: '',
            phone: '',
            psw: '',
            pswconfirm: '',
        };
    },
    onLoad() {
        this.phone = this.curUserinfo.phone;
    },
    computed: {
        ...mapGetters({
            curUserinfo: 'user/displayUserInfo',
        }),
    },
    methods: {
        codeChange(text) {
            this.codeText = text;
        },
        getCode() {
            request.get('/api/VerificationCode', { phone: this.phone }).then((res) => {
                const { code } = res;
                if (code === 0) {
                    // 通知验证码组件内部开始倒计时
                    this.$refs.uCode.start();
                }
            });
        },
        changePsw() {
            const { phone, code, psw, pswconfirm } = this;
            if (!code || !psw || !pswconfirm) {
                this.$refs.uToast.show({
                    title: '请完善信息后再提交！',
                });
                return;
            }
            if (psw !== pswconfirm) {
                this.$refs.uToast.show({
                    title: '密码不一致，请重新输入！',
                });
                return;
            }
            request
                .post('/api/Business/SetLoginPsd', {
                    phone: phone,
                    verificationcode: code,
                    psd: psw,
                })
                .then((res) => {
                    const { code, message } = res;
                    if (code === 0) {
                        this.$refs.uToast.show({
                            title: '修改成功！',
                            isTab: true,
                            url: '/pages/my/index',
                        });
                    } else {
                        this.$refs.uToast.show({
                            title: message,
                        });
                    }
                });
        },
    },
    onShareAppMessage(res) {
        if (res.from === 'button') {
            // 来自页面内分享按钮
            console.log(res.target);
        }
        return {
            title: '京台分享更多精彩',
            path: '/pages/index/index',
            imageUrl: '/static/images/share/WechatIMG96.png'
        };
    },
};
</script>

<style lang="scss" scoped>
.content {
    .header {
        background-color: #ffcb57;
    }
    .wrap {
        display: flex;
        flex-direction: column;
        height: calc(100vh - var(--window-top));
        width: 100%;
        .slMsg {
            display: flex;
            justify-content: flex-end;
            align-items: center;
        }
    }
}
</style>
