<template>
  <!-- 子项目插件 -->
  <view class="box shadow-lg">
    <view v-for="item in data.sonProject" :key="item.detailId">
      <span class="text-xs text-blue-500" @click="openProject(item)">{{ item.name }}</span>
    </view>
  </view>
</template>

<script setup>
import { onMounted } from 'vue';
import Config from '@/common/js/config.js';

const props = defineProps({
  task: {
    type: Object,
    default: () => {},
  },
});

const data = reactive({ sonProject: [] });

onMounted(() => {
  getSonProject();
});

async function getSonProject() {
  try {
    const data = await uni.$u.api.findSonProject({ projectId: props.task.detailId });
    data.sonProject = data;
  } catch (error) {
    console.error('p-subproject.vue getSonProject error: ', error);
  }
}

/**
 * 打开项目
 * @param {object} project 所点击的项目的信息
 */
function openProject(project) {
  const { name, id, url } = project;
  const { apiUrl } = Config;
  const defaultwbs = `${apiUrl}/defaultwbs`;
  url && (defaultwbs = url);
  uni.$u.route('pages/project/project', {
    u: userId.value,
    p: id,
    pname: name,
    url: encodeURIComponent(url),
  });
}
</script>

<style scoped lang="scss">
  .box{
    border-radius: 8px;
    background: #fff;
    padding: 16px;
    overflow: hidden;
  }
</style>
