<template>
    <van-popup
        round
        close-on-popstate
        safe-area-inset-bottom
        v-model:show="show"
        closeable
        position="bottom"
        :close-icon="require('@assets/icon_close.png')"
    >
        <div class="popup">
            <div class="title">
                <img src="@assets/icon-zhanghugauanli.png" alt="" />
                <span>绑定账户</span>
            </div>
            <van-form @submit="submit">
                <!-- <van-field
                    name="游戏昵称"
                    label="游戏昵称"
                    placeholder="请输入您的游戏昵称"
                    v-model="form.nickname"
                    :rules="[{ required: true, message: '请输入您的游戏昵称' }]"
                    clearable
                /> -->
                <van-field
                    type="digit"
                    name="游戏营地"
                    label="游戏营地"
                    placeholder="请输入您的营地ID"
                    v-model="form.campId"
                    clearable
                    :rules="[{ required: true, message: '请输入您的营地ID' }]"
                >
                    <template #extra>
                        <div class="tip-btn" @click="$router.push('/accountDes')">
                            <span>查看ID说明</span>
                            <img src="@assets/icon_inter.png" alt="" />
                        </div>
                    </template>
                </van-field>
                <div class="tips">
                    <div class="text1">温馨提示</div>
                    <div class="text2">便于游戏公平性，校验官方数据，请认真填写王者荣耀昵称及王者营地1D账号</div>
                    <div class="text2">如误填将有可能导致赛事结算延迟，漏结算等</div>
                    <div class="text2">
                        注：王者营地ID账号将用于无法结算，对方恶意结算，乱提交赛事截图等。方便人工后续查询，请您打开王者营地个人战绩，谢谢配合
                    </div>
                </div>
                <div class="btn">
                    <van-button round block type="primary" native-type="submit">提交</van-button>
                </div>
            </van-form>
        </div>
    </van-popup>
    <van-popup
        round
        close-on-popstate
        safe-area-inset-bottom
        v-model:show="showRoles"
        position="bottom"
        :close-icon="require('@assets/icon_close.png')"
    >
        <div class="popup">
            <div class="title">
                <img src="@assets/icon-zhanghugauanli.png" alt="" />
                <span>选择绑定账户</span>
            </div>
            <div class="account-box" v-if="roleItems.length !== 0">
                <div class="account-info" @click="choose(item)" v-for="(item, index) in roleItems" :key="index">
                    <div class="account-right">
                        <div class="text1">{{ item.roleName }}</div>
                        <div class="text2">
                            <span v-for="(text, textIndex) in item.roleText" :key="textIndex">{{ text }}</span>
                        </div>
                    </div>
                    <img class="img-icon" :src="checkRoleId === item.roleId ? activeIcon : inactiveIcon" />
                </div>
            </div>
        </div>
    </van-popup>
</template>

<script>
export default {
    data() {
        return {
            show: false,
            form: {
                campId: '',
                gameId: ''
            },
            checkRoleId: 0,
            showRoles: false,
            activeIcon: require('@assets/icon_gouxuan_pre.png'),
            inactiveIcon: require('@assets/icon_gouxuan_huise.png'),
            roleItems: []
        };
    },
    methods: {
        submit() {
            let form = { ...this.form };
            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });
            if (this.checkRoleId) {
                form.roleId = this.checkRoleId;
            }
            this.$http
                .post('/bindGame/bind', form)
                .then(res => {
                    if (!this.checkRoleId) {
                        this.$toast.clear();
                        // res.roleItems=res.roleItems.filter(item=>{
                        //     return
                        // })
                        if (res.roleItems.length === 0) {
                            this.$dialog.alert({
                                title: '角色列表为空',
                                message: '当前没有可选择的角色，请赶快打开王者创建吧'
                            });
                        } else {
                            this.showRoles = true;
                            this.roleItems = res.roleItems;
                        }
                    } else {
                        this.$toast.success('绑定成功');
                        this.$emit('update');
                    }
                    this.show = false;
                })
                .catch(e => {
                    this.$toast(e.error);
                });
        },
        init(gameInfo, zone) {
            this.form = { campId: '', gameId: gameInfo.id, zone: zone };
            this.checkRoleId = 0;
            this.show = true;
        },
        choose(info) {
            this.checkRoleId = info.roleId;
            this.showRoles = false;
            this.submit();
        }
    }
};
</script>

<style lang="less" scoped>
.title {
    .flex();
    img {
        width: 24px;
        height: 24px;
    }
    span {
        font-size: 14px;
        font-weight: bold;
        color: #ffffff;
        line-height: 24px;
        margin-left: 10px;
    }
    padding: 14px 16px 8px;
    border-bottom: 1px solid #313346;
}

/deep/.van-form {
    .van-field__label {
        color: #fff;
        width: 56px;
        margin-right: 12px;
    }
}

.tips {
    font-size: 12px;
    color: #6a6d83;
    line-height: 20px;
    padding: 20px 16px 60px;

    .text1 {
        padding-bottom: 2px;
    }
    .text2 + .text2 {
        margin-top: 20px;
    }
}

.btn {
    padding: 9px 28px;
    .bottom(9px);
}
.account-box {
    padding: 8px 0;
}
.account-info {
    .flex();
    padding: 8px 16px;
    .account-right {
        flex-grow: 1;
        .text1 {
            font-size: 14px;
            color: #ffffff;
            line-height: 24px;
        }
        .text2 {
            font-size: 12px;
            color: #6a6d83;
            line-height: 17px;
            margin-top: 3px;
            span + span {
                margin-left: 10px;
            }
        }
    }
    .img-icon {
        width: 24px;
        height: 24px;
    }
}
.account-box {
    max-height: 40vh;
    overflow: auto;
}
.account-info + .account-info {
    position: relative;
    &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 16px;
        right: 16px;
        height: 1px;
        background: #313346;
    }
}

.tip-btn {
    .flex();
    margin-left: 6px;
    span {
        font-size: 12px;
        color: #6a6d83;
        line-height: 17px;
    }

    img {
        width: 24px;
        height: 24px;
    }
}
</style>
