<script setup>
import { ref, onMounted } from 'vue';
import { consoleSurveys, getQuestionList, useTemplate } from '@/api/home/index.js';
import { snQuestions, saveQuestions } from '@/api/design/index.js';
import { useRouter } from 'vue-router';
import { useCounterStore } from '@/stores/counter';
import { storeToRefs } from 'pinia';
// 获取 Store 实例
const counterStore = useCounterStore();
const store = storeToRefs(counterStore);

const router = useRouter();
const surveys = ref([]);

const createdNewPage = defineModel('createdNewPage', {
  type: Boolean,
  default: false
});
const createdQuestion = (item) => {
  // 为 智能创建建立链接
  if (item.h5Title === 'AI智能创建') {
    return router.push({ name: 'intelligentGeneration' });
  }
  // 为 更多模板添加对应的跳转链接
  if (item.h5Title === '更多模板') {
    return router.push({ name: 'templateMarket' });
  }

  const query = {
    group_id: 0,
    source: 1,
    project_name: `${item.title ? item.title : '请输入问卷标题'}`,
    remarks: item.title
      ? '为优化活动服务品质，烦请完成问卷，感谢配合！您的反馈至关重要！'
      : '请输入问卷描述',
    scene_code: item.parentCode,
    scene_code_info: item.code,
    // 很迷茫  模板新增 tag 空数组  非模板 就是k
    tags: ''
  };
  if (createdNewPage.value) {
    query.scene_code = item.parentCode;
    query.tags = '';
  } else {
    if (item.sn) {
      query.scene_code = null;
      query.tags = [];
    }
  }

  // 如果放在了底部 当作新增组件
  if (createdNewPage.value) {
    consoleSurveys(query).then((res) => {
      if (res.data) {
        createdApx(res);
      }
    });
  } else {
    if (item.sn) {
      useTemplate(item.sn, query).then((temp) => {
        if (temp.data) {
          createdApx(temp);
        }
      });
    } else {
      consoleSurveys(query).then((res) => {
        if (res.data) {
          createdApx(res);
        }
      });
    }
  }
};
const createdApx = (res) => {
  snQuestions({ sn: res.data.data.sn }).then((ques) => {
    if (ques.data) {
      const { data } = res.data;
      ques.data.data.survey.introduction = `<p>${data.remarks}</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
          }
        });
      });
    }
  });
};

// 添加获取问卷列表的方法
const getList = () => {
  getQuestionList().then((res) => {
    if (res.data.code === 0) {
      if (res.data.data) {
        res.data.data.forEach((item) => {
          // if (item.parentCode && item.parentCode === 1) {
          surveys.value.push(item);
          // }
        });

        // surveys.value.push({});
      }
    }
  });
};

// 在组件挂载时调用
onMounted(() => {
  getList();
});
</script>

<template>
  <!--  <div class="home-pen">-->
  <!--    <img :src="homePen" alt="" />-->
  <!--  </div>-->
  <div class="create_survey">
    <div class="create_survey_title" style="color: #000; text-align: left">请选择新建类型</div>
    <div class="flex align-center space-between warp">
      <div
        v-for="survey in surveys"
        :key="survey.title"
        span="6"
        class="survey"
        @click="createdQuestion(survey)"
      >
        <img :src="survey.h5Image" alt="" width="40" />
        <span>{{ survey.h5Title }}</span>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.home-pen {
  //height: 200px;
  position: absolute;
  top: -10px;
  left: 0;
  z-index: 8;

  //width: 100%;
  background: #fff;

  img {
    width: 100%;

    //height: 200px;
  }
}

.create_survey {
  overflow: hidden;

  //border-radius: 16px;
  background: #fff;

  //padding: 15px;

  color: #000;

  .create_survey_title {
    margin: 16px;
    color: #000;
    font-weight: bold;
    font-size: 15px;
    font-family: PingFangSC, 'PingFang SC';
  }

  & .warp {
    flex-wrap: wrap;
  }
}

.survey {
  display: flex;

  //margin-right: 30px;
  flex-direction: column;
  align-items: center;

  //flex: 1;
  width: 25%;

  //margin: 0 16px;
  text-align: center;

  //justify-content: space-around;

  span {
    margin-top: 8px;
    margin-bottom: 18px;
    color: #000;
    font-weight: 400;
    font-size: 0.34rem;
  }
}
</style>
