<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="手机号码" :arrow="false">
                            <u-input
                                v-model="phone"
                                type="number"
                                height="48"
                                input-align="right"
                                maxlength="11"
                                placeholder="请输入手机号码"
                                :clearable="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"
                                    placeholder="请输入验证码"
                                    :clearable="false"
                                    :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-group>
                </view>
            </u-card>
            <u-toast ref="uToast" />
            <view class="mc-btn">
                <u-button
                    :custom-style="{margin: '16rpx 30rpx',backgroundColor: '#ffcb57',color: '#472d21'}"
                    hover-class="defaultBtnhover"
                    :loading="loading"
                    @click="handleCommit"
                >确定修改</u-button>
            </view>
        </view>
        <u-toast ref="uToast" />
    </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex';
import request from '../../common/service';

export default {
    data() {
        return {
            phone: '',
            code: '',
            codeText: '',
            loading: false,
        };
    },
    onLoad() {},
    computed: {
        ...mapGetters({
            wxDetail: 'user/wxDetail',
            userDetail: 'user/userDetail',
        }),
    },
    methods: {
        ...mapActions({
            handleLogin: 'user/login',
            updateOtherInfo: 'user/updateOtherInfo',
            updateRegisterStatus: 'user/updateRegisterStatus',
        }),
        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();
                }
            });
        },
        verifyCode() {
            return request.post('/api/VerificationCode', {
                Phone: this.phone,
                VerificationCode: this.code,
            });
        },
        async handleCommit() {
            const _t = this;
            if (!this.phone || !this.code) {
                this.$refs.uToast.show({
                    title: '请完善信息后再提交！',
                });
                return;
            }
            const verifyRes = await this.verifyCode();
            if (verifyRes.code === 0) {
                const { Id, FCustBusinessMember } = this.userDetail;
                const commitData = {
                    IsDeleteEntry: false,
                    Model: {
                        FID: Id, //商户内码
                        FCustBusinessMember: [
                            {
                                FEntryID: FCustBusinessMember[0].Id, //单据体内码
                                FPHONE: this.phone, //手机号码
                            },
                        ],
                    },
                };
                const res = await request.post('/api/Business/Modify', commitData);
                if (res.code === 0) {
                    this.$refs.uToast.show({
                        title: '修改成功',
                        isTab: true,
                        url: '/pages/index/index',
                    });
                } else {
                    this.$refs.uToast.show({
                        title: res.message,
                    });
                }
            } else {
                this.$refs.uToast.show({
                    title: verifyRes.message,
                });
            }
            // const { encryptedData, iv } = this.wxDetail;
            // let registerData = {
            //     Phone: this.phone,
            //     UserType: this.selectedTypeNumber,
            //     VerificationCode: this.code,
            //     encryptedData,
            //     iv,
            // };
            // if (this.selectedTypeNumber === '1') {
            //     registerData.RealName = this.nickName;
            // } else {
            //     registerData.ComPanyName = this.companyName;
            // }
            // this.loading = true;
            // const res = await request.post('/api/Business/save', registerData);
            // this.loading = false;
            // const { code, result } = res;
            // if (code === 0) {
            //     const { Id, Number } = result.Result;
            //     this.updateOtherInfo({
            //         Id,
            //         Number,
            //     });
            //     this.getUserInfo();
            //     return;
            // }
            // if (code === 100) {
            //     this.showJoin = true;
            // }
        },
    },
    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;
        }
        .tips {
            margin-top: 12rpx;
            padding-left: 32rpx;
            font-size: 24rpx;
            color: #ff7a45;
        }
    }
}
</style>
