<template>
  <div>
    <div class="header">
      <h1>🐷 猪场管理系统</h1>
      <div class="user-info">
        <span>{{ username }}</span>
        <button class="logout-btn" @click="logout">退出登录</button>
      </div>
    </div>

    <div class="nav">
      <ul>
        <li><router-link to="/dashboard" class="active">控制台</router-link></li>
        <li><router-link to="/device">设备管理</router-link></li>
      </ul>
    </div>

    <div class="container">
      <div class="dashboard-grid">
        <div class="card">
          <h3>📈 环境监控</h3>
          <p>实时监控猪舍环境参数，包括温度、湿度、氨气浓度等关键指标。</p>
        </div>
        
        <div class="card">
          <h3>🔔 告警信息</h3>
          <p>查看系统告警和异常情况，及时处理各种设备故障和环境异常。</p>
        </div>
        
        <div class="card">
          <h3>📊 数据分析</h3>
          <p>分析历史数据，生成趋势图表，为养殖决策提供数据支持。</p>
        </div>
      </div>

      <div class="device-control">
        <h3>⚙️ 设备控制</h3>
        <div class="control-grid">
          <div class="control-item">
            <h4>风机控制</h4>
            <div class="control-buttons">
              <button class="control-btn on-btn" @click="controlDevice('fan', 'on')">开启</button>
              <button class="control-btn off-btn" @click="controlDevice('fan', 'off')">关闭</button>
            </div>
          </div>
          
          <div class="control-item">
            <h4>水帘控制</h4>
            <div class="control-buttons">
              <button class="control-btn on-btn" @click="controlDevice('water_curtain', 'on')">开启</button>
              <button class="control-btn off-btn" @click="controlDevice('water_curtain', 'off')">关闭</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Dashboard',
  data() {
    return {
      username: localStorage.getItem('username') || '管理员'
    }
  },
  methods: {
    logout() {
      // 清除本地存储的认证信息
      localStorage.removeItem('authToken')
      localStorage.removeItem('userId')
      localStorage.removeItem('username')
      
      // 跳转到登录页面
      this.$router.push('/')
    },
    
    controlDevice(deviceType, action) {
      alert(`正在${action === 'on' ? '开启' : '关闭'}${deviceType === 'fan' ? '风机' : '水帘'}`)
      // 这里应该调用实际的设备控制API
    }
  }
}
</script>

<style scoped>
.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header h1 {
  font-size: 24px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.logout-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  padding: 8px 15px;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.nav {
  background: white;
  padding: 15px 20px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.nav ul {
  display: flex;
  list-style: none;
  gap: 20px;
}

.nav a {
  text-decoration: none;
  color: #666;
  padding: 8px 15px;
  border-radius: 5px;
  transition: all 0.3s ease;
}

.nav a:hover, .nav a.active {
  background: #667eea;
  color: white;
}

.container {
  max-width: 1200px;
  margin: 30px auto;
  padding: 0 20px;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.card {
  background: white;
  border-radius: 15px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
  padding: 25px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.12);
}

.card h3 {
  color: #333;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.card p {
  color: #666;
  line-height: 1.6;
}

.device-control {
  background: white;
  border-radius: 15px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
  padding: 25px;
  margin-bottom: 30px;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.control-item {
  background: #f8f9fa;
  border-radius: 10px;
  padding: 20px;
  text-align: center;
}

.control-item h4 {
  margin-bottom: 15px;
  color: #333;
}

.control-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.control-btn {
  padding: 8px 15px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.on-btn {
  background: #28a745;
  color: white;
}

.off-btn {
  background: #dc3545;
  color: white;
}

.control-btn:hover {
  opacity: 0.9;
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .dashboard-grid {
    grid-template-columns: 1fr;
  }
  
  .header {
    flex-direction: column;
    gap: 15px;
  }
}
</style>