<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 required title="企业名称" :arrow="false">
                            <u-input
                                v-model="company"
                                type="text"
                                height="48"
                                input-align="right"
                                placeholder="请输入企业名称"
                                :clearable="false"
                            />
                        </u-cell-item>
                        <u-cell-item required title="姓名" :arrow="false">
                            <u-input
                                v-model="name"
                                type="text"
                                height="48"
                                input-align="right"
                                placeholder="请输入姓名"
                                :clearable="false"
                            />
                        </u-cell-item>
                        <u-cell-item required 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-item title="备注" :arrow="false">
                            <u-input
                                v-model="mark"
                                type="text"
                                height="48"
                                input-align="right"
                                placeholder="备注：）"
                                :clearable="false"
                            />
                        </u-cell-item>
                    </u-cell-group>
                </view>
            </u-card>
            <u-modal
                v-model="showTips"
                content="请耐心等待企业管理员处理你的申请，如有任何问题可以联系在线客服寻求帮助"
                confirm-text="在线客服"
                @confirm="getHelp"
            ></u-modal>
            <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>
    </div>
</template>

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

export default {
    data() {
        return {
            company: '',
            name: '',
            phone: '',
            code: '',
            codeText: '',
            mark: '',
            loading: false,
            showTips: false,
        };
    },
    onLoad(options) {
        this.company = options.name || '';
        this.phone = options.phone || '';
        this.name = options.realname || '';
    },
    computed: {
        ...mapGetters({
            wxDetail: 'user/wxDetail',
        }),
    },
    methods: {
        ...mapActions({
            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();
                }
            });
        },
        handleCommit() {
            const _t = this;
            if (!this.company || !this.name || !this.phone || !this.code) {
                this.$refs.uToast.show({
                    title: '请完善信息后再提交！',
                });
                return;
            }
            const { encryptedData, iv, userInfo } = this.wxDetail;
            let registerData = {
                Model: {
                    ComPanyName: this.company,
                    FApplyPhone: this.phone,
                    VerificationCode: this.code,
                    FApplyName: this.name,
                    FNote: this.mark,
                    FUserImage: userInfo.avatarUrl,
                    encryptedData,
                    iv,
                },
            };
            this.loading = true;
            request.post('/api/Business/ApplyJoin', registerData).then((res) => {
                this.loading = false;
                const { code, message, result } = res;
                if (code === 0) {
                    this.$refs.uToast.show({
                        title: '您的申请正在审核中，请稍后查询！',
                        isTab: true,
                        url: '/pages/index/index',
                    });
                    return;
                }
                this.$refs.uToast.show({
                    title: message,
                });
            });
        },
        getHelp() {
            this.$u.route('/pages/index/index');
        },
    },
    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>
