<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="brandName">
                  <el-select-v2
                    v-model="queryParams.brandName"
                    :options="brandOptionsFormatted"
                    placeholder="请选择商品品牌"
                    clearable
                    filterable
                    :loading="brandLoading"
                    @visible-change="handleBrandVisibleChange"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="审核状态" prop="auditStatus">
                  <el-select
                    v-model="queryParams.auditStatus"
                    placeholder="请选择审核状态"
                    clearable
                  >
                    <el-option label="待审核" :value="1" />
                    <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 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.auditStatus === 1" type="warning">待审核</el-tag>
            <el-tag v-else-if="scope.row.auditStatus === 2" type="success">审核通过</el-tag>
            <el-tag v-else-if="scope.row.auditStatus === 3" type="danger">审核驳回</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.auditReason || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" fixed="right" class-name="border-left">
          <template #default="scope">
            <div class="flex justify-center">
              <!-- 只有待审核状态才显示审核按钮 -->
              <el-link v-if="scope.row.auditStatus === 1" type="primary" :underline="false" @click="handleAudit(scope.row)">审核</el-link>
              <span v-else>-</span>
            </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="auditStatus">
          <el-radio-group v-model="reviewForm.auditStatus">
            <el-radio :value="2">通过</el-radio>
            <el-radio :value="3">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditReason">
          <el-input v-model="reviewForm.auditReason" 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="BaseReview" lang="ts">
import { getGoodProductPage, brandList, categoryTree } from '@/api/product/base';
import { BaseVO, BaseQuery } from '@/api/product/base/types';
import { updateProductExquisite } from '@/api/product/productExquisite';
import { ProductExquisiteForm } from '@/api/product/productExquisite/types';
import { BrandVO } from '@/api/product/brand/types';
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);
const brandOptionsFormatted = computed(() => {
  return brandOptions.value.slice(0, 500).map((item) => ({
    label: item.brandName,
    value: item.brandName // review.vue使用brandName作为value
  }));
});
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>({
  productId: undefined,
  productNo: '',
  itemName: '',
  auditStatus: 2,
  auditReason: ''
});

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

const queryParams = ref<BaseQuery & { auditStatus?: number }>({
  pageNum: 1,
  pageSize: 10,
  way: undefined,
  productNo: undefined,
  itemName: undefined,
  brandName: undefined,
  purchaseNature: undefined,
  bottomCategoryId: undefined,
  isSelf: undefined,
  auditStatus: undefined,
  productStatus: undefined,
  lastSeenId: undefined // 游标分页的lastSeenId
});


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

    // 第一页不需要游标参数
    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 getGoodProductPage(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.auditStatus) {
    queryParams.value.auditStatus = Number(route.query.auditStatus);
  }
  if (route.query.brandName) {
    queryParams.value.brandName = route.query.brandName 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_review_${new Date().getTime()}.xlsx`
  );
};

/** 查看商品详情 */
const handleView = (row: BaseVO) => {
  router.push(`/product/base/detail/${row.id}`);
};

/** 审核商品 */
const handleAudit = (row: BaseVO) => {
  reviewDialog.visible = true;
  reviewDialog.title = '上架审核';
  reviewForm.value = {
    productId: row.id,
    productNo: row.productNo,
    itemName: row.itemName,
    auditStatus: 2,
    auditReason: ''
  };
};

/** 提交审核 */
const submitReview = async () => {
  await reviewFormRef.value?.validate();
  const data: ProductExquisiteForm = {
    productId: reviewForm.value.productId,
    auditStatus: Number(reviewForm.value.auditStatus),
    auditReason: reviewForm.value.auditReason
  };
  await updateProductExquisite(data);
  proxy?.$modal.msgSuccess(reviewForm.value.auditStatus === 2 ? '审核通过' : '审核驳回');
  reviewDialog.visible = false;
  await getList();
};

/** 查询品牌列表（实时请求，每次只加载500条） */
const getBrandList = async () => {
  try {
    brandLoading.value = true;
    const res = await brandList({ pageNum: 1, pageSize: 500 });
    brandOptions.value = res.data || [];
  } catch (error) {
    console.error('获取品牌列表失败:', error);
  } finally {
    brandLoading.value = false;
  }
};

/** 处理品牌下拉框显示/隐藏 */
const handleBrandVisibleChange = (visible: boolean) => {
  if (visible && brandOptions.value.length === 0) {
    getBrandList();
  }
};

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

onMounted(() => {
  getCategoryTree();
  initRouteParams();
  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>
