<template>
  <div class="device-home app-container">
    <div class="device-home-header">
      <h3 class="title">设备功能</h3>
      <div class="device-meta">
        <el-tag size="small" type="success">已连接设备</el-tag>
        <span>SN {{ deviceSn }}</span>
        <span v-if="deviceInfo.imei" class="imei">IMEI {{ deviceInfo.imei }}</span>
      </div>
    </div>

    <el-tabs v-model="activeTab" class="device-menu-tabs">
      <el-tab-pane
        v-for="(tabName, tabIndex) in tabNames"
        :key="tabName"
        :label="tabName"
        :name="String(tabIndex)"
      >
        <el-row :gutter="16" class="demo-grid">
          <el-col
            v-for="demo in demosByTab[tabIndex]"
            :key="demo.name"
            :xs="12"
            :sm="8"
            :md="6"
            :lg="4"
          >
            <div
              class="demo-card"
              :class="{ disabled: !demo.enabled }"
              :style="{ '--card-color': demo.color }"
              @click="openDemo(demo)"
            >
              <div class="demo-icon">
                <svg-icon :icon-class="demo.icon" />
              </div>
              <div class="demo-name">{{ demo.name }}</div>
              <div v-if="!demo.enabled" class="demo-badge">敬请期待</div>
            </div>
          </el-col>
          <el-col v-if="!demosByTab[tabIndex].length" :span="24">
            <el-empty description="暂无功能" :image-size="80" />
          </el-col>
        </el-row>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { DEVICE_TAB_NAMES, DEVICE_DEMOS } from './deviceMenus'
import { buildDeviceSession, saveTsbDeviceSession } from '@/utils/tsbDeviceSession'

export default {
  name: 'TsbDeviceHome',
  data() {
    return {
      activeTab: '0',
      tabNames: DEVICE_TAB_NAMES,
      demosByTab: DEVICE_DEMOS
    }
  },
  computed: {
    deviceSn() {
      return this.$store.getters.tsbCurrentDeviceSn
    },
    deviceInfo() {
      const list = this.$store.getters.tsbOpenedDevices || []
      return list.find(d => String(d.deviceSn) === String(this.deviceSn)) || {}
    }
  },
  methods: {
    openDemo(demo) {
      if (!demo.enabled || !demo.route) {
        this.$message.info('功能开发中，敬请期待')
        return
      }
      const sn = this.deviceSn
      if (sn == null) {
        return
      }
      this.$store.commit('tsb/SET_DEVICE_PANEL', { deviceSn: sn, panel: demo.route })
      if (demo.route === 'opw') {
        this.$store.commit('tsb/SET_OPW_SUB_PAGE', { deviceSn: sn, subPage: 'main' })
      }
      saveTsbDeviceSession(buildDeviceSession(
        this.$store.getters.tsbCurrentDevice,
        this.$store.getters.tsbOpenedDevices,
        this.$store.state.tsb.devicePanelMap
      ))
    }
  }
}
</script>

<style scoped lang="scss">
.device-home-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  flex-wrap: wrap;
  gap: 8px;
}
.title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}
.device-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #606266;
  font-size: 13px;
}
.imei {
  color: #909399;
}
.device-menu-tabs {
  ::v-deep .el-tabs__header {
    margin-bottom: 16px;
  }
}
.demo-grid {
  min-height: 200px;
}
.demo-card {
  position: relative;
  margin-bottom: 16px;
  padding: 20px 12px 16px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--card-color), rgba(255, 255, 255, 0.15));
  color: #fff;
  text-align: center;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  &:hover:not(.disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  }
  &.disabled {
    cursor: not-allowed;
    opacity: 0.55;
  }
}
.demo-icon {
  font-size: 28px;
  margin-bottom: 8px;
}
.demo-name {
  font-size: 14px;
  font-weight: 500;
}
.demo-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
}
</style>
