<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="login-header">
        <img class="logo mr10" src="../../assets/img/logo.svg" alt="" />
        <div class="login-title">大学生学业作品AI生成工具</div>
      </div>
      <el-form :model="param" :rules="rules" ref="login" size="large">
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="用户名或邮箱">
            <template #prepend>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            v-model="param.password"
          >
            <template #prepend>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <div class="pwd-tips">
          <el-checkbox
            class="pwd-checkbox"
            v-model="checked"
            label="记住密码"
          />
          <el-link type="primary" @click="$router.push('/reset-pwd')"
            >忘记密码</el-link
          >
        </div>
        <el-button
          class="login-btn"
          type="primary"
          size="large"
          @click="onLogin"
          >登录</el-button
        >
        <p class="login-text">
          没有账号？<el-link type="primary" @click="$router.push('/register')"
            >立即注册</el-link
          >
        </p>
      </el-form>
      <div>
    <!-- 第三登录 -->
    <el-divider content-position="center">第三方登录</el-divider>
    <div class="login-text">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-button
            type="primary"
            class="github-btn"
            @click="thirdLogin('github')"
            circle
          ></el-button>
        </el-col>
        <el-col :span="8">
          <el-button
            type="primary"
            class="gitee-btn"
            @click="thirdLogin('gitee')"
            circle
          ></el-button>
        </el-col>
        <el-col :span="8">
          <el-button
            type="primary"
            class="qq-btn"
            @click="thirdLogin('qq')"
            circle
          ></el-button>
        </el-col>
      </el-row>
    </div>
  </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, inject } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { loginService } from "@/api/user";
import { GetUserInfoService } from "@/api/user";
import { usePermissStore } from "@/store/permiss";
import {getThirdPartyUUID,getThirdPartyLoginStatus,getGithubLoginUrl,getQQLoginUrl,getThirdPartyLoginUrl} from "@/api/user";
// 从本地存储获取登录参数
const lgStr = localStorage.getItem("login-param");
const defParam = lgStr ? JSON.parse(lgStr) : null;
const globalData = inject("globalData");
const permiss = usePermissStore();
// 记住密码状态
const checked = ref(lgStr ? true : false);

const router = useRouter();
// 登录表单数据
const param = reactive({
  username: defParam ? defParam.username : "",
  password: defParam ? defParam.password : "",
});

const qqButtonBgImage = ref('https://wiki.connect.qq.com/wp-content/uploads/2016/12/Connect_logo_4.png');

// 表单验证规则
const rules = {
  username: [
    {
      required: true,
      message: "请输入用户名",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
  ],
};
// 表单引用
const login = ref(null);
const maxLoginRepeatRequest = 30; //最大请求次数
const currentLoginRequest = ref(0); //当前请求次数

//表单数据
var loginData = ref({
  username: "",
  email: "",
  password: "",
  ip: "",
});

const thirdLogin = async (type) => {
  //获取uuid
  let uuidResp = await getThirdPartyUUID({"type": type});
  if (uuidResp["code"] !== 0) {
    ElMessage.error("获取UUID失败！请稍后再试");
    return;
  }
  let uuid = uuidResp["data"];
  if (!uuid) {
    ElMessage.error("获取UUID失败！请稍后再试");
    return;
  }
  let result={};
  if(type !== "github" && type !== "qq" && type !== "gitee") {
    
    ElMessage.error("不支持的登录平台！请稍后再试："+type);
    return;
  }
  result = await getThirdPartyLoginUrl({uuid: uuid,"type": "login", "platform": type });
  if (result["code"] !== 0) {
    ElMessage.error("获取登录地址失败！请稍后再试");
    return;
  }
  let loginUrl = result["data"];
  if (!loginUrl) {
    ElMessage.error("获取登录地址失败！请稍后再试");
    return;
  }
  //获取登录状态每2秒请求一次
  let timer = setInterval(async () => {
    currentLoginRequest.value++;
    if (currentLoginRequest.value > maxLoginRepeatRequest) {
      clearInterval(timer);
      ElMessage.error("登录超时，请重新登录！");
      return;
    }
    let statusResp = await getThirdPartyLoginStatus({uuid: uuid});
    if (statusResp["code"] !== 0) {
      ElMessage.error("获取登录状态失败！请稍后再试");
      clearInterval(timer);
      return;
    }
    if(statusResp["data"]["status"] === 163) {
      ElMessage.error("该账号未绑定，请先绑定账号！");
      clearInterval(timer);
      return;
    }
    let status = statusResp["data"];
    if(status["status"] === 0) {
      //登录成功
      clearInterval(timer);
      let userInfo = status["user_info"];
      let token = userInfo["token"];
      if (!token) {
        ElMessage.error("获取登录状态失败！请稍后再试");
        return;
      }

      globalData["token"] = token;
      localStorage.setItem("token", token);
      localStorage.setItem("userId", userInfo["id"]);
      localStorage.setItem("username", status["username"]);
      let now = new Date();
      localStorage.setItem("end_time", (now.setDate(now.getHours() + 12)).toString()); //过期时间
      await getMyUserInfo(userInfo["id"]);
      return;
    }
  }, 2000);
  //新标签页打开登录地址
  window.open(loginUrl, "_blank");
  

};

//登录接口调用
const onLogin = async () => {
  console.log("params:", param);

  loginData.value.username = param.username;
  loginData.value.password = param.password;
  let result = await loginService(loginData);
  console.log("login result:", result);
  if (result["code"] !== 0) {
    //alert(result.message);
    ElMessage.error("登录失败！用户名或密码错误");
    return;
  }
  globalData["token"] = result.data;
  localStorage.setItem("token", result.data.token);
  localStorage.setItem("userId", result.data.id);
  localStorage.setItem("username", result.data.username);
  let now = new Date();
  localStorage.setItem("end_time", (now.setDate(now.getHours() + 12)).toString()); //过期时间
  await getMyUserInfo(result.data.id);
  //token.value= result.data;

};

const getMyUserInfo = async (id) => {
  let result = {};
  try {
    let tokenData = {
      token: localStorage.getItem("token"),
      id: id,
    };
    result = await GetUserInfoService(tokenData);
    if (result["code"] === 0) {
      //console.log("token data:",this.tokenData)
      // localStorage.setItem("video_func", result.data.VideoFunc);
      // localStorage.setItem("device_func", result.data.DeviceFunc);
      // localStorage.setItem("cid_func", result.data.CIDFunc);
      // localStorage.setItem("role", result.data.Role);

      ElMessage.success("登录成功");
        localStorage.setItem("ms_username", result["data"]["Name"]);
        const keys =
            permiss.defaultList[result["data"]["Role"] == "admin" ? "admin" : "user"];
            localStorage.setItem("ms_imgurl", result["data"]["Avatar"]);
        permiss.handleSet(keys);
        localStorage.setItem("ms_keys", JSON.stringify(keys));
        localStorage.setItem("ms_role", result["data"]["Role"]);
        router.push("/");

      //alert("video_func:" + localStorage.getItem("video_func")+"    type:" +typeof(localStorage.getItem("video_func")));
    }
  } catch (e) {
    console.log(e);
  }
};

// 获取标签存储并清空标签
</script>

<style scoped>
.login-bg {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: url(../../assets/img/login-bg3.jpg) center/cover no-repeat;
}

.login-header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 40px;
}

.logo {
  width: 35px;
}

.login-title {
  font-size: 22px;
  color: #333;
  font-weight: bold;
}

.login-container {
  width: 450px;
  border-radius: 5px;
  background: #fff;
  padding: 40px 50px 50px;
  box-sizing: border-box;
}

.pwd-tips {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  margin: -10px 0 10px;
  color: #787878;
}

.pwd-checkbox {
  height: auto;
}

.login-btn {
  display: block;
  width: 100%;
}

.login-tips {
  font-size: 12px;
  color: #999;
}

.login-text {
  display: flex;
  align-items: center;
  margin-top: 20px;
  font-size: 14px;
  color: #787878;
}

.login-text {
  text-align: center;
}

.login-btn {
  width: 100%;
  height: 40px;
  line-height: 40px;
  border-radius: 5px;
  font-size: 16px;
}
.github-btn {
  background-image: url(../../assets/img/github-logo.png) !important;
}


.gitee-btn {
  background-image: url(../../assets/img/gitee_logo.png) !important;
}
.qq-btn {
  background-image: url(https://wiki.connect.qq.com/wp-content/uploads/2016/12/Connect_logo_4.png);

}
</style>
