<template>
  <div class="dashboard-root">
    <!-- 第一行：头像+欢迎语 -->
    <UserGreeting />
    
    <!-- 业务功能区 -->
    <div class="business-area-header">
      <h2 class="business-area-title">常用应用</h2>
      <button class="edit-btn" @click="showSettingDialog = true">
        <svg-icon icon-class="edit" /> 编辑常用
      </button>
    </div>
    <div class="business-modules">
      <div v-for="(module, index) in businessModules" :key="index"
           class="business-module" @click="handleLink(module.link)">
        <div class="business-module-icon">
          <svg-icon :icon-class="module.icon" />
        </div>
        <h3 class="business-module-title">{{ module.title }}</h3>
      </div>
    </div>

    <!-- 全部应用 -->
    <AllApplications @addFavorites="handleAddFavorites" />

    <!-- 设置弹窗 -->
    <div class="dialog-mask" v-if="showSettingDialog">
      <div class="setting-dialog">
        <div class="dialog-header">
          <h3>编辑常用应用</h3>
          <button class="close-btn" @click="showSettingDialog = false">
            <svg-icon icon-class="close" />
          </button>
        </div>
        <div class="dialog-content">
          <div class="empty-tip" v-if="businessModules.length === 0">
            暂无常用应用，可从下方"全部应用"中添加
          </div>
          <ul class="module-list" v-else>
            <li v-for="(module, index) in businessModules" :key="index" class="module-item">
              <div class="module-info">
                <span class="module-index">{{ index + 1 }}</span>
                <span class="module-name">{{ module.title }}</span>
              </div>
              <div class="module-actions">
                <el-button 
                  @click="moveModule(index, 'up')" 
                  :disabled="index === 0"
                  icon="ArrowUp" type="primary"
                >
                </el-button>
                <el-button 
                  @click="moveModule(index, 'down')" 
                  :disabled="index === businessModules.length - 1"
                  icon="ArrowDown" type="primary"
                >
                </el-button>
                <el-button @click="deleteModule(index)" icon="Delete" danger>
                </el-button>
              </div>
            </li>
          </ul>
        </div>
        <div class="dialog-footer">
          <button class="cancel-btn" @click="showSettingDialog = false">取消</button>
          <button class="confirm-btn" @click="showSettingDialog = false">完成</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import AllApplications from '@/components/AllApplications.vue';
import UserGreeting from '@/views/components/Hello.vue';
import { useRouter } from 'vue-router';
import { useStorage } from '@vueuse/core'

// 响应式数据
const businessModules = useStorage('businessModules', [
  {
    title: '考勤日历',
    icon: 'code',
    link: '/people/calendar'
  },
  {
    title: '系统设置',
    icon: 'system',
    link: '/system/menu'
  },
]);
const showSettingDialog = ref(false); // 控制弹窗显示

// 路由实例
const router = useRouter();

// 方法定义
const handleLink = (link) => {
  if (link.startsWith('http') || link.endsWith('.html')) {
    window.open(link, '_blank');
  } else {
    router.push(link);
  }
};

const handleAddFavorites = (appInfo) => {
  // 检查是否已经存在，如果已经存在就移除，否则添加
  const index = businessModules.value.findIndex(module => module.link === appInfo.fullPath)
  if (index !== -1) {
    businessModules.value.splice(index, 1)
  } else {
    businessModules.value.push({
      title: appInfo.child.meta.title,
      icon: appInfo.child.meta.icon,
      link: appInfo.fullPath
    })
  }
};

// 移动模块位置（排序功能）
const moveModule = (index, direction) => {
  // 创建副本避免直接修改源数组导致的响应式问题
  const newModules = [...businessModules.value];
  // 上移：与前一个元素交换位置
  if (direction === 'up' && index > 0) {
    [newModules[index], newModules[index - 1]] = [newModules[index - 1], newModules[index]];
  }
  // 下移：与后一个元素交换位置
  if (direction === 'down' && index < newModules.length - 1) {
    [newModules[index], newModules[index + 1]] = [newModules[index + 1], newModules[index]];
  }
  // 更新响应式数据
  businessModules.value = newModules;
};

// 删除模块
const deleteModule = (index) => {
  if (confirm('确定要移除该常用应用吗？')) {
    businessModules.value.splice(index, 1);
  }
};
</script>

<style scoped>
.dashboard-root {
  min-height: 100vh;
  background: linear-gradient(135deg, #f9fafb 0%, #f3f4f6 100%);
  padding: 32px;
}

/* 业务功能区标题栏 */
.business-area-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.business-area-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.edit-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: #f3f4f6;
  border: none;
  border-radius: 6px;
  color: #6b7280;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
}

.edit-btn:hover {
  background: #e5e7eb;
  color: #374151;
}

/* 业务功能区样式 */
.business-modules {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 24px;
  margin-bottom: 32px;
}

.business-module {
  display: flex;
  align-items: center;
  padding: 16px;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
  transition: all 0.2s;
  cursor: pointer;
}

.business-module:hover {
  box-shadow: 0 4px 16px 0 rgba(0,0,0,0.1);
  transform: translateY(-2px);
}

.business-module-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-right: 12px;
  background: #3b82f6; /* 默认图标背景色 */
}

.business-module-icon i {
  font-size: 20px;
  color: #fff;
}

.business-module-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin: 0;
}

/* 弹窗样式 */
.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.setting-dialog {
  width: 500px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}

.dialog-header {
  padding: 16px 24px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

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

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #999;
  font-size: 18px;
}

.close-btn:hover {
  color: #333;
}

.dialog-content {
  padding: 24px;
  max-height: 400px;
  overflow-y: auto;
}

.module-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.module-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f5f5f5;
}

.module-item:last-child {
  border-bottom: none;
}

.module-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.module-index {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #f3f4f6;
  color: #6b7280;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.module-name {
  font-size: 16px;
  color: #333;
}

.module-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  background: none;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
}

.move-btn {
  color: #9ca3af;
  background: #f9fafb;
}

.move-btn:hover {
  background: #f3f4f6;
  color: #6b7280;
}

.move-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.delete-btn {
  color: #ef4444;
  background: #fee2e2;
}

.delete-btn:hover {
  background: #fecaca;
}

.empty-tip {
  text-align: center;
  padding: 40px 0;
  color: #9ca3af;
  font-size: 14px;
  background: #f9fafb;
  border-radius: 8px;
}

.dialog-footer {
  padding: 16px 24px;
  border-top: 1px solid #f0f0f0;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.cancel-btn, .confirm-btn {
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
}

.cancel-btn {
  background: #f3f4f6;
  border: none;
  color: #6b7280;
}

.cancel-btn:hover {
  background: #e5e7eb;
  color: #374151;
}

.confirm-btn {
  background: #3b82f6;
  border: none;
  color: #fff;
}

.confirm-btn:hover {
  background: #2563eb;
}
</style>