<template>
  <view class="login-container">

    <!-- 背景装饰 -->
    <view class="background-decoration"> </view>

    <!-- 返回按钮 -->
    <view class="back-btn" @click="showInterruptLoginModal = true" v-if="step === 2">
      <image src="@/static/images/login/back.png" mode="widthFix" style="width: 50rpx;height: 50rpx;"></image>
    </view>

    <!-- 主内容区 -->
    <view class="login-content">
      <!-- 标题 -->
      <view class="title">登录律师账号</view>
      <view class="subtitle">请使用手机号接收验证码进行登录</view>

      <!-- 第一步：输入手机号 -->
      <view class="form-section" v-if="step === 1">
        <view class="input-label">手机号码</view>
        <view class="input-wrapper">
          <input v-model="phone" type="number" class="phone-input" placeholder="请输入手机号码" maxlength="11" />
        </view>
        <view class="resend-code-btn" @click="handleGetCode"> 获取验证码</view>
      </view>

      <!-- 第二步：输入验证码 -->
      <view class="form-section" v-if="step === 2">
        <view class="input-label">验证码</view>
        <view class="input-wrapper">
          <input v-model="code" type="number" class="code-input" placeholder="请输入验证码" maxlength="6"
            @input="handleInputCode" />
        </view>

        <button class="resend-code-btn" @click="handleResendCode">
          {{ countdown > 0 ? `重新获取 (${countdown}s)` : '重新获取' }}
        </button>
        <!-- <button 
          class="login-btn" 
          :disabled="!canLogin"
          @click="handleLogin"
        >
          登录
        </button> -->
      </view>

      <!-- 用户协议 -->
      <view class="agreement-section">
        <view class="checkbox-wrapper" @click="toggleAgreement">
          <view class="checkbox" :class="{ 'checked': agreed }">
            <text class="check-icon" v-if="agreed">✓</text>
          </view>
          <text class="agreement-text">
            我已阅读并同意
            <text class="link-text" @click.stop="handleUserAgrement">《用户协议》</text>
            和
            <text class="link-text" @click.stop="handlePrivacy">《隐私政策》</text>
          </text>
        </view>
      </view>
    </view>

    <!-- 用户协议弹窗 -->
    <view class="modal-overlay" v-if="showAgreementModal" @click="showAgreementModal = false">
      <view class="modal-content" @click.stop>
        <view class="modal-title">用户协议及隐私政策</view>
        <view class="modal-text">
          为了更好的保障您的合法权益,请您阅读并同意以下协议
          <text class="link-text" @click="handleUserAgrement">《用户协议》</text>
          和
          <text class="link-text" @click="handlePrivacy">《隐私政策》</text>
        </view>
        <view class="modal-buttons">
          <button class="modal-btn reject-btn" @click="handleReject">拒绝</button>
          <button class="modal-btn agree-btn" @click="handleAgree">同意</button>
        </view>
      </view>
    </view>

    <!-- 中断登录弹窗 -->
    <view class="modal-overlay" v-if="showInterruptLoginModal">
      <view class="modal-content" @click.stop>
        <view class="modal-title" style="margin-bottom: 60rpx;">
          返回将中断登录，确定返回？
        </view>
        <view class="modal-buttons">
          <button class="modal-btn reject-btn" @click="showInterruptLoginModal = false">取消</button>
          <button class="modal-btn agree-btn" @click="interruptLogin">确定</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { sendSmsCode } from '@/api/login'
import { getToken } from '@/utils/auth'

export default {
  data() {
    return {
      step: 1, // 1: 输入手机号, 2: 输入验证码
      phone: '',
      code: '',
      countdown: 0,
      agreed: false,
      showAgreementModal: false,
      globalConfig: getApp().globalData.config,
      timer: null,
      showInterruptLoginModal: false
    }
  },
  computed: {
    canGetCode() {
      return /^1[3-9]\d{9}$/.test(this.phone) && this.agreed
    },
    canLogin() {
      return this.code.length >= 4 && this.agreed
    }
  },
  onLoad() {
    //#ifdef H5
    if (getToken()) {
      this.$tab.reLaunch('/pages/index')
    }
    //#endif
    // 首次进入显示协议弹窗
    this.showAgreementModal = true
  },
  onUnload() {
    if (this.timer) {
      clearInterval(this.timer)
    }
  },
  methods: {
    interruptLogin() {
      this.showInterruptLoginModal = false
      this.step = 1
      this.code = ''
    },
    toggleAgreement() {
      this.agreed = !this.agreed
    },
    handleGetCode() {
      if (!this.canGetCode) {
        if (!/^1[3-9]\d{9}$/.test(this.phone)) {
          this.$modal.msgError('请输入正确的手机号码')
        } else if (!this.agreed) {
          this.$modal.msgError('请先同意用户协议和隐私政策')
        }
        return
      }

      this.$modal.loading('发送中...')
      sendSmsCode(this.phone).then(() => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('验证码已发送')
        this.step = 2
        this.startCountdown()
      }).catch(() => {
        this.$modal.closeLoading()
      })
    },
    handleResendCode() {
      if (this.countdown > 0) return
      this.handleGetCode()
    },
    startCountdown() {
      this.countdown = 60
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.countdown--
        if (this.countdown <= 0) {
          clearInterval(this.timer)
          this.timer = null
        }
      }, 1000)
    },
    handleInputCode() {
      if (this.code.length >= 6) {
        this.handleLogin()
      }
    },
    async handleLogin() {
      if (!this.canLogin) {
        if (this.code.length < 4) {
          this.$modal.msgError('请输入验证码')
        } else if (!this.agreed) {
          this.$modal.msgError('请先同意用户协议和隐私政策')
        }
        return
      }

      this.$modal.loading('登录中，请耐心等待...')

      this.$modal.closeLoading()
      this.loginSuccess()

      try {
        await this.$store.dispatch('LoginBySms', {
          phone: this.phone,
          code: this.code
        })
        this.$modal.closeLoading()
        this.loginSuccess()
      } catch (error) {
        this.$modal.closeLoading()
      }

    },
    loginSuccess() {
      // this.$store.dispatch('GetInfo').then(res => {
        this.$tab.reLaunch('/pages/setUserInfo')
      // })
    },
    handleUserAgrement() {
      let site = this.globalConfig.appInfo.agreements[1]
      this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
    },
    handlePrivacy() {
      let site = this.globalConfig.appInfo.agreements[0]
      this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
    },
    handleReject() {
      this.showAgreementModal = false
      this.agreed = false
    },
    handleAgree() {
      this.showAgreementModal = false
      this.agreed = true
    }
  }
}
</script>

<style lang="scss" scoped>
page {
  background: linear-gradient(135deg, #f5f7fa 0%, #e8ecf0 100%);
  min-height: 100vh;
}

.login-container {
  position: relative;
  width: 100%;
  min-height: 100vh;
  padding: 0 60rpx;
  padding-top: 120rpx;
  overflow: hidden;
  background-image: url('@/static/images/login/bg1.png');
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center center;
}

.background-decoration {
  position: absolute;
  top: 0;
  right: 0;
  width: 100%;
  height: 80%;
  background-image: url('@/static/images/login/bg2.png');
  background-size: cover;
}

.back-btn {
  position: absolute;
  top: 100rpx;
  left: 40rpx;
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;

  .back-icon {
    font-size: 40rpx;
    color: #333;
    font-weight: bold;
  }
}


.login-content {
  position: relative;
  z-index: 1;
  top: 260rpx;
}

.title {
  font-size: 56rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
  text-align: left;
}

.subtitle {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 80rpx;
  text-align: left;
}

.form-section {
  margin-bottom: 60rpx;
}

.input-label {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 20rpx;
  text-align: left;
}

.input-wrapper {
  background-color: #f8f9fa;
  border-radius: 16rpx;
  padding: 24rpx 30rpx;
  margin-bottom: 40rpx;
}

.phone-input,
.code-input {
  width: 100%;
  font-size: 32rpx;
  color: #333;
  background: transparent;
  border: none;
}

.get-code-btn,
.resend-code-btn {
  width: 100%;
  height: 88rpx;
  background: linear-gradient(135deg, #4a90e2 0%, #5ba0f2 100%);
  color: #fff;
  font-size: 32rpx;
  border-radius: 16rpx;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 40rpx;

  &[disabled] {
    background: #d0d0d0;
    color: #999;
  }
}

.login-btn {
  width: 100%;
  height: 88rpx;
  background: linear-gradient(135deg, #4a90e2 0%, #5ba0f2 100%);
  color: #fff;
  font-size: 32rpx;
  border-radius: 16rpx;
  border: none;
  margin-top: 40rpx;

  &[disabled] {
    background: #d0d0d0;
    color: #999;
  }
}

.agreement-section {
  position: fixed;
  bottom: 60rpx;
  left: 0;
  right: 0;
  padding: 0 60rpx;
  z-index: 2;
}

.checkbox-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
}

.checkbox {
  width: 36rpx;
  height: 36rpx;
  border: 2rpx solid #ddd;
  border-radius: 50%;
  margin-right: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;

  &.checked {
    background: #4a90e2;
    border-color: #4a90e2;

    .check-icon {
      color: #fff;
      font-size: 24rpx;
      font-weight: bold;
    }
  }
}

.agreement-text {
  font-size: 24rpx;
  color: #666;
  line-height: 1.6;
}

.link-text {
  color: #4a90e2;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  width: 600rpx;
  background: #fff;
  border-radius: 24rpx;
  padding: 60rpx 40rpx 0 40rpx;
  margin: 0 40rpx;
}

.modal-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 30rpx;
  text-align: center;
}

.modal-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.8;
  margin-bottom: 50rpx;
  text-align: left;
}

.modal-buttons {
  display: flex;
  border-top: 1rpx solid #eee;
  // padding-top: 30rpx;

}

.modal-btn {
  flex: 1;
  height: 100rpx;
  font-size: 32rpx;
  border: none;
  background: transparent;
  border-radius: 12rpx;
  padding: 10rpx;

  &.reject-btn {
    color: #999;
    margin-right: 20rpx;
    border-right: 1rpx solid #eee;
  }

  &.agree-btn {
    color: #4a90e2;
    font-weight: bold;
  }
}

uni-button:after {
  border: none !important;
}
</style>