<template>
  <div class="login">
    <div class="contents">
      <!-- 头部logo -->
      <div class="header">
        <img src="@/assets/hongqi.png" alt="logo" />
      </div>
      
      <!-- 欢迎登录标题 -->
      <div class="welcome-title">欢迎登录</div>
      
      <!-- 主体表单 -->
      <div class="form-container">
        <div class="main">
          <wInput
            type="text"
            maxlength="11"
            placeholder="请输入您的手机号码"
            v-model="phoneData"
          ></wInput>
          <wInput
            type="password"
            maxlength="20"
            placeholder="请输入您的密码"
            v-model="passData"
            :isShowPass="true"
          ></wInput>
        </div>
        
        <wButton
          text="登录"
          :rotate="isRotate"
          @click.native="startLogin()"
          class="wbutton"
        ></wButton>
      </div>

      <!-- 底部链接 -->
      <div class="footer">
        <div style="color: #fff;font-size: 14px;cursor:pointer;" @click="toKefu">找回密码</div>
        <span>|</span>
        <router-link to="register">注册账号</router-link>
      </div>
    </div>

    <!-- 网站底部信息 -->
    <div class="site-footer">
      <div class="copyright">版权所有：中国建设科技集团股份有限公司</div>
      <div class="address">地址：北京市西城区德胜门外大街36号A座</div>
      <div class="beian">
        京ICP备 2022001408号-6号 京公网安备 11010202010460号
      </div>
    </div>
  </div>
</template>

<script>
import wInput from "../components/watch-login/watch-input.vue";
import wButton from "../components/watch-login/watch-button.vue";
import { Toast } from "vant";
import { getUserInfo, getLink } from '@/api/home';

export default {
  data() {
    return {
      phoneData: "", // 账号
      passData: "", // 密码
      smsCode: "", // 短信验证码
      lang: "zh", // 语言
      countryCode: "+86", // 国家代码
      isRotate: false, // 是否加载旋转
      link: '', // 新增客服链接
    };
  },
  mounted() {
    this.getUserInfo();
    this.getLink();
  },
  methods: {
    async getUserInfo() {
      const res = await getUserInfo();
      this.userInfo = res.data;
    },
    async getLink() {
      const res = await getLink();
      if (res.data && res.data.link && res.data.link[0] && res.data.link[0].value) {
        this.link = res.data.link[0].value;
      } else {
        this.link = '';
      }
    },
    toKefu() {
      if (this.link) {
        window.open(this.link, '_blank');
      } else {
        Toast('未获取到客服链接');
      }
    },
    async startLogin() {
      if (this.isRotate) {
        return false;
      }
      if (!this.phoneData) {
        Toast("账号不能为空");
        return;
      }
      if (this.passData.length < 6) {
        Toast("密码不少于6位");
        return;
      }
      this.isRotate = true;
      try {
        let data = {
          account: this.phoneData,
          password: this.passData,
          sms_code: this.smsCode,
          lang: this.lang,
          country_code: this.countryCode
        };
        await this.$store.dispatch("Login", data);
        this.isRotate = false;
       
        this.$router.replace("home");
      } catch (error) {
        this.isRotate = false;
      }
    },
  },
  components: {
    wInput,
    wButton
  }
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  width: 100%;
  background: #b43a39;
  background-size: cover;
  background-position: center;
  overflow: hidden;
}

.contents {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  padding-bottom: 120px;
}

.header {
  width: 165px;
  height: 73px;
  margin-top: 46px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #fff;
  padding: 20px;
  border-radius: 50%;
  img {
    width: 150px;
    height: 150px;
    object-fit: cover;
  }
}

.welcome-title {
  color: #fff;
  font-size: 18px;
  font-weight: 500;
  margin: 20px 0;
  text-align: center;
}

.form-container {
  width: 76%;
  background: #fff;
  border-radius: 10px;
  padding: 40px 30px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.main {
  width: 100%;
}

.wbutton {
  margin-top: 40px;
  width: 100%;
}

.footer {
  margin-top: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 40px;
  
  a {
    color: #fff;
    text-decoration: none;
    font-size: 14px;
    opacity: 0.9;
    transition: opacity 0.3s;
    
    &:hover {
      opacity: 1;
    }
  }
  
  span {
    color: #fff;
    opacity: 0.5;
  }
}

.site-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 15px 0;
  text-align: center;
  
  .copyright, .address, .beian {
    color: #fff;
    font-size: 12px;
    opacity: 0.8;
    margin-bottom: 3px;
    line-height: 1.2;
  }
}
</style>

<style>
@import url("../components/watch-login/css/icon.css");
</style>

