<template>
  <view class="login-page">
    <!-- 背景图（青山绿水，不修改、不重新生成） -->
    <image class="bg-img" src="/static/login-bg.jpeg" mode="aspectFill" />
    <!-- 柔光蒙版：保证文字与卡片清晰，不与背景混在一起 -->
    <view class="veil"></view>

    <StatusBar />

    <!-- 内容层 -->
    <view class="content">
      <!-- 顶部品牌区 -->
      <view class="brand">
        <view class="logo">
          <view class="leaf">
            <view class="leaf-vein"></view>
          </view>
        </view>
        <text class="title">绿水青山</text>
        <text class="subtitle">与自然同行</text>
      </view>

      <!-- 登录卡片：半透明白色磨砂玻璃 -->
      <view class="login-card">
        <text class="card-title">欢迎回来</text>

        <!-- 账号输入框 -->
        <view class="field" :class="{ 'field-focus': focusedField === 'account' }">
          <text class="uni-icons uniui-person field-icon"></text>
          <input
            class="field-input"
            placeholder="请输入账号"
            placeholder-class="field-ph"
            v-model="form.account"
            @focus="focusedField = 'account'"
            @blur="focusedField = ''"
          />
        </view>

        <!-- 密码输入框 -->
        <view class="field" :class="{ 'field-focus': focusedField === 'password' }">
          <text class="uni-icons uniui-locked field-icon"></text>
          <input
            class="field-input"
            type="text"
            :password="!showPassword"
            placeholder="请输入密码"
            placeholder-class="field-ph"
            v-model="form.password"
            @focus="focusedField = 'password'"
            @blur="focusedField = ''"
          />
          <text
            class="uni-icons field-eye"
            :class="showPassword ? 'uniui-eye' : 'uniui-eye-slash'"
            @click="showPassword = !showPassword"
          ></text>
        </view>

        <!-- 记住密码 -->
        <view class="remember-row" @click="remember = !remember">
          <view class="checkbox" :class="{ 'checkbox-checked': remember }">
            <text v-if="remember" class="uni-icons uniui-checkmarkempty checkbox-icon"></text>
          </view>
          <text class="remember-text">记住密码</text>
        </view>

        <!-- 登录按钮：青山绿 → 流水青 -->
        <button
          class="login-btn"
          :class="{ 'login-btn-disabled': !canLogin }"
          :disabled="!canLogin || loading"
          :hover-class="'login-btn-hover'"
          @click="handleLogin"
        >
          {{ loading ? '登录中…' : '登录' }}
        </button>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from 'vue'
import { useAuthStore } from '@/stores/auth'
import StatusBar from '@/components/common/StatusBar.vue'

const authStore = useAuthStore()

const REMEMBER_KEY = 'login_remember'

const form = reactive({ account: '', password: '' })
const focusedField = ref('')
const loading = ref(false)
const remember = ref(false)
const showPassword = ref(false)

const canLogin = computed(() => form.account.trim() && form.password.trim())

onMounted(() => {
  const saved = uni.getStorageSync(REMEMBER_KEY)
  if (saved && saved.account) {
    form.account = saved.account
    form.password = saved.password || ''
    remember.value = true
  }
})

async function handleLogin() {
  if (!canLogin.value || loading.value) return
  loading.value = true
  try {
    // 后端按账号识别角色，无需前端选择角色
    await authStore.doLogin(form.account.trim(), form.password)
    if (remember.value) {
      uni.setStorageSync(REMEMBER_KEY, {
        account: form.account.trim(),
        password: form.password
      })
    } else {
      uni.removeStorageSync(REMEMBER_KEY)
    }
    uni.switchTab({ url: '/pages/home/home' })
  } catch (error) {
    // 请求封装已统一 toast，避免重复提示
    console.error('登录失败', error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
/* ==================== 页面外壳：max-w-md 居中 ==================== */
.login-page {
  position: relative;
  max-width: 448px;              /* max-w-md */
  margin: 0 auto;                /* 居中 */
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #f6fbf9;     /* 云雾白兜底 */
}

/* 背景图铺满（竖屏 aspectFill 不变形；fixed+100vh 不依赖父高度） */
.bg-img {
  position: fixed;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 448px;
  height: 100vh;
  z-index: 0;
}

/* 蒙版：顶部提亮、中部通透、底部微压，保证层级清晰 */
.veil {
  position: fixed;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 448px;
  height: 100vh;
  z-index: 1;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.52) 0%,
    rgba(246, 251, 249, 0.18) 38%,
    rgba(246, 251, 249, 0.10) 58%,
    rgba(31, 71, 56, 0.22) 100%
  );
}

/* ==================== 内容层 ==================== */
.content {
  position: relative;
  z-index: 2;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 56px 32px calc(40px + env(safe-area-inset-bottom));
  transform: translateY(-4vh);   /* 整体上移一些 */
}

/* 品牌区 */
.brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 40px;
}

.logo {
  width: 76px;
  height: 76px;
  border-radius: 24px;
  background-color: rgba(255, 255, 255, 0.86);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 28px -8px rgba(54, 143, 111, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

/* 叶子 Logo（CSS 绘制） */
.leaf {
  position: relative;
  width: 38px;
  height: 38px;
  background: linear-gradient(135deg, #a4d898 0%, #368f6f 100%);
  border-radius: 4px 28px 4px 28px;
  transform: rotate(45deg);
}

.leaf-vein {
  position: absolute;
  left: 50%;
  top: 4px;
  bottom: 4px;
  width: 2px;
  margin-left: -1px;
  border-radius: 2px;
  background-color: rgba(255, 255, 255, 0.7);
}

.title {
  margin-top: 22px;
  font-size: 34px;
  font-weight: 800;
  letter-spacing: 6px;
  color: #368f6f;                /* 青山绿 */
}

.subtitle {
  margin-top: 10px;
  font-size: 14px;
  letter-spacing: 4px;
  color: #709484;                /* 晨雾灰绿 */
}

/* ==================== 登录卡片：磨砂玻璃 ==================== */
.login-card {
  background-color: rgba(255, 255, 255, 0.55);  /* 更通透的白色 */
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border: 1px solid rgba(164, 216, 152, 0.35);  /* 嫩芽绿描边 */
  border-radius: 24px;
  padding: 30px 26px 28px;
  box-shadow: 0 18px 50px -12px rgba(54, 143, 111, 0.28); /* 柔和绿色阴影 */
}

.card-title {
  display: block;
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  color: #1f4738;                /* 文字深墨绿 */
  margin-bottom: 26px;
  letter-spacing: 1px;
}

/* ==================== 输入框 ==================== */
.field {
  display: flex;
  align-items: center;
  height: 54px;
  padding: 0 18px;
  margin-bottom: 18px;
  border-radius: 16px;
  background-color: #f6fbf9;     /* 云雾白 */
  border: 1.5px solid rgba(164, 216, 152, 0.45);
  transition: border-color 0.18s ease, box-shadow 0.18s ease, background-color 0.18s ease;
}

/* 浅嫩芽绿聚焦高亮 */
.field-focus {
  background-color: #ffffff;
  border-color: #a4d898;         /* 嫩芽绿 */
  box-shadow: 0 0 0 4px rgba(164, 216, 152, 0.32);
}

.field-icon {
  width: 22px;
  margin-right: 12px;
  font-size: 18px;
  color: #6f9686;
  text-align: center;
}

.field-input {
  flex: 1;
  height: 100%;
  font-size: 15px;
  color: #1f4738;                /* 深墨绿 */
  background-color: transparent;
  border: none;
  outline: none;
}

.field-ph {
  color: #a8bcb3;
  font-size: 15px;
}

.field-eye {
  width: 22px;
  margin-left: 12px;
  font-size: 18px;
  color: #6f9686;
  text-align: center;
}

/* ==================== 记住密码 ==================== */
.remember-row {
  display: flex;
  align-items: center;
  margin: -4px 2px 16px;
}

.checkbox {
  width: 18px;
  height: 18px;
  border-radius: 6px;
  background-color: #ffffff;
  border: 1.5px solid #c4d4cc;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.18s ease, border-color 0.18s ease;
}

.checkbox-checked {
  background-color: #368f6f;       /* 青山绿 */
  border-color: #368f6f;
}

.checkbox-icon {
  font-size: 13px;
  color: #ffffff;
  line-height: 1;
}

.remember-text {
  margin-left: 8px;
  font-size: 14px;
  color: #4d6b5e;
}

/* ==================== 登录按钮 ==================== */
.login-btn {
  height: 54px;
  line-height: 54px;
  margin-top: 10px;
  border: none;
  border-radius: 16px;
  background: linear-gradient(135deg, #368f6f 0%, #5ab8d0 100%); /* 青山绿 → 流水青 */
  color: #ffffff;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 4px;
  box-shadow: 0 14px 28px -10px rgba(54, 143, 111, 0.5);
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.login-btn::after {
  border: none;                  /* 去掉小程序 button 默认边框 */
}

/* 点击反馈（小程序 hover-class + H5 active） */
.login-btn-hover {
  opacity: 0.9;
  transform: scale(0.985);
}

.login-btn:active {
  opacity: 0.9;
  transform: scale(0.985);
}

.login-btn-disabled {
  opacity: 0.55;
  box-shadow: none;
}
</style>
