<template>
  <div class="center">
    <div class="item">
      <span
        style="color: #bbb; font-size: 21px; padding-top: 7px"
        class="icon iconfont icon-wode"
      ></span>
      <input
        type="text"
        v-model="name"
        :placeholder="L['请输入用户名']"
        class="input"
      />
      <div data-type="userName" class="cancel" @click="clearInputVal">
        <span style="color: #bbb" class="iconfont icon-cuowu"></span>
      </div>
      <div class="error" v-if="nameErrorMsg">
        <span
          style="color: #e1251b; font-size: 14px"
          class="iconfont icon-jubao"
        ></span>
        {{ nameErrorMsg }}
      </div>
    </div>
    <div class="item password">
      <span
        style="color: #bbb; font-size: 21px; padding-top: 7px"
        class="icon iconfont icon-mima1"
      ></span>
      <input
        :type="showPwdFlag ? 'text' : 'password'"
        v-model="password"
        :placeholder="L['请输入密码']"
        class="input"
        @focus="checkPwdValidate"
        @blur="checkPwdValidate"
      />
      <div class="cancel" @click="isShowPwd">
        <span
          :style="{
            color: '#bbb',
            fontSize: showPwdFlag ? '20px' : '16px',
          }"
          :class="{
            iconfont: true,
            'icon-bukejian11': !showPwdFlag,
            'icon-kejian': showPwdFlag,
            show_pwd: showPwdFlag,
          }"
        ></span>
      </div>
    </div>
    <div class="error" v-if="checkPwdErrorMsg">
      <span
        style="color: #e1251b; font-size: 14px"
        class="iconfont icon-jubao"
      ></span>
      {{ checkPwdErrorMsg }}
    </div>
    <div class="item confirm">
      <span style="color: #bbb; font-size: 21px; padding-top: 7px" class="icon"
        ><img src="/register/pwd_confirm.png" alt=""
      /></span>
      <input
        class="input"
        v-model="confirmPassword"
        :type="showConfirmPwdFlag ? 'text' : 'password'"
        :placeholder="L['register']['请确认密码']"
        @blur="checkPwdMatched"
      />
      <div class="cancel" @click="isShowConfirmPwd">
        <span
          :style="{
            color: '#bbb',
            fontSize: showConfirmPwdFlag ? '20px' : '16px',
          }"
          :class="{
            iconfont: true,
            'icon-bukejian11': !showConfirmPwdFlag,
            'icon-kejian': showConfirmPwdFlag,
            show_pwd: showConfirmPwdFlag,
          }"
        ></span>
      </div>
    </div>
    <div class="error" v-if="checkErrorMsg">
      <span
        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" v-if="agreeErrorMsg">
      <span
        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 regex = /^[A-Za-z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{6,20}$/;
  if (regex.test(password.value)) {
    checkPwdErrorMsg.value = "";
    return true;
  }
  checkPwdErrorMsg.value = L['请输入6～20位英文、数字、符号'];
  return false;
};

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

<style lang="scss" scoped>
.center {
  padding: 30px 30px 40px;

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

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

    .icon {
      position: absolute;
      left: 1px;
      top: 1px;
      width: 50px;
      text-align: center;
      height: 38px;
      background: #f8f8f8;

      img {
        width: 20px;
      }
    }

    .input {
      border: 1px solid #e8e8e8;
      height: 40px;
      padding: 0 44px 0 60px;
      width: 326px;
    }

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

  .cancel {
    position: absolute;
    right: 0;
    top: 1px;
    width: 44px;
    height: 38px;
    cursor: pointer;

    :before {
      position: absolute;
      top: 9px;
      // left: 14px;
    }
  }

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

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