<template>
  <div class="train">
    <Header @confirmExit="getExit" :showTool="false" closeClass="close2"></Header>
    <div class="main">
      <div class="left">
        <div class="title">
          <el-input class="myInput" :prefix-icon="Search" v-model="keywords" placeholder="搜索" clearable />
        </div>
        <ul>
          <li v-for="(item, index) in newTaskList" :key="index" :class="{ 'current': currentTask.id == item.id }"
            @click="clickTask(item)">{{ item.name
            }}</li>
        </ul>
      </div>
      <div class="menu" v-if="projectList.length" :key="projectList.length"
        :class="{ 'menu1': projectList.length <= 10, 'menu2': projectList.length > 10, 'prohibit': !currentTask.id }">
        <swiper :slides-per-view="5" :modules="[Grid]" :grid="{
          fill: projectList.length <= 10 ? 'row' : 'column',
          rows: 2,
        }" :space-between="20" :slides-per-group="10">
          <swiper-slide v-for="(item, index) in projectList " :key="index" @click="getOption(item)">
            <div class="li">
              <div class="pic"><img :src="'static/images/train/' + item.key + '.png'"></div>
              <div class="name">
                {{ item.name }}
              </div>
            </div>
          </swiper-slide>
        </swiper>
      </div>
    </div>
    <div class="close" @click="getExit"></div>
    <OptionWindow ref="optionWindowRef" :projectList="projectList" :parentData="currentTask" />
  </div>
</template>

<script setup name="TestIndex" lang="ts">
import { clearInterval, clearTimeout, setInterval, setTimeout } from 'worker-timers';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { Grid } from 'swiper/modules';
import { Search } from '@element-plus/icons-vue'
import 'dayjs/locale/zh-cn'
import dayjs from 'dayjs'
import weekOfYear from 'dayjs/plugin/weekOfYear'
import 'swiper/css';
import 'swiper/css/grid';
dayjs.locale('zh-cn')
dayjs.extend(weekOfYear)

const router = useRouter();
const { proxy } = getCurrentInstance() as any;
const optionWindowRef = ref();

const data = reactive<any>({
  projectList: [],
  timerManager: {},
  keywords: '',
  taskList: [],
  currentTask: {},
  myProject: [],
});
const { projectList, timerManager, keywords, taskList, currentTask, myProject } = toRefs(data);

/**
 * 点击课程
*/
const clickTask = (data: any) => {
  currentTask.value = data
  getProjectTaskList()
};

/**
 * 获取测试项目
*/
const getProjectTaskList = () => {
  let params = {
    exam_type: currentTask.value.exam_type,
    grade: currentTask.value.grade
  };
  proxy?.$http.test.getTaskProject(params).then((res: any) => {
    myProject.value = res.data;
    getExam();
  });
};

/**
 * 获取测试列表
*/
const getTaskList = () => {
  //自由
  let params1 = {};
  proxy?.$http.test.freeTasksList(params1).then((res: any) => {
    let list = res.data.map((item: any) => {
      item.type = item.test_type;
      item.task_cate = 0;
      return item;
    });
    taskList.value.push(...list);
  });


  //常规
  let today = dayjs().format('YYYY-MM-DD')
  let params2 = {
    start_time: today
  };
  proxy?.$http.test.conventionTasksList(params2).then((res: any) => {
    let list = res.data.map((item: any) => {
      item.type = item.exam_type;
      item.task_cate = 1;
      return item;
    });
    taskList.value.push(...list);
  });
};

/**
 * 获取项目
*/
const getExam = () => {
  let examList: any = []
  myProject.value.forEach((item: any) => {
    item.exams.forEach((items: any) => {
      examList.push(items[0]);
    })
  })
  proxy?.$http.train.projectList().then((res: any) => {
    projectList.value = proxy?.$utils.getProject(res.exams).filter((item: any) => {
      //只显示能开的
      if (currentTask.value.id) {
        return item.area.length > 0 && examList.includes(item.key);
      } else {
        return item.area.length
      }
    });
  });
};

/**
 * 弹出选项窗口
*/
const getOption = (data: any) => {
  if (!currentTask.value.id) {
    proxy?.$modal.msgWarning(`请先选择左侧专题！`);
    return false;
  }
  optionWindowRef.value.open(data);
};

/**
 * 清空定时任务
*/
const getClearTimer = () => {
  for (let key in timerManager.value) {
    if (timerManager.value.hasOwnProperty(key)) {
      clearInterval(timerManager.value[key])
      timerManager.value[key] = null;
    }
  }
};

/**
 * 初始化项目
*/
const getInitExam = () => {
  getExam();
  //定时刷新
  timerManager.value.exam = setInterval(() => {
    getExam();
  }, 5000)
};

/**
 * 退出
*/
const getExit = () => {
  //router.go(-1);
  router.push({ path: '/home' });
};

/**
 * 新的测试列表
*/
const newTaskList: any = computed(() => {
  let list = taskList.value.filter((item: any) => {
    return item.name.indexOf(keywords.value) != -1
  })
  return list;
});

onBeforeMount(() => {
  getTaskList();
  getInitExam();
})

onBeforeUnmount(() => {
  getClearTimer();
})
</script>

<style lang="scss" scoped>
$topPadding: 5.19rem;
$waiPadding: 6.51rem;

.main {
  width: calc(100% - ($waiPadding * 2));
  height: 72vh;
  padding-top: 10rem;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
}

.left {
  width: calc(23.4% - 20px);
  height: 72vh;
  display: flex;
  flex-direction: column;

  overflow: hidden;

  .title {
    height: 8.3vh;
    width: 100%;
    padding: 0 8%;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    background: radial-gradient(120% 126% at 5% 93%, #8EFFA9 0%, #07FFE7 100%);
    border-top-left-radius: 1.6rem;
    border-top-right-radius: 1.6rem;

    ::v-deep(.myInput) {
      width: 100%;
      height: 4.5vh;

      .el-input__wrapper {
        border-radius: 2vh;
        box-shadow: 0px 1px 6px 0px rgba(0, 0, 0, 0.3);
      }
    }
  }


  ul {
    height: 100%;
    overflow-y: scroll;
    border: 1px solid #48677E;
    border-bottom-left-radius: 1.6rem;
    border-bottom-right-radius: 1.6rem;
    background: linear-gradient(27deg, #092941 -82%, #2A484B 94%);

    li {
      border-bottom: 1px solid #48677E;
      font-size: 1.6rem;
      color: #ffffff;
      padding: 1.6vh 0 1.6vh 2rem;
      cursor: pointer;
      transition: all 0.2s;

      &:hover {
        background: rgba(255, 255, 255, 0.2);
      }

      &:last-child {
        border-bottom: none;
      }
    }

    .current {
      background: rgba(255, 255, 255, 0.2);
    }

    &::-webkit-scrollbar {
      width: 0px;
    }

    &::-webkit-scrollbar-thumb {
      border-width: 2px;
      border-radius: 4px;
      border-style: dashed;
      border-color: transparent;
      background-color: rgba(216, 216, 216, 0.8);
      background-clip: padding-box;
    }

    &::-webkit-scrollbar-button:hover {
      border-radius: 6px;
      background: rgba(216, 216, 216, 0.8);
    }
  }

}

.menu {
  width: calc(100% - 23.4%);
  height: 72vh;

  .li {
    // width: calc((100% / 6) - 1rem + (1rem/6));
    // margin-right: 1rem;
    // margin-bottom: 1rem;
    width: 100%;
    height: calc((72vh / 2) - 10px);
    padding: 2.2vh 0;
    border-radius: 1.6rem;
    box-sizing: border-box;
    box-shadow: inset 0px 1px 0px 2px rgba(255, 255, 255, 0.9046), inset 0px 3px 6px 0px rgba(0, 0, 0, 0.0851);
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    text-align: center;

    flex-shrink: 0;
    cursor: pointer;

    .name {
      width: 100%;
      font-size: 2.48rem;
      color: #1A293A;
      padding: 0.5rem 0;
    }


    .pic {
      width: 11.36vw;
      height: 11.36vw;
      border-radius: 50%;
      background: radial-gradient(78% 78% at 53% 50%, #07121A 0%, #2A4256 49%, #5180A9 100%);
      box-shadow: 0px 0px 2px 2px #FFFFFF;
      margin-bottom: 2vh;
      overflow: hidden;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;

      img {
        max-width: 88%;
        max-height: 88%;
        transition: all 1s;
      }
    }

    &:hover {
      img {
        transform: translateY(-0.5vw);
      }
    }
  }

  .swiper-slide {
    border-radius: 1.6rem;
    overflow: hidden;
  }
}

.menu1 {
  .swiper-slide {
    margin-bottom: 20px;

    .li {
      background: radial-gradient(96% 96% at 2% 32%, #FFFFFF 0%, #FCFDFD 54%, #E1E4E7 100%);
    }

    &:nth-child(2),
    &:nth-child(4),
    &:nth-child(6),
    &:nth-child(8),
    &:nth-child(10) {
      .li {
        background: radial-gradient(167% 126% at 97% 6%, #35FFC6 0%, #00FFE8 100%);
      }
    }
  }
}

.menu2 {
  display: flex;

  .swiper-slide {

    &:nth-child(1),
    &:nth-child(4),
    &:nth-child(5),
    &:nth-child(8),
    &:nth-child(9),
    &:nth-child(12),
    &:nth-child(13),
    &:nth-child(16),
    &:nth-child(17),
    &:nth-child(20),
    &:nth-child(21),
    &:nth-child(24),
    &:nth-child(25),
    &:nth-child(28),
    &:nth-child(29),
    &:nth-child(32),
    &:nth-child(33),
    &:nth-child(36),
    &:nth-child(37),
    &:nth-child(40),
    &:nth-child(41),
    &:nth-child(44) {
      .li {
        background: radial-gradient(96% 96% at 2% 32%, #FFFFFF 0%, #FCFDFD 54%, #E1E4E7 100%);
      }
    }

    &:nth-child(2),
    &:nth-child(3),
    &:nth-child(6),
    &:nth-child(7),
    &:nth-child(10),
    &:nth-child(11),
    &:nth-child(14),
    &:nth-child(15),
    &:nth-child(18),
    &:nth-child(19),
    &:nth-child(22),
    &:nth-child(23),
    &:nth-child(26),
    &:nth-child(27),
    &:nth-child(30),
    &:nth-child(31),
    &:nth-child(34),
    &:nth-child(35),
    &:nth-child(38),
    &:nth-child(39),
    &:nth-child(42),
    &:nth-child(43) {
      .li {
        background: radial-gradient(167% 126% at 97% 6%, #35FFC6 0%, #00FFE8 100%);
      }
    }
  }
}

.prohibit {
  opacity: 0.5;
}

::v-deep(.menu) {
  .swiper-horizontal {
    width: 100%;
  }
}

.close {
  position: absolute;
  // right: calc($waiPadding - 3.2rem);
  left: auto;
  right: calc($waiPadding - 3.2rem);
  top: auto;
  bottom: calc($topPadding / 2 - 3.2rem / 4);
}


@media screen and (max-width: 1450px) {
  .menu {

    .li {
      .name {
        font-size: 1.8rem;
      }

      .pic {
        width: 10vw;
        height: 10vw;
      }
    }
  }
}
</style>
