<template>
  <div class="login">
      <div style="margin-right: 105px;">
          <img src="../assets/images/bg-2.png" alt="logo" />
      </div>
      <div class="login-box">
          <img class="log-icon" @click="checkOut(1)" v-if="loginType == 2" src="../assets/images/login/loginpc.png" alt="logo" />
          <img class="log-icon" @click="checkOut(2)" v-else src="../assets/images/login/loginwx.png" alt="logo" />


          <el-form ref="loginRef"  :model="loginForm" :rules="loginRules" class="login-form">

              <img  :style="{'margin-bottom': loginType == 1 ? '60px' : 0} " src="../assets/images/login-logo.png" />

              <template v-if="loginType == 1">
                  <h3 class="title">账户登录</h3>
                  <el-form-item prop="username">
                      <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
                          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
                      </el-input>
                  </el-form-item>
                  <el-form-item prop="password">
                      <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码" @keyup.enter="handleLogin">
                          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
                      </el-input>
                  </el-form-item>
                  <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
                  <el-form-item style="float: right;">

                  </el-form-item>
                  <el-form-item style="width:100%;">
                      <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
                          <span v-if="!loading">登 录</span>
                          <span v-else>登 录 中...</span>
                      </el-button>
                  </el-form-item>
              </template>


              <div v-else class="code-box" id="xwork_id">
              </div>

          </el-form>
      </div>

  </div>
</template>

<script setup lang="ts">
import { getCodeImg, getTenantList, getXworkLoginParam } from '@/api/login';
import { authBinding } from '@/api/system/social/auth';
import { useUserStore } from '@/store/modules/user';
import {LoginData, TenantVO} from '@/api/types';
import { to } from 'await-to-js';
import { HttpStatus } from "@/enums/RespEnum";
import { userHobit } from '@/store/modules/userHobit'
const userHobitStore = userHobit()
const userStore = useUserStore();
const router = useRouter();
const loginForm = ref<LoginData>({
  username: localStorage.getItem('username')|| '',
  password: localStorage.getItem('password')  || '',
  rememberMe: Boolean(localStorage.getItem('rememberMe')) as boolean || false,
} as LoginData);

const loginRules: ElFormRules = {
  username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
  code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
};

const loading = ref(false);
const baseUrl =  'https://' + document.domain + '/'



//
const checkOut = async (type: number) => {
    loginType.value = type
    if(type == 2) {
        const res = await getXworkLoginParam()
        console.log(res, ">>>>>>>>>>>>>..")
        var wwLogin = new WwLogin({
            "id": "xwork_id",
            "appid": res.result.appid,
            "agentid": res.result.agentid,
            "scope": "snsapi_privateinfo",
            //配置回调URL
            "redirect_uri": encodeURIComponent(baseUrl)+"?scope=snsapi_privateinfo",
            //callback
            "state": "fromQYwx"
        });
    }

}


// 注册开关
const redirect = ref(undefined);
const loginRef = ref<ElFormInstance>();

const loginType = ref(1)

watch(() => router.currentRoute.value, (newRoute: any) => {
  redirect.value = newRoute.query && newRoute.query.redirect;
}, { immediate: true });

const handleLogin = () => {
  loginRef.value?.validate(async (valid: boolean, fields: any) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 localStorage 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        localStorage.setItem('username', String(loginForm.value.username));
        localStorage.setItem('password', String(loginForm.value.password));
        localStorage.setItem('rememberMe', String(loginForm.value.rememberMe));
      } else {
        // 否则移除
        localStorage.removeItem('username');
        localStorage.removeItem('password');
        localStorage.removeItem('rememberMe');
      }
      // 调用action的登录方法
        const [err] = await to(userStore.login(loginForm.value));
        //获取用户列表配置
        // userHobitStore.gethobit()
      if (!err) {
        await router.push({ path: redirect.value || '/' });
        loading.value = false;
      } else {
        loading.value = false;

      }
    } else {
      console.log('error submit!', fields);
    }
  });
};
onMounted(() => {
    checkOut(1)
});
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-bg.png");
  background-size: cover;
}
.login-box{
    position: relative;
    .log-icon{
        position: absolute;
        right: 0;
        top: 0;
        cursor: pointer;
    }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 20px;
    color: #1874FF;
    font-style: normal;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 520px;
    height: 600px;
  padding: 70px 90px;
    //img{
    //    margin-bottom: 63px;
    //}
  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial, serif;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
.code-box{
    display: flex;
    justify-content: center;
}
</style>
