<template>
  <page-container :showBreadcrumb="false" :title="t('pages.dashboard.workplace.title')">
    <template #content>
      <div class="page-header-content">
        <div class="avatar">
          <h-avatar size="large" :src="currentUser.avatar" />
        </div>
        <div class="content">
          <div class="content-title">
            早安，{{ currentUser.nickname }}
            <span class="welcome-text">，祝你开心每一天！</span>
          </div>
          <div class="pb10">{{ currentUser.extra.group }}</div>
        </div>
      </div>
    </template>
    <template #extraContent>
      <div class="extra-content">
        <div class="stat-item">
          <h-statistic title="项目数" :value="56" />
        </div>
        <div class="stat-item">
          <h-statistic title="团队内排名" :value="8" suffix="/ 24" />
        </div>
        <div class="stat-item">
          <h-statistic title="项目访问" :value="2223" />
        </div>
      </div>
    </template>
    <div>
      <h-row :gutter="24">
        <h-col :xl="16" :lg="24" :md="24" :sm="24" :xs="24">
          <h-card
            style="margin-bottom: 24px"
            class="project-list"
            title="进行中的项目"
            :loading="projects.loading"
            :bordered="false"
            :body-style="{ padding: 0 }"
          >
            <template #extra>
              <a>全部项目</a>
            </template>
            <h-card-grid
              class="project-card-grid"
              :key="item.id"
              v-for="item in projects.dataSource"
            >
              <h-card :bordered="false" :body-style="{ padding: 0 }">
                <h-card-meta>
                  <template #title>
                    <div class="card-title">
                      <h-avatar size="small" :src="item.logo" />
                      <a>{{ item.title }}</a>
                    </div>
                  </template>
                  <template #description>
                    <div class="card-description">
                      {{ item.description }}
                    </div>
                  </template>
                </h-card-meta>
                <div class="project-item">
                  <a href="/#/">科学搬砖组</a>
                  <span class="datetime">9小时前</span>
                </div>
              </h-card>
            </h-card-grid>
          </h-card>

          <h-card :loading="activities.loading" title="动态" :bordered="false">
            <h-list>
              <h-list-item :key="item.id" v-for="item in activities.dataSource">
                <h-list-item-meta>
                  <template #avatar>
                    <h-avatar :src="item.user.avatar" />
                  </template>
                  <template #title>
                    <div>
                      <a>{{ item.user.name }}</a>
                      &nbsp;
                      <span class="events">
                        <template
                          v-for="event in item.template.split(/@\{([^{}]*)\}/gi)"
                          :key="event"
                        >
                          <a v-if="item[event]" :href="item[event].link">
                            {{ item[event].name }}
                          </a>
                          <span v-else>{{ event }}</span>
                        </template>
                      </span>
                    </div>
                  </template>
                  <template #description>
                    <div>{{ item.time }}</div>
                  </template>
                </h-list-item-meta>
              </h-list-item>
            </h-list>
          </h-card>
        </h-col>
        <h-col style="padding: 0 12px" :xl="8" :lg="24" :md="24" :sm="24" :xs="24">
          <h-card
            title="快速开始 / 便捷导航"
            style="margin-bottom: 24px"
            :bordered="false"
            :body-style="{ padding: 0 }"
          >
            <div class="item-group">
              <a>操作一</a>
              <a>操作二</a>
              <a>操作三</a>
              <a>操作四</a>
              <a>操作五</a>
              <a>操作六</a>
              <h-button size="small" type="primary" ghost>
                <template #icon><plus-outlined /></template>
                添加
              </h-button>
            </div>
          </h-card>
          <h-card
            title="XX 指数"
            style="margin-bottom: 24px"
            :loading="radar.loading"
            :bordered="false"
            :body-style="{ padding: 0 }"
          >
            <div style="min-height: 400px">
              <!-- :scale="scale" :axis1Opts="axis1Opts" :axis2Opts="axis2Opts"  -->
              <radar :data="radar.dataSource" :height="350" />
            </div>
          </h-card>
          <h-card :loading="projects.loading" title="团队" :bordered="false">
            <div class="members">
              <h-row>
                <h-col
                  v-for="item in projects.dataSource"
                  :key="`members-item-${item.id}`"
                  :span="12"
                >
                  <a :href="item.href">
                    <h-avatar size="small" :src="item.logo" />
                    <span class="member">{{ item.member }}</span>
                  </a>
                </h-col>
              </h-row>
            </div>
          </h-card>
        </h-col>
      </h-row>
    </div>
  </page-container>
</template>

<script lang="ts">
import { defineComponent, reactive, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { PlusOutlined } from '@ant-design/icons-vue';
import { useFetchData } from '@/utils/hooks/useFetchData';
import { fakeChartData, queryActivities, queryProjectNotice } from '@/api/dashboard/workplace';
import Radar from './components/radar/index.vue';
import { useStore } from 'vuex';

export default defineComponent({
  setup() {
    const { t } = useI18n();
    const store = useStore();

    const state = reactive({
      loading: true,
      radarLoading: true,
      teams: [],
      // data
      axis1Opts: {
        dataKey: 'item',
        line: null,
        tickLine: null,
        grid: {
          lineStyle: {
            lineDash: null,
          },
          hideFirstLine: false,
        },
      },
      axis2Opts: {
        dataKey: 'score',
        line: null,
        tickLine: null,
        grid: {
          type: 'polygon',
          lineStyle: {
            lineDash: null,
          },
        },
      },
      scale: [
        {
          dataKey: 'score',
          min: 0,
          max: 80,
        },
      ],
      axisData: [
        { item: '引用', a: 70, b: 30, c: 40 },
        { item: '口碑', a: 60, b: 70, c: 40 },
        { item: '产量', a: 50, b: 60, c: 40 },
        { item: '贡献', a: 40, b: 50, c: 40 },
        { item: '热度', a: 60, b: 70, c: 40 },
        { item: '引用', a: 70, b: 50, c: 40 },
      ],
    });

    const { context: projects } = useFetchData(() => {
      return queryProjectNotice().then(res => {
        return {
          data: res,
          total: 100,
        };
      });
    });
    const { context: activities } = useFetchData(() => {
      return queryActivities().then(res => {
        return {
          data: res,
          total: 100,
        };
      });
    });

    const { context: radar } = useFetchData(() => {
      return fakeChartData().then(res => {
        return {
          data: res.radarData,
          total: 100,
        };
      });
    });

    return {
      t,
      state,
      currentUser: computed(() => store.getters[`user/currentUser`]),
      projects,
      activities,
      radar,
    };
  },
  components: {
    Radar,
    PlusOutlined,
  },
});
</script>

<style lang="less" scoped>
@import './index.less';
</style>
