<template>
  <div id="index">
    <list-frame :title="pageTitle" @query="search" :total="total" :needFilter="false" :needAdd="false">
      <data-table :fields="fields" :data="tableData" :opera="opera" @view="toTaskList"></data-table>
    </list-frame>
  </div>
</template>

<script>
import listFrame from '@frame/layout/admin/list-frame';
import dataTable from '@frame/components/data-table';
import _ from 'lodash';
import { mapState, createNamespacedHelpers, mapGetters } from 'vuex';
const { mapActions: lesson } = createNamespacedHelpers('lesson');
const { mapActions: util } = createNamespacedHelpers('util');
export default {
  name: 'index',
  props: {},
  components: { listFrame, dataTable },
  data: () => ({
    opera: [
      {
        label: '查看学生评分',
        icon: 'el-icon-view',
        method: 'view',
      },
    ],
    fields: [
      { label: '课程日期', prop: 'date' },
      { label: '课程名称', prop: 'subname' },
      { label: '课程教师', prop: 'teaname' },
    ],
    tableData: [],
    total: 0,
  }),
  created() {
    this.search();
  },
  computed: {
    ...mapState(['user', 'defaultOption']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
    id() {
      return this.defaultOption.classid;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  methods: {
    ...lesson({ lessionInfo: 'fetch', lessionList: 'query' }),
    ...util({ modelFetch: 'fetch' }),
    async search({ skip, limit, ...info } = {}) {
      const classid = this.id;
      const userid = this.user.userid;
      const lesson = await this.modelFetch({ model: 'lesson', classid });
      let lessons = _.get(lesson.data, '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);
      });
      console.log(r);
      this.$set(this, `tableData`, r);
      this.$set(this, `total`, r.length);
    },
    toTaskList(item) {
      this.$router.push({ path: '/stuscore/scoreList', query: { lessonid: item.data._id, teacherid: item.data.teaid } });
    },
  },
  watch: {
    id: {
      handler(val) {
        if (val) this.search();
        else this.$set(this, `tableData`, []);
      },
      immediate: true,
    },
  },
};
</script>

<style lang="less" scoped></style>
