<template>
  <div class="user-config">
    <!-- 标题 -->
    <div class="title">
    <el-icon><User /></el-icon>
      用户
    </div>

    <!-- 搜索和操作区域 -->
    <div class="search-area">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <div class="left-area">
          <el-form-item label="姓名:">
            <el-input v-model="searchForm.realname" clearable placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="用户名:">
            <el-input v-model="searchForm.username" clearable placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input v-model="searchForm.remark" clearable placeholder="请输入备注"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleSearch">搜索</el-button>
          </el-form-item>
        </div>
        <div class="right-area">
          <el-button type="primary" icon="Plus" @click="handleAdd">新增用户</el-button>
        </div>
      </el-form>
    </div>

    <!-- 用户列表 -->
    <div class="user-list">
      <div class="user-grid">
        <div v-for="(user, index) in userList" :key="index" class="user-card" :class="{ 'admin-card': user.username === 'admin' }">
          <div class="card-header" :class="{ 'admin-header': user.username === 'admin' }">
            <div class="header-info">
              <div class="name">{{ user.realname }}</div>
              <div class="username">{{ user.username }}</div>
            </div>
            <div class="user-avatar">
              <el-avatar :size="50" icon="User" :src="user.avatar"></el-avatar>
            </div>
          </div>
          <div class="card-content">
            <div class="info-row">
              <div class="info-label">备注:</div>
              <div class="info-value">{{ user.remark || '无' }}</div>
            </div>
            <div class="info-row">
              <div class="info-label">创建时间:</div>
              <div class="info-value">{{ user.createtime || '' }}</div>
            </div>
          </div>
          <!-- 遮罩层和操作按钮 -->
          <div class="hover-mask">
            <div class="operation-buttons">
              <div class="operation-btn" @click="handleDelete(user)">
                <el-icon><Delete /></el-icon>
                <span>删除</span>
              </div>
              <div class="operation-btn" @click="handleEdit(user)">
                <el-icon><Edit /></el-icon>
                <span>编辑</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <div class="pagination-info">
        共有记录 {{ total }} 条，每页显示 {{ pageSize }} 条，共 {{ Math.max(1, Math.ceil(total / pageSize)) }} 页
      </div>
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :total="total"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        @current-change="handlePageChange">
      </el-pagination>
    </div>

    <!-- 编辑弹窗 -->
    <user-edit
      v-model:visible="editVisible"
      :form-data="editData"
      @success="handleEditSuccess"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue'
import { ElMessageBox } from 'element-plus'
import { User, Delete, Edit } from '@element-plus/icons-vue'
import UserEdit from './edit.vue'
import { query, remove } from '@/api/safetyManagement/userConfig.js'

// 定义组件名称
defineOptions({
  name: 'UserConfig'
})

// 响应式数据
const searchForm = reactive({
  name: '',
  username: '',
  remark: ''
})
const userList = ref([])
const total = ref(1)
const currentPage = ref(1)
const pageSize = ref(8)
const editVisible = ref(false)
const editData = ref(null)
const instance = getCurrentInstance()

// 获取用户列表
const getList = async () => {
  const params = {
    ...searchForm,
    pageno: currentPage.value,
    pagesize: pageSize.value
  }
  try {
    const res = await query(params)
    if (res.code === 'POP_00014') {
      userList.value = res.data.list || []
      total.value = res.data.total || 0
    } else {
      const modal = instance?.appContext.config.globalProperties.$modal
      modal && modal.msgError ? modal.msgError(res.msg || '查询失败') : alert(res.msg || '查询失败')
    }
  } catch (e) {
    const modal = instance?.appContext.config.globalProperties.$modal
    modal && modal.msgError ? modal.msgError('查询异常') : alert('查询异常')
  }
}

// 处理搜索
const handleSearch = () => {
  currentPage.value = 1
  getList()
}

// 添加用户
const handleAdd = () => {
  editData.value = null
  editVisible.value = true
}

// 分页变化
const handlePageChange = (page) => {
  currentPage.value = page
  getList()
}

// 删除用户
const handleDelete = async (row) => {
  ElMessageBox.confirm('确认删除该用户吗？', '提示', {
    type: 'warning'
  }).then( async () => {
    // 调用删除接口
    console.log('删除用户', row)
    await remove({id: row.id})
     getList()
  }).catch(() => {})
}

// 编辑用户
const handleEdit = async (row) => {
  editData.value = { id: row.id }
  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: 8px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      overflow: hidden;
      position: relative;
      border-left: 4px solid #3498db;
      transition: all 0.3s ease;
      
      &:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        transform: translateY(-2px);
      }
      
      &.admin-card {
        border-left: 4px solid #f39c12;
      }
      
      .card-header {
        padding: 15px 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        background: linear-gradient(to right, #3498db, #73b4f8);
        color: white;
        
        &.admin-header {
          background: linear-gradient(to right, #f39c12, #f8c471);
        }
        
        .header-info {
          flex: 1;
          .name {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
          }
          
          .username {
            font-size: 14px;
            opacity: 0.9;
          }
        }
        
        .user-avatar {
          margin-left: 15px;
        }
      }
      
      .card-content {
        padding: 15px 20px;
        
        .info-row {
          display: flex;
          margin-bottom: 10px;
          
          &:last-child {
            margin-bottom: 0;
          }
          
          .info-label {
            color: #666;
            font-size: 14px;
            width: 80px;
            flex-shrink: 0;
          }
          
          .info-value {
            color: #333;
            font-size: 14px;
            flex: 1;
          }
        }
      }

      .hover-mask {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.6);
        display: none;
        justify-content: center;
        align-items: center;
        transition: all 0.3s;

        .operation-buttons {
          display: flex;
          gap: 30px;

          .operation-btn {
            display: flex;
            flex-direction: column;
            align-items: center;
            color: white;
            cursor: pointer;
            
            .el-icon {
              // font-size: 24px;
              margin-bottom: 8px;
            }

            // span {
            //   font-size: 14px;
            // }

            &:hover {
              color: #409EFF;
              transform: scale(1.1);
            }
          }
        }
      }

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

  .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>
