<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
    
    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 左侧品牌区域 -->
      <div class="brand-section">
        <div class="brand-content">
          <h1 class="brand-title">自助无人机租赁</h1>
          <h2 class="brand-subtitle">管理后台</h2>
          <p class="brand-desc">智能化无人机租赁管理平台</p>
        </div>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-section">
        <div class="login-card">
          <div class="login-header">
            <h3>欢迎登录</h3>
            <p>请输入您的账号信息</p>
          </div>
          
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <div class="form-group">
              <div class="input-wrapper">
                <div class="input-icon">
                  <img src="../../assets/images/1.png" alt="用户" />
                </div>
                <input
                  type="text"
                  name="account"
                  v-model="loginForm.account"
                  ref="account"
                  tabindex="1"
                  placeholder="请输入用户名"
                  class="form-input"
                />
              </div>
            </div>
            
            <div class="form-group">
              <div class="input-wrapper">
                <div class="input-icon">
                  <img src="../../assets/images/2.png" alt="密码" />
                </div>
                <input
                  type="password"
                  name="password"
                  v-model="loginForm.password"
                  ref="password"
                  tabindex="2"
                  :placeholder="$t('login.password')"
                  class="form-input"
                />
              </div>
            </div>
            
            <div class="form-actions">
              <el-button
                :loading="loading"
                type="primary"
                @click.native.prevent="handleLogin"
                class="login-btn"
              >
                {{ $t("login.logIn") }}
              </el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>

export default {
  name: "Login",
  data: function () {
    return {
      loginForm: {
        account: "",
        password: "",
        hospitalname:"",
      },
      loginRules: {
        account: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("login.accountTip"),
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("login.passwordTip"),
          },
        ],
      },
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  computed: {
    loginLoading() {
      return this.$store.state.user.loginLoading;
    },
  },
  watch: {
    loginLoading(val) {
      if (!val) this.loading = false;
    },
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          // this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true,
    },
  },
  created() {
    const _this = this;
    
    // const _this = this;
    // document.onkeydown = function (e) {
    //   const { keyCode } = e;
    //   if (keyCode === 13) {
    //     _this.handleLogin();
    //   }
    // };
  },
  mounted() {
    this.handlemyLogin();
    // if (this.loginForm.account == "") {
    //   this.$refs.account.focus();
    // } else if (this.loginForm.password == "") {
    //   this.$refs.password.focus();
    // }
    // this.$store.commit("user/SET_LOGIN_LOADING", false);
  },
  destroyed() {
    document.onkeydown = function (e) {
      const { keyCode } = e;
      if (keyCode === 13) {
      }
    };
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    handleLogin() {
      if (this.loading) return;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store.commit("user/SET_LOGIN_LOADING", true);
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/home",
                query: this.otherQuery,
              });
            })
            .catch(() => {
              this.$store.commit("user/SET_LOGIN_LOADING", false);
            });
        } else {
          return false;
        }
      });
    },
    handlemyLogin() {
    //this.$store.dispatch('user/logout')
    console.log("开始执行单点登录!");
    if (this.loading) return;
    let username = this.$route.query.username;
    let hospitalname=this.$route.query.hospitalname;
    console.log("username:"+username);
    if (username != null && username != "") {
      this.loginForm.account = username;
      this.loginForm.hospitalname =hospitalname;
      this.loading = true;
      this.$store.commit("user/SET_LOGIN_LOADING", true);
      this.$store
        .dispatch("user/pislogin", this.loginForm)
        .then(() => {
          this.$router.push({
            path: this.redirect || "/home",
            query: this.otherQuery,
          });
        })
        .catch(() => {
          this.$store.commit("user/SET_LOGIN_LOADING", false);
        });
    }
  },
  },
  
  getOtherQuery(query) {
    return Object.keys(query).reduce((acc, cur) => {
      if (cur !== "redirect") {
        acc[cur] = query[cur];
      }
      return acc;
    }, {});
  },
};
</script>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.login-container {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #4f46e5 0%, #06b6d4 100%);
  background-image: url(../../assets/images/4.jpg);
  background-size: cover;
  background-position: center;
  background-blend-mode: overlay;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

// 背景装饰圆圈
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

// 主要内容区域
.main-content {
  width: 100%;
  max-width: 1200px;
  height: 600px;
  display: flex;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  z-index: 2;
}

// 左侧品牌区域
.brand-section {
  flex: 1;
  background: linear-gradient(135deg, rgba(79, 70, 229, 0.8) 0%, rgba(6, 182, 212, 0.8) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.brand-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse"><path d="M 10 0 L 0 0 0 10" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="0.5"/></pattern></defs><rect width="100" height="100" fill="url(%23grid)"/></svg>');
  opacity: 0.3;
}

.brand-content {
  text-align: center;
  color: white;
  z-index: 1;
  position: relative;
}

.logo {
  margin-bottom: 30px;
}

.logo-icon {
  font-size: 80px;
  margin-bottom: 20px;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.3));
}

.brand-title {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 10px;
  letter-spacing: 2px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.brand-subtitle {
  font-size: 24px;
  font-weight: 500;
  margin-bottom: 20px;
  opacity: 0.9;
  letter-spacing: 1px;
}

.brand-desc {
  font-size: 16px;
  opacity: 0.8;
  font-weight: 300;
  letter-spacing: 1px;
}

// 右侧登录区域
.login-section {
  flex: 1;
  background: rgba(255, 255, 255, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
}

.login-card {
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h3 {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
  font-weight: 400;
}

// 表单样式
.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 24px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border: 2px solid #e9ecef;
  border-radius: 12px;
  padding: 0 16px;
  height: 56px;
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  border-color: #4f46e5;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.input-icon {
  margin-right: 12px;
  display: flex;
  align-items: center;
}

.input-icon img {
  width: 20px;
  height: 20px;
  opacity: 0.6;
  filter: brightness(0) saturate(100%) invert(27%) sepia(51%) saturate(2878%) hue-rotate(346deg) brightness(104%) contrast(97%);
}

.form-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 16px;
  color: #333;
  font-weight: 400;
  height: 100%;
}

.form-input::placeholder {
  color: #999;
  font-weight: 400;
}

.form-actions {
  margin-top: 32px;
}

.login-btn {
  width: 100%;
  height: 56px;
  background: linear-gradient(135deg, #4f46e5 0%, #06b6d4 100%);
  border: none;
  border-radius: 12px;
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(79, 70, 229, 0.4);
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(79, 70, 229, 0.6);
}

.login-btn:active {
  transform: translateY(0);
}

// 响应式设计
@media (max-width: 768px) {
  .main-content {
    flex-direction: column;
    height: auto;
    margin: 20px;
    max-width: none;
  }
  
  .brand-section {
    min-height: 200px;
  }
  
  .brand-title {
    font-size: 24px;
  }
  
  .brand-subtitle {
    font-size: 18px;
  }
  
  .logo-icon {
    font-size: 60px;
  }
  
  .login-section {
    padding: 40px 20px;
  }
}
</style>
