<template>
  <div v-loading="loading">
    <searchContainer
      bannerKey="首页>专家库>专家详情"
      title="专家库"
      @handleQuery="handleQuery"
    >
      <template v-slot>
        <div class="head">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>
              <span class="one">找专家</span>
            </el-breadcrumb-item>
            <el-breadcrumb-item>
              <span>专家详情</span>
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-row type="flex" style="padding-bottom: 20px">
          <div style="flex: 1">
            <expertItem :data="state.expertDetail"></expertItem>
            <div class="btns">
              <div class="order">预约对接</div>
              <div class="share">一键分享</div>
            </div>

            <div class="detail">
              <div style="padding: 20px 0">
                <div class="pointTit">个人简介</div>
              </div>
              <div class="html" v-html="state.expertDetail.introduce"></div>
              <div style="padding: 20px 0">
                <div class="pointTit">研究方向</div>
              </div>
              <section v-if="state.expertDetail.researchs">
                <div
                  v-for="item in state.expertDetail.researchs"
                  class="describe"
                >
                  {{ item }}
                </div>
              </section>
              <div style="padding: 20px 0">
                <div class="pointTit">专家视频</div>
              </div>
            </div>
            <div v-if="state.expertDetail.id" style="padding: 20px 0">
              <el-tabs v-model="state.activeName">
                <el-tab-pane label="代表成果及合作模式" name="first">
                  <loadMore
                    :getData="achievement"
                    :params="{ expert_id: state.expertDetail.id }"
                  >
                    <template #default="scope">
                      <div class="achievementItem">
                        <div class="tit">
                          【代表成果】{{ scope.item.title }}
                        </div>
                        <div class="line">
                          技术领域：
                          <span>{{
                            scope.item.industrys[
                              scope.item.industrys.length - 1
                            ]
                          }}</span>
                        </div>
                        <div class="line">
                          完成单位：
                          <span>{{ scope.item.cooperation_mode_title }}</span>
                        </div>
                        <div class="line">
                          合作模式：
                          <span>{{ scope.item.cooperation_mode_title }}</span>
                        </div>
                      </div>
                    </template>
                  </loadMore>
                </el-tab-pane>
                <el-tab-pane label="科研项目" name="second">
                  <loadMore
                    :getData="project"
                    :params="{ expert_id: state.expertDetail.id }"
                  >
                    <template #default="scope">
                      <div class="loadMoreItem">
                        <div class="tit">
                          【科研项目】{{ scope.item.title }}
                        </div>
                        <div class="line">
                          <el-row>
                            <el-col :span="12"
                              >发布日期：{{
                                parseTime(scope.item.created_at)
                              }}</el-col
                            >
                          </el-row>
                        </div>
                        <div class="line">
                          <el-row>
                            <el-col :span="12"
                              >开始日期：{{
                                parseTime(scope.item.begin_at)
                              }}</el-col
                            >
                            <el-col :span="12"
                              >结束日期：{{
                                parseTime(scope.item.finish_at)
                              }}</el-col
                            >
                          </el-row>
                        </div>
                        <div class="line">
                          <el-row>
                            <el-col :span="12"
                              >科研负责人：{{ scope.item.director }}</el-col
                            >
                            <el-col :span="12"
                              >项目类型：{{ scope.item.kind }}</el-col
                            >
                          </el-row>
                        </div>
                        <div class="line">
                          <el-row>
                            <el-col :span="12"
                              >资助经费：{{ scope.item.amount }}元</el-col
                            >
                            <el-col :span="12"
                              >资助来源：{{ scope.item.source }}</el-col
                            >
                          </el-row>
                        </div>
                      </div>
                    </template>
                  </loadMore>
                </el-tab-pane>
                <el-tab-pane label="代表专利" name="third">
                  <loadMore
                    :getData="patent"
                    :params="{ expert_id: state.expertDetail.id }"
                  >
                    <template #default="scope">
                      <div class="patentItem">
                        <div class="tit">
                          【发明专利】{{ scope.item.title }}
                        </div>
                        <div class="line">
                          申请日期：
                          <span>{{ scope.item.apply_at }}</span>
                        </div>
                        <div class="line">
                          <p>摘要：</p>
                          <span>{{ scope.item.description }}</span>
                        </div>
                      </div>
                    </template>
                  </loadMore>
                </el-tab-pane>
                <el-tab-pane label="代表论文" name="fourth">
                  <loadMore
                    :getData="paper"
                    :params="{ expert_id: state.expertDetail.id }"
                  >
                    <template #default="scope">
                      <div class="loadMoreItem">
                        <div class="tit">
                          【期刊论文】{{ scope.item.title }}
                        </div>
                        <div class="line">
                          <el-row>
                            <el-col :span="12"
                              >出版日期：
                              {{ parseTime(scope.item.publish_at) }}</el-col
                            >
                            <el-col :span="12"
                              >刊物： {{ scope.item.ext }}</el-col
                            >
                          </el-row>
                        </div>
                        <div class="line">
                          关键词：
                          <span
                            v-for="keyword in scope.item.keywords"
                            style="margin-right: 10px"
                            >{{ keyword }}</span
                          >
                        </div>
                        <div class="line">
                          <p>摘要：</p>
                          <span>{{ scope.item.remark }}</span>
                        </div>
                      </div>
                    </template>
                  </loadMore>
                </el-tab-pane>
                <el-tab-pane label="已合作企业" name="five">
                  <loadMore
                    :getData="cooperate"
                    :params="{ expert_id: state.expertDetail.id }"
                  >
                    <template #default="scope">
                      <div class="loadMoreItem">
                        <el-row v-loading="scope.item['loading']">
                          <el-col :span="12">
                            <div
                              class="tit"
                              style="line-height: 35px; margin: 0"
                            >
                              {{ scope.item.name }}
                            </div>
                          </el-col>
                          <el-col
                            v-if="scope.item['patent']"
                            @click="
                              () => {
                                delete scope.item['patent'];
                              }
                            "
                            style="cursor: pointer"
                            :offset="6"
                            :span="6"
                            >所发表专利
                            {{ scope.item["patent"].length }} 篇</el-col
                          >
                          <el-col
                            v-else-if="scope.item['paper']"
                            @click="
                              () => {
                                delete scope.item['paper'];
                              }
                            "
                            style="cursor: pointer"
                            :offset="6"
                            :span="6"
                            >所发表论文
                            {{ scope.item["paper"].length }} 篇</el-col
                          >
                          <el-col v-else :span="12">
                            <el-row>
                              <el-col :span="12">
                                <div
                                  class="line"
                                  @click="cooperateDetail(scope.item, 'patent')"
                                  style="cursor: pointer"
                                >
                                  专利：{{ scope.item.patent_count }}
                                </div>
                              </el-col>
                              <el-col :span="12">
                                <div
                                  class="line"
                                  @click="cooperateDetail(scope.item, 'paper')"
                                  style="cursor: pointer"
                                >
                                  论文：{{ scope.item.paper_count }}
                                </div>
                              </el-col>
                            </el-row>
                          </el-col>
                        </el-row>
                      </div>

                      <div
                        class="patentItem"
                        v-for="item in scope.item['patent']"
                      >
                        <div class="tit">【发明专利】{{ item.title }}</div>
                        <div class="line">
                          申请日期：
                          <span>{{ item.apply_at }}</span>
                        </div>
                        <div class="line">
                          <p>摘要：</p>
                          <span>{{ item.description }}</span>
                        </div>
                      </div>
                      <div
                        class="loadMoreItem"
                        v-for="item in scope.item['paper']"
                      >
                        <div class="tit">【期刊论文】{{ item.title }}</div>
                        <div class="line">
                          <el-row>
                            <el-col :span="12"
                              >出版日期：
                              {{ parseTime(item.publish_at) }}</el-col
                            >
                            <el-col :span="12">刊物： {{ item.ext }}</el-col>
                          </el-row>
                        </div>
                        <div class="line">
                          关键词：
                          <span
                            v-for="keyword in item.keywords"
                            style="margin-right: 10px"
                            >{{ keyword }}</span
                          >
                        </div>
                        <div class="line">
                          <p>摘要：</p>
                          <span>{{ item.remark }}</span>
                        </div>
                      </div>
                    </template>
                  </loadMore>
                </el-tab-pane>
              </el-tabs>
            </div>

            <div style="padding: 20px 0">
              <div class="pointTit">专家推荐</div>
            </div>
            <div>
              <section v-for="item in state.list" :key="item.id">
                <expertItem :data="item"></expertItem>
              </section>
            </div>
          </div>
          <div class="r">
            <webContact />
          </div>
        </el-row>
      </template>
    </searchContainer>
  </div>
</template>

<script setup>
import webContact from "@/components/webContact/index.vue";
import expertItem from "./components/expertItem.vue";
import loadMore from "./components/loadMore.vue";
import request from "@/utils/request";
const router = useRouter();
const route = useRoute();
import { onMounted } from "vue";
import searchContainer from "./components/searchContainer.vue";
import wordcloud from "./components/wordcloud.vue";
import productItem from "./components/productItem.vue";
const loading = ref(true);
const state = reactive({
  pageNum: 1,
  pageSize: 4,
  keyword: "",
  val: 3,
  expertDetail: {},
  arr: [
    {
      name: "Cat",
      value: 26,
    },
    {
      name: "fish",
      value: 19,
    },
  ],
  activeName: "first",
});

// 建议 列表前4条
function recommend() {
  return request({
    url: "/v1/search",
    method: "post",
    data: {
      mode: 5,
      page_num: state.pageNum,
      page_size: state.pageSize,
      keyword: state.keyword,
    },
  });
}

// 专家详情
function expert(id) {
  return request({
    url: "/v1/manage/expert",
    method: "post",
    data: { expert_id: id },
  });
}

// 合作成果
function achievement(data) {
  return request({
    url: "/v1/manage/expert/achievement",
    method: "post",
    data,
  });
}

// 科研项目
function project(data) {
  return request({
    url: "/v1/manage/expert/project",
    method: "post",
    data,
  });
}

// 代表专利
function patent(data) {
  return request({
    url: "/v1/manage/expert/patent",
    method: "post",
    data,
  });
}
// 代表论文
function paper(data) {
  return request({
    url: "/v1/manage/expert/paper",
    method: "post",
    data,
  });
}

// 合作企业
function cooperate(data) {
  return request({
    url: "/v1/manage/expert/cooperate",
    method: "post",
    data,
  });
}
async function cooperateDetail(item, type) {
  item["loading"] = true;
  let { code, msg, data } = await request({
    url: "/v1/manage/expert/cooperate/detail",
    method: "post",
    data: { id: item.id },
  });
  if (200 == code) {
    item[type] = data[type];
  }
  item["loading"] = false;
}

function createdData(arr) {
  let l = [];
  let snap = JSON.parse(JSON.stringify(arr));
  snap.map((e) => {
    l.push({ name: e, value: 30 });
    return { name: e, value: 30 };
  });
  return l;
}

function handleDetail(id) {
  let routeData = router.resolve({
    path: `/searchList/0/detail/${id}`,
    query: { keyword: state.keyword },
  });
  window.open(routeData.href, "_blank");
}

function handleList(mode, keyword) {
  router.push({ path: `/searchList/${mode}`, query: { keyword } });
}

// 首次加载数据，其后跳转页面
let flag = true;

function handleQuery(keyword) {
  state.keyword = keyword;
  if (flag) {
    getDataList();
    flag = false;
  } else {
    handleList(0, keyword);
  }
}

function getDataList() {
  loading.value = true;
  recommend().then((res) => {
    if (200 == res.code) {
      // 总条数
      state.total = res.data.count;
      state.list = res.data.data;
    }
  });
  let id = route.params.id;
  if (!id) return;
  expert(id)
    .then((res) => {
      if (200 == res.code) {
        let str = " ".repeat(8);
        let nbsp = "\u00A0".repeat(8);
        res.data.introduce = res.data.introduce.replace(/\n/g, "<br>");
        res.data.introduce = res.data.introduce.replace(
          new RegExp(str, "g"),
          nbsp
        );
        state.expertDetail = res.data;
        loading.value = false;
      }
    })
    .catch(() => {
      loading.value = false;
    });
  // companyProduct(id).then(res => {
  //     if (200 == res.code) {
  //         state.companyProduct = res.data.data;
  //     }
  // })
}
</script>

<style lang="scss" scoped>
:deep(.el-tabs) {
  .el-tabs__item {
    color: #666666;
    font-size: 14px;
  }
  .el-tabs__item:hover {
    opacity: 0.8;
  }

  .el-tabs__item.is-active {
    font-size: 16px;
    font-family: Source Han Sans CN;
    font-weight: bold;
    color: #333333;
  }
  .el-tabs__active-bar {
    background-color: #333333;
    height: 4px;
  }

  .el-tabs__nav-wrap::after {
    background-color: transparent;
  }
}
.head {
  padding: 15px 0px;
  span {
    font-size: 16px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #666666;
  }
  .one {
    color: #333333;
  }
}
.r {
  display: inline-block;
  width: 325px;
  margin-left: 14px;
  min-height: 200px;
}

.detail {
  width: 661px;
  box-sizing: border-box;
  padding: 20px;
  background: #fff;
  .describe {
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #666666;
    margin-bottom: 5px;
  }
}

.pointTit {
  position: relative;
  padding-left: 10px;
  font-size: 16px;
  font-family: Source Han Sans CN;
  font-weight: 500;
  color: #333333;
  &:before {
    content: "";
    top: 8px;
    left: 0px;
    position: absolute;
    display: block;
    width: 5px;
    height: 5px;
    background: #0054ff;
  }
}

.btns {
  overflow: hidden;
  padding: 0 20px 20px 20px;
  width: 100%;
  background-color: #fff;
  margin-top: -20px;
  div {
    text-align: center;
    float: right;
    cursor: pointer;
    user-select: none;
    width: 101px;
    height: 36px;
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    line-height: 36px;
    box-sizing: border-box;
    &:hover {
      opacity: 0.7;
    }
  }
  .share {
    color: #333333;
    background: #f2f6ff;
    border: 1px solid #dcdcdc;
    margin-right: 10px;
  }
  .order {
    background: #0054ff;
    color: #ffffff;
    border: none;
  }
}

.achievementItem,
.loadMoreItem {
  padding: 20px;
  background-color: #fff;
  margin-bottom: 10px;
  .tit {
    margin-left: -8px;
    font-size: 16px;
    font-family: Source Han Sans CN;
    font-weight: bold;
    color: #333333;
  }
  .line {
    margin: 10px 0;
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #666666;
  }
}

.patentItem {
  padding: 20px;
  background-color: #fff;
  margin-bottom: 10px;
  .tit {
    font-size: 16px;
    font-family: Source Han Sans CN;
    font-weight: bold;
    color: #333333;
  }
  .line {
    padding-left: 8px;
    margin: 10px 0;
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #666666;
  }
}
</style>