<template>
  <div class="app-container participants-page">
    <el-card class="box-card border-none main-card" shadow="never">
      <!-- 1. 顶部操作栏 -->
      <div class="toolbar mb20">
        <div class="left-actions">
          <div class="search-wrapper">
            <el-input
              v-model="queryParams.kwd"
              placeholder="请输入"
              clearable
              class="search-input"
              @keyup.enter="handleQuery"
            >
              <template #append>
                <el-button class="search-btn" @click="handleQuery">
                  <el-icon><i-ep-search /></el-icon>
                </el-button>
              </template>
            </el-input>
          </div>
        </div>
      </div>

      <!-- 2. 数据表格 -->
      <el-table
        ref="tableRef"
        v-loading="loading"
        :data="participantList"
        style="width: 100%"
        header-cell-class-name="table-header-cell"
        row-class-name="table-row"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        
        <el-table-column label="签约人" prop="signerName" min-width="120">
          <template #default="scope">
            <span class="text-main">{{ scope.row.signerName }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="商品名称" prop="goodsName" min-width="180">
          <template #default="scope">
            <span class="text-main">{{ scope.row.goodsName }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="类型" prop="type" width="180">
          <template #default="scope">
            <span class="text-main">{{ scope.row.type }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="签约时间" prop="signTime" width="280">
          <template #default="scope">
            <span class="text-main">{{ scope.row.signTime }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="scope">
            <el-button link class="action-btn view-btn" @click="handleView(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 3. 分页区 -->
      <div class="pagination-section mt30">
        <div class="pagination-info">
          <span class="total-text">共 {{ total }} 条</span>
          <span class="page-size-select">
            显示 
            <el-select v-model="queryParams.pageSize" size="small" style="width: 80px" @change="handleSizeChange">
              <el-option label="10 条" :value="10" />
              <el-option label="20 条" :value="20" />
              <el-option label="30 条" :value="30" />
              <el-option label="50 条" :value="50" />
            </el-select>
          </span>
        </div>
        <div class="pagination-pager">
          <el-pagination
            v-model:current-page="queryParams.pageNum"
            v-model:page-size="queryParams.pageSize"
            :total="total"
            layout="prev, pager, next"
            background
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

/** ---- 数据定义 ---- */
const route = useRoute();
const router = useRouter();
const loading = ref(false);
const total = ref(243);
const tableRef = ref();

// 查询参数
const queryParams = reactive({
  pageNum: 11,
  pageSize: 10,
  kwd: '',
  goodsId: route.query.goodsId as string || ''
});

// 模拟列表数据
const participantList = ref([
  {
    id: 1,
    signerName: '张三',
    goodsName: '签约',
    type: '签约',
    signTime: '2021-02-28 10:30'
  },
  {
    id: 2,
    signerName: '李四',
    goodsName: '',
    type: '签约',
    signTime: '2021-02-28 10:30'
  }
]);

/** ---- 交互方法 ---- */
const getList = () => {
  // loading.value = true;
  // 模拟 API 调用
  console.log('Fetching participants list with params:', queryParams);
};

const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

const handleView = (row: any) => {
  router.push({ path: '/system/goods/participant-detail', query: { id: row.id } });
};

const handleSelectionChange = (selection: any[]) => {
  console.log('Selection changed:', selection);
};

const handleSizeChange = (val: number) => {
  queryParams.pageSize = val;
  getList();
};

const handleCurrentChange = (val: number) => {
  queryParams.pageNum = val;
  getList();
};

// 初始化获取数据
getList();
</script>

<style scoped lang="scss">
/* 全局变量模拟 */
$primary-color: #007bff;
$text-secondary: #909399;
$bg-light: #f8f9fb;
$border-color: #ebeef5;

.participants-page {
  background-color: #fff;
  min-height: 100%;
  padding: 0;
}

.main-card {
  border: none !important;
  padding: 10px;
}

.mb20 { margin-bottom: 20px; }
.mt30 { margin-top: 30px; }
.mr5 { margin-right: 5px; }

/* 1. 顶部搜索与操作 */
.toolbar {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 10px 0;
}

.search-wrapper {
  .search-input {
    width: 320px;
    
    :deep(.el-input__wrapper) {
      border-radius: 4px 0 0 4px;
      box-shadow: 0 0 0 1px #dcdfe6 inset;
      
      &.is-focus {
        box-shadow: 0 0 0 1px $primary-color inset !important;
      }
    }
    
    :deep(.el-input-group__append) {
      background-color: $primary-color;
      border: 1px solid $primary-color;
      border-radius: 0 4px 4px 0;
      padding: 0;
      width: 48px;
      
      .search-btn {
        width: 100%;
        height: 100%;
        border: none;
        background: transparent;
        color: #fff;
        margin: 0;
        
        &:hover {
          background-color: rgba(255, 255, 255, 0.1);
        }
        
        .el-icon {
          font-size: 18px;
        }
      }
    }
  }
}

/* 2. 表格视觉强化 */
:deep(.table-header-cell) {
  background-color: $bg-light !important;
  color: #606266;
  font-weight: 600;
  height: 48px;
  border-bottom: 1px solid $border-color;
}

:deep(.table-row) {
  height: 56px;
  
  &:hover > td {
    background-color: #f5f7fa !important;
  }
}

.text-main {
  font-size: 14px;
  color: #303133;
}

.action-btn {
  font-size: 14px;
  padding: 0 8px;
  
  &.view-btn {
    color: $primary-color;
    font-weight: 500;
  }
}

/* 3. 分页美化 */
.pagination-section {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 20px;
  padding: 20px 0;
  
  .pagination-info {
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 14px;
    color: #606266;
    
    .total-text {
      color: #606266;
    }
    
    .page-size-select {
      display: flex;
      align-items: center;
      gap: 5px;
    }
  }
}

:deep(.el-pagination.is-background) {
  .el-pager li {
    background-color: #fff;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    font-weight: normal;
    color: #606266;
    min-width: 32px;
    transition: all 0.2s;
    
    &:not(.is-disabled).is-active {
      background-color: #fff;
      color: $primary-color;
      border-color: $primary-color;
      font-weight: bold;
    }
    
    &:not(.is-disabled):hover {
      color: $primary-color;
      border-color: $primary-color;
    }
  }
  
  .btn-prev, .btn-next {
    background-color: #fff;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    color: #606266;
    
    &:disabled {
      background-color: #f5f7fa;
      color: #c0c4cc;
    }
    
    &:hover:not(:disabled) {
      color: $primary-color;
      border-color: $primary-color;
    }
  }
}
</style>
