<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="12" >
        <!-- thirdPartyLogoInfoList -->
         <template v-for="(item, index) in thirdPartyLogoInfoList" :key="index">
          <el-col :span="8">
            <el-tooltip :content="item.label" placement="top">
              <img :src="item.logo" alt="" @click="thirdLogin(item.name)" />
            </el-tooltip>
          </el-col>
        </template>
        
        
      </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 { UserToken } from "@/types/user";
import { usePermissStore } from "@/store/permiss";
import Cookies from 'js-cookie';
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 = 60; //最大请求次数
const currentLoginRequest = ref(0); //当前请求次数

const  thirdPartyLogoInfoList = ref([
  {
    name: "github",
    logo: "https://www.ljsea.top/wp-content/uploads/2025/05/github-48-logo.png",
    label: "GitHub",
  },
  {
    name: "gitee",
    logo: "https://www.ljsea.top/wp-content/uploads/2025/04/gitee.png",
    label: "Gitee",
  },
  {
    name: "qq",
    logo: "https://www.ljsea.top/wp-content/uploads/2025/05/icons8-qq-48.png",
    label: "QQ",
  },
  {
    name: "google",
    logo: "https://www.ljsea.top/wp-content/uploads/2025/05/icons8-google-48.png",
    label: "Google",
  },
  {
    name: "facebook",
    logo: "https://www.ljsea.top/wp-content/uploads/2025/05/icons8-facebook.png",
    label: "Facebook",
  },
  {
    name: "stackoverflow",
    logo: "https://www.ljsea.top/wp-content/uploads/2025/05/logo-stackoverflow-48.png",
    label: "StackOverflow",
  },
  {
    name: "my_gitea",
    logo: "https://www.ljsea.top/wp-content/uploads/2025/05/my_Gitea.png",
    label: "Gitea自建",
  },
  {
    name: 'gitea',
    logo: 'https://www.ljsea.top/wp-content/uploads/2025/05/PajamasGitea.png',
    label: 'Gitea官方',
  },
  {
    name: 'microsoft',
    logo: 'https://www.ljsea.top/wp-content/uploads/2025/05/icons8-microsoft48.png',
    label: 'Microsoft',
  }

]);


//表单数据
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={};
  //平台
  // for(let i = 0; i < thirdPartyLogoInfoList.value.length; i++) {
  //   if(thirdPartyLogoInfoList.value[i].name === type) {
  //     result = thirdPartyLogoInfoList.value[i];
  //     break;
  //   }
  // }
  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["code"] === 9){
        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:UserToken = status["user_info"];
      globalData["token"] = userInfo.access_token;
      localStorage.setItem("token", userInfo.access_token);
      localStorage.setItem("refresh_token", userInfo.refresh_token);
      localStorage.setItem("userId", userInfo.user_id.toString());
      localStorage.setItem("username", userInfo.username);
      let now = new Date();
      localStorage.setItem("end_time", (now.setDate(now.getHours() + 12)).toString()); //过期时间
      await getMyUserInfo(userInfo.user_id);
      return;
    }
  }, 2000);
  //新标签页打开登录地址
  //window.open(loginUrl, "_blank");

  //新的浏览器窗口打开标签页
  const width = 800;
  const height = 600;
  // 计算窗口在屏幕中心的位置
  const left = (window.screen.width - width) / 2;
  const top = (window.screen.height - height) / 2;
  const features = `width=${width},height=${height},left=${left},top=${top}`;
  // 创建新窗口
  const newWindow = window.open(loginUrl, '_blank', features);
  

};

//登录接口调用
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;
  }
  let userTokenInfo: UserToken = result["data"];
  globalData["token"] = result.data;
  localStorage.setItem("token", userTokenInfo.access_token);
  localStorage.setItem("refresh_token", userTokenInfo.refresh_token);
  localStorage.setItem("userId", userTokenInfo.user_id.toString());
  localStorage.setItem("username", userTokenInfo.username);
  let now = new Date();
  localStorage.setItem("end_time", (now.setDate(now.getHours())).toString()); //过期时间
  await getMyUserInfo(userTokenInfo.user_id);
  //token.value= result.data;

};

const getMyUserInfo = async (id) => {
  let result = {};
  try {
    let tokenData = {
      token: localStorage.getItem("token"),
      id: id,
    };
    result = await GetUserInfoService(tokenData);
    const cookie= Cookies.get("user_token");
        console.log("cookie:", cookie);
    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: 470px;
  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;
  /* 文字水平竖直居中 */
  text-align: center;

  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;
}

.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);

}
.google-btn {
  background-image: url(../../assets/img/google-logo_resized.png) !important;
}
</style>
