<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]">
        <el-card shadow="hover" class="mb-[10px]" v-if="!roles.includes('expert')">
          <div class="flex items-center">
            <span class="sel-label">专家姓名：</span>
            <el-select v-model="expert" placeholder="" @change="changeExpert" filterable style="width: 240px">
              <el-option
                v-for="item in expertOptions"
                :key="item.userId"
                :label="`${item.workUnit} - ${item.expertName}（${item.contact}）`"
                :value="item.userId"
              >
              </el-option>
            </el-select>
          </div>
        </el-card>
        <div class="card-body mb-[10px]">
          <div class="card-title flex justify-between items-center mb-0">
            <div>专家画像全景图</div>
          </div>
          <div class="card-content p-[20px] flex gap-[20px] pt-0">
            <div class="flex-1">
              <div class="flex justify-between flex-1">
                <div class="flex items-start flex-grow-[6] expert-info">
                  <div class="avatar">
                    <img :src="expertInfo.avatar" class="h-[100%] w-auto" />
                  </div>
                  <div class="info lh-[24px]">
                    <div>
                      <div class="info-label">姓名：</div>
                      <div class="info-value color-click" @click="gotoDetail" v-if="!roles.includes('expert')">{{ expertInfo.name }}</div>
                      <div class="info-value" v-else>{{ expertInfo.name }}</div>
                    </div>
                    <div>
                      <div class="info-label">性别：</div>
                      <div class="info-value">{{ sexObj[expertInfo.sex] }}</div>
                    </div>
                    <div>
                      <div class="info-label">生日：</div>
                      <div class="info-value">{{ expertInfo.birthDate }}</div>
                    </div>
                  </div>
                  <div class="tag">
                    <div class="flex items-center flex-wrap">
                      <div class="info-label">领域：</div>
                      <div class="info-value flex gap-[10px]">
                        <el-tag v-for="item in expertInfo.performWorkId">{{ performWorkTypeObj[item] }}</el-tag>
                      </div>
                    </div>
                    <div class="flex items-center flex-wrap">
                      <div class="flex-1 flex items-center lh-[39px] border-right">
                        <div class="info-label">标签：</div>
                        <div class="info-value" v-if="expertInfo.tag.length > 0">
                          <el-tag type="danger" v-for="item in expertInfo.tag">{{ item }}</el-tag>
                        </div>
                        <div class="info-value" v-else>
                          <el-tag type="danger">-</el-tag>
                        </div>
                      </div>
                      <div class="flex-1 flex items-center lh-[39px]">
                        <div class="info-label">积分：</div>
                        <div class="info-value">
                          <el-tag type="danger">{{ expertInfo.score ? +expertInfo.score + '分' : '-' }}</el-tag>
                        </div>
                      </div>
                    </div>
                    <div class="flex items-center flex-wrap">
                      <div class="info-label">信用等级：</div>
                      <div class="info-value">
                        <el-tag type="danger">{{ expertInfo.creditGrade || '-' }}</el-tag>
                      </div>
                    </div>
                  </div>
                </div>
              </div>

              <div class="other-info flex mt-[20px] expert-base-info">
                <div v-for="(value, key, index) in otherInfo" class="flex items-start base-info-item" :class="{ 'is-last': value.isLast }">
                  <div class="font-bold base-info-label">{{ value.label }}：</div>
                  <div class="base-info-value">
                    {{ value.value || '-' }}
                  </div>
                </div>
              </div>
            </div>
            <div class="chart-radar">
              <radarChart :data="radarData" />
            </div>
          </div>
        </div>
        <div class="knowledge-graph">
          <div class="card-title flex justify-between items-center">
            <div>专家知识图谱</div>
          </div>
          <div class="card-content gap-[20px]">
            <div class="flex-1">
              <div class="card-body mb-[10px]">
                <div class="card-title flex justify-between items-center">
                  <div>同毕业院校专家</div>
                </div>
                <div class="card-content chart-content">
                  <graphChart name="同毕业院校专家" :loading="graphLoading" :curUser="curUser" :data="schoolUsers" />
                </div>
              </div>
            </div>
            <div class="flex-1">
              <div class="card-body mb-[10px]">
                <div class="card-title flex justify-between items-center">
                  <div>同单位专家</div>
                </div>
                <div class="card-content chart-content">
                  <graphChart name="同单位专家" :loading="graphLoading" :curUser="curUser" :data="workUsers" />
                </div>
              </div>
            </div>
            <div class="flex-1">
              <div class="card-body mb-[10px]">
                <div class="card-title flex justify-between items-center">
                  <div>同专业领域专家</div>
                </div>
                <div class="card-content chart-content">
                  <graphChart name="同专业领域专家" :loading="graphLoading" :curUser="curUser" :data="performUsers" />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script setup name="LeaveLog" lang="ts">
import { chartBase, sameOther, expertList } from '@/api/expert/portrait';
import useUserStore from '@/store/modules/user';
import radarChart from './radar-chart.vue';
import graphChart from './graph-chart.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { work_professial_year } = toRefs<any>(proxy?.useDict('work_professial_year'));
const expert = ref('');
const expertOptions = ref([]);
const userStore = useUserStore();
const roles = ref(userStore.roles);
const getExpertLists = async () => {
  let res = await expertList();
  if (res.code != 200) {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: 'error'
    });
    return;
  }
  expertOptions.value = res.data;
  expert.value = expertOptions.value[0].userId;
  changeExpert();
};
const defaultAvatar = new URL('@/assets/images/avatar.png', import.meta.url).href;
const { perform_work_type } = toRefs<any>(proxy?.useDict('perform_work_type'));
const performWorkType = computed(() => perform_work_type.value);
const performWorkTypeObj = computed(() => {
  let obj: any = {};
  performWorkType.value.forEach((item: any) => {
    obj[item.value] = item.label;
  });
  return obj;
});
const { sys_user_sex } = toRefs<any>(proxy?.useDict('sys_user_sex'));
const sysUserSex = computed(() => sys_user_sex.value);
let sexObj = computed(() => {
  let obj = {};
  sysUserSex.value.forEach((item: any) => {
    obj[item.value] = item.label;
  });
  return obj;
});
const { politics } = toRefs<any>(proxy?.useDict('politics'));
let politicsObj = computed(() => {
  let obj = {};
  politics.value.forEach((item: any) => {
    obj[item.value] = item.label;
  });
  return obj;
});

const expertInfo = ref({
  id: '',
  avatar: defaultAvatar,
  name: '',
  sex: '',
  birthDate: '',
  performWorkId: [],
  tag: [],
  creditGrade: '',
  score: 0
});
const tipJson = {
  1: '积分异常，待管理员复核。',
  2: '经复核，账号积分存在问题，暂时冻结。'
};
const otherInfo = ref({
  workUnit: {
    label: '工作单位',
    value: ''
  },
  duty: {
    label: '职务',
    value: ''
  },
  politics: {
    label: '政治面貌',
    value: ''
  },
  contact: {
    label: '联系方式',
    value: ''
  },
  school: {
    label: '毕业院校',
    value: ''
  },
  workMajor: {
    label: '工作年限',
    value: ''
  },
  email: {
    label: '邮箱',
    value: ''
  },
  origin: {
    label: '籍贯',
    value: ''
  },
  major: {
    label: '专业/学历',
    value: ''
  }
});
const radarData = ref([]);
const getExpertInfo = async () => {
  let res = await chartBase(expert.value);
  if (res.code != 200) {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: 'error'
    });
    return;
  }
  if (res.data) {
    expertInfo.value = {
      id: res.data.expertId,
      avatar: res.data.photograph || defaultAvatar,
      name: res.data.expertName,
      sex: res.data.sex,
      birthDate: res.data.birthDate ? res.data.birthDate.split(' ')[0] : '-',
      performWorkId: res.data.performWorkId.replace(/\r/, '').split(','),
      tag: res.data.rewardTitle ? res.data.rewardTitle : [],
      creditGrade: res.data.creditName,
      score: res.data.points
    };
    otherInfo.value.contact.value = res.data.contact;
    otherInfo.value.workUnit.value = res.data.workUnit;
    otherInfo.value.duty.value = res.data.duty;
    otherInfo.value.school.value = res.data.graduateSchool;
    otherInfo.value.email.value = res.data.email;
    otherInfo.value.politics.value = politicsObj.value[res.data.politics];
    otherInfo.value.major.value = res.data.major;
    otherInfo.value.origin.value = res.data.origin;
    const findYear = work_professial_year.value.find(fitem => fitem.value === res.data.workProfessialYear.toString())
    otherInfo.value.workMajor.value = findYear ? findYear.label : '';
    radarData.value = [
      res.data.chart.scorePerformWork || 0,
      res.data.chart.scoreCredit || 0,
      res.data.chart.scoreJoinTime || 0,
      res.data.chart.scoreEvaluate || 0,
      res.data.chart.scoreAwards || 0,
      res.data.chart.scoreWorkYear || 0,
      res.data.chart.scorePatent || 0,
      res.data.chart.scorePaper || 0
    ];
  }

};
const gotoDetail = () => {
  proxy?.$router.push({
    path: '/expert/expertDetail',
    query: {
      id: expertInfo.value.id,
      type: 'detail'
    }
  });
};
const curUser = ref(null);
const schoolUsers = ref([]);
const workUsers = ref([]);
const performUsers = ref([]);
const graphLoading = ref(false);
const getSameOther = async () => {
  graphLoading.value = true;
  let res = await sameOther(expert.value);
  if (res.code != 200) {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: 'error'
    });
    return;
  }
  curUser.value = res.data.curUser;
  schoolUsers.value = res.data.schoolUsers;
  workUsers.value = res.data.workUsers;
  performUsers.value = res.data.performUsers;
  graphLoading.value = false;
};
const changeExpert = () => {
  getExpertInfo();
  getSameOther();
};
onMounted(() => {
  if (!roles.value.includes('expert')) {
    getExpertLists();
  } else {
    changeExpert();
  }
});
</script>
<style lang="scss" scoped>
.sel-label {
  font-weight: bold;
  white-space: nowrap;
}

.card-body {
  padding-top: 50px;
  border: 1px solid var(--el-border-color-light);
  border-radius: 6px;
  transition: var(--el-transition-duration);
  position: relative;
  box-sizing: border-box;
  height: 100%;
  position: relative;
  font-size: 14px;
  &:hover {
    box-shadow: var(--el-box-shadow-light);
  }
}
.card-title {
  position: absolute;
  top: 10px;
  left: 20px;
  font-weight: bold;
  font-size: 20px;
  line-height: 20px;
  width: calc(100% - 20px);
}
.card-content {
  display: flex;
  align-items: center;
  position: relative;
  .card-title {
    font-size: 16px;
  }
}
.knowledge-graph {
  padding-top: 50px;
  position: relative;
  box-sizing: border-box;
  height: 100%;
  position: relative;
  font-size: 14px;
}
.chart-radar {
  width: 262px;
  aspect-ratio: 1/1;
}
.expert-base-info {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-top: 1px solid #ddd;
  border-left: 1px solid #ddd;
  .base-info-item {
    flex: 1;
    &.is-last {
      grid-column: 2 / -1;
    }
    border-bottom: 1px solid #ddd;
    border-right: 1px solid #ddd;
    min-height: 40px;
    line-height: 38px;
    display: flex;
    justify-content: center;
    align-items: center;
    .base-info-label {
      width: 200px;
      padding: 0 5px;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      border-right: 1px solid #ddd;
      background-color: #f9f9f9;
    }
    .base-info-value {
      text-align: center;
      flex: 1;
      padding: 0 5px;
    }
  }
}
.expert-info {
  border-top: 1px solid #ddd;
  border-left: 1px solid #ddd;
  box-sizing: border-box;
  & > div {
    border-right: 1px solid #ddd;
    height: 120px;
    &.avatar {
      width: 120px;
      padding: 10px;
      border-bottom: 1px solid #ddd;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: auto;
        height: auto;
        max-width: 100%;
        max-height: 100%;
      }
    }
    &.info {
      width: calc(100% / 3 - 120px);
      line-height: 39px;
      & > div {
        display: flex;
        align-items: center;
        border-bottom: 1px solid #ddd;
        .info-label {
          width: 80px;
          padding: 0 10px;
          border-right: 1px solid #ddd;
          font-weight: bold;
          background-color: #f9f9f9;
          text-align: center;
        }
        .info-value {
          flex: 1;
          padding: 0 10px;
        }
      }
    }
    &.tag {
      width: calc(100% / 3 * 2);
      & > div {
        height: 40px;
        border-bottom: 1px solid #ddd;
        line-height: 39px;
        .info-label {
          width: 100px;
          padding: 0 10px;
          border-right: 1px solid #ddd;
          font-weight: bold;
          background-color: #f9f9f9;
          text-align: center;
        }
        .info-value {
          flex: 1;
          padding: 0 10px;
        }
      }
    }
  }
}
.border-right {
  border-right: 1px solid #ddd;
}
.color-click {
  color: var(--el-color-primary);
  cursor: pointer;
}
.chart-content {
  aspect-ratio: 1/0.8;
}
</style>
