<template>
  <div class="p-2">
    <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" :inline="true">
            <el-form-item label="货品名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入货品名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                <el-option label="上架" value="on_sale" />
                <el-option label="下架" value="off_sale" />
              </el-select>
            </el-form-item>
            <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-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['physical:merchantItem:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['physical:merchantItem:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['physical:merchantItem:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Edit" @click="goToProductBanner()" v-hasPermi="['physical:merchantItem:remove']"
              >推荐商品广告配置</el-button
            >
          </el-col>
          <!--          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['physical:merchantItem:export']">导出</el-button>
          </el-col>-->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="merchantItemList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" prop="id" v-if="true" />
        <el-table-column label="商品名称" align="center" prop="name" />
        <el-table-column label="商品图片" align="center" width="90">
          <template #default="scope">
            <el-image
              v-if="scope.row.productImage"
              :src="scope.row.productImage"
              style="width: 40px; height: 40px; border-radius: 4px; cursor: zoom-in"
              :preview-src-list="[scope.row.productImage]"
              :preview-teleported="true"
              fit="cover"
            />
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column label="所属商家" align="center" prop="merchantId" />
        <el-table-column label="价格 (元)" align="center" prop="price" />
        <el-table-column label="数量" align="center" prop="quantity" />
        <el-table-column label="库存" align="center" prop="stock" />
        <el-table-column label="总销量" align="center" prop="totalSales" />
        <el-table-column label="商品排序" align="center" prop="sortOrder" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'on_sale' ? 'success' : 'danger'" style="cursor: pointer">
              {{ scope.row.status === 'on_sale' ? '上架' : '下架' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createdBy" />
        <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip :content="scope.row.status === 'on_sale' ? '下架' : '上架'" placement="top">
              <el-button
                link
                :type="scope.row.status === 'on_sale' ? 'warning' : 'success'"
                :icon="scope.row.status === 'on_sale' ? 'VideoPause' : 'VideoPlay'"
                @click="handleToggleStatus(scope.row)"
                v-hasPermi="['physical:merchantItem:edit']"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['physical:merchantItem:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['physical:merchantItem:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改商城货品管理对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="800px" append-to-body>
      <el-form ref="merchantItemFormRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="货品名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入货品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态">
                <el-option label="上架" value="on_sale" />
                <el-option label="下架" value="off_sale" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="价格 (元)" prop="price">
              <el-input v-model="form.price" placeholder="请输入价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="quantity">
              <el-input v-model="form.quantity" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品库存" prop="stock">
              <el-input v-model="form.stock" placeholder="请输入商品库存" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品排序" prop="sortOrder">
              <el-input v-model="form.sortOrder" placeholder="请输入商品排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属商家" prop="merchantId">
              <el-select v-model="form.merchantId" placeholder="请选择所属商家" filterable clearable style="width: 100%">
                <el-option v-for="item in merchantOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属标签" prop="serviceTabId">
              <el-select v-model="form.categoryTagId" placeholder="请选择所属标签" filterable clearable style="width: 100%">
                <el-option v-for="item in serviceTabOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品详情" prop="description">
              <el-input v-model="form.description" type="textarea" :rows="4" placeholder="请输入商品详情描述" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品主图" prop="productImage">
              <div class="upload-container">
                <el-upload
                  class="upload-icon"
                  action="#"
                  :on-change="handleProductImageChange"
                  :on-remove="handleProductImageRemove"
                  :file-list="productImageFileList"
                  :auto-upload="false"
                  :limit="1"
                  accept="image/*"
                >
                  <template #trigger>
                    <el-button type="primary">点击选择主图</el-button>
                  </template>

                  <template #default>
                    <div class="preview-area" @click="handleProductImagePreviewClick">
                      <img
                        v-if="productImagePreviewUrl || form.productImage"
                        :src="productImagePreviewUrl || form.productImage"
                        alt="预览图"
                        style="max-width: 100px; max-height: 100px; margin-top: 10px; cursor: pointer"
                      />
                      <div v-else style="margin-top: 10px; color: #999">无图片</div>
                    </div>
                  </template>

                  <template #tip>
                    <div class="el-upload__tip">
                      <span v-if="productImageFileList.length > 0">当前已选文件：{{ productImageFileList[0].name }}</span>
                    </div>
                  </template>
                </el-upload>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品详情图片" prop="detailImage">
              <div class="upload-container">
                <el-upload
                  class="upload-icon"
                  action="#"
                  :on-change="handleDetailImageChange"
                  :on-remove="handleDetailImageRemove"
                  :file-list="detailImageFileList"
                  :auto-upload="false"
                  :limit="1"
                  accept="image/*"
                >
                  <template #trigger>
                    <el-button type="primary">点击选择详情图</el-button>
                  </template>

                  <template #default>
                    <div class="preview-area" @click="handleDetailImagePreviewClick">
                      <img
                        v-if="detailImagePreviewUrl || form.detailImage"
                        :src="detailImagePreviewUrl || form.detailImage"
                        alt="预览图"
                        style="max-width: 100px; max-height: 100px; margin-top: 10px; cursor: pointer"
                      />
                      <div v-else style="margin-top: 10px; color: #999">无图片</div>
                    </div>
                  </template>

                  <template #tip>
                    <div class="el-upload__tip">
                      <span v-if="detailImageFileList.length > 0">当前已选文件：{{ detailImageFileList[0].name }}</span>
                    </div>
                  </template>
                </el-upload>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品轮播图" prop="carouselImages">
              <div class="upload-container">
                <el-upload
                  class="upload-icon"
                  action="#"
                  :on-change="handleCarouselImageChange"
                  :on-remove="handleCarouselImageRemove"
                  :file-list="carouselImageFileList"
                  :auto-upload="false"
                  :limit="9"
                  multiple
                  accept="image/*"
                >
                  <template #trigger>
                    <el-button type="primary">点击选择轮播图</el-button>
                  </template>
                  <template #default>
                    <div class="carousel-preview-area">
                      <div v-for="(item, index) in carouselImageFileList" :key="index" class="carousel-item">
                        <img
                          :src="item.url || item.response"
                          alt="轮播图"
                          style="max-width: 100%; max-height: 100%"
                          @click.stop="handleCarouselImagePreviewClick(index)"
                        />
                        <div class="carousel-item-mask" @click.stop="handleCarouselImageRemoveByIndex(index)">
                          <el-icon><Delete /></el-icon>
                        </div>
                      </div>
                      <div v-if="carouselImageFileList.length === 0" style="margin-top: 10px; color: #999">暂无图片，请点击左上角按钮添加</div>
                    </div>
                  </template>
                  <template #tip>
                    <div class="el-upload__tip">
                      <span>最多可上传 9 张图片，支持 jpg/png/gif 格式</span>
                    </div>
                  </template>
                </el-upload>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MerchantItem" lang="ts">
import { listMerchantItem, getMerchantItem, delMerchantItem, addMerchantItem, updateMerchantItem } from '@/api/system/physical/merchantItem';
import { MerchantItemVO, MerchantItemQuery, MerchantItemForm } from '@/api/system/physical/merchantItem/types';
import { uploadTournament } from '@/api/system/business/tournaments';
import { selectPhysicalMerchantPartnerAllEnabled } from '@/api/system/physical/merchantPartner';
import { selectEnabledTabsByCategoryList } from '@/api/system/physical/serviceTab';
import { ElMessage, ElMessageBox } from 'element-plus';
import { MerchantPartnerVO } from '@/api/system/physical/merchantPartner/types';
import { ServiceTabVO } from '@/api/system/physical/serviceTab/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const merchantItemList = ref<MerchantItemVO[]>([]);
const buttonLoading = ref(false);
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 queryFormRef = ref<ElFormInstance>();
const merchantItemFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: MerchantItemForm = {
  id: undefined,
  name: undefined,
  merchantId: undefined,
  price: undefined,
  quantity: undefined,
  stock: undefined,
  totalSales: undefined,
  sortOrder: undefined,
  status: 'on_sale',
  productImage: undefined,
  detailImage: undefined,
  createdBy: undefined,
  createdAt: undefined,
  updatedBy: undefined,
  updatedAt: undefined
};
const data = reactive<PageData<MerchantItemForm, MerchantItemQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    merchantId: undefined,
    price: undefined,
    quantity: undefined,
    stock: undefined,
    totalSales: undefined,
    sortOrder: undefined,
    status: undefined,
    createdBy: undefined,
    createdAt: undefined,
    updatedBy: undefined,
    updatedAt: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键 ID 不能为空', trigger: 'blur' }],
    name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
    merchantId: [{ required: true, message: '所属商家不能为空', trigger: 'blur' }],
    price: [{ required: true, message: '价格不能为空', trigger: 'blur' }],
    stock: [{ required: true, message: '当前库存不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
    createdBy: [{ required: true, message: '创建人不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

// 商品主图上传相关
const productImageFileList = ref<any[]>([]);
const productImagePreviewUrl = ref('');

// 商品详情图片上传相关
const detailImageFileList = ref<any[]>([]);
const detailImagePreviewUrl = ref('');

// 商品轮播图上传相关
const carouselImageFileList = ref<any[]>([]);
const isUploading = ref(false);
const uploadQueue = ref<any[]>([]);
const merchantOptions = ref<MerchantPartnerVO[]>([]);
const serviceTabOptions = ref<ServiceTabVO[]>([]);
/** 查询商城货品管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMerchantItem(queryParams.value);
  merchantItemList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  merchantItemFormRef.value?.resetFields();
  productImageFileList.value = [];
  productImagePreviewUrl.value = '';
  detailImageFileList.value = [];
  detailImagePreviewUrl.value = '';
  carouselImageFileList.value = [];
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

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

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: MerchantItemVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getMerchantItem(_id);
  Object.assign(form.value, res.data);

  // 处理商品主图
  if (res.data.productImage) {
    productImageFileList.value = [
      {
        name: '已上传主图',
        url: res.data.productImage,
        status: 'success'
      }
    ];
    productImagePreviewUrl.value = res.data.productImage;
  }

  // 处理商品详情图片
  if (res.data.detailImage) {
    detailImageFileList.value = [
      {
        name: '已上传详情图',
        url: res.data.detailImage,
        status: 'success'
      }
    ];
    detailImagePreviewUrl.value = res.data.detailImage;
  }
  // 处理商品轮播图
  if (res.data.carouselImages) {
    const imageUrls = res.data.carouselImages.split(',').filter((url: string) => url.trim());
    carouselImageFileList.value = imageUrls.map((url: string, index: number) => ({
      name: `轮播图${index + 1}`,
      url: url,
      status: 'success'
    }));
  }
  dialog.visible = true;
  dialog.title = '修改';
};

/** 提交按钮 */
const submitForm = () => {
  merchantItemFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      try {
        // 处理轮播图数据
        if (carouselImageFileList.value.length > 0) {
          form.value.carouselImages = carouselImageFileList.value.map((item) => item.url || item.response).join(',');
        } else {
          form.value.carouselImages = undefined;
        }

        if (form.value.id) {
          await updateMerchantItem(form.value).finally(() => (buttonLoading.value = false));
        } else {
          await addMerchantItem(form.value).finally(() => (buttonLoading.value = false));
        }
        proxy?.$modal.msgSuccess('操作成功');
        dialog.visible = false;
        await getList();
      } catch (error) {
        console.error('提交失败:', error);
        proxy?.$modal.msgError('提交失败，请重试');
      } finally {
        buttonLoading.value = false;
      }
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: MerchantItemVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delMerchantItem(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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

// 商品主图上传处理
const handleProductImageChange = async (file: any) => {
  const index = productImageFileList.value.findIndex((f) => f.uid === file.uid);
  productImageFileList.value = [];

  if (file.raw) {
    productImagePreviewUrl.value = URL.createObjectURL(file.raw);
  }

  if (index === -1) {
    productImageFileList.value.push(file);
  }

  try {
    const rawFile = file.raw;
    const res = await uploadTournament(rawFile);
    if (res.code === 200) {
      productImageFileList.value[index] = {
        ...file,
        status: 'success',
        response: res.data.url
      };
      form.value.productImage = productImageFileList.value[index].response;
      productImagePreviewUrl.value = productImageFileList.value[index].response;
      ElMessage.success('上传成功');
    } else {
      throw new Error(res.msg);
    }
  } catch (error) {
    productImageFileList.value[index] = {
      ...file,
      status: 'fail',
      error: '上传失败'
    };
    ElMessage.error('上传失败，请重试');
  }
};

// 商品主图移除处理
const handleProductImageRemove = () => {
  productImageFileList.value = [];
  productImagePreviewUrl.value = '';
  form.value.productImage = '';
};

// 商品主图预览点击
const handleProductImagePreviewClick = () => {
  if (productImagePreviewUrl.value || form.value.productImage) {
    ElMessageBox.alert(`<img src="${productImagePreviewUrl.value || form.value.productImage}" style="max-width: 100%;" />`, '商品主图预览', {
      dangerouslyUseHTMLString: true,
      confirmButtonText: '关闭'
    });
  }
};

// 商品详情图片上传处理
const handleDetailImageChange = async (file: any) => {
  const index = detailImageFileList.value.findIndex((f) => f.uid === file.uid);
  detailImageFileList.value = [];

  if (file.raw) {
    detailImagePreviewUrl.value = URL.createObjectURL(file.raw);
  }

  if (index === -1) {
    detailImageFileList.value.push(file);
  }

  try {
    const rawFile = file.raw;
    const res = await uploadTournament(rawFile);
    if (res.code === 200) {
      detailImageFileList.value[index] = {
        ...file,
        status: 'success',
        response: res.data.url
      };
      form.value.detailImage = detailImageFileList.value[index].response;
      detailImagePreviewUrl.value = detailImageFileList.value[index].response;
      ElMessage.success('上传成功');
    } else {
      throw new Error(res.msg);
    }
  } catch (error) {
    detailImageFileList.value[index] = {
      ...file,
      status: 'fail',
      error: '上传失败'
    };
    ElMessage.error('上传失败，请重试');
  }
};

// 商品详情图片移除处理
const handleDetailImageRemove = () => {
  detailImageFileList.value = [];
  detailImagePreviewUrl.value = '';
  form.value.detailImage = '';
};

// 商品详情图片预览点击
const handleDetailImagePreviewClick = () => {
  if (detailImagePreviewUrl.value || form.value.detailImage) {
    ElMessageBox.alert(`<img src="${detailImagePreviewUrl.value || form.value.detailImage}" style="max-width: 100%;" />`, '商品详情图片预览', {
      dangerouslyUseHTMLString: true,
      confirmButtonText: '关闭'
    });
  }
};
// 商品轮播图上传处理
const handleCarouselImageChange = async (file: any) => {
  if (file.raw) {
    // 创建临时预览 URL
    const tempUrl = URL.createObjectURL(file.raw);
    const tempFile = {
      ...file,
      url: tempUrl
    };

    // 添加到文件列表
    carouselImageFileList.value.push(tempFile);
    const currentIndex = carouselImageFileList.value.length - 1;

    // 如果正在上传，加入队列
    if (isUploading.value) {
      uploadQueue.value.push({ file, index: currentIndex });
      return;
    }

    // 开始上传
    await uploadFile(file, currentIndex);
  }
};

// 执行文件上传
const uploadFile = async (file: any, index: number) => {
  isUploading.value = true;

  try {
    const rawFile = file.raw;
    const res = await uploadTournament(rawFile);

    if (res.code === 200) {
      carouselImageFileList.value[index] = {
        ...file,
        status: 'success',
        response: res.data.url
      };
      ElMessage.success(`第${index + 1}张图片上传成功`);
    } else {
      throw new Error(res.msg || '上传失败');
    }
  } catch (error) {
    console.error('上传失败:', error);
    carouselImageFileList.value[index] = {
      ...file,
      status: 'fail',
      error: '上传失败'
    };
    ElMessage.error('图片上传失败，请重试');
  } finally {
    isUploading.value = false;

    // 检查队列中是否有待上传的文件
    if (uploadQueue.value.length > 0) {
      const next = uploadQueue.value.shift()!;
      await uploadFile(next.file, next.index);
    }
  }
};
// 商品轮播图移除处理
const handleCarouselImageRemove = (file: any) => {
  const index = carouselImageFileList.value.findIndex((f) => f.uid === file.uid);
  if (index > -1) {
    carouselImageFileList.value.splice(index, 1);
  }
};
// 商品轮播图移除处理（通过索引）
const handleCarouselImageRemoveByIndex = (index: number) => {
  carouselImageFileList.value.splice(index, 1);
};
// 商品轮播图预览点击
const handleCarouselImagePreviewClick = (index: number) => {
  if (carouselImageFileList.value.length > 0) {
    const currentImageUrl = carouselImageFileList.value[index].url || carouselImageFileList.value[index].response;

    ElMessageBox.alert(
      `<img src="${currentImageUrl}" style="max-width: 100%;" />`,
      `轮播图预览 (${index + 1}/${carouselImageFileList.value.length})`,
      {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '关闭'
      }
    );
  }
};
onMounted(() => {
  const route = proxy?.$route;
  if (route.query.categoryTagId) {
    queryParams.value.categoryTagId = String(route.query.categoryTagId);
  }
  if (route.query.merchantId) {
    queryParams.value.merchantId = String(route.query.merchantId);
  }
  getList();
  loadMerchantOptions();
  loadServiceTabOptions();
});

/** 加载商家选项列表 */
const loadMerchantOptions = async () => {
  try {
    const res = await selectPhysicalMerchantPartnerAllEnabled();
    if (res.code === 200 && Array.isArray(res.data)) {
      merchantOptions.value = res.data;
    } else if (Array.isArray(res)) {
      merchantOptions.value = res;
    }
  } catch (error) {
    console.error('加载商家列表失败:', error);
    proxy?.$modal.msgError('加载商家列表失败');
  }
};

/** 加载页签选项列表 */
const loadServiceTabOptions = async () => {
  try {
    const res = await selectEnabledTabsByCategoryList('shop');
    if (res.code === 200 && Array.isArray(res.data)) {
      serviceTabOptions.value = res.data;
    } else if (Array.isArray(res)) {
      serviceTabOptions.value = res;
    }
  } catch (error) {
    console.error('加载页签列表失败:', error);
    proxy?.$modal.msgError('加载页签列表失败');
  }
};
/** 切换商品上架/下架状态 */
const handleToggleStatus = async (row: MerchantItemVO) => {
  const newStatus = row.status === 'on_sale' ? 'off_sale' : 'on_sale';
  const actionText = newStatus === 'on_sale' ? '上架' : '下架';
  try {
    await proxy?.$modal.confirm(`确认要${actionText}该商品吗？`);

    // 创建更新数据的副本
    const updateData = { ...row, status: newStatus };

    buttonLoading.value = true;
    await updateMerchantItem(updateData);
    proxy?.$modal.msgSuccess(`${actionText}成功`);
    await getList();
  } catch (error: any) {
    if (error !== 'cancel') {
      console.error('切换状态失败:', error);
      proxy?.$modal.msgError(`${actionText}失败，请重试`);
    }
  } finally {
    buttonLoading.value = false;
  }
};
const goToProductBanner = () => {
  proxy?.$router.push({
    path: '/physical/productBanner'
  });
};
</script>

<style scoped lang="scss">
.upload-container {
  width: 100%;
}

.upload-icon {
  width: 100%;
}

.preview-area {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    border-color: #409eff;
  }
}
</style>
