<template>
  <div class="app-container goods-page">
    <el-card class="box-card border-none main-card modern-card" shadow="never">
      <!-- 1. 顶部操作栏 -->
      <div class="toolbar mb20">
        <div class="left-actions">
          <div class="search-wrapper">
            <el-input
              v-model="queryParams.productName"
              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 class="right-actions">
          <el-button type="primary" class="add-btn" @click="handleAdd">
            <el-icon class="mr5"><i-ep-plus /></el-icon>
            添加
          </el-button>
        </div>
      </div>

      <!-- 2. 数据表格 -->
      <el-table
        ref="tableRef"
        v-loading="loading"
        :data="goodsList"
        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="productName" min-width="180">
          <template #default="scope">
            <span class="goods-name">{{ scope.row.productName }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="类型" prop="productType" width="180">
          <template #default="scope">
            <dict-tag :options="main_goods_type" :value="scope.row.productType" />
          </template>
        </el-table-column>
        
        <el-table-column label="创建时间" prop="createTime" width="280">
          <template #default="scope">
            <span class="time-text">{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="scope">
            <el-button link class="action-btn edit-btn" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button link class="action-btn delete-btn" @click="handleDelete(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, onMounted, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
import { listGoods, delGoods } from '@/api/main/goods';
import { GoodsVO, GoodsQuery } from '@/api/main/goods/types';

const { proxy } = getCurrentInstance() as any;
const { main_goods_type } = proxy.useDict('main_goods_type');

/** ---- 数据定义 ---- */
const router = useRouter();
const loading = ref(true);
const total = ref(0);
const tableRef = ref();

// 查询参数
const queryParams = reactive<GoodsQuery>({
  pageNum: 1,
  pageSize: 10,
  productName: ''
});

// 列表数据
const goodsList = ref<GoodsVO[]>([]);

/** ---- 交互方法 ---- */
const getList = async () => {
  loading.value = true;
  try {
    const res = await listGoods(queryParams);
    goodsList.value = res.rows.map(item => {
      if (item.productType !== undefined && item.productType !== null) {
        item.productType = String(item.productType) as any;
      }
      return item;
    });
    total.value = res.total;
  } finally {
    loading.value = false;
  }
};

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

const handleAdd = () => {
  router.push('/system/goods/detail');
};

const handleUpdate = (row: any) => {
  router.push(`/system/goods/detail?id=${row.id}`);
};

const handleDelete = (row: GoodsVO) => {
  const ids = row.id;
  ElMessageBox.confirm('是否确认删除该商品？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    confirmButtonClass: 'confirm-delete-btn',
    type: 'warning'
  }).then(async () => {
    await delGoods(ids);
    getList();
    ElMessage.success('删除成功');
  }).catch(() => {});
};

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();
};

const handleViewParticipation = (row: any) => {
  router.push({ path: '/system/goods/participants', query: { goodsId: row.id } });
};

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

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

.goods-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: space-between;
  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;
        }
      }
    }
  }
}

.add-btn {
  background-color: $primary-color;
  border-color: $primary-color;
  border-radius: 4px;
  height: 36px;
  padding: 0 20px;
  font-weight: 500;
  
  &:hover {
    background-color: lighten($primary-color, 10%);
    border-color: lighten($primary-color, 10%);
  }
}

/* 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;
  }
}

.goods-name, .type-text, .time-text {
  font-size: 14px;
  color: #303133;
}

.count-link {
  font-weight: 500;
  color: $primary-color;
  font-size: 14px;
  
  &:hover {
    text-decoration: underline;
  }
}

.action-btn {
  font-size: 14px;
  padding: 0 8px;
  
  &.edit-btn {
    color: $primary-color;
  }
  
  &.delete-btn {
    color: #409eff; /* 对应原图中的浅蓝色删除，或者是标准链接色 */
  }
}

/* 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>
