<template>
  <div class="center">
    <div class="item">
      <span class="icon">
        <img src="/login/User.png" alt="">
      </span>
      <input
        type="text"
        v-model="name"
        :placeholder="L['请输入用户名']"
        class="input"
      />
      <div data-type="userName" class="cancel" @click="clearInputVal">
        <span class="iconfont icon-cuowu"></span>
      </div>
      <div class="error">
        <span
          v-if="nameErrorMsg"
          class="iconfont icon-jubao"
        ></span>
        {{ nameErrorMsg }}
      </div>
    </div>
    <div class="item password">
      <span class="icon">
        <img src="/login/Password.png" alt="">
      </span>
      <input
        :type="showPwdFlag ? 'text' : 'password'"
        v-model="password"
        :placeholder="L['请输入密码']"
        class="input"
        @focus="checkPwdValidate"
        @blur="checkPwdValidate"
      />
      <div class="cancel" @click="isShowPwd">
        <span
          :class="{
            iconfont: true,
            'icon-bukejian11': !showPwdFlag,
            'icon-kejian': showPwdFlag,
            show_pwd: showPwdFlag,
          }"
        ></span>
      </div>
    </div>
    <div class="error" >
      <span
        v-if="checkPwdErrorMsg"
        style="color: #e1251b; font-size: 14px"
        class="iconfont icon-jubao"
      ></span>
      {{ checkPwdErrorMsg }}
    </div>
    <div class="item confirm">
      <span class="icon">
        <img src="/login/Password.png" alt="">
      </span>
      <input
        class="input"
        v-model="confirmPassword"
        :type="showConfirmPwdFlag ? 'text' : 'password'"
        :placeholder="L['register']['请确认密码']"
        @blur="checkPwdMatched"
      />
      <div class="cancel" @click="isShowConfirmPwd">
        <span
          :class="{
            iconfont: true,
            'icon-bukejian11': !showConfirmPwdFlag,
            'icon-kejian': showConfirmPwdFlag,
            show_pwd: showConfirmPwdFlag,
          }"
        ></span>
      </div>
    </div>
    <div class="error">
      <span
        v-if="checkErrorMsg"
        style="color: #e1251b; font-size: 14px"
        class="iconfont icon-jubao"
      ></span>
      {{ checkErrorMsg }}
    </div>
    <el-button
      @click="joinForFree"
      :class="{ submit: true, disabled: joinForFreeDisabled }"
      :disabled="joinForFreeDisabled"
      :loading="joinForFreeLoading"
      >{{ L["去注册"] }}</el-button
    >
    <div class="agree_wrap">
      <input
        type="checkbox"
        :class="{ checkbox: true, default: true, checked: agreeFlag }"
      />
      <span class="agree_selected iconfont icon-finish" @click="agree" />
      <span class="text">
        {{ L["我同意"]
        }}<nuxt-link
          target="_blank"
          class="agreement"
          :to="`/member/login/agreement?type=1`"
        >
          {{ L["《用户注册协议》"] }}</nuxt-link
        >
        <nuxt-link
          target="_blank"
          class="agreement"
          :to="`/member/login/agreement?type=2`"
          >{{ L["《隐私政策》"] }}
        </nuxt-link>
      </span>
    </div>
    <div class="error">
      <span
        v-if="agreeErrorMsg"
        style="color: #e1251b; font-size: 14px"
        class="iconfont icon-jubao"
      ></span>
      {{ agreeErrorMsg }}
    </div>
  </div>
</template>

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

const L = getCurLanguage();

const emits = defineEmits(["success"]);
const props = defineProps({
  email: {
    type: String,
    default: "",
  },
  mobile: {
    type: String,
    default: "",
  }
});

const agreeFlag = ref(false); //同意注册协议标识，默认不同意
const agreeErrorMsg = ref(); // 协议错误提示
const name = ref(""); //用户名
const nameErrorMsg = ref(""); //用户名错误提示
const checkErrorMsg = ref(""); // 密码一致性错误提示
const password = ref(""); //密码
const confirmPassword = ref(""); //二次确认密码
const showPwdFlag = ref(false); //密码是否明文显示，默认密文
const showConfirmPwdFlag = ref(false); // 二次密码是否明文显示，默认密文
const joinForFreeLoading = ref(false);
const checkPwdErrorMsg = ref("");

const joinForFreeDisabled = computed(
  () => !name.value || !password.value || !confirmPassword.value
);

const clearInputVal = () => {
  name.value = "";
};

//密码是否显示
const isShowPwd = () => {
  showPwdFlag.value = !showPwdFlag.value;
};

// 二次确认密码是否显示
const isShowConfirmPwd = () => {
  showConfirmPwdFlag.value = !showConfirmPwdFlag.value;
};

//是否同意用户注册协议
const agree = () => {
  agreeFlag.value = !agreeFlag.value;
  if (agreeFlag.value) {
    agreeErrorMsg.value = "";
  }
};

const joinForFree = () => {
  if (!checkPwdValidate()) return;
  if (!checkPwdMatched()) return;
  if (!agreeFlag.value) {
    agreeErrorMsg.value = L["请同意用户注册协议及隐私政策"];
    return false;
  }
  agreeErrorMsg.value = "";

  // 注册账号
  joinForFreeLoading.value = true;
  const baseParams = {
    confirmPassword: confirmPassword.value,
    email: props.email,
    nickName: name.value,
    password: password.value,
    mobile: props.mobile
  }
  const params = {}
  Object.keys(baseParams).forEach(key => {
    if (baseParams[key]) {
      params[key] = baseParams[key]
    }
  })
  post("/v3/member/front/active/register", params)
    .then((res) => {
      if (res.state === 200) {
        showMessage({
          message: L["register"]["注册成功"],
          type: "success",
        });

        emits("success");
      } else {
        showMessage({
          message: res.msg,
          type: "warning",
        });
      }
    })
    .finally(() => {
      joinForFreeLoading.value = false;
    });
};

const checkPwdMatched = () => {
  if (password.value === confirmPassword.value) {
    checkErrorMsg.value = "";
    return true;
  }
  checkErrorMsg.value = L["register"]["两次输入的密码不一致"];
  return false;
};

const checkPwdValidate = () => {
  const res = unifyValidatePassword(password.value); 
  if (!res.ok) {
    checkPwdErrorMsg.value = res.reasons[0];
    return false;
  }

  checkPwdErrorMsg.value = ''
  return true;
};

watch(name, (val) => {
  nameErrorMsg.value = val ? "" : L["请输入用户名"];
});
</script>

<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;
    text-align: center;
    line-height: 46px;
    cursor: pointer;

    .iconfont {
      font-size: 16px;
      color: '#bbb' !important;

      &.show_pwd {
        font-size: 20px;
      }
    }
  }

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

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

  .submit {
    margin-top: 100px;
    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;
    }
  }

  .agree_wrap {
    margin-top: 11px;
    height: 14px;
    line-height: 14px;
    color: #999;
    padding-left: 2px;
    position: relative;
    cursor: pointer;
    .agree_selected {
      color: #fff;
      position: absolute;
      top: 1px;
      left: 3px;
      z-index: 2;
      font-size: 13px;
    }

    .checkbox {
      width: 14px;
      height: 14px;
      display: inline-block;
      vertical-align: top;
      position: relative;
      outline: none;
      -webkit-appearance: none;
      background: none;
      border: none;
      box-sizing: border-box;
      cursor: pointer;
      box-shadow: none;

      &.checked {
        &:before {
          background: $colorMain;
          border-color: $colorMain;
        }
      }

      &:before {
        border: 1px solid #ddd;
        background: #fff;
        z-index: 1;
        position: absolute;
        top: 0;
        left: 0;
        content: " ";
        display: block;
        width: 100%;
        height: 100%;
        box-sizing: border-box;
      }
    }

    .text {
      margin-left: 5px;
      display: inline-block;
      vertical-align: top;

      .agreement {
        color: #000;

        &:hover {
          text-decoration: underline;
        }
      }
    }
  }
}
</style>