<template>
  <div id="homework">
    <el-row>
      <el-col :span="24" class="style">
        <el-col :span="24" class="top">
          <NavBar v-show="navShow" :title="title" :isleftarrow="isleftarrow"> </NavBar>
        </el-col>
        <el-col :span="24" class="main">
          <el-col :span="24" class="leaveList">
            <workList :workLists="workLists" :display="display"></workList>
          </el-col>
          <el-col :span="24" class="foot">
            <footInfo></footInfo>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import NavBar from '@/layout/common/topInfo.vue';
import footInfo from '@/layout/common/footInfo.vue';
import workList from './homework/workList.vue';
import { mapState, createNamespacedHelpers, mapGetters } from 'vuex';
const { mapActions: mapClass } = createNamespacedHelpers('classes');
const { mapActions: mapLesson } = createNamespacedHelpers('lesson');
const { mapActions: mapTask } = createNamespacedHelpers('task');
const { mapActions: mapSubject } = createNamespacedHelpers('subject');
const { mapActions: uploadtask } = createNamespacedHelpers('uploadtask');
const { mapActions: util } = createNamespacedHelpers('util');

export default {
  name: 'homework',
  props: {},
  components: {
    NavBar, //头部导航
    workList,
    footInfo,
  },
  data: () => ({
    workLists: [],
    display: '',
    navShow: true,
    title: '',
    isleftarrow: '',
  }),
  mounted() {
    this.title = this.$route.meta.title;
    this.isleftarrow = this.$route.meta.isleftarrow;
  },
  watch: {
    $route(to, from) {
      this.title = to.meta.title;
      this.isleftarrow = to.meta.isleftarrow;
    },
  },
  created() {
    this.search();
  },
  computed: {
    ...mapState(['user']),
  },
  methods: {
    ...util({ modelFetch: 'fetch' }),
    ...uploadtask({ getUploadTask: 'query' }),
    ...mapTask({ list: 'query', add: 'create', fet: 'fetch', updates: 'update' }),
    ...mapClass({ classinfo: 'fetch', classquery: 'query' }),
    ...mapLesson({ lessonList: 'query', lessonadd: 'create', lessonfet: 'fetch', lessonupdates: 'update' }),
    ...mapSubject({ subjectList: 'query', subjectListadd: 'create', subjectListfet: 'fetch', subjectListpdates: 'update' }),
    async search() {
      let classid = this.user.classid;
      const ress = await this.classinfo(classid);

      let lessonid = ress.data.lessonid;
      const res = await this.lessonList({ classid });
      const resout = await this.list();

      let lessons = _.get(res.data[0], `lessons`);
      // lesson排序,只去时间最早的作为作业的上传lessonid,需要和教师处相同处理
      let r = lessons.filter(f => f.subid);
      r = r.map(r => {
        let time = r.time.split('-');
        r.start = `${r.date} ${time[0]}`;
        return r;
      });
      r = Object.values(_.groupBy(r, 'subid'));
      r = r.map(a => {
        let na = _.orderBy(a, ['start'], ['asc']);
        return _.head(na);
      });
      var crr = r.filter(item => item.subid);
      // console.log(crr);
      // let data = {
      //   date: '2020-03-15',
      //   day: '0',
      //   start: '2020-03-15 08:00',
      //   subid: '5f2b656868f88c65600822d7',
      //   subname: '就业科目4',
      //   teaid: '5f2b78044ca39a590481d56f',
      //   teaname: '教师20',
      //   time: '08:00-11:30',
      //   _id: '5f4088f2624726b578b5d0fa',
      // };
      // crr.push({ data }, { data }, { data }, { data }, { data });
      this.$set(this, `workLists`, crr);
      for (const val of resout.data) {
        var arr = crr.filter(item => item.subid === val.code);
        for (const att of arr) {
          att.display = 'second';
        }
      }
      this.getUpload();
    },
    async getUpload() {
      let list = _.cloneDeep(this.workLists);
      for (let i of list) {
        let res = await this.modelFetch({ model: 'uploadtask', studentid: this.user.userid, lessonid: i._id });
        if (this.$checkRes(res)) {
          let r = res.data;
          if (r) {
            const { answers, picurl, score } = r;
            if (score) i.score = score;
            if (answers && answers.length > 0) i.onlinetask = true;
            if (picurl) i.pictask = true;
          }
        }
      }
      this.$set(this, `workLists`, list);
    },
  },
};
</script>

<style lang="less" scoped>
.style {
  width: 100%;
  min-height: 667px;
  position: relative;
  background-color: #f9fafc;
}
.top {
  height: 46px;
  overflow: hidden;
}
.main {
  min-height: 570px;
}
.main .leaveBtn {
  text-align: center;
  padding: 15px 0;
}
.leaveList {
  position: relative;
  background-color: #f9fafc;
}
</style>
