<template>
  <div class="center">
    <p style="width: 100%; text-align: center;">
      {{ L["register"]["我们将向您的手机号发送验证码，该手机号将用作登录用户名"] }}
    </p>
    <div class="item account">
      <span class="icon">
        <img src="/login/User.png" alt="">
      </span>
      <input
        type="text"
        v-model="mobile"
        :placeholder="L['请输入手机号']"
        class="input"
        autocomplete="off"
      />
      <div data-type="userName" class="cancel" @click="clearInputVal">
        <span style="color: #bbb" class="iconfont icon-cuowu"></span>
      </div>
    </div>
    <div class="error">
      <span
        v-if="phoneErrorMsg"
        class="iconfont icon-jubao"
      ></span>
      {{ phoneErrorMsg }}
    </div>
    <!-- 手机号验证码 -->
    <div class="verify-code">
      <span class="verify-code-icon">
        <img src="/login/Code.png" alt="">
      </span>
      <input
        type="text"
        v-model="phoneCode"
        :placeholder="L['请输入验证码']"
        class="input"
        autocomplete="off"
      />
      <span class="verify-code-accept">
        <el-button
          @click="showHMVerify"
          :disabled="countDownNumer"
          :loading="getVerifyCodeLoading"
        >
          <span>{{ codeText }}</span>
          <span v-if="countDownNumer">{{ `(${countDownNumer})` }}</span>
        </el-button>
      </span>
    </div>
    <div class="error">
      <span
        v-if="phoneCodeErrorMsg"
        class="iconfont icon-jubao"
      ></span>
      {{ phoneCodeErrorMsg }}
    </div>
    <el-button
      @click="next"
      :class="{ submit: true, disabled: nextDisabled }"
      :disabled="nextDisabled"
      :loading="nextActionLoading"
      >{{ L["下一步"] }}</el-button
    >
    <el-dialog
      :title="L['register']['滑动验证']"
      destroy-on-close
      width="500px"
      center
      modal-class="register-verify-model"
      v-model="modalVisible"
    >
      <SliderVerify
        :slideVerifyOptions="{ show: false, w: 450, h: 220 }"
        @onSuccess="verifySuccess"
        @onFail="verifyFail"
      />
    </el-dialog>
  </div>
</template>

<script setup>
import { getCurLanguage } from "@/composables/common.js";
import { showMessage, startCountdown } from "@/utils/common";

const emits = defineEmits(["success"]);

const L = getCurLanguage();

const phoneCalc = ref();
const mobile = ref(""); //手机号
const phoneCode = ref(""); // 手机号验证码
const phoneErrorMsg = ref(); //错误提示
const phoneCodeErrorMsg = ref(); //验证码错误提示

// 人机验证弹窗显示标识
const modalVisible = ref(false);

// 重新获取验证码标识
const isReacquireCode = ref(false);

const getVerifyCodeLoading = ref(false);

const nextActionLoading = ref(false);

// 倒计时展示
const countDownNumer = ref(0);

// 获取验证码的文案
const codeText = computed(() => {
  return isReacquireCode.value
    ? L["register"]["重新获取"]
    : L["register"]["获取验证码"];
});

// 注册手机号时按钮置灰状态
const nextDisabled = computed(() => !mobile.value || !phoneCode.value);

//开启人机校验
const showHMVerify = () => {
  // 校验手机号非空以及格式
  if (!validatePhone()) return;
  modalVisible.value = true;
};

// 获取验证码
const getVerifyCode = () => {
  getVerifyCodeLoading.value = true;
  post("v3/member/front/active/verification/code", {
    mobile: mobile.value,
    source: 1,
    type: 1,
  })
    .then((res) => {
      if (res.state === 200) {
        showMessage({
          message: L["register"]["验证码已发送"],
          type: "success",
        });
        // 设置倒计时
        startCountdown(60, (time) => (countDownNumer.value = time));
        isReacquireCode.value = true;
      } else {
        showMessage({
          message: res.msg,
          type: "warning",
        });
      }
    })
    .finally(() => {
      getVerifyCodeLoading.value = false;
    });
};

// 人机验证成功
const verifySuccess = () => {
  modalVisible.value = false;
  getVerifyCode();
};

// 人机校验失败
const verifyFail = () => {
  showMessage({
    message: L["register"]["校验不通过"],
    type: "warning",
  });
};

// 校验手机号
const validatePhone = () => {
  //手机号非空的验证
  if (!mobile.value) {
    phoneErrorMsg.value = L["请输入手机号"];
    return false;
  }
  // 手机号格式验证
  phoneCalc.value = checkPhone(mobile.value);
  if (phoneCalc.value !== true) {
    phoneErrorMsg.value = phoneCalc.value;
    return false;
  }
  phoneErrorMsg.value = "";

  return true;
};

// 手机号验证码验证
const validatePhoneCode = () => {
  if (!phoneCode.value) {
    phoneCodeErrorMsg.value = L["register"]["请输入手机号验证码"];
    return false;
  }
  phoneCodeErrorMsg.value = "";

  return true;
};

const next = () => {
  if (!validatePhone() || !validatePhoneCode()) return;
  nextActionLoading.value = true;
  post("v3/member/front/active/check/verification/code", {
    mobile: mobile.value,
    type: 1,
    verificationCode: phoneCode.value,
  })
    .then((res) => {
      if (res.state == 200) {
        // showMessage({
        //   message: L["register"]["注册成功"],
        //   type: "success",
        // });
        //成功提示，并返回到登录页面
        emits("success", mobile.value);
      } else {
        //提示错误
        showMessage({
          message: res.msg,
          type: "warning",
        });
      }
    })
    .finally(() => {
      nextActionLoading.value = false;
    });
};

//清空输入框内容
const clearInputVal = (type) => {
  mobile.value = "";
};
</script>
<style lang="scss">
.register-verify-model {
  .el-dialog__title {
    font-weight: 900 !important;
  }

  .el-dialog__body {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
<style lang="scss" scoped>
.center {
  flex: 1;
  height: 100%;
  position: relative;
  padding: 0 28px;
  margin-top: 60px;

  .item {
    position: relative;
    margin-top: 15px;
    border-radius: 2px;

    &:first-child {
      margin-top: 0;
    }

    .icon {
      display: flex;
      align-items: center;
      justify-content: flex-end;
      position: absolute;
      left: 1px;
      top: 1px;
      width: 50px;
      height: 46px;

      img {
        width: 22px;
        height: 22px;
        position: relative;
        right: 5px;
      }
    }

    .input {
      border: 1px solid #e8e8e8;
      height: 48px;
      padding: 0 44px 0 60px;
      width: 431px;
      font-size: 14px;

    }

    &.code {
      .input {
        padding-right: 10px;
        width: 150px;
      }
    }
  }

  .cancel {
    position: absolute;
    right: 0;
    top: 1px;
    width: 44px;
    height: 46px;
    line-height: 46px;
    text-align: center;
    cursor: pointer;
  }

  .error {
    margin-top: 10px;
    position: relative;
    color: #e2231a;
    height: 16px;
    line-height: 16px;

    .iconfont {
      font-size: 14px;
    }
  }

  .verify-code {
    display: flex;
    width: 431px;
    height: 48px;
    margin-top: 10px;

    &-icon {
      display: flex;
      align-items: center;
      justify-content: flex-end;     
      width: 50px;
      height: 48px;
      border: 1px solid #e8e8e8;
      border-right: none;

      img {
        width: 22px;
        height: 22px;
        position: relative;
        right: 5px;
      }
    }

    input {
      flex: 1;
      border: 1px solid #e8e8e8;
      border-left: none;
      border-right: none;
      height: 48px;
      padding: 0 0 0 10px;
      width: 100%;
      font-size: 14px;
    }

    &-accept {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 90px;

      .el-button {
        width: 100%;
        height: 100%;
        color: #fff;
        background: $colorMain;
        border-left: none;
        border-radius: 0;
        font-size: 12px;

        &:hover {
          // border-color: #e8e8e8;
          // color: $colorMain;
        }
      }
    }
  }

  .submit {
    margin-top: 160px;
    background: $colorMain;
    color: #fff;
    text-align: center;
    border-radius: 2px;
    width: 100%;
    height: 45px;
    font-size: 18px;
    letter-spacing: 0px;

    &:hover {
      opacity: 0.9;
    }

    &.disabled {
      background-color: #909399;
    }
  }
}
</style>