<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
        <div class="w_1200">
          <el-col :span="24" class="one">
            <el-col :span="24" class="oneText">
              <el-col :span="4" class="one_1">
                <el-image class="image" :src="info.icon || zj" fit="fill" />
              </el-col>
              <el-col :span="14" class="one_2">
                <el-col :span="24" class="name textOver">
                  <el-tooltip effect="dark" :content="info.name" placement="top">
                    {{ info.name || '暂无姓名' }}
                  </el-tooltip>
                  <div class="tags">
                    <el-tag type="primary" v-if="info.title">{{
                      getDict(info.title || '暂无', 'title')
                    }}</el-tag>
                    <el-tag type="success" v-if="info.education">{{
                      getDict(info.education || '暂无', 'education')
                    }}</el-tag>
                  </div>
                </el-col>
                <div class="other"><span> 出生年月：</span>{{ info.birth || '暂无' }}</div>
                <div class="other"><span> 所在地区：</span>{{ getArea(info.area) }}</div>
                <div class="other"><span> 技术领域：</span>{{ getDict(info.field, 'field') }}</div>
                <div class="other"><span> 研究方向：</span>{{ info.direction || '暂无' }}</div>
                <div class="other"><span> 工作单位：</span>{{ info.work || '暂无' }}</div>
              </el-col>
              <el-col :span="6" class="one_3">
                <el-col :span="10" class="file" @click="toCollection(0)" v-if="info.is_collection">
                  <el-icon :size="18"><StarFilled /></el-icon>
                  <span>已收藏</span>
                </el-col>
                <el-col :span="10" class="file" @click="toCollection(1)" v-else>
                  <el-icon :size="18"><Star /></el-icon>
                  <span>收藏</span>
                </el-col>
                <el-col :span="14">
                  <a-button type="primary" @click="toChat"> 在线洽谈 </a-button>
                </el-col>
              </el-col>
            </el-col>
            <el-col :span="24" class="twoText">
              <el-col :span="24" class="title">个人简介</el-col>
              <el-col :span="24" class="brief">{{ info.brief || '暂无相关简介' }}</el-col>
            </el-col>
            <el-col :span="24" class="twoText">
              <el-col :span="24" class="title">相关成果</el-col>
              <el-col :span="24" class="content">
                <a-list :loading="loading" :grid="{ gutter: 16, column: 4 }" :data-source="list">
                  <template #renderItem="{ item }">
                    <el-col :span="24" class="list">
                      <el-col :span="24" class="name textOver">
                        <el-tooltip effect="dark" :content="item.name" placement="top">
                          {{ item.name || '暂无名称' }}
                        </el-tooltip>
                      </el-col>
                      <el-col :span="24" class="two_1">
                        <span>技术领域：</span>{{ getDict(item.field, 'field') }}
                      </el-col>
                      <el-col :span="24" class="two_1">
                        <span>成果地区：</span>{{ getArea(item.area) }}
                      </el-col>
                      <el-col :span="24" class="two_1">
                        <span>单位：</span>{{ item.user || '暂无单位' }}
                      </el-col>
                      <el-col :span="24" class="bottom">
                        <a-button size="small" type="primary" @click="toView(item)">
                          查看详情
                        </a-button>
                      </el-col>
                    </el-col>
                  </template>
                </a-list>
                <el-col :span="24" class="page">
                  <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :page-sizes="[10, 20, 50, 100, 200]"
                    :total="total"
                    :page-size="limit"
                    v-model:current-page="currentPage"
                    @current-change="changePage"
                    @size-change="sizeChange"
                  >
                  </el-pagination>
                </el-col>
              </el-col>
            </el-col>
          </el-col>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import moment from 'moment'
// 基础
import { get } from 'lodash-es'
const $checkRes = inject('$checkRes')
// 接口
import { ExpertStore } from '@/store/api/user/expert'
import { AchievementStore } from '@/store/api/platform/achievement'
import { CollectionStore } from '@/store/api/platform/collection'
import { DictDataStore } from '@/store/api/system/dictData'
const achievementStore = AchievementStore()
const store = ExpertStore()
const dictDataStore = DictDataStore()
const collectionStore = CollectionStore()
import { UserStore } from '@/store/user'
const userStore = UserStore()
const user = computed(() => userStore.user)
// 图片引入
import zj from '@/assets/zj.png'
// 路由
const route = useRoute()
const router = useRouter()
// 加载中
const loading = ref(false)
const info = ref({})
// 字典表
const educationList = ref([])
const fieldList = ref([])
const titleList = ref([])
// 成果列表
const list = ref([])
let skip = 0
let limit = inject('limit')
const total = ref(0)
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search()
  await searchAchieve({ skip, limit })
  loading.value = false
})
const search = async () => {
  let id = route.query.id
  if (id) {
    let res = await store.detail(id)
    if (res.errcode == '0') info.value = res.data
  }
}
const searchAchieve = async (query = { skip: 0, limit }) => {
  const info = {
    skip: query.skip,
    limit: query.limit,
    is_use: '0',
    status: '1'
  }
  const res = await achievementStore.query(info)
  if (res.errcode == '0') {
    list.value = res.data
    total.value = res.total
  }
}
const searchOther = async () => {
  let result
  // 学历
  result = await dictDataStore.query({ code: 'education', is_use: '0' })
  if ($checkRes(result)) educationList.value = result.data
  // 技术领域
  result = await dictDataStore.query({ code: 'field', is_use: '0' })
  if ($checkRes(result)) fieldList.value = result.data
  // 职称
  result = await dictDataStore.query({ code: 'title', is_use: '0' })
  if ($checkRes(result)) titleList.value = result.data
}
// 字典数据转换
const getDict = (data, model) => {
  let res
  if (model == 'education') res = educationList.value.find((f) => f.value == data)
  if (model == 'field') res = fieldList.value.find((f) => f.value == data)
  if (model == 'title') res = titleList.value.find((f) => f.value == data)
  return get(res, 'label')
}
// 地区
const getArea = (data) => {
  if (data) return data.join(',')
  else return '暂无地区'
}
// 在线洽谈
const toChat = () => {
  router.push({ path: '/chat', query: { id: info.value.id || info.value._id } })
}
// 收藏
const toCollection = async (status) => {
  if (user.value._id) {
    let res
    let message
    const data = {
      user: user.value._id,
      source: info.value._id,
      type: 'expert',
      time: moment().format('YYYY-MM-DD')
    }
    if (status == '1') {
      message = '收藏成功'
      res = await collectionStore.create(data)
    } else {
      message = '取消收藏成功'
      res = await collectionStore.cancel(data)
    }
    if (res.errcode === 0) {
      ElMessage({
        message,
        type: 'success'
      })
      await search()
    }
  } else {
    ElMessage({
      message: '未登录无法进行收藏 请登录',
      type: 'warning'
    })
  }
}
// 查看
const toView = (item) => {
  router.push({ path: '/achievement/detail', query: { id: item.id || item._id } })
}
const currentPage = ref(1)
// 分页
const changePage = (page = currentPage.value) => {
  searchAchieve({ skip: (page - 1) * limit, limit: limit })
}
const sizeChange = (limits) => {
  console.log(limits)
  limit = limits
  currentPage.value = 1
  searchAchieve({ skip: 0, limit: limit })
}
</script>
<style scoped lang="scss">
.main {
  background: #f7f7f7;
  .one {
    margin: 10px 0;
    background-color: #fff;
    padding: 24px;
    border-top: 6px solid #2374ff;
    overflow: hidden;
    border-radius: 0 0 5px 5px;

    .oneText {
      display: flex;
      padding: 20px;
      margin: 10px 0;
      border-radius: 10px;
      border: 1px solid #edeff2;
      .one_1 {
        .image {
          width: 150px;
          height: 150px;
          border-radius: 90px;
        }
      }
      .one_2 {
        .name {
          display: flex;
          align-items: center;
          font-size: 16px;
          color: #121834;
          font-weight: 500;
          margin: 5px 0 0 0;
          .tags {
            grid-gap: 0.5rem;
            gap: 0.5rem;
            display: flex;
            margin: 0 0 0 10px;
          }
        }

        .name:hover {
          color: #2374ff;
        }
        .other {
          font-size: 12px;
          text-align: justify;
          line-height: 12px;
          font-weight: 400;
          letter-spacing: 0;
          color: #8f97a3;
          margin-top: 10px;

          span:last-child {
            color: #525a68;
          }
        }
      }
      .one_3 {
        display: flex;
        align-items: center;
        justify-content: center;
        .file {
          display: flex;
          align-items: center;
          justify-content: center;
          font-family: PingFangSC-Regular;
          font-size: 14px;
          color: #2374ff;
          span {
            margin: 0 0 0 5px;
          }
        }
      }
    }
    .twoText {
      padding: 20px;
      border-radius: 10px;
      margin: 10px 0;
      border: 1px solid #edeff2;
      .title {
        font-size: 20px;
        color: #383b40;
        letter-spacing: 0;
        line-height: 20px;
        font-weight: 600;
        margin-bottom: 30px;
      }
      .content {
        margin-top: 20px;

        .list {
          border: 1px solid #edeff2;
          background: #fff;
          box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.03);
          border-radius: 2px;
          width: 270px;
          height: 180px;
          margin-bottom: 15px;
          cursor: pointer;
          transition: all 0.3s;
          padding: 15px;
          .name {
            font-size: 16px;
            color: #121834;
            height: 16px;
            line-height: 13px;
            font-weight: 500;
            margin: 10px 0;
          }

          .name:hover {
            color: #2374ff;
          }

          .two_1 {
            font-size: 12px;
            text-align: justify;
            line-height: 12px;
            font-weight: 400;
            letter-spacing: 0;
            color: #8f97a3;
            margin-top: 15px;

            span:last-child {
              color: #525a68;
            }
          }
          .bottom {
            margin: 10px 0 0 0;
            text-align: right;
          }
        }

        .list:hover {
          background: #f0f7ff;
          box-shadow: 0 0 16px rgba(205, 205, 205, 0.6);
        }

        .page {
          display: flex;
          flex-direction: row-reverse;
          padding: 20px;
        }
      }
    }
  }
}
</style>
