<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['请输入6～20位英文、数字、符号']"
        class="input"
      />
      <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="item confirm">
      <span
        style="color: #bbb; font-size: 21px; padding-top: 7px"
        class="icon iconfont icon-mima1"
      ></span>
      <input
        :type="showConfirmPwdFlag ? 'text' : 'password'"
        v-model="confirmPassword"
        :placeholder="L['register']['请确认密码']"
        class="input"
      />
      <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>
    <a
      href="javascript:void(0)"
      @click="joinForFree"
      :class="{ submit: true, disabled: joinForFreeDisabled }"
      >{{ L["去注册"] }}</a
    >
    <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";

const L = getCurLanguage();

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

const joinForFreeDisabled = computed(() => false);

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

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

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

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

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

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;

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

    .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: $colorMain;
    height: 16px;
    line-height: 16px;
  }

  .submit {
    display: block;
    margin-top: 35px;
    background: $colorMain;
    color: #fff;
    text-align: center;
    border-radius: 2px;
    height: 45px;
    line-height: 45px;
    font-size: 18px;
    letter-spacing: 0px;

    &:hover {
      opacity: 0.9;
    }

    &.disabled {
      background-color: #909399;
      pointer-events: none;
    }
  }

  .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>