<template>
  <div id="achieve">
    <el-row>
      <el-col :span="24" class="style">
        <el-col :span="24" class="top">
          <!-- <topInfo></topInfo> -->
          <NavBar v-show="navShow" :title="title" :isleftarrow="isleftarrow"> </NavBar>
        </el-col>
        <el-col :span="24" class="main">
          <achieves
            @clickDialog="opensave"
            @submit="onsave"
            :peaceAchieveList="peaceAchieveList"
            :showPicker="showPicker"
            :peaceScore="peaceScore"
            @showBtn="showBtn"
            @onConfirm="onConfirm"
            :achieveList="achieveList"
            @submitss="onsist"
            :show="show"
            :form="form"
            :deptList="deptList"
            :lesson="lesson"
            :lesslist="lesslist"
            :names="names"
            :score="score"
          ></achieves>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import NavBar from '@/layout/common/topInfo.vue';
import achieves from '@/layout/class/achieve.vue';
import { mapState, createNamespacedHelpers, mapGetters } from 'vuex';
const { mapActions: mapStudent } = createNamespacedHelpers('student');
const { mapActions: mapClass } = createNamespacedHelpers('classes');
const { mapActions: mapLesson } = createNamespacedHelpers('lesson');
const { mapActions: mapUploadtask } = createNamespacedHelpers('uploadtask');
const { mapActions: mapSubject } = createNamespacedHelpers('subject');
const { mapActions: group } = createNamespacedHelpers('group');
export default {
  name: 'achieve',
  props: {},
  components: {
    NavBar, //头部导航
    achieves, //班级学生成绩列表
  },
  data: () => ({
    peaceAchieveList: [],
    showPicker: false,
    peaceScore: '',
    studid: '',
    lesson: [],
    lesslist: [],
    names: '',
    score: '',
    deptList: [],
    achieveList: [],
    show: false,
    form: {},
    title: '',
    isleftarrow: '',
    transitionName: 'fade',
    navShow: true,
    // 修改
    stuid: '',
    taskid: '',
    subname: '',
    task: [],

    groupsocre: '',
  }),
  created() {
    this.search();
    this.searchinfo();
  },
  computed: {
    ...mapState(['user']),
    classid() {
      return this.$route.query.classid;
    },
  },
  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;
    },
  },
  methods: {
    ...mapStudent({ list: 'query', add: 'create', fet: 'fetch', updates: 'update' }),
    ...mapClass({ classinfo: 'fetch', classlist: 'query' }),
    ...mapLesson({ lessoninfo: 'fetch', lessonlist: 'query' }),
    ...mapUploadtask({ uploadtaskinfo: 'fetch', uploadtasklist: 'query', uploadtaskupdate: 'update' }),
    ...mapSubject({ subjectinfo: 'fetch', subjectlist: 'query' }),
    ...group({ groupinfo: 'query', groupfetch: 'fetch', findbystuid: 'findbystuid', groupupdate: 'update' }),
    // 平时成绩学生名单查询+作业成绩学生名单查询
    async search() {
      let classid;
      if (this.user.type === '1') classid = this.classid;
      else classid = this.user.classid;
      const res = await this.list({ classid });

      this.$set(this, `peaceAchieveList`, res.data);
      let classsid = this.user.classid;
      const result = await this.uploadtasklist({ classsid });

      for (const accc of res.data) {
        var arr = result.data.filter(item => item.studentid === accc.id);
      }

      // let lessons = [];
      let lessone = [];
      let stus = [];
      for (const val of result.data) {
        let newsid = val.lessonid;

        let classid = this.user.classid;
        //查询这个班的课程
        const lessonInfo = await this.lessonlist({ classid });

        for (const hic of lessonInfo.data) {
          //过滤出有作业的科目

          var fff = hic.lessons.filter(item => item.subid === newsid);

          for (const less of fff) {
            console.log(less.subname);

            this.$set(this, `subname`, less.subname);
          }
        }
        val.subName = this.subname;
        const stuInfo = await this.fet(val.studentid);
        if (stuInfo.data.name) {
          val.stuName = stuInfo.data.name;
          //创建学生名字相关数组
          let stu = [{ stuname: stuInfo.data.name, id: val.studentid, lesson: [] }];
          //合并数组

          stus = [...stu, ...stus];

          //去掉重复数组对象
          var appArray = _.uniqBy(stus, _.isEqual);

          //合并数组
          this.$set(this, `achieveList`, appArray);
          let lessonss = [{ name: this.subname, id: val.studentid, taskid: val.id, score: val.score }];
          lessone = [...lessonss, ...lessone];
        }

        for (const ss of appArray) {
          var vre = lessone.filter(item => item.id === ss.id);

          ss.lesson = vre;
        }
        // for (const eee of appArray) {
        //   eee.less = lessone;
        //   var qq = eee.less.filter(item => item.id === eee.id);

        // }
      }
      this.$set(this, `task`, appArray);
    },
    // 学委上作业分+存储学生id
    clickAchieve({ id }) {
      this.$set(this, `stuid`, id);
      this.show = true;
    },
    // 学委上作业分提交
    async submitAchieve(form) {
      this.form.id = this.stuid;
      let data = this.form;
      const res = await this.uploadtaskupdate(data);
      this.show = false;
    },
    // 平时成绩上分显示+学生id
    showBtn(id) {
      this.$set(this, `studid`, id);
      this.showPicker = true;
    },
    // 给学生上分
    async onConfirm(peaceScore) {
      let data = {};
      data.id = this.studid;

      const ress = await this.fet(this.studid);

      data.selfscore = Number(ress.data.selfscore) + Number(peaceScore);

      const res = await this.updates(data);
      this.$message({
        message: '平时成绩上分成功',
        type: 'success',
      });

      let classid = this.user.classid;

      const group = await this.findbystuid({ stuid: this.studid });

      for (const acc of group.data.students) {
        var ref = this.peaceAchieveList.filter(item => item.id === acc.stuid);

        for (const aaa of ref) {
          acc.stores = aaa.selfscore;
        }
        let asd = group.data.students.map(i => i.stores);
        console.log(asd);
        let nes = asd.filter(d => d);
        console.log(nes);
        let snewcore = eval(nes.join('+'));
        this.groupsocre = snewcore;
      }
      group.data.score = Number(data.selfscore) + Number(this.groupsocre);
      let groups = group.data;
      const groupup = await this.groupupdate(groups);
      this.search();
      this.showPicker = false;
    },
    //  作业成绩列表
    async searchinfo({ ...info } = {}) {
      // let classid = this.user.classid;
      // const result = await this.uploadtasklist({ classid });
      // const res = await this.list({ classid });
      // for (const accc of res.data) {
      //   var arr = result.data.filter(item => item.studentid === accc.id);
      // }
      // // let lessons = [];
      // let lessone = [];
      // let stus = [];
      // for (const val of result.data) {
      //   let newsid = val.lessonid;
      //   let classid = this.user.classid;
      //   //查询这个班的课程
      //   const lessonInfo = await this.lessonlist({ classid });
      //   for (const hic of lessonInfo.data) {
      //     //过滤出有作业的科目
      //     var fff = hic.lessons.filter(item => item._id === newsid);
      //     for (const less of fff) {
      //       this.$set(this, `subname`, less.subname);
      //     }
      //   }
      //   val.subName = this.subname;
      //   const stuInfo = await this.fet(val.studentid);
      //   val.stuName = stuInfo.data.name;
      //   //创建学生名字相关数组
      //   let stu = [{ stuname: stuInfo.data.name, id: val.studentid }];
      //   //合并数组
      //   stus = [...stu, ...stus];
      //   //去掉重复数组对象
      //   var appArray = _.uniqWith(stus, _.isEqual);
      //   //合并数组
      //   this.$set(this, `achieveList`, appArray);
      //   let lessonss = [{ name: this.subname, id: val.studentid, taskid: val.id, score: val.score, less: [] }];
      //   lessone = [...lessonss, ...lessone];
      //   var appArrayss = _.uniqWith(lessone, _.isEqual);
      //   this.$set(this, `task`, lessone);
      //   console.log(appArrayss);
      // }
    },
    //为了获取当前点击学生id和当前学生上传的作业
    async onsist({ id }) {
      var arr = this.task.filter(item => item.id === id);

      this.$set(this, `lesson`, arr);
    },
    async opensave({ id, name, score }) {
      this.show = true;
      this.$set(this, `names`, name);
      this.$set(this.form, `score`, score);
      this.$set(this, `taskid`, id);
    },
    async onsave(form) {
      this.form.id = this.taskid;
      let res = await this.uploadtaskupdate(this.form);
      this.show = false;
    },
  },
};
</script>

<style lang="less" scoped>
.style {
  width: 100%;
  min-height: 667px;
  position: relative;
  background-color: #eeeeee;
}
.top {
  height: 46px;
  overflow: hidden;
}
.main {
  min-height: 570px;
}
</style>
