<template>
  <el-container class="container">
    <el-header>
      <backIndex />
    </el-header>
    <el-container class="main-container">
      <el-main class="content">
        <div class="content-header"></div>
        <el-row :gutter="70">
          <el-col :xs="24" :sm="12">
            <el-input v-model="searchQuery" placeholder="搜索商品名称以进行批量操作" suffix-icon="el-icon-search"
              style="width: 130%;" />
          </el-col>
          <el-col :xs="24" :sm="12" class="batch-actions" style="margin-top: 10px;">
            <el-button @click="handleBatchDelete" type="danger" size="small">批量删除</el-button>
            <el-button @click="handleBatchStatusChange" type="warning" size="small">批量上架/下架</el-button>
          </el-col>
        </el-row>
        
        <el-table :data="currentPageGoodsData" style="width: 100%; table-layout: auto">
          <el-table-column prop="name" label="商品名称" width="150">
            <template v-slot="scope">
              <el-checkbox v-model="scope.row.selected" />
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column prop="id" label="商品ID" width="120" />
          <el-table-column prop="category" label="所属分类" width="150" />
          <el-table-column prop="price" label="商品价格" width="100" />
          <el-table-column prop="audience" label="适用人群" width="120" />
          <el-table-column prop="status" label="状态" width="120">
            <template v-slot="scope">
              <el-tag :type="scope.row.status === '已上架' ? 'success' : 'info'">
                {{ scope.row.status }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="250">
            <template v-slot="scope">
              <el-button @click="editItem(scope.row)" type="primary" size="small">编辑</el-button>
              <el-button @click="deleteItem(scope.row)" type="danger" size="small">删除</el-button>
              <el-button @click="toggleStatus(scope.row)" :type="scope.row.status === '已上架' ? 'warning' : 'success'"
                size="small">
                {{ scope.row.status === '已上架' ? '下架' : '上架' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination class="pagination" :current-page="currentPage" :page-size="pageSize"
          :total="filteredGoodsData.length" @current-change="handlePageChange"
          layout="total, prev, pager, next, jumper" />
      </el-main>
    </el-container>
    <el-footer>
      <Footer />
    </el-footer>



    <!-- 弹窗 -->
    <!-- 弹窗 -->
    <el-dialog :visible.sync="deleteDialogVisible" title="确认删除">
      <p>您确定要删除商品 "{{ itemToDelete ? itemToDelete.name : '' }}" 吗？</p>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDelete">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>



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

const searchQuery = ref('');
const currentPage = ref(1);
const pageSize = ref(11);
const itemToDeleteName = computed(() => itemToDelete.value?.name || '');

const deleteDialogVisible = ref(false);
const editForm = ref<any>({});

const itemToDelete = ref<any>(null); // 保存待删除的商品

const deleteItem = (item: any) => {
  itemToDelete.value = item; // 将要删除的商品保存
  deleteDialogVisible.value = true; // 显示弹窗
};
const cancelDelete = () => {
  itemToDelete.value = null; // 清空临时数据
  deleteDialogVisible.value = false; // 隐藏弹窗
};

const confirmDelete = () => {
  if (itemToDelete.value) {
    goodsData.value = goodsData.value.filter(item => item.id !== itemToDelete.value.id);
    ElMessage.success(`商品 "${itemToDelete.value.name}" 删除成功`);
    itemToDelete.value = null; // 清空临时数据
  }
  deleteDialogVisible.value = false; // 隐藏弹窗
};

const goodsData = ref([
  { name: '商品1', id: '001', category: '电子产品', price: '100', audience: '成年人', status: '已下架', selected: false },
  { name: '商品2', id: '002', category: '家居用品', price: '50', audience: '家庭', status: '已上架', selected: false },
  { name: '商品1', id: '003', category: '电子产品', price: '100', audience: '成年人', status: '已下架', selected: false },
  { name: '商品2', id: '004', category: '家居用品', price: '50', audience: '家庭', status: '已上架', selected: false },
  { name: '商品1', id: '005', category: '电子产品', price: '100', audience: '成年人', status: '已下架', selected: false },
  { name: '商品2', id: '006', category: '家居用品', price: '50', audience: '家庭', status: '已上架', selected: false },
  { name: '商品1', id: '007', category: '电子产品', price: '100', audience: '成年人', status: '已下架', selected: false },
  { name: '商品2', id: '008', category: '家居用品', price: '50', audience: '家庭', status: '已上架', selected: false },
  { name: '商品1', id: '009', category: '电子产品', price: '100', audience: '成年人', status: '已下架', selected: false },
  { name: '商品2', id: '010', category: '家居用品', price: '50', audience: '家庭', status: '已上架', selected: false },
  { name: '商品1', id: '011', category: '电子产品', price: '100', audience: '成年人', status: '已下架', selected: false },
  { name: '商品2', id: '012', category: '家居用品', price: '50', audience: '家庭', status: '已上架', selected: false },
  { name: '商品1', id: '013', category: '电子产品', price: '100', audience: '成年人', status: '已下架', selected: false },
  { name: '商品2', id: '014', category: '家居用品', price: '50', audience: '家庭', status: '已上架', selected: false },
]);

const filteredGoodsData = computed(() => {
  if (!searchQuery.value) return goodsData.value;
  return goodsData.value.filter(item => item.name.includes(searchQuery.value));
});

const currentPageGoodsData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return filteredGoodsData.value.slice(start, end);
});

const handlePageChange = (page: number) => {
  currentPage.value = page;
};

const handleBatchDelete = () => {
  const selectedItems = goodsData.value.filter(item => item.selected);
  if (selectedItems.length === 0) {
    return ElMessage.warning('请先选择要删除的商品');
  }
  deleteDialogVisible.value = true;
};

const confirmBatchDelete = () => {
  goodsData.value = goodsData.value.filter(item => !item.selected);
  deleteDialogVisible.value = false;
  ElMessage.success('删除成功');
};

const handleBatchStatusChange = () => {
  const selectedItems = goodsData.value.filter(item => item.selected);
  if (selectedItems.length === 0) {
    return ElMessage.warning('请先选择要更改状态的商品');
  }
  selectedItems.forEach(item => {
    item.status = item.status === '已上架' ? '已下架' : '已上架';
  });
  ElMessage.success('状态修改成功');
};

const editItem = (item: any) => {
  editForm.value = { ...item };
};

const toggleStatus = (item: any) => {
  item.status = item.status === '已上架' ? '已下架' : '已上架';
};
</script>



<style scoped>
.main-container {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 50px;
  height: 100vh;
  box-sizing: border-box;
}

.pagination {
  margin: 20px auto;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  overflow-x: auto;
}
</style>
