<template>
  <div class="login-page">
    <div class="login-panel">
      <div class="login-header">
        <div class="header-mark">S</div>
        <div class="header-right">
          <div class="header-title">SIMATIC L2</div>
          <div class="header-sub">推拉酸洗线 过程控制系统</div>
        </div>
      </div>

      <div class="login-body">
        <div class="field-group">
          <div class="field-label">用户名</div>
          <input v-model="form.username" class="kv-input" autocomplete="username" @keyup.enter="handleLogin" />
        </div>
        <div class="field-group">
          <div class="field-label">密码</div>
          <input v-model="form.password" type="password" class="kv-input" autocomplete="current-password" @keyup.enter="handleLogin" />
        </div>
        <div v-if="error" class="login-error">{{ error }}</div>
        <button class="btn btn-primary fw" style="margin-top:18px;padding:7px;" :disabled="loading" @click="handleLogin">
          {{ loading ? '验证中...' : '登录' }}
        </button>
      </div>

      <div class="login-footer">
        <i class="dot g"></i>系统就绪
        <span style="margin-left:auto;">v1.0.0</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      error: '',
      form: { username: '', password: '' }
    }
  },
  methods: {
    async handleLogin() {
      if (!this.form.username || !this.form.password) {
        this.error = '用户名和密码不能为空'
        return
      }
      this.loading = true
      this.error = ''
      try {
        await this.$store.dispatch('auth/login', this.form)
        this.$router.push('/')
      } catch {
        this.error = '用户名或密码错误'
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/styles/variables';

.login-page {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: $bg-base;
}

.login-panel {
  width: 360px;
  background: $bg-card;
  border: 1px solid $border;
  border-top: 3px solid $sms-teal;
  border-radius: 2px;
  overflow: hidden;
}

.login-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 24px;
  background: $bg-panel;
  border-bottom: 1px solid $border;
}

.header-mark {
  width: 36px;
  height: 36px;
  background: $sms-teal;
  color: $bg-base;
  font-size: 18px;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 2px;
  flex-shrink: 0;
}

.header-title {
  font-size: 14px;
  font-weight: 700;
  color: $text-primary;
  letter-spacing: 2px;
}

.header-sub {
  font-size: 10px;
  color: $text-secondary;
  margin-top: 3px;
  letter-spacing: .3px;
}

.login-body {
  padding: 24px;
}

.field-group {
  margin-bottom: 14px;

  .field-label {
    font-size: 10px;
    font-weight: 700;
    color: $text-secondary;
    text-transform: uppercase;
    letter-spacing: .6px;
    margin-bottom: 6px;
  }

  .kv-input { padding: 7px 9px; font-size: 13px; }
}

.login-error {
  margin-top: 8px;
  font-size: 11px;
  color: $accent-red;
  padding: 5px 8px;
  background: rgba($accent-red, .08);
  border-left: 2px solid $accent-red;
}

.login-footer {
  padding: 8px 24px;
  background: $bg-panel;
  border-top: 1px solid $border;
  font-size: 10px;
  color: $text-muted;
  display: flex;
  align-items: center;
  gap: 6px;
  letter-spacing: .3px;

  .dot {
    width: 5px; height: 5px; border-radius: 50%; display: inline-block;
    &.g { background: $status-run; }
  }
}
</style>
