<template>
  <!--底部-->
  <div class="invitation">
    <div class="iv_content flex">
      <div class="flex">
        <div class="code_titile">邀请码</div>
      </div>
      <div>
        <div class="code flex">
          <el-input
            :disabled="!inputFlag"
            :autofocus="true"
            ref="input0"
            @input="changeValue1"
            @focus="getFocus(0, $event)"
            v-model="invatation1"
            maxlength="19"
          >
          </el-input>
          <el-input
            :disabled="!inputFlag"
            :autofocus="true"
            ref="input1"
            @input="changeValue1"
            @focus="getFocus(1, $event)"
            v-model="invatation2"
            maxlength="4"
          >
          </el-input>
          <el-input
            :disabled="!inputFlag"
            :autofocus="true"
            ref="input2"
            @input="changeValue1"
            @focus="getFocus(2, $event)"
            v-model="invatation3"
            maxlength="4"
          >
          </el-input>
          <el-input
            :disabled="!inputFlag"
            :autofocus="true"
            ref="input3"
            @input="changeValue1"
            @focus="getFocus(3, $event)"
            v-model="invatation4"
            maxlength="4"
          ></el-input>
        </div>
        <!-- <div class="errorCode">该邀请码无效，请输入正确的邀请码。</div> -->
      </div>
    </div>

    <div class="tips">
      备注：每个邀请码只能绑定一个用户，请谨慎使用。无邀请码可直接点【下一步】
    </div>

    <div class="btn">
      <el-button @click="next()">下一步</el-button>
    </div>
  </div>
</template>

<script>
/* eslint-disable */
import { setInviteCode } from "r/index/signUp";
export default {
  name: "Invitation",
  data() {
    return {
      invatation1: "",
      invatation2: "",
      invatation3: "",
      invatation4: "",
      inputFlag: true,
    };
  },
  watch: {
    invatation1: function () {
      this.invatation1 = this.invatation1
        .replace(/[\W]/g, "")
        .toUpperCase()
        .substring(0, 4);
    },
    invatation2: function () {
      this.invatation2 = this.invatation2
        .replace(/[\W]/g, "")
        .toUpperCase()
        .substring(0, 4);
    },
    invatation3: function () {
      this.invatation3 = this.invatation3
        .replace(/[\W]/g, "")
        .toUpperCase()
        .substring(0, 4);
    },
    invatation4: function () {
      this.invatation4 = this.invatation4
        .replace(/[\W]/g, "")
        .toUpperCase()
        .substring(0, 4);
    },
  },
  methods: {
    next() {
      this.$router.push("/signUp/referInfo");
    },
    //第一个输入框
    changeValue1(e) {
      // console.log(e, "输入");
      if (e.length == 19 && e.indexOf("-") !== -1) {
        //如果是复制过来的带'-'邀请码
        let copyCode = e.split("-");
        // console.log(copyCode, "输入");
        this.invatation1 = copyCode[0];
        this.invatation2 = copyCode[1];
        this.invatation3 = copyCode[2];
        this.invatation4 = copyCode[3];
        this.$refs.input3.focus();
      } else if (e.length > 4) {
        this.changeValue(e.substring(0, 4));
      } else {
        this.changeValue(e);
      }
    },
    // 输入框
    changeValue(e) {
      if (e.length >= 4) {
        // console.log(e, "length");
        // console.log(this.currentIndex, "this.currentIndex");
        switch (this.currentIndex) {
          case 0:
            // console.log(this.text0.length);
            if (this.invatation1.length >= 4) {
              // this.$nextTick(() => {
              this.$refs.input1.focus();
              // })
            }
            break;
          case 1:
            if (this.invatation2.length >= 4) {
              this.$refs.input2.focus();
            }
            break;
          case 2:
            if (this.invatation3.length >= 4) {
              this.$refs.input3.focus();
            }
            break;
          case 3:
            if (this.invatation4.length >= 4) {
              this.$refs.input3.focus();
            }
            break;
          default:
            break;
        }
      } else if (e.length === 0) {
        switch (this.currentIndex) {
          case 0:
            this.$refs.input0.focus();
            break;
          case 1:
            this.$refs.input0.focus();
            break;
          case 2:
            this.$refs.input1.focus();
            break;
          case 3:
            this.$refs.input2.focus();
            break;
          default:
            break;
        }
      } else {
      }
    },
    getFocus(index, e) {
      this.currentIndex = index;
    },
    // 下一步
    async next() {
      console.log(111);
      let code;
      if (
        this.invatation1 == "" &&
        this.invatation2 == "" &&
        this.invatation3 == "" &&
        this.invatation4 == ""
      ) {
        code =
          this.invatation1 +
          this.invatation2 +
          this.invatation3 +
          this.invatation4;
      } else {
        code =
          this.invatation1 +
          "-" +
          this.invatation2 +
          "-" +
          this.invatation3 +
          "-" +
          this.invatation4;
      }

      setInviteCode({ code: code }).then((res) => {
        console.log(res, "code");
        if (res.data.code !== 200) {
          return this.$message.error(res.data.message);
        }
        // this.status=res.data.status
        this.$emit('getstatus',res.data.status.status)
        this.$message.success(res.data.message);
        this.$router.push("/signUp/referInfo");
      });
    },
  },
};
</script>

<style  lang="scss" scoped>
.flex {
  display: flex;
}
.invitation {
  .iv_content {
    justify-content: center;
    height: 44px;
    ::v-deep .el-input__inner {
      width: 110px;
      height: 38px;
      border-radius: 4px;
      border: 2px solid #60194a;
      margin-right: 15px;
    }
    .code_titile {
      height: 16px;
      font-size: 16px;
      font-weight: 600;
      color: #1b1419;
      line-height: 48px;
      margin-right: 10px;
    }
    .errorCode {
      margin-top: 10px;
    }
  }
  .tips {
    font-size: 14px;
    font-weight: 500;
    color: #a7a2a6;
    line-height: 54px;
    margin: 30px 0;
    text-align: center;
  }
  .errorCode {
    font-size: 12px;
    font-weight: 400;
    color: #d7373f;
    line-height: 12px;
  }
  .btn {
    text-align: center;
    padding-bottom: 224px;
  }
  .el-button {
    width: 200px;
    height: 50px;
    background: #60194a;
    border-radius: 4px;
    opacity: 0.8;
    line-height: 50px;
    text-align: center;
    color: #fff;
    font-size: 16px;
    padding: 0;
    margin: 30px 0 0 0;
  }
}
</style>
