1
<script setup lang="ts">
import MarketItem from '@/components/TemplateMarketItem/Index.vue';
import { templates } from '../../Hooks/useSurveySearch';
import { consoleSurveys, useTemplate } from '@/api/home';
import { saveQuestions, snQuestions } from '@/api/design';
import { useRoute, useRouter } from 'vue-router';
import { useCounterStore } from '@/stores/counter';
import { storeToRefs } from 'pinia';
import { computed } from 'vue';

const router = useRouter();
const route = useRoute();

// 获取 Store 实例
const counterStore = useCounterStore();
const store = storeToRefs(counterStore as any);
//
const limit = defineModel<number>('limit');
const currentTemplate = computed(() => templates.value.slice(0, limit.value));
function handleTemplateClick(template: any) {
  // ?sn=4O5xanLV&is_template=1&source=4O5xanLV&title=报名签到&parentCode=1&scene_code_info=11&user=苗闻博&quote_nums=3

  const isSearch = route.path.includes('/search');

  router.push({
    path: '/templatePreview',
    query: {
      sn: template.sn,
      user: template.creater_user,
      is_template: 1,
      source: template.sn,
      // H5TITLE 是为搜索页面传入的参数做兼容
      title: isSearch ? template.h5_title : template.title,
      parentCode: template.parentCode,
      scene_code_info: template.scene_code_info,
      quote_nums: template.quote_nums
    }
  });

  return;

  const query = {
    group_id: 0,
    source: 1,
    project_name: `${template.title}问卷 `,
    remarks: '为优化活动服务品质，烦请完成问卷，感谢配合',
    scene_code: template.scene_code,
    scene_code_info: template.scene_code_info,
    tags: undefined
  };

  if (template.sn) {
    query.scene_code = null;
    query.tags = [] as any;
  }

  // 如果放在了底部 当作新增组件
  if (template.sn) {
    useTemplate(template.sn, query).then((temp) => {
      if (temp.data) {
        createdApx(temp);
      }
    });
  } else {
    consoleSurveys(query).then((res) => {
      if (res.data) {
        createdApx(res);
      }
    });
  }

  function createdApx(res: any) {
    snQuestions({ sn: res.data.data.sn }).then((ques) => {
      if (ques.data) {
        ques.data.data.survey.introduction = `<p>为优化活动服务品质，烦请完成问卷，感谢配合！您的反馈至关重要！</p>`;
        store.questionsInfo.value = ques.data.data;
        saveQuestions({
          sn: res.data.data.sn,
          introduction: ques.data.data.survey.introduction,
          title: ques.data.data.survey.title
        }).then(() => {
          router.push({
            path: '/create',
            query: {
              sn: res.data.data.sn
            }
          });
        });
      }
    });
  }
}
</script>

<template>
  <section class="template-container">
    <market-item
      v-for="template in currentTemplate"
      :key="template"
      :template="template"
      @click="handleTemplateClick(template)"
    />
  </section>
</template>

<style lang="scss" scoped>
.template-container {
  overflow: scroll;
}
</style>
