<template>
  <view class="login-page">
    <view class="login-content">
      <!-- Logo 区域 -->
      <view class="logo-section">
        <view class="logo">
          <view class="logo-icon">
            <view class="logo-leaf"></view>
            <view class="logo-leaf right"></view>
          </view>
        </view>
        <text class="brand-name">哈哈运营平台</text>
        <text class="brand-subtitle">欢迎回来，请登录您的账户</text>
      </view>
      
      <!-- 登录表单 -->
      <view class="login-card">
        <view class="form-group">
          <text class="form-label">用户名</text>
          <view class="input-wrapper">
            <view class="input-icon user"></view>
            <input 
              class="form-input" 
              type="text" 
              v-model="formData.username" 
              placeholder="请输入用户名"
              placeholder-class="input-placeholder"
            />
          </view>
        </view>
        
        <view class="form-group">
          <text class="form-label">密码</text>
          <view class="input-wrapper">
            <view class="input-icon lock"></view>
            <input 
              class="form-input" 
              :password="!showPassword" 
              v-model="formData.password" 
              placeholder="请输入密码"
              placeholder-class="input-placeholder"
            />
            <view class="input-action" @click="showPassword = !showPassword">
              <view :class="['eye-icon', { active: showPassword }]"></view>
            </view>
          </view>
        </view>
        
        <button class="login-btn" :loading="loading" :disabled="loading" @click="handleLogin">
          <text v-if="!loading">立即登录</text>
          <text v-else>登录中...</text>
        </button>
        
        <view class="login-tips">
          <view class="tips-icon"></view>
          <text class="tips-label">测试账号</text>
          <text class="tips-value">admin / 123456</text>
        </view>
      </view>
    </view>
    
    <!-- 底部 -->
    <view class="login-footer">
      <text>© 2025 哈哈零售 All Rights Reserved</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { login } from '@/api/auth';

const formData = reactive({
  username: '',
  password: ''
});

const showPassword = ref(false);
const loading = ref(false);

const handleLogin = async () => {
  if (!formData.username) {
    uni.showToast({ title: '请输入用户名', icon: 'none' });
    return;
  }
  if (!formData.password) {
    uni.showToast({ title: '请输入密码', icon: 'none' });
    return;
  }
  
  loading.value = true;
  try {
    await login(formData);
    uni.showToast({ title: '登录成功', icon: 'success' });
    setTimeout(() => {
      uni.switchTab({ url: '/pages/index/index' });
    }, 500);
  } catch (error: any) {
    uni.showToast({ title: error.message || '登录失败', icon: 'none' });
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: #f8fafc;
  display: flex;
  flex-direction: column;
  padding-top: constant(safe-area-inset-top);
  padding-top: env(safe-area-inset-top);
}

.login-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 40rpx;
}

/* Logo 区域 */
.logo-section {
  text-align: center;
  margin-bottom: 48rpx;
  
  .logo {
    width: 120rpx;
    height: 120rpx;
    background: #10b981;
    border-radius: 32rpx;
    margin: 0 auto 28rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    
    .logo-icon {
      position: relative;
      width: 48rpx;
      height: 48rpx;
      
      .logo-leaf {
        position: absolute;
        width: 24rpx;
        height: 40rpx;
        background: #ffffff;
        border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
        left: 0;
        top: 4rpx;
        transform: rotate(-20deg);
        
        &.right {
          left: auto;
          right: 0;
          transform: rotate(20deg);
        }
      }
    }
  }
  
  .brand-name {
    display: block;
    font-size: 44rpx;
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 12rpx;
    letter-spacing: 2rpx;
  }
  
  .brand-subtitle {
    display: block;
    font-size: 28rpx;
    color: #64748b;
  }
}

/* 登录卡片 */
.login-card {
  background: #ffffff;
  border: 1rpx solid #e2e8f0;
  border-radius: 24rpx;
  padding: 40rpx 32rpx;
}

.form-group {
  margin-bottom: 28rpx;
  
  .form-label {
    display: block;
    font-size: 26rpx;
    color: #475569;
    margin-bottom: 12rpx;
    font-weight: 500;
  }
}

.input-wrapper {
  display: flex;
  align-items: center;
  background: #f8fafc;
  border: 2rpx solid #e2e8f0;
  border-radius: 16rpx;
  padding: 0 20rpx;
  transition: all 0.2s;
  
  &:focus-within {
    border-color: #10b981;
    background: #ffffff;
    box-shadow: 0 0 0 4rpx rgba(16, 185, 129, 0.1);
  }
  
  .input-icon {
    width: 40rpx;
    height: 40rpx;
    margin-right: 12rpx;
    flex-shrink: 0;
    
    &.user {
      position: relative;
      
      &::before {
        content: '';
        position: absolute;
        top: 6rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 16rpx;
        height: 16rpx;
        background: #94a3b8;
        border-radius: 50%;
      }
      
      &::after {
        content: '';
        position: absolute;
        bottom: 4rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 26rpx;
        height: 14rpx;
        background: #94a3b8;
        border-radius: 14rpx 14rpx 0 0;
      }
    }
    
    &.lock {
      position: relative;
      
      &::before {
        content: '';
        position: absolute;
        top: 4rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 18rpx;
        height: 14rpx;
        border: 3rpx solid #94a3b8;
        border-bottom: none;
        border-radius: 16rpx 16rpx 0 0;
      }
      
      &::after {
        content: '';
        position: absolute;
        bottom: 4rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 22rpx;
        height: 18rpx;
        background: #94a3b8;
        border-radius: 4rpx;
      }
    }
  }
  
  .form-input {
    flex: 1;
    height: 100rpx;
    font-size: 30rpx;
    color: #1e293b;
  }
  
  .input-action {
    padding: 16rpx;
    margin: -16rpx;
    margin-left: 0;
    
    .eye-icon {
      width: 40rpx;
      height: 28rpx;
      border: 3rpx solid #94a3b8;
      border-radius: 50%;
      position: relative;
      
      &::before {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 12rpx;
        height: 12rpx;
        background: #94a3b8;
        border-radius: 50%;
      }
      
      &.active::before {
        background: #10b981;
      }
      
      &.active {
        border-color: #10b981;
      }
    }
  }
}

.input-placeholder {
  color: #cbd5e1;
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 100rpx;
  background: #10b981;
  color: #fff;
  font-size: 32rpx;
  font-weight: 600;
  border-radius: 16rpx;
  margin-top: 20rpx;
  border: none;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  
  &::after {
    border: none;
  }
  
  &:active {
    background: #059669;
    transform: scale(0.98);
  }
  
  &[disabled] {
    opacity: 0.6;
  }
}

.login-tips {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 28rpx;
  padding: 20rpx;
  background: #ecfdf5;
  border-radius: 12rpx;
  
  .tips-icon {
    width: 32rpx;
    height: 32rpx;
    background: #10b981;
    border-radius: 50%;
    margin-right: 12rpx;
    position: relative;
    
    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 12rpx;
      height: 4rpx;
      background: #fff;
      border-radius: 2rpx;
    }
    
    &::after {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 4rpx;
      height: 12rpx;
      background: #fff;
      border-radius: 2rpx;
    }
  }
  
  .tips-label {
    font-size: 24rpx;
    color: #64748b;
    margin-right: 12rpx;
  }
  
  .tips-value {
    font-size: 24rpx;
    color: #10b981;
    font-weight: 600;
  }
}

/* 底部 */
.login-footer {
  padding: 40rpx 0;
  text-align: center;
  
  text {
    font-size: 24rpx;
    color: #cbd5e1;
  }
}
</style>
