<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" label-width="100px">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="商品编号" prop="productNo">
                  <el-input v-model="queryParams.productNo" placeholder="请输入商品编号" clearable @keyup.enter="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="商品名称" prop="itemName">
                  <el-input v-model="queryParams.itemName" placeholder="请输入商品名称" clearable @keyup.enter="handleQuery" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="商品类别" prop="bottomCategoryId">
                  <el-tree-select
                    v-model="queryParams.bottomCategoryId"
                    :data="categoryOptions"
                    :props="{ value: 'id', label: 'label', children: 'children' } as any"
                    value-key="id"
                    placeholder="请选择商品类别"
                    clearable
                    check-strictly
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="商品品牌" prop="brandId">
                  <el-select
                    v-model="queryParams.brandId"
                    placeholder="请输入品牌名称搜索"
                    filterable
                    remote
                    clearable
                    :remote-method="handleBrandSearch"
                    :loading="brandLoading"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in brandOptions"
                      :key="item.id"
                      :label="item.brandName"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item label="上下架状态" prop="productStatus">
                  <el-select v-model="queryParams.productStatus" placeholder="请选择" clearable>
                    <el-option label="已上架" :value="1" />
<!--                    <el-option label="下架" :value="0" />-->
                    <el-option label="上架中" :value="2" />
                    <el-option label="驳回上架" :value="3" />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item>
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </transition>

    <!-- 上下架审核商品信息列表 -->
    <el-card shadow="never" class="table-card">
      <template #header>
        <div class="flex items-center justify-between">
          <span class="font-semibold">上下架审核商品信息列表</span>
          <div class="flex gap-2">
            <el-button type="primary" icon="Download" @click="handleExport">导出商品</el-button>
            <el-button type="success" icon="Upload">导入商品</el-button>
            <el-button circle icon="Refresh" @click="getList"></el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" border :data="baseList" :height="tableHeight" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商品图片" align="center" prop="productImage" width="100" >
          <template #default="scope">
            <image-preview :src="scope.row.productImage" :width="60" :height="60"/>
          </template>
        </el-table-column>
        <el-table-column label="商品编号" align="center" prop="productNo" width="120" >
          <template #default="scope">
            <el-link type="primary" @click="handleView(scope.row)">{{ scope.row.productNo }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="商品信息" align="center" min-width="250" show-overflow-tooltip>
          <template #default="scope">
            <div class="text-left">
              <div>{{ scope.row.itemName }}</div>
              <div class="text-gray-500" style="font-size: 12px;">品牌: {{ scope.row.brandName || '-' }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="基本情况" align="center" width="180">
          <template #default="scope">
            <div class="text-left" style="font-size: 12px;">
              <div>
                <span class="text-gray-500">商品分类:</span>
                <span>{{ scope.row.categoryName || '-' }}</span>
              </div>
              <div>
                <span class="text-gray-500">单位:</span>
                <span>{{ scope.row.unitName || '-' }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="SKU价格" align="center" width="180">
          <template #default="scope">
            <div class="text-left" style="font-size: 12px;">
              <div>
                <span class="text-gray-500">市场价:</span>
                <span class="text-red-500">¥{{ scope.row.marketPrice || '0.00' }}</span>
              </div>
              <div>
                <span class="text-gray-500">官网价:</span>
                <span class="text-red-500">¥{{ scope.row.memberPrice || '0.00' }}</span>
              </div>
              <div>
                <span class="text-gray-500">最低价:</span>
                <span class="text-red-500">¥{{ scope.row.minSellingPrice || '0.00' }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="成本预算" align="center" width="150">
          <template #default="scope">
            <div class="text-left" style="font-size: 12px;">
              <div>
                <span class="text-gray-500">采购价:</span>
                <span>¥{{ scope.row.purchasingPrice || '0.00' }}</span>
              </div>
              <div>
                <span class="text-gray-500">暂估毛利率:</span>
                <span>{{ scope.row.tempGrossMargin || '0.00' }}%</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品来源" align="center" width="100">
          <template #default="scope">
            <span>{{ scope.row.dataSource || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品状态" align="center" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.productStatus === '2' || scope.row.productStatus === 2" type="warning">上架中</el-tag>
            <el-tag v-else-if="scope.row.productStatus === '1' || scope.row.productStatus === 1" type="success">已上架</el-tag>
            <el-tag v-else-if="scope.row.productStatus === '0' || scope.row.productStatus === 0" type="info">已下架</el-tag>
            <el-tag v-else-if="scope.row.productStatus === '3' || scope.row.productStatus === 3" type="info">驳回上架</el-tag>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="审核意见" align="center" width="180" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ scope.row.shelfComments || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200" fixed="right" class-name="border-left">
          <template #default="scope">
            <div class="flex flex-col gap-1">
              <!-- 根据商品状态显示不同按钮 -->
              <template v-if="scope.row.productStatus === '2' || scope.row.productStatus === 2">
                <!-- 上架中：显示上架审核按钮 -->
                <div class="flex gap-1 justify-center">
                  <el-link type="primary" :underline="false" @click="handleEdit(scope.row)">编辑</el-link>
                  <el-link type="success" :underline="false" @click="handleShelfReview(scope.row)">上架审核</el-link>
                </div>
              </template>
              <template v-else-if="scope.row.productStatus === '1' || scope.row.productStatus === 1">
                <!-- 已上架：显示下架按钮 -->
                <div class="flex gap-1 justify-center">
                  <el-link type="primary" :underline="false" @click="handleEdit(scope.row)">编辑</el-link>
                  <el-link type="danger" :underline="false" @click="handleOffShelf(scope.row)">下架</el-link>
                </div>
              </template>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 游标分页控制 -->
      <pagination
        v-show="baseList.length > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        v-model:way="queryParams.way"
        :cursor-mode="true"
        :has-more="hasMore"
        @pagination="getList"
      />
    </el-card>

    <!-- 上架审核对话框 -->
    <el-dialog v-model="reviewDialog.visible" :title="reviewDialog.title" width="600px" append-to-body>
      <el-form ref="reviewFormRef" :model="reviewForm" :rules="reviewRules" label-width="100px">
        <el-form-item label="商品编号">
          <el-input v-model="reviewForm.productNo" disabled />
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="reviewForm.itemName" disabled />
        </el-form-item>
        <el-form-item label="审核结果" prop="productStatus">
          <el-radio-group v-model="reviewForm.productStatus">
            <el-radio :label="1">通过上架</el-radio>
            <el-radio :label="3">驳回下架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="shelfComments">
          <el-input v-model="reviewForm.shelfComments" type="textarea" :rows="4" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="reviewDialog.visible = false">取消</el-button>
          <el-button type="primary" @click="submitReview">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ShelfReview" lang="ts">
import { listBase, getBase, shelfReview, brandList, categoryTree } from '@/api/product/base';
import { BaseVO, BaseQuery, BaseForm } from '@/api/product/base/types';
import { BrandVO } from '@/api/product/brand/types';
import { listBrand } from '@/api/product/brand';
import { categoryTreeVO } from '@/api/product/category/types';
import { useRouter, useRoute } from 'vue-router';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const router = useRouter();
const route = useRoute();

const baseList = ref<BaseVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const brandOptions = ref<BrandVO[]>([]);
const brandLoading = ref(false);
let brandSearchTimer: ReturnType<typeof setTimeout> | null = null;
const categoryOptions = ref<categoryTreeVO[]>([]);
const hasMore = ref(true); // 是否还有更多数据
const pageHistory = ref([]);

// 动态计算表格高度
const tableHeight = computed(() => {
  // 基础高度 = 视口高度 - 顶部导航(84) - 容器padding(16) - 搜索区域 - 卡片header(60) - 分页器(60)
  const baseHeight = window.innerHeight - 84 - 16;
  const searchHeight = showSearch.value ? 150 : 10; // 搜索区域高度
  const cardHeaderHeight = 60; // 卡片header高度
  const paginationHeight = 60; // 分页器高度
  return baseHeight - searchHeight - cardHeaderHeight - paginationHeight;
});

const queryFormRef = ref<ElFormInstance>();
const reviewFormRef = ref<ElFormInstance>();

// 审核对话框
const reviewDialog = reactive({
  visible: false,
  title: '上架审核'
});

// 审核表单
const reviewForm = ref<any>({
  id: undefined,
  productNo: '',
  itemName: '',
  productStatus: 1,
  shelfComments: ''
});

// 审核表单验证规则
const reviewRules = ref({
  productStatus: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
  shelfComments: [{ required: true, message: '请输入审核意见', trigger: 'blur' }]
});

const queryParams = ref<BaseQuery>({
  pageNum: 1,
  pageSize: 10,
  way: undefined,
  productNo: undefined,
  itemName: undefined,
  brandId: undefined,
  purchaseNature: undefined,
  bottomCategoryId: undefined,
  isSelf: undefined,
  productReviewStatus: 1, // 只查询审核通过的数据
  productStatus: undefined, // 用于筛选商品状态
  isShelfAudit: 1,
  lastSeenId: undefined // 游标分页的lastSeenId
});


/** 查询商品列表 */
const getList = async () => {
  loading.value = true;
  try {
    const params = { ...queryParams.value };
    const currentPageNum = queryParams.value.pageNum;

    // 强制只查询审核通过的数据
    params.productReviewStatus = 1;


    // 如果没有选择商品状态，默认查询上架中和已上架的数据
    // 后端需要支持多状态查询，这里通过不传productStatus让后端返回所有状态，前端再过滤
    // 或者后端支持传入多个状态值

    // 第一页不需要游标参数
    if (currentPageNum === 1) {
      delete params.lastSeenId;
      delete params.way;
    } else {
      // way参数：0=上一页，1=下一页
      if (queryParams.value.way === 0) {
        // 上一页：使用目标页（即当前显示页）的firstId
        const nextPageHistory = pageHistory.value[currentPageNum];
        if (nextPageHistory) {
          params.firstSeenId = nextPageHistory.firstId;
          params.way = 0;
        }
      } else {
        // 下一页：使用前一页的lastId作为lastSeenId
        const prevPageHistory = pageHistory.value[currentPageNum - 1];
        if (prevPageHistory) {
          params.lastSeenId = prevPageHistory.lastId;
          params.way = 1;
        }
      }
    }

    const res = await listBase(params);
    baseList.value = res.rows || [];

    // 判断是否还有更多数据
    hasMore.value = baseList.value.length === queryParams.value.pageSize;

    // 记录当前页的第一个id和最后一个id
    if (baseList.value.length > 0) {
      const firstItem = baseList.value[0];
      const lastItem = baseList.value[baseList.value.length - 1];
      //如果长度小于currentPageNum则创建

      if (pageHistory.value.length <= currentPageNum) {
        pageHistory.value[currentPageNum] = {
          firstId: firstItem.id,
          lastId: lastItem.id
        };
      }
    }
    total.value = res.total || 0;
  } catch (error) {
    console.error('获取列表失败:', error);
  } finally {
    loading.value = false;
  }
};

/** 初始化路由参数 */
const initRouteParams = () => {
  // 从路由参数中获取筛选条件
  if (route.query.productStatus) {
    queryParams.value.productStatus = Number(route.query.productStatus);
  }
  if (route.query.brandId) {
    queryParams.value.brandId = route.query.brandId as string;
  }
  if (route.query.bottomCategoryId) {
    queryParams.value.bottomCategoryId = route.query.bottomCategoryId as string;
  }
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  queryParams.value.lastSeenId = undefined;
  pageHistory.value = [0]; // 重置页面历史
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.lastSeenId = undefined;
  pageHistory.value = [0]; // 重置页面历史
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: BaseVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'product/base/export',
    {
      ...queryParams.value
    },
    `base_shelf_review_${new Date().getTime()}.xlsx`
  );
};

/** 查看商品详情 */
const handleView = (row: BaseVO) => {
  const url = `https://item.xiaoluwebsite.xyz/item?id=${row.id}`;
  window.open(url, '_blank');
};

/** 编辑商品 */
const handleEdit = (row: BaseVO) => {
  router.push(`/product/base/edit/${row.id}`);
};

/** 上架审核 */
const handleShelfReview = (row: BaseVO) => {
  reviewDialog.visible = true;
  reviewDialog.title = '上架审核';
  reviewForm.value = {
    id: row.id,
    productNo: row.productNo,
    itemName: row.itemName,
    productStatus: 1,
    shelfComments: ''
  };
}

/** 下架操作 */
const handleOffShelf = async (row: BaseVO) => {
  await proxy?.$modal.confirm('确认下架该商品吗？');
  const data: BaseForm = {
    id: row.id,
    productStatus: '0' // 设置为下架状态
  };
  await shelfReview(data);
  proxy?.$modal.msgSuccess('下架成功');
  await getList();
};

/** 提交审核 */
const submitReview = async () => {
  await reviewFormRef.value?.validate();
  const data: BaseForm = {
    id: reviewForm.value.id,
    productStatus: String(reviewForm.value.productStatus),
    shelfComments: reviewForm.value.shelfComments
  };
  await shelfReview(data);
  proxy?.$modal.msgSuccess(reviewForm.value.productStatus === 1 ? '上架成功' : '驳回成功');
  reviewDialog.visible = false;
  await getList();
};

/** 加载品牌选项（默认100条） */
const loadBrandOptions = async (keyword?: string) => {
  brandLoading.value = true;
  try {
    const res = await listBrand({ pageNum: 1, pageSize: 100, brandName: keyword });
    brandOptions.value = res.rows || [];
  } catch (error) {
    console.error('加载品牌列表失败:', error);
  } finally {
    brandLoading.value = false;
  }
};

/** 品牌远程搜索（防抖） */
const handleBrandSearch = (query: string) => {
  if (brandSearchTimer) clearTimeout(brandSearchTimer);
  brandSearchTimer = setTimeout(() => {
    loadBrandOptions(query || undefined);
  }, 300);
};

/** 查询分类树 */
const getCategoryTree = async () => {
  const res = await categoryTree();
  categoryOptions.value = res.data || [];
};

onMounted(() => {
  getCategoryTree();
  initRouteParams();
  loadBrandOptions();
  getList();
});
</script>

<style scoped lang="scss">
.app-container {
  padding: 8px;
  height: calc(100vh - 84px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.table-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  :deep(.el-card__body) {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  :deep(.el-table) {
    flex: 1;
  }

  // 确保固定列左侧有边框
  :deep(.el-table__fixed-right) {
    box-shadow: -1px 0 0 var(--el-table-border-color) !important;
  }

  // 固定列的单元格左边框
  :deep(.el-table__fixed-right .el-table__cell) {
    border-left: 1px solid var(--el-table-border-color) !important;
  }
}
</style>
