<template>
  <view :class="'car-number ' + themeClass">
    <view class="wrap" @tap="focusHandler">
      <template v-for="(val, index) in fill">
        <view
          :class="[
            'cell',
            { first: index === 0 },
            { last: index === length - 1 },
            { active: index === current },
          ]"
          :style="{
            borderColor:
              index === length - 1 || index === current ? color : '#333333',
          }"
          :key="index"
          @tap.stop="focusHandler(index)"
        >
          <view
            class="val"
            :style="{ color: index === current ? color : '#333333' }"
            >{{ val }}</view
          >
          <k-icon
            class="bg"
            size="24"
            v-if="index === length - 1 && val == ''"
            :name="`/static/icons/${theme}-leaf.svg`"
            :useCdn="true"
          />
        </view>
        <view :key="index + 'dot'" class="dot" v-if="index === 1"></view>
      </template>
    </view>

    <PlateNumberKeyBoard
      v-if="focus"
      :type="kType"
      :color="color"
      :confirmText="confirmText"
      :titleText="titleText"
      :cancelText="cancelText"
      @on-delete="keyDeleteHandler"
      @on-input="keyInputHandler"
      @on-hide="keyHideHandler"
    ></PlateNumberKeyBoard>
  </view>
</template>

<script>
import PlateNumberKeyBoard from './k-plate-number-keyboard.vue';

export default {
  name: 'k-plate-number-input',
  components: {
    PlateNumberKeyBoard,
  },
  props: {
    confirmText: {
      type: String,
      default: '',
    },
    titleText: {
      type: String,
      default: '',
    },
    cancelText: {
      type: String,
      default: '',
    },
    color: {
      type: String,
      default: '#333333',
    },
    value: {
      type: String,
      default: '',
    },
    theme: {
      type: String,
      default: '',
    },
    themeClass: {
      type: String,
      default: '',
    },
    length: {
      type: Number,
      default: 8,
    },
  },
  data() {
    return {
      current: 0,
      focus: false,
      fill: new Array(this.length).fill(''),
    };
  },
  computed: {
    kType() {
      return this.current === 0 ? 'provinces' : 'areas';
    },
  },
  watch: {
    fill(val) {
      this.$emit('input', val.join(''));
      this.$emit('change', val);
    },
  },
  methods: {
    focusHandler(index = 0) {
      this.focus = true;
      this.current = index;
    },
    keyDeleteHandler() {
      this.$set(this.fill, this.current, '');

      if (this.current <= 0) {
        return;
      }

      this.current -= 1;
    },
    keyInputHandler(key) {
      this.$set(this.fill, this.current, key);

      if (this.current >= this.length - 1) {
        return;
      }

      this.current += 1;
    },
    keyHideHandler() {
      this.focus = false;
    },
  },
  beforeMount() {
    if (this.value) {
      this.value.split('').forEach((key, index) => {
        if (index >= this.length) {
          return;
        }
        this.$set(this.fill, index, key);
      });

      this.current = Math.min(this.value.length, this.length - 1);
    }
  },
  mounted() {
    this.focus = true;
  },
};
</script>

<style scoped lang="scss">
/* stylelint-disable selector-class-pattern */
/* stylelint-disable declaration-no-important */
/* stylelint-disable comment-empty-line-before */
/* stylelint-disable declaration-block-no-redundant-longhand-properties */
.car-number {
  position: relative;
  width: 100%;
  margin-top: 40px;

  .wrap {
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 0 28rpx;
  }

  .cell {
    position: relative;
    width: 70rpx;
    height: 90rpx;
    display: flex;
    flex-direction: column;
    color: #333;
    font-size: 40rpx;
    text-align: center;
    border-radius: 4rpx;
    border: 2rpx solid #000;
    box-sizing: border-box;

    .val {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .border {
      position: absolute;
      flex-shrink: 0;
      flex-grow: 0;
      width: 2rpx;
      background: transparent;
      height: 100%;
    }

    .bg {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    &.first {
      width: 86rpx;
      font-size: 46rpx;
      border-radius: 16rpx 4rpx 4rpx 16rpx;
    }

    &.last {
      border: 2rpx dashed #333;
    }

    &.active {
      border: 2rpx solid #333;

      .val {
        color: #333;
      }
    }
  }

  .dot {
    width: 14rpx;
    height: 14rpx;
    border-radius: 14rpx;
    background: #333;
  }
}
</style>
