<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <div class="search-list">

            <div class="search-item">

              <div class="item-title">
                <span>专业领域：</span>
              </div>

              <div class="item-content">
                <span @click="handleFind(0, 0)"
                  :class="conditionList[0] == 0 ? 'item-text active' : 'item-text'">全部</span>
                <span @click="handleFind(0, parseInt(item.value))"
                  :class="conditionList[0] == item.value ? 'item-text active' : 'item-text'"
                  v-for="item in perform_work_type">{{ item.label }}</span>

              </div>

            </div>

            <div class="search-item">

              <div class="item-title">
                <span>学历：</span>
              </div>

              <div class="item-content">
                <span @click="handleFind(1, 0)"
                  :class="conditionList[1] == 0 ? 'item-text active' : 'item-text'">全部</span>
                <span @click="handleFind(1, parseInt(item.value))"
                  :class="conditionList[1] == item.value ? 'item-text active' : 'item-text'"
                  v-for="item in degree_type">{{ item.label }}</span>

              </div>

            </div>

            <!-- <div class="search-item">
              <div class="item-title">
                <span>专业技术资格：</span>
              </div>
              <div class="item-content">
                <span @click="handleFind(2, item.value)"
                  :class="conditionList[2] == item.value ? 'item-text active' : 'item-text'"
                  v-for="item in tech_qualification">{{ item.label }}</span>
              </div>
            </div> -->

            <div class="search-item">

              <div class="item-title">
                <span>工作年限：</span>
              </div>

              <div class="item-content">
                <span @click="handleFind(3, 0)"
                  :class="conditionList[3] == 0 ? 'item-text active' : 'item-text'">全部</span>
                <span @click="handleFind(3, parseInt(item.value))"
                  :class="conditionList[3] == item.value ? 'item-text active' : 'item-text'"
                  v-for="item in work_professial_year">{{ item.label }}</span>
              </div>

            </div>

            <div class="search-item">

              <div class="item-title">
                <span>单位类型：</span>
              </div>

              <div class="item-content">
                <span @click="handleFind(4, 0)"
                  :class="conditionList[4] == 0 ? 'item-text active' : 'item-text'">全部</span>
                <span @click="handleFind(4, parseInt(item.value))"
                  :class="conditionList[4] == item.value ? 'item-text active' : 'item-text'"
                  v-for="item in unit_type">{{ item.label }}</span>

              </div>

            </div>

          </div>

        </el-card>
      </div>
    </transition>

    <el-card v-loading="loading" shadow="hover" style="position: relative">
      <template #header>
        <div class="card-header">
          <p>入库专家人数：{{ total }}</p>
          <div class="query-name">
            <el-input @clear="getList" clearable v-model="queryData.expertName" placeholder="请输入专家姓名"> <template
                #prefix>
                <el-icon class="el-input__icon">
                  <search />
                </el-icon>
              </template>
            </el-input>
            <el-button @click="getList" plain type="primary">查询</el-button>
          </div>
        </div>
      </template>

      <div v-if="dataList.length > 0" class="list">
        <el-card @click="jumpToDetail(item)" v-for="item in dataList" v-loading="loading" shadow="hover">
          <template #header>
            <div class="card-top">
              <div class="avatar">
                <img v-if="item.photograph" :src="item.photograph" alt="">
                <img v-else src="@/assets/images/avatar.png" alt="">
              </div>
              <div class="user">
                <p>{{ item.expertName }}</p>
                <p>{{ sys_user_sex[item.sex] ? sys_user_sex[item.sex].label : '' }}</p>
                <!-- <p>{{ sys_user_sex[item.sex].label }}</p> -->
                <p>{{ parseTime(item.birthDate, '{y}-{m}-{d}') }}</p>
              </div>
              <div class="tags" v-if="Array.isArray(item.workArr) && item.workArr.length > 0">

              <template v-for="wItem in item.workArr" :key="wItem">
                  <el-tag v-if="perform_work_type.find(fitem => fitem.value === wItem)" type="primary">
                    {{ typeFn(wItem) }}
                  </el-tag>
                </template>
              </div>

            </div>
          </template>
          <div class="card-bot">
            <p>职务：{{ item.duty }}</p>
            <p>工作单位：{{ item.workUnit }}</p>
          </div>

        </el-card>

      </div>

      <el-empty v-else-if="dataList.length == 0" description="暂无数据"></el-empty>



      <pagination v-show="total > 0" :total="total" v-model:page="queryData.pageNum" v-model:limit="queryData.pageSize"
        @pagination="getList" />
    </el-card>


  </div>
</template>

<script setup name="Expert" lang="ts">
import { queryExpert, queryProList } from '@/api/expert/expert/index';
import { ExpertVO, ExpertQuery, ExpertForm } from '@/api/expert/expert/types';
import { globalHeaders } from '@/utils/request';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_user_sex } = toRefs<any>(proxy?.useDict('sys_user_sex'));
const { perform_work_type } = toRefs<any>(proxy?.useDict('perform_work_type'));
const { degree_type } = toRefs<any>(proxy?.useDict('degree_type'));
const { work_professial_year } = toRefs<any>(proxy?.useDict('work_professial_year'));
const { unit_type } = toRefs<any>(proxy?.useDict('unit_type'));
const tech_qualification = ref([{ label: '全部', value: 0 }]);

const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();

const queryFormRef = ref<ElFormInstance>();
const expertFormRef = ref<ElFormInstance>();
//条件列表
const conditionList = ref([0, 0, 0, 0, 0])

const queryData = reactive({
  pageNum: 1,
  pageSize: 8,

  performWorkId: undefined,
  degree: undefined,
  professional: undefined,
  workProfessialYear: undefined,
  unitType: undefined,

  expertName: '',
});
const dataList = ref([]);
/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const res = await queryExpert(queryData).finally(() => { loading.value = false; });
  total.value = res.total;
  res.rows.forEach(v => {
    if (v.performWorkId) {
      let workArr = [];
      v.performWorkId.toString().split(',').forEach(v => {
        workArr.indexOf(v) == -1 ? workArr.push(v) : null;
      });
      v.workArr = workArr;
    }

  });
  dataList.value = res.rows;

}

/** 条件查询列表 */
const handleFind = async (dictIndex, index) => {
  if (conditionList.value[dictIndex] == index) {
    return
  }
  conditionList.value[dictIndex] = index;
  conditionList.value.forEach((v, i) => {
    switch (i) {
      case 0:
        v == 0 ? queryData.performWorkId = null : queryData.performWorkId = v;
        break;
      case 1:
        v == 0 ? queryData.degree = null : queryData.degree = v;
        break;
      case 2:
        v == 0 ? queryData.professional = null : queryData.professional = tech_qualification.value[v].label;
        break;
      case 3:
        v == 0 ? queryData.workProfessialYear = null : queryData.workProfessialYear = v;
        break;
      case 4:
        v == 0 ? queryData.unitType = null : queryData.unitType = v;
        break;
      default:
        break;
    };
  })

  getList();


}

/** 取消按钮 */
const cancel = () => {
  reset();
}
/** 表单重置 */
const reset = () => {
  expertFormRef.value?.resetFields();
}

/** 查看专家详情 */
const jumpToDetail = (row) => {
  const _id = row?.expertId;
  router.push({ path: '/expert/expertDetail', query: { id: _id, type: 'detail' } })
};

const typeFn = (data) => {
  const findData = perform_work_type.value.find(fitem => fitem.value === data )
  if (findData) {
    return findData.label.length > 7 ? findData.label.substring(0,7) + '...' : findData.label
  }
}

onActivated(() => {
  getList();
})
onMounted(async () => {

  getList();
});
</script>

<style scoped lang="scss">
.search-list {
  width: 100%;

  .search-item {
    width: 100%;
    display: flex;
    margin-bottom: 1vh;

    .item-title {
      width: auto;

      span {
        display: inline-block;
        text-wrap: nowrap;
        width: 7vw;
      }
    }

    .item-content {

      span {
        display: inline-block;
        margin-right: 1vw;
        cursor: pointer;
        font-weight: bolder;
        padding: 1px 4px;
      }

      span:hover {
        background-color: rgba(0, 0, 0, 0.1)
      }

      span.active {
        background-color: #36d;
        color: #fff
      }
    }


  }

}

.list {
  display: flex;
  flex-wrap: wrap;


  .el-card {
    width: 24%;
    margin-bottom: 1.5vh;
    margin-left: 0.6vw;
    cursor: pointer;

    .card-top {
      display: flex;
      justify-content: space-between;
      height: 12vh;

      .avatar {
        width: 30%;
        height: 100%;

        img {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }

      .user {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        font-size: 15px;

        p {
          margin: 0;
        }
      }

      .tags {
        width: 35%;
        display: flex;
        justify-content: flex-start;
        flex-wrap: wrap;
      }
    }
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  p {
    position: relative;
    padding-left: 1vw;
    font-size: 1.2rem;
  }

  p::before {
    content: "";
    width: 5px;
    height: 20px;
    display: block;
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 3px;
    background-color: #36d;
  }
}

.query-name {
  .el-input {
    width: 15vw;

  }

  .el-button {
    margin: 0 1vw 0 2vw;
  }
}
</style>
