<script setup>
import { reactive, onMounted, toRefs, ref, nextTick } from "vue";
import { useRouter, useRoute } from "vue-router";
import { RaiApiPc } from "@/api/cygxPc/api.js";
import { Field, CellGroup, Icon, Button, CountDown, Toast } from "vant";
const router = useRouter();
const route = useRoute();
//获取手机号模块
const selectOptions = ref([
  {
    value: "+86",
    label: "86",
  },
  {
    value: "+852",
    label: "852",
  },
  {
    value: "+886",
    label: "886",
  },
  {
    value: "+1",
    label: "1",
  },
  {
    value: "+65",
    label: "65",
  },
]);
const valueMobile = ref("");
const selectMobileValue = ref("86");
const isShowMobile = ref(false);
const dropDownCode = () => {
  isShowMobile.value = !isShowMobile.value;
};
const selectMobileHandle = (val) => {
  selectMobileValue.value = val;
  isShowMobile.value = false;
};

//获取验证码模块
const smsVerify = ref("");
const countDownTime = ref(null);
const smsTime = ref(60 * 1000);
const isShowSmsTime = ref(false);
const finishTimeHandler = () => {
  isShowSmsTime.value = false;
};

//发送短信验证码
const startTime = async () => {
  if (!valueMobile.value) return Toast("手机号不能为空");
  const res = await RaiApiPc.getPhoneCode({
    Mobile: valueMobile.value,
    CountryCode: selectMobileValue.value,
  });
  if (res.Ret === 200) {
    Toast("验证码已发送");
    countDownTime.value.start();
    isShowSmsTime.value = true;
  }
};
const submitContactInformation = async () => {
  if (!valueMobile.value) return Toast("手机号不能为空");
  if (!smsVerify.value) return Toast("验证码不能为空");
  const res = await RaiApiPc.Bind({
    LoginType: 3,
    Mobile: valueMobile.value,
    VCode: smsVerify.value,
    CountryCode: selectMobileValue.value,
  });
  if (res.Ret === 200) {
    localStorage.setItem("cygx_token", res.Data.Authorization);
    router.back();
    console.log(localStorage.getItem('cygx_token'));
  } else {
    Toast(res.Msg);
  }
};
onMounted(() => {
});
</script>

<template>
  <div class="container-cygx-login">
    <div class="login-box">
      <h4>验证码登陆</h4>
      <p>手机号</p>
      <div class="mobile">
        <div class="area-code" @click="dropDownCode">
          <span> +{{ selectMobileValue }} </span>
          <Icon name="arrow-down" color="#333333" size="12" />
          <div class="drop-down">
            <template v-if="isShowMobile">
              <span @click.stop="selectMobileHandle(item.label)" :class="['item', selectMobileValue == item.label && 'active']" v-for="item in selectOptions" :key="item.label">{{ item.value }}</span>
            </template>
          </div>
        </div>
        <div class="mobile-input">
          <CellGroup>
            <Field v-model.trim="valueMobile" autocomplete="off" placeholder="请输入手机号" />
          </CellGroup>
        </div>
      </div>
      <p>验证码</p>
      <div class="sms-verification">
        <CellGroup>
          <Field v-model.trim="smsVerify" autocomplete="off"  placeholder="请输入验证码" />
        </CellGroup>
        <div class="sms-button">
          <span v-if="!isShowSmsTime" @click="startTime">发送验证码</span>
          <CountDown ref="countDownTime" :auto-start="false" :time="smsTime" format="ss 秒" @finish="finishTimeHandler" />
        </div>
      </div>
      <div class="login-btn">
        <Button type="primary" @click="submitContactInformation">验证码登陆</Button>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container-cygx-login {
  background: #f5f7f9;
  width: 100%;
  height: 100vh;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-box {
    width: 591px;
    background: #ffffff;
    box-shadow: 0px 0px 12px 0px #e4e7ea;
    border-radius: 16px;
    padding: 70px 40px;
    h4 {
      font-size: 30px;
      font-weight: 500;
      color: #333333;
    }
    p {
      font-size: 24px;
      font-weight: 400;
      color: #333333;
      margin: 35px 0 16px;
    }
    .mobile {
      display: flex;
      align-items: center;
      height: 40px;
      border-radius: 2px;
      border: 1px solid #d9d9d9;
      .mobile-input {
        height: 100%;
        width: 100%;
        overflow: hidden;
      }
      .area-code {
        position: relative;
        width: 72px;
        height: 100%;
        line-height: 38px;
        text-align: center;
        background: #fafafa;
        border-right: 1px solid #d9d9d9;
        cursor: pointer;
        .drop-down {
          position: absolute;
          top: 40px;
          left: 0;
          width: 108px;
          box-shadow: 0px 9px 28px 8px rgba(0, 0, 0, 0.05), 0px 6px 16px 0px rgba(0, 0, 0, 0.08), 0px 3px 6px -4px rgba(0, 0, 0, 0.12);
          border-radius: 2px;
          z-index: 99;
          background-color: #fff;
          .item {
            display: block;
            height: 32px;
            line-height: 32px;
            text-indent: 1em;
            text-align: left;
            cursor: pointer;
            &:hover {
              background-color: #f9f9f9;
            }
          }
          .active {
            color: #3385ff;
            background-color: #f5f5f5;
          }
        }
      }
    }
    .sms-verification {
      border: 1px solid #d9d9d9;
      border-radius: 2px;
      overflow: hidden;
      position: relative;
      .sms-button {
        position: absolute;
        cursor: pointer;
        top: 0;
        right: 0;
        width: 112px;
        height: 40px;
        background: #1890ff;
        box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.04);
        border: 1px solid #1890ff;
        text-align: center;
        line-height: 40px;
        font-size: 16px;
        color: #fff;
        .van-count-down {
          color: #fff;
          line-height: 40px;
          font-size: 16px;
        }
      }
    }
    .login-btn {
      text-align: center;
      margin-top: 50px;
    }
  }
  .van-cell-group {
    height: 40px;
    width: 100%;
    line-height: 38px;
  }
  .van-cell {
    padding: 0 20px !important;
    line-height: 40px;
  }
  @media screen and (max-width: 790px) {
    padding: 10px;
    .login-box {
      width: 691px;
      padding: 30px 20px;
      h4 {
        font-size: 22px;
        text-align: center;
      }
      p {
        font-size: 16px;
        margin: 25px 0 10px;
      }
      .sms-verification {
        position: relative;
        .sms-button {
          position: absolute;
          top: 0;
          right: 0;
          width: 82px;
          height: 40px;
          background: #1890ff;
          box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.04);
          border: 1px solid #1890ff;
          text-align: center;
          line-height: 40px;
          font-size: 14px;
          color: #fff;
          .van-count-down {
            color: #fff;
            line-height: 40px;
            font-size: 14px;
          }
        }
      }
    }
  }
}
</style>
