<template>
  <div class="p-2">

    <div class="top-search">
      <el-input v-model="searchName" style="max-width: 600px" placeholder="请输入课程名称关键字搜索" class="input-with-select"
        clearable @clear="clearName" :prefix-icon="Search">
        <template #append>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </template>
      </el-input>
    </div>

    <div class="content">

      <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 course_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, 1)"
                    :class="conditionList[1] == 1 ? 'item-text active' : 'item-text'">是</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 class="title">课程数据条数：{{ total }}</p>


            <div class="learnInfo">
              <p>
                <span>学习计划学时：{{ learnInfo.systemLearningHours }} 学时</span>
                <span>累计学习学时：{{ learnInfo.learningHours }} 学时</span>
                <span :style="learnInfo.compliance == true ? 'color:green':'color:red'">学习状态：{{ learnInfo.compliance==true?'已完成':'未完成' }} </span>
              </p>
            </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">
            <div v-if="item.recommended" class="silk">
              <span>推荐</span>
            </div>

            <div class="card-bot">
              <img :src="item.coursePic" alt="">
            </div>
            <div class="filter">
              <h3>{{ item.courseName }}</h3>
              <span>{{ item.courseClassify }}</span>
              <div class="online">
                <span>{{ item.teachingType }}</span>
              </div>
            </div>
            <div class="mask"></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>



  </div>
</template>

<script setup name="Expert" lang="ts">
import { Search } from '@element-plus/icons-vue'
import { listLearningCourse , learningCompliance} from '@/api/study/lesson/index';
import { globalHeaders } from '@/utils/request';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { course_type, give_course_type } = toRefs<any>(proxy?.useDict('course_type', 'give_course_type'));
const tech_qualification = ref([{ label: '全部', value: 0 }]);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);

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

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

const queryData = reactive({
  pageNum: 1,
  pageSize: 20,
  status: '2',
  courseName: '',
  courseClassify: '',
  recommended: null,

});
const dataList = ref([]);
//查询项目名称
const handleSearch = () => {
  queryData.courseName = searchName.value;
  getList();
}

const learnInfo =ref({
  learningHours:null,
  systemLearningHours:null,
  compliance:null,
});

const getLearningStatus = async ()=>{
  const res = await learningCompliance();
  learnInfo.value = res.data;
}

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const res = await listLearningCourse(proxy?.addDateRange(queryData, dateRange.value, 'start')).finally(() => { loading.value = false; })
  dataList.value = res.rows.map(item => {
    const findCourseType = course_type.value.find(fItem => fItem.value === item.courseClassify)
    if (findCourseType) {
      item.courseClassify = findCourseType.label
    }
    const findGiveCourseType = give_course_type.value.find(fItem => fItem.value === item.teachingType)
    if (findGiveCourseType) {
      item.teachingType = findGiveCourseType.label
    }
    return item
  })
  total.value = res.rows.length;
}
const clearName = () => {
  queryData.courseName = null;
  getList();
}
const clearTime = () => {
  dateRange.value = ['', ''];
  getList();
}

/** 条件查询列表 */
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.courseClassify = null : queryData.courseClassify = v.toString();
        break;
      case 1:
        v == 1 ? queryData.recommended = true : queryData.recommended = null;
        break;
      default:
        break;
    };
  })
  getList();
}

/** 查看项目详情 */
const jumpToDetail = (row) => {
  const _id = row?.id;
  router.push({ path: '/study/lessonDetail', query: { id: _id, mode: 'detail' } })
};

onActivated(() => {
  getList();
  getLearningStatus();
})
onMounted(async () => {
  getList();
  getLearningStatus();
});
</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;
    position: relative;

    .card-top {
      .title {
        position: relative;
        padding-left: 0.8vw;
        height: 2vh;
        line-height: 2vh;
        margin: 0;
        font-size: 15px;
        font-weight: bolder;
      }

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

    :deep(.el-card__body) {
      padding: 10px !important;
      position: relative !important;
    }

    .card-bot {
      img {
        width: 100%;
        height: 200px;
        object-fit: cover;
      }
    }

    .mask {
      background-color: #fff;
      opacity: 0.7;
      width: 100%;
      height: 29%;
      position: absolute;
      bottom: 0;
      z-index: 100;
    }

    .filter {
      width: 100%;
      height: 28%;
      position: absolute;
      z-index: 200;
      bottom: 0;
      padding: 5px 0 0 10px;

      h3 {
        color: #000;
        margin: 0;
        font-weight: bolder;
      }

      span {
        font-size: 14px;
      }

      .online {
        position: absolute;
        right: 10%;
        top: 1vh;
        color: #fff;
        padding: 5px 10px;
        border-radius: 10px;
        background-color: #36d;
      }
    }

    .card-footer {
      display: flex;
      justify-content: space-between;
      font-size: 12px;

      span:nth-child(1) {
        padding-left: 5px;
        display: flex;
        align-items: center;
      }

      span:nth-child(2) {
        color: #36d;
      }

      span:nth-child(2)::after {
        content: " >>";


      }
    }

    :deep(.el-card__footer) {
      padding: 8px;
    }
  }
}

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

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

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

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

  }

  .el-button {
    margin: 0 1vw 0 2vw;
  }
}

.top-search {
  padding: 4vh 0;
  text-align: center;

  .el-input {
    height: 5vh;

    :deep(.el-input-group__append) {
      cursor: pointer;
      width: 6vw;
      background-color: #0066CC;
      color: #fff;
    }
  }
}

.content {
  width: 100%;
}

.silk {
  position: absolute;
  top: 8%;
  left: 0px;
  padding: 2px 10px;
  background-color: #f56c6c;
  font-size: 12px;
  color: #fff;

  &::before {
    content: "";
    position: absolute;
    left: 0;
    bottom: -4px;
    border-top: 4px solid #f56c6c;
    border-left: 4px solid transparent;
  }
}

.learnInfo{
  span{
    margin-left: 30px;
  }
}
</style>
