<template>
    <uni-popup ref="popup" type="center">
        <view class="my-modal">
            <view class="close" @click="close">
                <uni-icons type="closeempty" size="40" color="#fff"></uni-icons>
            </view>
            <div class="title">
                <span class="title-text">密码验证</span>
            </div>    
            <div class="content">
                <a-row class="code" type="flex" justify="center" align="center">
                    <a-col>
                        <span v-for="item in value" :key="item">
                            <b v-if="item != ' '">*</b>
                        </span>
                    </a-col>
                </a-row>
                <input-num 
                    class="input-num" 
                    :type="2" 
                    ref="inputNum" 
                    @enter="enter" 
                    @input="input" 
                    @backspace="backspace">
                </input-num>
            </div>
        </view>
    </uni-popup>

</template>

<script>
import { trim } from 'lodash';
import InputNum from './InputNum.vue';
export default {
    name: "InputNumPop",
    components: {
        InputNum
    },

    data() {
        return {
            value: [" ", " ", " ", " "]
        }
    },
    methods: {
        show() {
            this.$refs.popup.open()
            this.value = [" ", " ", " ", " "]
        },
        close() {
            this.$refs.popup.close()
        },
        backspace() {
            for (let i = this.value.length - 1; i >= 0; i--) {
                if (this.value[i] != " ") {
                    this.value[i] = " "
                    break
                }
            }
        },
        input(val) {
            let cur = this.value.findIndex(el => el === " ")
            if (cur >= 0) {
                this.value[cur] = val
            }
        },
        enter() { 
            let value = this.value.toString()
            value = value.replace(/,/g, "")
            if (trim(value).length == 4) {
                this.$emit("submit", value)
            }
        }
    }
}
</script>

<style scoped>
.my-modal{
    background: #777D90;
    padding: 30px;
    width: 500px;
    border-radius: 30px;
    position: relative;
}
.my-modal .title{
    text-align: center;
    font-size: 30px;
    color: #fff;
}

.input-num :deep(.number){
    background-color: #fff;
    color: #5A5A5A;
    font-weight: 700;
    border-radius: 10px;
}

.code{
    width: 100%;
    margin-bottom: 20px;
}
.code .ant-col{
    display: flex;
    justify-content: space-around;
    flex: 0 0 290px;
}
.code span{
    text-align: center;
    width: 56px;
    height: 52px;
    background-color: #fff;
    border-radius: 10px;
    color: #5A5A5A;
    font-size: 20px;
    font-weight: 700;
    line-height: 52px;
}
.close{
    position: absolute;
    top: 20px;
    right: 20px;
    line-height: 1;
}
</style>