<template>
  <div id="index">
    <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="lessonList" v-for="(item, index) in lessonList" :key="index">
            <el-col :span="16" class="name">
              {{ item.subname }}
            </el-col>
            <el-col :span="8" class="btn">
              <el-button type="primary" size="mini" @click="scoreBtn(item)">小组科目打分</el-button>
            </el-col>
            <el-col :span="24" class="other">
              <span>科目教师：{{ item.teaname }}</span>
              <span>科目时间：{{ item.date }}</span>
            </el-col>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
    <van-dialog v-model="show" title="小组分" :showConfirmButton="false" :showCancelButton="true">
      <van-form>
        <van-field v-model="form.score" name="小组分" label="小组分" placeholder="请输入小组分" />
        <div style="margin: 16px;">
          <van-button round block type="info" @click="onSubmit">
            提交
          </van-button>
        </div>
      </van-form>
    </van-dialog>
  </div>
</template>

<script>
import NavBar from '@/layout/common/topInfo.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: lesson } = createNamespacedHelpers('lesson');
const { mapActions: util } = createNamespacedHelpers('util');
const { mapActions: groupscore } = createNamespacedHelpers('groupscore');
export default {
  name: 'index',
  props: {},
  components: {
    NavBar,
  },
  data: function() {
    return {
      title: '',
      isleftarrow: '',
      navShow: true,
      lessonList: [],
      show: false,
      form: {},
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...lesson({ lessionInfo: 'fetch', lessionList: 'query' }),
    ...util({ modelFetch: 'fetch' }),
    ...groupscore({ groupscoreQuery: 'query', groupscoreUpdate: 'update', groupscoreCreate: 'create' }),
    async search() {
      let classid = this.user.classid;
      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, `lessonList`, r);
    },
    // 打开小组分
    async scoreBtn(item) {
      let res = await this.groupscoreQuery({ classid: this.user.classid, subid: item.subid, groupid: this.$route.query.id });
      if (res.total > 0) {
        this.$set(this, `form`, res.data[0]);
      } else {
        let data = {
          classid: this.user.classid,
          subid: item.subid,
          date: item.date,
          groupid: this.$route.query.id,
          score: 0,
        };
        this.$set(this, `form`, data);
      }
      this.show = true;
    },
    // 保存小组分
    async onSubmit() {
      let data = this.form;
      if (data.id) {
        const res = await this.groupscoreUpdate(data);
        if (res.errcode === 0) {
          this.$notify({
            message: '修改小组打分成功',
            type: 'success',
          });
          this.show = false;
        } else {
          this.$notify({
            message: res.errmsg,
            type: 'danger',
          });
        }
      } else {
        const res = await this.groupscoreCreate(data);
        if (res.errcode === 0) {
          this.$notify({
            message: '小组打分成功',
            type: 'success',
          });
          this.show = false;
        } else {
          this.$notify({
            message: res.errmsg,
            type: 'danger',
          });
        }
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  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;
    },
  },
};
</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;
  .lessonList {
    border-bottom: 1px dashed #ccc;
    padding: 10px 5px;
    margin: 0 10px;
    background: #fff;
    width: 95%;
    .name {
      padding: 5px 0;
    }
    .btn {
      padding: 5px 0;
      text-align: center;
    }
    .other {
      padding: 5px 0 0 0;
      span {
        display: inline-block;
        width: 50%;
      }
    }
  }
}
p {
  padding: 0;
  margin: 0;
}
</style>
