
<template>
  <div class="app-container">
    <!-- 头部区域 -->
    <!-- <header class="app-header">
      <h1 class="title">
        <i class="el-icon-menu"></i>
        
      </h1>
    </header> -->


    <!-- 搜索区域 -->
    <!-- <div class="search-container">
      <el-input
        v-model="searchQuery"
        placeholder="搜索应用..."
        clearable
        prefix-icon="el-icon-search"
        class="search-input"
      ></el-input>
    </div> -->

    <!-- 空状态 -->
    <div v-if="Object.keys(filteredCategories).length === 0" class="empty-state">
      <i class="el-icon-warning empty-icon"></i>
      <h3>未找到匹配的应用</h3>
      <p>请尝试调整搜索关键词</p>
    </div>

    <!-- 分类应用展示 -->
    <div
v-for="(category, categoryName) in filteredCategories" 
         :key="categoryName" 
         class="category-section">
      <div class="category-header">
        <h2 class="category-title">{{ categoryName }}</h2>
        <el-tag size="small" class="app-count">{{ category.apps.length }} 个应用</el-tag>
      </div>
      
      <div class="apps-grid">
        <div
v-for="app in category.apps"
             :key="app.id"
             class="app-card"
             @click="handleAppClick(app)">
          <div class="app-icon-wrapper">
            <img :src="app.logo" class="app-icon-img" />
          </div>
          <div class="app-name">{{ app.name }}</div>
        </div>
      </div>
    </div>

    <!-- 应用详情对话框 -->
    <el-dialog
      v-model:visible="dialogVisible"
      title="应用详情"
      width="30%"
      custom-class="app-dialog">
      <div v-if="selectedApp" class="dialog-content">
        <div class="dialog-icon-wrapper">
          <img :src="selectedApp.logo" class="dialog-app-icon-img" />
        </div>
        <h3 class="dialog-app-name">{{ selectedApp.name }}</h3>
        <p class="dialog-app-id">ID: {{ selectedApp.id }}</p>
        <p class="dialog-app-category" v-if="selectedApp.category">分类: {{ selectedApp.category }}</p>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmLaunch">启动</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import * as ClientApi from '@/api/system/oauth2/client'
import { DICT_TYPE, getDictLabel } from '@/utils/dict'
export default {
  name: 'AppManager',
  data() {
    return {
      searchQuery: '',
      dialogVisible: false,
      selectedApp: null,
      appsData: []
    };
  },
  mounted() {
    this.fetchApps()
  },
  computed: {
    categorizedApps() {
      const result = {};
      this.appsData.forEach(app => {
        const categoryLabel = app.categoryLabel || '未分类'
        if (!result[categoryLabel]) {
          result[categoryLabel] = { apps: [] }
        }
        result[categoryLabel].apps.push(app)
      })
      return result;
    },
    filteredCategories() {
      if (!this.searchQuery.trim()) {
        return this.categorizedApps;
      }
      
      const query = this.searchQuery.toLowerCase();
      const result = {};
      
      for (const [categoryName, categoryData] of Object.entries(this.categorizedApps)) {
        const filteredApps = categoryData.apps.filter(app => 
          app.name.toLowerCase().includes(query) || 
          String(app.categoryLabel || '').toLowerCase().includes(query)
        );
        
        if (filteredApps.length > 0) {
          result[categoryName] = {...categoryData, apps: filteredApps};
        }
      }
      
      return result;
    },
    totalApps() {
      return this.appsData.length;
    },
    categoriesCount() {
      return Object.keys(this.categorizedApps).length;
    },
    visibleCategories() {
      return Object.keys(this.filteredCategories).length;
    }
  },
  methods: {
    async fetchApps() {
      try {
        const data = await ClientApi.getOAuth2ClientPage({ pageNo: 1, pageSize: 1000 })
        const list = data?.list || []
        this.appsData = list.map(item => ({
          id: item.id,
          name: item.name,
          logo: item.logo,
          category: item.category,
          categoryLabel: getDictLabel(DICT_TYPE.APP_CATEGORY, item.category),
          redirectUris: item.redirectUris
        }))
      } catch (e) {
        this.$message.error('应用数据加载失败')
      }
    },
    handleAppClick(app) {
      const uri = Array.isArray(app.redirectUris) ? app.redirectUris[0] : app.redirectUris
      if (uri) {
        window.location.href = uri
      } else {
        this.$message.warning('未配置重定向地址')
      }
    },
    confirmLaunch() {
      this.$message.success(`正在启动 ${this.selectedApp.name}...`);
      this.dialogVisible = false;
    }
  }
};
</script>

<style scoped>


/* 头部样式 */
.app-header {
  padding: 30px 0;
  color: white;
  text-align: center;
}

.title {
  margin-bottom: 10px;
  font-size: 2.5rem;
  text-shadow: 0 2px 4px rgb(0 0 0 / 10%);
}

.subtitle {
  font-size: 1.1rem;
  opacity: 0.9;
}

/* 统计信息样式 */
.stats-container {
  display: flex;
  padding: 20px;
  margin-bottom: 30px;
  background: rgb(255 255 255 / 20%);
  border-radius: 15px;
  justify-content: space-around;
  backdrop-filter: blur(10px);
}

.stat-card {
  color: white;
  text-align: center;
}

.stat-value {
  margin-bottom: 5px;
  font-size: 2rem;
  font-weight: bold;
}

.stat-label {
  font-size: 0.9rem;
  opacity: 0.9;
}

/* 搜索区域样式 */
.search-container {
  max-width: 500px;
  margin: 0 auto 30px;
}

.search-input ::v-deep .el-input__inner {
  padding-left: 40px;
  border-radius: 25px;
}

/* 空状态样式 */
.empty-state {
  padding: 60px 20px;
  margin-bottom: 30px;
  color: rgb(255 255 255 / 80%);
  text-align: center;
  background: rgb(255 255 255 / 10%);
  border-radius: 15px;
}

.empty-icon {
  display: block;
  margin-bottom: 15px;
  font-size: 3rem;
}

/* 分类区域样式 */
.category-section {
  padding: 25px;
  margin-bottom: 30px;
  background: white;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 10%);
  animation: fadeIn 0.5s ease-out;
}

.category-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.category-title {
  padding-left: 15px;
  margin: 0;
  font-size: 1.5rem;
  color: #303133;
  border-left: 5px solid #409EFF;
}

.app-count {
  color: #409eff;
  background: #ecf5ff;
}

/* 应用网格样式 */
.apps-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 25px;
}

.app-card {
  padding: 20px 15px;
  text-align: center;
  cursor: pointer;
  background: #f8f9fa;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgb(0 0 0 / 5%);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.app-card:hover {
  color: white;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  transform: translateY(-5px);
  box-shadow: 0 10px 25px rgb(64 158 255 / 20%);
}

.app-icon-wrapper {
  display: flex;
  width: 60px;
  height: 60px;
  margin: 0 auto 12px;
  font-size: 28px;
  line-height: 60px;
  color: white;
  border-radius: 50%;
  align-items: center;
  justify-content: center;
}

.app-icon-img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
}

.app-name {
  overflow: hidden;
  font-size: 14px;
  font-weight: 500;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 对话框样式 */
::v-deep .app-dialog .el-dialog__body {
  padding: 30px 20px;
}

.dialog-content {
  text-align: center;
}

.dialog-icon-wrapper {
  display: flex;
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  font-size: 36px;
  line-height: 80px;
  color: white;
  border-radius: 50%;
  align-items: center;
  justify-content: center;
}

.dialog-app-icon-img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
}

.dialog-app-name {
  margin: 0 0 10px;
  font-size: 1.5rem;
}

.dialog-app-id,
.dialog-app-category {
  margin: 5px 0;
  color: #909399;
}

.dialog-footer {
  text-align: right;
}
</style>
