<template>
  <div class="login-form-wrapper3">
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      <div class="formItem"></div>
      <FormItem name="phoneNumber" class="enter-x">
        <a-input
          class="fix-auto-fill"
          size="large"
          :placeholder="t('sys.login.mobile')"
          :type="'text'"
          v-model:value="formData.phoneNumber"
        >
          <template #suffix>
            <a-tooltip>
              <span></span>
            </a-tooltip>
          </template>
        </a-input>
      </FormItem>
      <div class="sliderBar">
        <SliderBar ref="sliderBar" :promptTextStyleSuccess="{ color: '#ffffff' }" :multiple="1" />
      </div>
      <FormItem name="code" class="enter-x bottonDiv">
        <CountdownInput
          :sendCodeApi="sendLoginSms"
          size="large"
          class="fix-auto-fill"
          v-model:value="formData.code"
          :placeholder="t('sys.login.smsCode')"
        />
      </FormItem>

      <FormItem class="enter-x">
        <Button
          class="login-but ant-btn1"
          type="primary"
          size="large"
          block
          @click="handleLogin"
          :loading="loading"
        >
          {{ t('sys.login.loginButton') }}
        </Button>
      </FormItem>
    </Form>
  </div>
</template>
<script lang="ts" setup>
  import { reactive, ref, toRaw, provide } from 'vue';
  import { Form, Button, message } from 'ant-design-vue';
  import { CountdownInput } from '/@/components/CountDown';
  import SliderBar from './SliderBar.vue';

  import { useI18n } from '/@/hooks/web/useI18n';
  import { useFormRules, useFormValid } from '../login/useLogin';
  import { SendLoginSmsCode } from '/@/api/sys/user';
  import { useUserStore } from '/@/store/modules/user';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { notification, createMessage } = useMessage();
  const FormItem = Form.Item;
  const { t } = useI18n();
  const { getFormRules } = useFormRules();

  const formRef = ref();
  const loading = ref(false);

  const formData = reactive({
    phoneNumber: '',
    code: '',
  });

  async function sendLoginSms() {
    const reg =
      /^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/;
    if (reg.test(formData.phoneNumber)) {
      const sendRes = await SendLoginSmsCode(formData.phoneNumber);
      if (!sendRes) {
        message.error('发送失败');
        return false;
      }
      return true;
    } else {
      message.error('请输入正确手机号码');
    }
  }

  const { validForm } = useFormValid(formRef);
  const userStore = useUserStore();
  // const getShow = computed(() => unref(getLoginState) === LoginStateEnum.MOBILE);
  let verifySuccess = ref(false);
  provide('verifySuccess', verifySuccess);
  async function handleLogin() {
    const data = await validForm();
    if (!data) return;
    if (!data.phoneNumber || data.phoneNumber.trim() == '') {
      message.info('请输入电话号码！');
      return;
    }
    if (!verifySuccess.value) {
      createMessage.error('请先通过滑动验证！');
      return;
    }
    if (!data.code || data.code.trim() == '') {
      message.info('请输入验证码！');
      return;
    }
    const userInfo = await userStore
      .smsCodelogin(
        toRaw({
          phoneNumber: data.phoneNumber,
          code: data.code,
          mode: 'none', //不要默认的错误提示
        })
      )
      .catch((data) => {
        //登录失败如果是502，返回的html,那么提示框什么都没有
        if (data.message == 'Invalid username or password' && typeof data.message == 'string') {
          createMessage.error(data.message);
        } else if (data.message == '账号已过期,请联系管理员') {
          createMessage.error(data.message);
        } else {
          createMessage.error(data.message);
        }
      });
    if (userInfo) {
      notification.success({
        message: t('sys.login.loginSuccessTitle'),
        description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.realName}`,
        duration: 3,
      });
    }
  }
</script>

<style lang="less" scoped>
  .login-form-wrapper3 .p-4 {
    padding: 0;
  }

  .sliderBar {
    width: 307px;
    height: 42px;
    margin-bottom: 24px;
  }

  .ant-form-item-control-input-content input {
    box-shadow: inset 0 0 6px 2px #16c0ff;
  }

  .ant-input-wrapper #code {
    box-shadow: inset 0 0 6px 2px #16c0ff;
  }

  .login-but {
    height: 40px;
    background: linear-gradient(-90deg, #0bf, #01d8eb);
    box-shadow: 0 12px 8px 0 rgba(3, 30, 55, 0.1);
    border-radius: 2px;
    &:hover,
    &:focus {
      background: linear-gradient(-90deg, #0bf, #01d8eb) !important;
      box-shadow: 0 12px 8px 0 rgba(3, 30, 55, 0.1) !important;
      border-radius: 2px !important;
    }
  }

  :deep(.vben-countdown-input) .ant-input-group-addon button {
    box-shadow: inset 0px 0px 6px 2px #16c0ff;
    background: transparent;
    border: none;
    color: #fff;
    border-radius: 2px;
    color: #16c7fd;
  }
  :deep(.ant-input-group-addon) {
    box-shadow: inset 0px 0px 6px 2px #16c0ff;
    background: transparent;
    border: none;
    color: #fff;
    border-radius: 2px;
  }
  :deep(.ant-form-item-explain.ant-form-item-explain-error) {
    color: rgba(0, 255, 255, 0.7);
  }

  :deep(#code) {
    box-shadow: inset 0 0 6px 2px #16c0ff;
    height: 42px !important;
    margin: 0 !important;
  }

  :deep(#code),
  :deep(#phoneNumber) {
    font-size: 14px;

    &::-webkit-input-placeholder {
      color: rgba(255, 255, 255, 0.4);
    }

    &::-moz-placeholder {
      color: rgba(255, 255, 255, 0.4);
    }

    &:-ms-input-placeholder {
      color: rgba(255, 255, 255, 0.4);
    }
  }

  .fix-auto-fill.ant-input-affix-wrapper {
    box-shadow: inset 0 0 6px 2px #16c0ff;
    background-color: transparent;
    border: none;
  }

  .formItem {
    width: 100%;
    height: 30px;
  }

  .bottonDiv {
    margin-bottom: 64px;
  }
</style>
