<template>
  <div class="app-group-container">
    <!-- 过滤器部分 -->
    <!-- <div class="filter-section">
      <el-form :inline="true" :model="condition" class="filter-form">
        <el-form-item label="产品组名称">
          <el-input
            v-model="condition.name"
            placeholder="请输入产品组名称"
            class="filter-input"
            @change="fetchData">
          </el-input>
        </el-form-item>
      </el-form>
    </div> -->

    <!-- <el-divider></el-divider> -->

    <!-- 表格头部操作 -->
    <div class="header-actions">
      <el-button type="primary" @click="showAddDialog">新增产品组</el-button>
      <div class="header-right">
        <el-button icon="el-icon-refresh" @click="fetchData">刷新</el-button>
      </div>
    </div>

    <!-- 主表格 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%;"
      v-loading="loading">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="名称" width="250"></el-table-column>
      <el-table-column label="产品" min-width="300">
        <template slot-scope="scope">
          <div v-for="(pkg, index) in scope.row.pkgs || []" :key="index" class="app-item">
            {{ getAppName(pkg) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
        <template slot-scope="scope">
          {{ formatDate(scope.row.createdAt) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="total"
        layout="total, prev, pager, next, sizes">
      </el-pagination>
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" ref="form" label-width="120px">
        <el-form-item label="产品组名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item label="选择产品" prop="pkgs">
          <app-transfer v-model="form.pkgs" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AppTransfer from '@/components/AppTransfer'
import { getAppGroupList, createAppGroup, updateAppGroup, deleteAppGroup, getAllApps } from '@/api/appGroup'

export default {
  name: 'AppGroupManage',
  components: {
    AppTransfer
  },
  data() {
    return {
      condition: {
        name: ''
      },
      loading: false,
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      dialogTitle: '',
      form: {
        name: '',
        pkgs: []
      },
      rules: {
        name: [
          { required: true, message: '请输入产品组名称', trigger: 'blur' }
        ],
        pkgs: [
          { type: 'array', required: true, message: '请至少选择一个产品', trigger: 'change' }
        ]
      },
      appMap: new Map() // 存储包名和产品名称的映射
    }
  },
  created() {
    this.fetchData()
    this.fetchApps()
  },
  methods: {
    async fetchData() {
      this.loading = true
      try {
        const response = await getAppGroupList({
          page: this.currentPage,
          size: this.pageSize,
          name: this.condition.name
        })
        
        if (response && Array.isArray(response)) {
          this.tableData = response
          this.total = response.length
        } else if (response && response.status === 200 && response.result) {
          this.tableData = response.result.data || []
          this.total = response.result.total || 0
        } else {
          this.$message.error('获取数据失败')
        }
      } catch (error) {
        console.error('获取数据失败:', error)
        this.$message.error('获取数据失败')
      }
      this.loading = false
    },

    async fetchApps() {
      try {
        const response = await getAllApps()
        console.log('产品列表响应:', response)
        
        if (response.status === 200 && response.result && response.result.data) {
          const apps = response.result.data
          console.log('产品列表:', apps)
          
          // 清空当前映射
          this.appMap.clear()
          
          // 遍历数组并正确存储每个产品的包名和名称
          apps.forEach(app => {
            if (app && app.pkg && app.appName) {
              console.log('添加产品映射:', app.pkg, app.appName)
              this.appMap.set(app.pkg, app.appName)
            }
          })
        }
      } catch (error) {
        console.error('获取产品列表失败:', error)
      }
    },

    getAppName(pkg) {
      return this.appMap.get(pkg) || pkg
    },

    formatDate(timestamp) {
      if (!timestamp) return ''
      const date = new Date(timestamp)
      
      // u683cu5f0fu5316u4e3a YYYY-MM-DD HH:MM:SS
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      const seconds = String(date.getSeconds()).padStart(2, '0')
      
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },

    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.fetchData()
    },

    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },

    showAddDialog() {
      this.dialogTitle = '新增产品组'
      this.form = {
        name: '',
        pkgs: []
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate()
      })
    },

    handleEdit(row) {
      this.dialogTitle = '编辑产品组'
      this.form = {
        id: row.id,
        name: row.name,
        pkgs: row.pkgs || []
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate()
      })
    },

    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该产品组吗？', '提示', {
          type: 'warning'
        })
        
        const response = await deleteAppGroup(row.id)
        if (response.status === 200) {
          this.$message.success('删除成功')
          this.fetchData()
        } else {
          this.$message.error(response.msg || '删除失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除失败:', error)
          this.$message.error('删除失败')
        }
      }
    },

    async submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const formData = {
            ...this.form
          }

          try {
            let response
            if (formData.id) {
              response = await updateAppGroup(formData)
            } else {
              response = await createAppGroup(formData)
            }

            if (response.status === 200) {
              this.$message.success(formData.id ? '更新成功' : '创建成功')
              this.dialogVisible = false
              this.fetchData()
            } else {
              this.$message.error(response.msg || (formData.id ? '更新失败' : '创建失败'))
            }
          } catch (error) {
            console.error(formData.id ? '更新失败:' : '创建失败:', error)
            this.$message.error(formData.id ? '更新失败' : '创建失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.app-group-container {
  padding: 20px;
}

.filter-section {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
}

.filter-form {
  display: flex;
  flex-wrap: wrap;
}

.filter-input {
  width: 200px;
}

.header-actions {
  margin: 20px 0;
  display: flex;
  justify-content: space-between;
}

.header-right {
  display: flex;
  gap: 10px;
}

.app-item {
  margin-bottom: 5px;
  line-height: 1.5;
}

.el-table--enable-row-hover .el-table__body tr:hover > td {
  background-color: #f5f7fa;
}

.el-button--primary {
  background-color: #1890ff;
  border-color: #1890ff;
}

.el-button--primary:hover,
.el-button--primary:focus {
  background-color: #40a9ff;
  border-color: #40a9ff;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  padding: 10px 0;
}

.el-pagination {
  font-weight: normal;
}

.el-pagination .el-select .el-input {
  width: 110px;
}

.el-pagination .el-select .el-input__inner {
  padding-right: 25px;
}

.el-pagination .btn-prev,
.el-pagination .btn-next {
  background: transparent;
}

.el-pagination .el-pager li {
  background: transparent;
  border: none;
}

.el-pagination .el-pager li.active {
  color: #1890ff;
  background-color: #e6f7ff;
  border-radius: 2px;
}
</style>
