<template>
  <!-- 这里是适配的状态栏的代码 -->
  <view class="statbar">
    <view class="status_bar"></view>
  </view>

  <view class="business-box">
    <view class="business-wrap" v-for="(item, index) in list" :key="index" @click="toUpload(item.url)">
      <view class="business-info">
        <view class="name">{{ item.name }}</view>
        <!-- <view class="desc">{{item.url}}</view> -->
      </view>

      <view class="mwbs-list" v-if="item.mwbsList && item.mwbsList.length > 0">
        <view class="mwbs-item" v-for="(val, key) in item.mwbsList" :key="key" @click.stop="toUpload(item.url)">
          <view class="name">{{ val.projectName }}</view>
          <!-- <view class="desc">{{val}}</view> -->
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useStore } from 'vuex';
import Config from '@/common/js/config.js';

const store = useStore();
const list = ref([]);
const user = computed(() => store.state.user.user);
const userJson = uni.$storage.getStorageSync('user');

if (userJson) {
  const user = JSON.parse(userJson);
  store.commit('user/setUser', user);
}

getList();

async function getList() {
  try {
    const res = await uni.$u.api.getBusinessList();
    list.value = res;
  } catch (error) {
    console.error('error: ', error);
  }
}

async function toUpload(url) {
  store.commit('setDomain', url);

  try {
    const res = await uni.$u.api.import('');
    // 导入WBS成功后
    // 直接打开导入的项目
    uni.$ui.showToast('导入成功，即将打开新项目', 3000);
    const { apiUrl } = Config;
    let defaultwbs = `${apiUrl}/defaultwbs`;
    res.url && (defaultwbs = res.url);
    
    store.commit('project/setIsRefresh', 1);
    
    setTimeout(() => {
      uni.navigateTo({ url: `/pages/project/project?u=${user.value.id}&p=${res.id}&pname=${res.name}&url=${encodeURIComponent(res.url)}` });
    }, 2000);
  } catch (error) {
    console.error('error: ', error);
    uni.$ui.showToast(error || '导入失败', 6000);
  }
}
</script>

<style lang="scss" scoped>
.business-box {
  padding: 0 16px;
}

.business-wrap {
  width: 100%;
  box-sizing: border-box;

  .business-info,
  .mwbs-item {
    padding: 10px;
    border-bottom: 1px solid #eeeeee;
  }

  .name {
    line-height: 36px;
  }

  .desc {
    font-size: 12px;
    color: #999;
  }

  .mwbs-list {
    padding-left: 16px;

    .mwbs-item:last-child {
      border-bottom: none;
    }
  }
}
</style>
