<template>
  <div id="stutask">
    <van-row>
      <van-col class="style">
        <van-col :span="24" class="top">
          <NavBar v-show="navShow" :title="title" :isleftarrow="isleftarrow"> </NavBar>
        </van-col>
        <van-col class="main" :span="24" v-if="display === 'list'">
          <van-col :span="24" class="taskList" v-for="(item, index) in taskList" :key="index">
            <van-cell is-link @click="toDetail(item)">
              <!-- 使用 title 插槽来自定义标题 -->
              <template #title>
                <span class="custom-title">{{ item.stuname }}</span>
                <span v-if="item.score" style="margin-left:20px" class="custom-title">分数：{{ item.score }}</span>
              </template>
            </van-cell>
          </van-col>
        </van-col>
        <van-col :span="24" v-else-if="display === 'detail'">
          <van-col :span="12" style="margin:5px 10px">
            <van-button plain icon="arrow-left" size="small" @click="display = 'list'">返回学生列表</van-button>
          </van-col>
          <template v-if="tasktype === 'task'">
            <van-col :span="24" class="title">
              {{ task.title }}
            </van-col>
            <van-col :span="24" class="question" v-for="(item, index) in task.question" :key="index">
              <p>
                {{ index + 1 }}、(<template>{{ item.type === '0' ? '单选' : item.type === '1' ? '多选' : item.type === '2' ? '简答' : '' }}</template
                >)({{ item.score }}分)
              </p>
              <p>{{ item.topic }}({{ item.stuanswer }})</p>
              <p v-for="i in item.option" :key="i._id">{{ i.number }}、{{ i.opname }}</p>
              <p v-if="item.answer">正确答案:{{ item.answer }}</p>
            </van-col>
          </template>
          <template v-else-if="tasktype === 'pic'">
            <van-image :src="picrul"></van-image>
          </template>
          <van-field v-model="score" type="number" label="作业成绩：" placeholder="请输入学生成绩" />
          <van-button round type="info" @click="onSubmit">提交成绩</van-button>
        </van-col>
      </van-col>
    </van-row>
  </div>
</template>

<script>
import NavBar from '@/layout/common/topInfo.vue';
import { mapState, createNamespacedHelpers, mapGetters } from 'vuex';
const { mapActions: uploadtask } = createNamespacedHelpers('uploadtask');
const { mapActions: task } = createNamespacedHelpers('task');
export default {
  name: 'stutask',
  props: {},
  components: { NavBar },
  data: () => ({
    title: '',
    isleftarrow: '',
    navShow: true,
    taskList: [],
    display: 'list',
    task: {},
    tasktype: '',
    picrul: '',
    score: '',
    uploadtask: {},
  }),
  created() {
    this.search();
  },
  computed: {
    id() {
      return this.$route.query.id;
    },
  },
  methods: {
    ...uploadtask(['fetch', 'query', 'update']),
    ...task({ taskInfo: 'fetch' }),
    async search() {
      const stutasks = await this.query({ lessonid: this.id });
      this.$set(this, `taskList`, stutasks.data);
    },
    async toDetail(item) {
      this.$set(this, `uploadtask`, item);
      this.display = 'detail';
      if (item.taskid) {
        this.tasktype = 'task';
        const task = await this.taskInfo(item.taskid);
        for (const question of task.data.question) {
          const answer = item.answers.find(item => item.questionid === question._id);
          question.stuanswer = answer.answer;
        }
        this.$set(this, `task`, task.data);
        console.log(task.data);
      } else if (item.picrul) {
        this.tasktype = 'pic';
        this.picrul = item.picrul;
      }
      if (item.score) {
        this.score = item.score;
      }
    },
    async onSubmit() {
      this.uploadtask.score = this.score;
      const res = await this.update(this.uploadtask);
      if (res.errcode === 0) {
        this.$toast('提交学生成绩成功');
        this.display = 'list';
      }
      console.log(this.uploadtask);
    },
  },
  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;
}
.title {
  text-align: center;
  font-weight: bold;
  font-size: 20px;
}
/deep/.van-field {
  width: 70%;
  border: 1px solid #f5f5f5;
  display: -webkit-inline-box;
  margin-right: 10px;
}
</style>
