<template>
  <el-dialog
    class="mai-login-modal"
    v-model="loginVisible"
    footer=""
    header=""
    :show-close="false"
    @close="onClose"
  >
    <template #header>
      <h2 class="mb-3 text-2xl font-bold text-center xl:text-3xl enter-x xl:text-left">
        欢迎来到
        <p>{{ title }}</p>
      </h2>
    </template>
    <el-form
      class="p-4 enter-x"
      :model="formData"
      :rules="formRules"
      ref="formRef"
    >
      <el-form-item name="account" prop="account" class="enter-x">
        <el-input
          size="large"
          v-model="formData.account"
          placeholder="请输入账号"
          class="fix-auto-fill"
        />
      </el-form-item>
      <el-form-item name="password" prop="password" class="enter-x">
        <el-input
          type="password"
          size="large"
          v-model="formData.password"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item name="captcha" prop="captcha" class="enter-x">
        <el-input
          size="large"
          v-model="formData.captcha"
          placeholder="请输入验证码"
        >
          <template #append>
            <img class="cursor-pointer" @click="getCode($event)" src="http://research.mcnetmart.com/index.php?s=/captcha" alt="" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item class="enter-x">
        <el-button type="primary" size="large" block @click="handleLogin(formRef)" :loading="loading">
          登录
        </el-button>
        <!-- <Button size="large" class="mt-4 enter-x" block @click="handleRegister">
          {{ t('sys.login.registerButton') }}
        </Button> -->
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script lang="ts" setup>
import { computed, reactive, ref, unref } from 'vue';
import {FormInstance} from "element-plus";
const captcha = ref('')
const userStore = useUserInfo();
const title = computed(()=> import.meta.env.VITE_GLOB_APP_TITLE)
const formRules = ref({
  account:{message:'请输入用户名', trigger:'blur', required: true},
  password: {message:'请输入密码', trigger:'blur', required: true},
  captcha: {message:'请输入验证码', trigger:'blur', required: true},
})

const formRef = ref<FormInstance>();
const loading = ref(false);

const formData = reactive({
  account: 'test',
  password: '123456',
  captcha: '',
});
const prefixCls = useDesign('login');
const { commonApi } = useApi()

async function handleLogin(formEl: FormInstance | undefined) {
  if (!formEl) return;
  await formEl.validate(async (vail: boolean) => {
    if (vail) {
      loading.value = true;
      try {
        const userInfo = await userStore.login({
          ...formData
        });
        if (userInfo) {
          ElNotification.success('登录成功')
        }
      } catch (e) {
        loading.value = false;
      } finally {
        loading.value = false;
      }
    }else {
      loading.value = false;
    }

  })

}

async function getCode(ev: any){
  // this.src = ''
  // this.src = ''
  ev.target.setAttribute('src', 'http://research.mcnetmart.com/index.php?s=/captcha&u='+ new Date().getTime())
  // captcha.value = await commonApi.getCaptcha() as unknown as string;
}
// getCode()
const loginVisible = ref(userStore.getLoginVisible);
watchEffect(()=>{
  loginVisible.value = userStore.getLoginVisible;
  console.log('loginStatus>>>', loginVisible.value)
})
const onClose = () => {
  userStore.setLoginVisible(false);
};
</script>

<style lang="less">
//$elPrefix: 'el-dialog';
.el-dialog {
  width: 1105px !important;
  max-width: 1105px !important;
  min-width: 1105px !important;
  border-radius: 10px;
  &__header {
    h2 {
      font-size: 48px;
      line-height: 67px;
      text-align: center;
      padding-top: 54px;

      p {
        color: #999;
        font-size: 28px;
        line-height: 30px;
        margin-top: 12px;
      }
    }
  }
  &__body {
    padding: 0 105px;
    border-radius: 16px;
    box-sizing: border-box;


    .el-input__wrapper {
      height: 117px;
      font-size: 32px;
      border-radius: 8px;
      padding: 0 60px;
    }

    .el-input-group--append {
      height: 117px;
      .el-input__wrapper {
        border-radius: 8px 0 0 8px;
      }
      .el-input-group__append {
        padding: 1px;
        box-shadow: 0 0 0 1px var(--el-input-border-color, var(--el-border-color)) inset;
      }
      background-color: unset;
      img {
        width: 100%;
        height: 100%;
      }
    }

    .el-button {
      font-size: 28px;
      height: 106px;
      margin-bottom: 107px;
      background-color: #14417a;
      border-radius: 8px;
      @apply w-full;
    }
  }
}
</style>
