<template>
  <uni-popup ref="popup" type="bottom">
    <view class="modal">
      <view class="title">
        请输入支付密码
        <view class="close" @click="close">
          <uni-icons type="closeempty" size="20"></uni-icons>
        </view>
      </view>

      <view class="input">
        <view
          class="row"
          v-for="i in 6"
          :class="current == i ? 'active' : ''"
        >
          <view class="pwd" v-if="current > i"></view>
        </view>
      </view>

      <view class="tip" @click="forgetPwd">忘记密码？</view>

      <view class="keyboard">
        <view
          class="row"
          v-for="i in key"
          :key="i"
          :class="{ 'no-i': i === -2 }"
          @click="i !== -2 && input(i)"
        >
          <uni-icons
            v-if="i == -1"
            type="closeempty"
            size="24"
          ></uni-icons>
          <text v-if="i != -1">
            {{ i >= 0 ? i : '' }}
          </text>
        </view>
      </view>
    </view>
    <view></view>
  </uni-popup>
</template>
<script>
  export default {
    data() {
      return {
        current: 1,
        key: [1, 2, 3, 4, 5, 6, 7, 8, 9, -2, 0, -1],
        value: []
      }
    },
    methods: {
      forgetPwd() {
        // uni.navigateTo({
        //   url: '/pages/myInfo/changePayPwd'
        // })
      },
      open() {
        this.$refs.popup.open()
        this.value = []
        this.current = 1
      },
      close() {
        this.$refs.popup.close()
      },
      input(n) {
        if (this.value.length < 6 && n >= 0) {
          this.current++
          this.value.push(n)
          if (this.value.length == 6) {
            const v = [...this.value]
            this.value = []
            console.log(this.value)
            this.$emit('success', v)
          }
          return
        }

        if (n == -1 && this.current > 1) {
          this.current--
          this.value.pop()
          return
        }
      }
    },
    mounted() {
      // this.$refs.popup.open()
    }
  }
</script>
<style lang="scss">
  .modal {
    width: 750rpx;
    background-color: white;
    padding: 30rpx;
    box-sizing: border-box;

    .title {
      text-align: center;
      font-size: 20px;
      position: relative;
      margin-bottom: 50px;
      width: 100%;

      .close {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        left: 0px;
      }
    }

    .input {
      display: grid;
      width: 100%;
      grid-template-columns: repeat(6, 1fr);
      grid-column-gap: 20rpx;

      .row {
        background: #f7f7f7;
        border-radius: 5px;
        height: 90rpx;
        border: 1px solid #efefef;
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .pwd {
        background-color: black;
        width: 30rpx;
        border-radius: 50%;
        height: 30rpx;
      }

      .active {
        border: 1px solid #2667ff;
      }
    }

    .tip {
      text-align: center;
      color: #2667ff;
      font-size: 15px;
      margin-top: 80rpx;
      margin-bottom: 50rpx;
    }

    .keyboard {
      width: 100%;
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(4, 1fr);
      grid-gap: 20rpx;
      color: #545454;

      .row {
        display: flex;
        border-radius: 10rpx;
        align-items: center;
        justify-content: center;
        font-weight: bold;
        font-size: 18px;
        height: 100rpx;
        background: #f4f4f4;

        &:hover {
          background: #e1e1e1;
        }
      }

      .no-i {
        background: #ffffff !important;
      }
    }
  }
</style>
