<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="title">
              <el-input v-model="queryParams.title" placeholder="请输入推文标题" clearable style="width: 240px" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="推文类别" prop="category">
              <el-select v-model="queryParams.category" placeholder="请选择" clearable style="width: 200px">
                <el-option
                  v-for="item in purchaseCategoryOptions"
                  :key="item.id"
                  :label="item.categoryName"
                  :value="item.id"
                />
              </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>
        <div class="flex justify-between items-center">
          <span class="text-lg font-medium">产品推荐信息列表</span>
          <div>
            <el-button type="primary" icon="Plus" @click="handleAdd" >新增</el-button>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" style="display: inline-block; margin-left: 8px;"></right-toolbar>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" border :data="programList">
        <el-table-column label="编号" align="center" prop="programNo" width="120" />
        <el-table-column label="封面图片" align="center" prop="coverImageUrl" width="120">
          <template #default="scope">
            <image-preview :src="scope.row.coverImageUrl" :width="60" :height="60"/>
          </template>
        </el-table-column>
        <el-table-column label="推文标题" align="center" prop="title" min-width="200" show-overflow-tooltip>
          <template #default="scope">
            <el-button link type="primary" @click="handleView(scope.row)">{{ scope.row.title }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="推文类型" align="center" prop="category" width="150">
          <template #default="scope">
            <span>{{ getCategoryName(scope.row.category) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否显示" align="center" prop="isShow" width="100">
          <template #default="scope">
            <span :style="{ color: scope.row.isShow === '1' ? '#409eff' : '#f56c6c' }">
              {{ scope.row.isShow === '1' ? '显示' : '不显示' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
            <el-button link type="primary" @click="handleUpdate(scope.row)" >编辑</el-button>
            <el-button link type="danger" @click="handleDelete(scope.row)" >删除</el-button>
          </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>
  </div>
</template>

<script setup name="Program" lang="ts">
import { listProgram, delProgram } from '@/api/pmsProduct/program';
import { listPurchaseCategory } from '@/api/globalSetting/purchaseCategory';
import { ProgramVO, ProgramQuery } from '@/api/pmsProduct/program/types';

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

const programList = ref<ProgramVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

// 下拉选项数据
const purchaseCategoryOptions = ref<any[]>([]);

const queryParams = ref<ProgramQuery>({
  pageNum: 1,
  pageSize: 10,
  title: undefined,
  category: undefined,
  params: {}
});

/** 获取采购分类列表 */
const getPurchaseCategoryList = async () => {
  const res = await listPurchaseCategory({ isShow: 1 });
  purchaseCategoryOptions.value = res.rows || [];
};

/** 获取分类名称 */
const getCategoryName = (id: any) => {
  if (!id) return '';
  const category = purchaseCategoryOptions.value.find(item => String(item.id) === String(id));
  return category ? category.categoryName : id;
}

/** 查询产品推荐列表 */
const getList = async () => {
  loading.value = true;
  const res = await listProgram(queryParams.value);
  programList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

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

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

/** 新增按钮操作 */
const handleAdd = () => {
  router.push({ path: '/product/productProgram/form' });
}

/** 查看按钮操作 */
const handleView = (row: ProgramVO) => {
  router.push({ path: '/product/productProgram/form', query: { id: row.id, mode: 'view' } });
}

/** 修改按钮操作 */
const handleUpdate = (row: ProgramVO) => {
  router.push({ path: '/product/productProgram/form', query: { id: row.id } });
}

/** 删除按钮操作 */
const handleDelete = async (row: ProgramVO) => {
  await proxy?.$modal.confirm('是否确认删除该产品推荐？').finally(() => loading.value = false);
  await delProgram(row.id);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

onMounted(async () => {
  await getPurchaseCategoryList();
  getList();
});
</script>
