<template>
  <view class="login-container">
    <view class="logo-area">
      <text class="title">居家上门服务</text>
      <text class="subtitle">服务人员端</text>
    </view>
    <view class="form-area">
      <input class="input" v-model="username" placeholder="请输入账号" />
      <input class="input" v-model="password" type="password" placeholder="请输入密码" />
      <button class="login-btn" @click="handleLogin" :disabled="loading">
        {{ loading ? '登录中...' : '登录' }}
      </button>
    </view>
  </view>
</template>

<script>
import { BASE_URL } from '@/common/api.js';
export default {
  data() {
    return { username: '', password: '', loading: false };
  },
  methods: {
    async handleLogin() {
      if (!this.username || !this.password) {
        uni.showToast({ title: '请输入账号密码', icon: 'none' });
        return;
      }
      this.loading = true;
      try {
        const res = await uni.request({
          url: BASE_URL + '/auth/login',
          method: 'POST',
          data: { username: this.username, password: this.password, role: 'STAFF' }
        });
        if (res.data.code === 200) {
          uni.setStorageSync('token', res.data.data.token);
          uni.setStorageSync('userInfo', res.data.data);
          uni.switchTab({ url: '/pages/delivery/index/index' });
        } else {
          uni.showToast({ title: res.data.message, icon: 'none' });
        }
      } catch (e) {
        uni.showToast({ title: '网络错误', icon: 'none' });
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style>
.login-container { padding: 80rpx 60rpx; }
.logo-area { text-align: center; margin-bottom: 80rpx; }
.title { font-size: 48rpx; font-weight: bold; display: block; }
.subtitle { font-size: 28rpx; color: #999; margin-top: 10rpx; }
.input { border: 1px solid #ddd; border-radius: 12rpx; padding: 24rpx; margin-bottom: 24rpx; font-size: 30rpx; }
.login-btn { background: #1677ff; color: #fff; border-radius: 12rpx; height: 88rpx; line-height: 88rpx; font-size: 32rpx; margin-top: 40rpx; }
</style>
