<template>
  <div class="app-container scroller">
    <PageTitle>
      <template #title>
        <div style="display: flex;align-items: center;">
          <img class="title-icon" :src="pageIcon" alt="">
          <span style="margin-left: 8px;">用户组</span>
        </div>
      </template>

    </PageTitle>

    <div class="app-container__body">
      <query-form
        ref="QueryFormRef"
        v-model="queryParams"
        @query="handleSearch"
        @reset="onReset"
        @handleAdd="handleAdd"
      />


      <div style="flex: 1;">
        <el-table
          :data="groupList"
          stripe
          height="100%"
          style="width: 100%"
        >
        <el-table-column
            type="index"
            label="序号"
            width="80"
            align="center"
            fixed
          />
          <!-- 发现任务名称列（左侧浮动） -->
          <el-table-column 
            prop="groupname" 
            label="用户组名" 
            min-width="180"
            
          >

          </el-table-column>

          <el-table-column 
            prop="remark" 
            label="备注" 
            min-width="180"
          >
          </el-table-column>
          <el-table-column 
            prop="createtime" 
            label="创建时间" 
            min-width="180"
          >
          </el-table-column>


          <el-table-column 
            label="操作" 
            width="320"
            fixed="right"
            align="center"
          >
            <template #default="{ row }">
              <div class="operation-buttons">
                <!-- 编辑按钮 -->
                <el-button 
                  link
                  type="primary" 
                  size="small" 
                  icon="edit"
                  @click="handleEdit(row)"
                  class="op-btn"
                >
                  编辑
                </el-button>
                <el-button 
                  link
                  type="primary" 
                  size="small" 
                  icon="delete"
                  @click="handleDelete(row)"
                  class="op-btn"
                >
                  删除
                </el-button>

    
              </div>
            </template>
          </el-table-column>
        </el-table> 
      </div>




       <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.page"
          v-model:limit="queryParams.rows"
          @pagination="getList"
        />
    </div>
    <group-edit
      v-model:visible="editVisible"
      :form-data="editData"
      @success="handleEditSuccess"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessageBox } from 'element-plus'
import GroupEdit from './edit.vue'
import { query, del } from '@/api/safetyManagement/groupConfig.js'
import QueryForm from './QueryForm.vue'
import pageIcon from '@/assets/images/user.png'
const data = reactive({
  queryParams: {
    page: 1,
    rows: 10,
    groupname: '',
    remark: '',
  }
})
const total = ref(0)

// 表格数据
const { queryParams } = toRefs(data)
const groupList = ref([
  {
    // realname: 'admin',
    // groupname: 'admin',
    // remark: ''
  }
])

function onReset(formQuery) {
  console.log('onReset')
  formQuery.resetFields()
  handleSearch()
}

const editVisible = ref(false)
const editData = ref(null)

const getList = async () => {
  try {
    const res = await query(queryParams.value)
    if (res.code === 'POP_00014') {
      groupList.value = res.data.list || []
      total.value = res.data.total || 0
    }
  } catch (error) {
    console.error('获取用户组列表失败', error)
  }
}

const handleSearch = () => {

  getList()
}
const handleAdd = () => {
  editData.value = null
  editVisible.value = true
}

const handleDelete = async (row) => {
  ElMessageBox.confirm('确认删除该用户组吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    // 调用删除接口
    try {
      const res = await del({ groupid: row.id })
      if (res.code === 'POP_00014') {
        getList()
      }
    } catch (error) {
      console.error('删除用户组失败', error)
    }
  }).catch(() => {})
}
const handleEdit = (row) => {
  editData.value = { ...row }
  editVisible.value = true
}
const handleEditSuccess = () => {
  editVisible.value = false
  getList()
}
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.user-config {
  padding: 20px;

  .title {
    font-size: 18px;
    color: #333;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    
    i {
      color: #409EFF;
      margin-right: 8px;
      font-size: 20px;
    }
  }

  .search-area {
    background: #fff;
    padding: 20px;
    border-radius: 4px;
    margin-bottom: 20px;
    
    .search-form {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .left-area {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
      }

      .right-area {
        margin-left: 20px;
      }
    }
    
    .el-form-item {
      margin-bottom: 0;
    }
  }

  .user-list {
    margin-bottom: 20px;

    .user-grid {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
      gap: 20px;
    }

    .user-card {
      background: #fff;
      border-radius: 12px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.04);
      overflow: hidden;
      position: relative;
      padding: 0;
      border: none;
      transition: box-shadow 0.2s;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      &:hover {
        box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
      }

      &:hover .hover-mask {
        display: flex !important;
      }
    }

    .card-left-bar {
      position: absolute;
      left: 0;
      top: 24px;
      bottom: 24px;
      width: 8px;
      background: #409EFF;
      border-radius: 4px;
    }

    .user-info {
      display: flex;
      align-items: flex-start;
      padding: 24px 24px 0 24px;
      position: relative;
      height: auto;
      cursor: pointer;

      .avatar {
        margin-left: auto;
        margin-right: 0;
        width: 80px;
        height: 80px;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .info {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;

        .name {
          font-size: 15px;
          font-weight: 600;
          color: #409EFF;
          margin-bottom: 12px;
        }

        .remark {
          font-size: 12px;
          color: #666;
          margin-bottom: 8px;
          display: flex;
          align-items: center;
          gap: 6px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          max-width: 300px;
        }
      }
    }

    .card-divider {
      height: 2px;
      background: #f2f6fa;
      margin: 12px 24px 0 24px;
      border-radius: 1px;
    }

    .card-bottom {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 8px 24px 12px 24px;
      color: #909399;
      font-size: 15px;

      .el-icon {
        margin-right: 6px;
      }

      .lock {
        font-size: 18px;
        color: #bdbdbd;
        margin-left: 8px;
      }
    }

    .hover-mask {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.6);
      display: none !important;
      justify-content: center;
      align-items: center;
      z-index: 2;

      .operation-buttons {
        display: flex;
        gap: 80px;
        justify-content: center;
        align-items: center;

        .operation-btn {
          display: flex;
          flex-direction: column;
          align-items: center;
          color: white;
          cursor: pointer;
          background: none;
          border-radius: 0;
          padding: 0;
          box-shadow: none;
          transition: color 0.2s;

          i {
            margin-bottom: 8px;
          }

          span {
            font-weight: 500;
          }

          &:hover {
            color: #409EFF;
          }
        }
      }
    }

    .operation-time {
      color: #666;
      i {
        margin-right: 5px;
      }
    }
  }

  .pagination {
    background: #fff;
    padding: 15px 20px;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .pagination-info {
      color: #666;
      font-size: 14px;
    }
  }
}
</style>
