<template>
  <div id="app">
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header height="60px" class="app-header">
        <div class="header-left">
          <!-- 移动端菜单按钮 -->
          <el-button 
            v-if="isMobile" 
            type="text" 
            class="menu-toggle-btn"
            @click="drawerVisible = true"
          >
            <i class="el-icon-s-fold" style="font-size: 24px; color: white;"></i>
          </el-button>
          <h1 class="app-title">租房管理系统</h1>
        </div>
        <el-button type="primary" plain size="small" @click="logout">退出</el-button>
      </el-header>
      
      <el-container>
        <!-- PC端侧边栏 -->
        <el-aside 
          v-if="!isMobile" 
          width="200px" 
          class="app-aside"
        >
          <el-menu
            :default-active="activeIndex"
            class="el-menu-vertical-demo"
            @select="handleSelect"
            background-color="#f0f2f5"
            text-color="#333"
            active-text-color="#409EFF"
          >
            <el-menu-item index="dashboard">
              <i class="el-icon-s-home"></i>
              <span>首页</span>
            </el-menu-item>
            <el-menu-item index="region-list">
              <i class="el-icon-location"></i>
              <span>区域管理</span>
            </el-menu-item>
            <el-menu-item index="apartment-list">
              <i class="el-icon-office-building"></i>
              <span>公寓管理</span>
            </el-menu-item>
            <el-menu-item index="room-list">
              <i class="el-icon-menu"></i>
              <span>房间管理</span>
            </el-menu-item>
            <el-menu-item index="rental-list">
              <i class="el-icon-key"></i>
              <span>租房管理</span>
            </el-menu-item>
            <el-menu-item index="rental-archive">
              <i class="el-icon-document"></i>
              <span>租赁档案</span>
            </el-menu-item>
            <el-menu-item index="water-archive">
              <i class="el-icon-document"></i>
              <span>水费档案</span>
            </el-menu-item>
            <el-menu-item index="rent-statistics">
              <i class="el-icon-data-analysis"></i>
              <span>租金统计</span>
            </el-menu-item>
            <el-menu-item index="room-statistics">
              <i class="el-icon-data-analysis"></i>
              <span>房间状态统计</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        
        <!-- 主内容区 -->
        <el-main class="app-main">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
    
    <!-- 移动端侧边栏抽屉 -->
    <el-drawer
      :visible.sync="drawerVisible"
      :with-header="false"
      :size="drawerWidth"
      direction="ltr"
      class="mobile-drawer"
    >
      <div class="drawer-header">
        <h3>菜单</h3>
        <el-button type="text" @click="drawerVisible = false">
          <i class="el-icon-close" style="font-size: 20px;"></i>
        </el-button>
      </div>
      <el-menu
        :default-active="activeIndex"
        class="el-menu-vertical-demo"
        @select="handleMobileSelect"
        background-color="#fff"
        text-color="#333"
        active-text-color="#409EFF"
      >
        <el-menu-item index="dashboard">
          <i class="el-icon-s-home"></i>
          <span>首页</span>
        </el-menu-item>
        <el-menu-item index="region-list">
          <i class="el-icon-location"></i>
          <span>区域管理</span>
        </el-menu-item>
        <el-menu-item index="apartment-list">
          <i class="el-icon-office-building"></i>
          <span>公寓管理</span>
        </el-menu-item>
        <el-menu-item index="room-list">
          <i class="el-icon-menu"></i>
          <span>房间管理</span>
        </el-menu-item>
        <el-menu-item index="rental-list">
          <i class="el-icon-key"></i>
          <span>租房管理</span>
        </el-menu-item>
        <el-menu-item index="rental-archive">
          <i class="el-icon-document"></i>
          <span>租赁档案</span>
        </el-menu-item>
        <el-menu-item index="water-archive">
          <i class="el-icon-document"></i>
          <span>水费档案</span>
        </el-menu-item>
        <el-menu-item index="rent-statistics">
          <i class="el-icon-data-analysis"></i>
          <span>租金统计</span>
        </el-menu-item>
        <el-menu-item index="room-statistics">
          <i class="el-icon-data-analysis"></i>
          <span>房间状态统计</span>
        </el-menu-item>
      </el-menu>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      activeIndex: 'dashboard',
      isMobile: false,
      drawerVisible: false,
      drawerWidth: '70%'
    }
  },
  mounted() {
    this.checkDevice()
    window.addEventListener('resize', this.checkDevice)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkDevice)
  },
  methods: {
    checkDevice() {
      const width = window.innerWidth
      this.isMobile = width <= 768
      // 根据屏幕宽度调整抽屉大小
      if (width <= 375) {
        this.drawerWidth = '80%'
      } else if (width <= 768) {
        this.drawerWidth = '70%'
      }
    },
    handleSelect(key, keyPath) {
      this.activeIndex = key
      this.navigateToRoute(key)
    },
    handleMobileSelect(key, keyPath) {
      this.activeIndex = key
      this.drawerVisible = false
      this.navigateToRoute(key)
    },
    navigateToRoute(key) {
      const routeMap = {
        'dashboard': '/',
        'region-list': '/region/list',
        'region-add': '/region/add',
        'apartment-list': '/apartment/list',
        'apartment-add': '/apartment/add',
        'room-list': '/room/list',
        'room-add': '/room/add',
        'rental-list': '/rental/list',
        'rental-add': '/rental/add',
        'rental-archive': '/rental/archive',
        'water-archive': '/water/archive',
        'rent-statistics': '/statistics/rent',
        'room-statistics': '/statistics/room'
      }
      if (routeMap[key] && this.$route.path !== routeMap[key]) {
        this.$router.push(routeMap[key])
      }
    },
    logout() {
      this.$message.success('退出成功')
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}

/* 顶部导航栏样式 */
.app-header {
  background-color: #333;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.menu-toggle-btn {
  padding: 0;
  margin: 0;
}

.app-title {
  margin: 0;
  font-size: 18px;
}

/* 侧边栏样式 */
.app-aside {
  background-color: #f0f2f5;
  min-height: calc(100vh - 60px);
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

/* 主内容区样式 */
.app-main {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

/* 移动端抽屉样式 */
.mobile-drawer .el-drawer__body {
  padding: 0;
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
  background-color: #f5f7fa;
}

.drawer-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .app-header {
    padding: 0 15px;
  }
  
  .app-title {
    font-size: 16px;
  }
  
  .app-main {
    padding: 10px;
  }
  
  /* 表格横向滚动 */
  .el-table {
    width: 100%;
    overflow-x: auto;
  }
  
  .el-table__body-wrapper {
    overflow-x: auto;
  }
  
  /* 表单元素适配 */
  .el-form-item {
    margin-bottom: 15px;
  }
  
  .el-form-item__label {
    float: none;
    display: block;
    text-align: left;
    padding: 0 0 8px;
    line-height: 1.5;
  }
  
  .el-form-item__content {
    margin-left: 0 !important;
  }
  
  /* 搜索表单适配 */
  .search-form .el-form-item {
    display: block;
    margin-right: 0;
  }
  
  .search-form .el-form-item__content {
    width: 100%;
  }
  
  .search-form .el-input,
  .search-form .el-select {
    width: 100%;
  }
  
  /* 卡片适配 */
  .el-card {
    margin-bottom: 10px;
  }
  
  .el-card__header {
    padding: 12px 15px;
  }
  
  .el-card__body {
    padding: 15px;
  }
  
  /* 分页适配 */
  .el-pagination {
    text-align: center;
    padding: 10px 0;
  }
  
  .el-pagination .el-pagination__total,
  .el-pagination .el-pagination__sizes,
  .el-pagination .el-pagination__jump {
    display: none;
  }
  
  /* 按钮组适配 */
  .el-button + .el-button {
    margin-left: 5px;
  }
  
  /* 对话框适配 */
  .el-dialog {
    width: 90% !important;
    margin-top: 10vh !important;
  }
  
  .el-dialog__body {
    padding: 15px;
  }
}

/* 小屏幕手机适配 */
@media screen and (max-width: 375px) {
  .app-title {
    font-size: 14px;
  }
  
  .app-main {
    padding: 8px;
  }
  
  .el-card__body {
    padding: 12px;
  }
}
</style>
