<template>
  <div>
    <back-box>
      <template slot="title">
        <span>{{ "学生画像" }}</span>
      </template>
    </back-box>
    <div class="answer-header">
      <div class="sel-box">
        <el-select
          class="sel"
          v-model="query.classId"
          placeholder="选择班级"
          @change="changeclass"
        >
          <el-option
            v-for="item in classList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-input
          placeholder="请输入学生姓名"
          v-model="query.studentName"
          class="input-with-select"
          maxlength="30"
          @keyup.enter.native="(query.studentCode = ''), _QueryData()"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="(query.studentCode = ''), _QueryData()"
          ></el-button>
        </el-input>
        <el-input
          placeholder="请输入学生学号"
          v-model="query.studentCode"
          class="input-with-select"
          maxlength="30"
          @keyup.enter.native="(query.studentName = ''), _QueryData()"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="(query.studentName = ''), _QueryData()"
          ></el-button>
        </el-input>
      </div>
    </div>
    <div class="page-content">
      <ul class="stu-ul">
        <li class="stu-li" v-for="item in tableData" :key="item.studentId">
          <div class="tx">
            <i class="fa fa-mortar-board"></i>
            <p>{{ item.studentCode }}</p>
          </div>
          <div class="info">
            <p class="p1">
              {{ item.studentName }}
              <template v-if="item.clickerSn"
                ><i class="fa fa-calculator"></i> {{ item.clickerSn }}</template
              >
            </p>
            <p class="p2">{{ gradeName(item.grade) }}</p>
            <p class="p3">最近活跃：{{ item.modifiedTime }}</p>
          </div>
          <el-button
            class="btn"
            type="primary"
            round
            circle
            icon="el-icon-right"
            @click="linkDetail(item)"
          ></el-button>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { formatDate, setGradeName } from "utils";
import BusEvent from "@/utils/busEvent";
export default {
  data() {
    return {
      role: "",
      query: {
        //搜索条件
        classId: "",
        studentName: "",
        studentCode: "",
      },
      date: "",
      classList: [],
      tableData: [],
      page: 1,
      size: 999,
      total: 0,
    };
  },
  async created() {
    this.role =
      this.$store.getters.info.showRole ||
      this.$store.getters.info.permissions[0].role;
    await this._QueryClassList();
    await this._QueryData();
  },
  activated() {
    const that = this;
    BusEvent.$on("keepAlive", async function () {
      await that._QueryClassList();
      await that._QueryData();
    });
  },
  methods: {
    gradeName(type) {
      return setGradeName(type);
    },
    linkDetail(obj) {
      this.$router.push({
        path: "/portraitDetail",
        query: {
          studentName: obj.studentName,
          studentCode: obj.studentCode,
          id: obj.id,
          classId: this.query.classId,
        },
      });
    },
    changeclass() {
      this.page = 1;
      this.query.studentName = "";
      this.query.studentCode = "";
      this._QueryData();
    },
    async _QueryClassList() {
      let fetchClassList =
        this.role == "ROLE_PERSONAL"
          ? this.$request.pClassList
          : this.role == "ROLE_BANZHUREN"
          ? this.$request.cTClassList
          : this.$request.fetchClassList;

      const { data, status, info } = await fetchClassList();
      if (status === 0) {
        this.classList = data.list.map((item) => {
          return {
            value: this.role == "ROLE_PERSONAL" ? item.id : item.classId,
            label: item.className,
          };
        });
        this.query.classId = this.classList[0]?.value;
      } else {
        this.$message.error(info);
      }
    },
    async _QueryData() {
      this.loading = true;
      let query = {};
      for (let key in this.query) {
        if (this.query[key] != "") {
          query[key] = this.query[key];
        }
      }
      let studentList =
        this.role == "ROLE_PERSONAL"
          ? this.$request.pStudentList
          : this.$request.tStudentList;
      const { data, status, info } = await studentList({
        ...query,
        size: this.size,
      });
      this.loading = false;
      if (status === 0) {
        this.tableData = (data?.list && [...data?.list]) || [];
        this.total = data.count || 0;
      } else {
        this.$message.error(info);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.page-content {
  padding: 0 20px;
}
.stu-ul {
  border-radius: 20px;
  background: #f8f8f8;
  padding: 12px;
  .stu-li {
    border-radius: 12px;
    background: #fff;
    display: flex;
    align-items: center;
    padding: 12px 10px;
    margin-bottom: 16px;
    &:last-of-type {
      margin-bottom: 0;
    }
    .tx {
      width: 100px;
      height: 80px;
      background: #667ffd;
      border-radius: 8px;
      text-align: center;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      font-size: 16px;
      color: #fff;
      margin-right: 12px;
      .fa-mortar-board {
        font-size: 32px;
      }
    }
    .info {
      flex: 1;
      .p1 {
        font-size: 18px;
        color: #333;
        .fa-calculator {
          font-size: 20px;
          color: #a6a6a6;
          margin: 0 5px 10px 10px;
        }
      }
      .p2 {
        font-size: 16px;
        padding-bottom: 6px;
      }
      .p3 {
        font-size: 16px;
        color: #7f7f7f;
      }
    }
    .btn {
      margin-right: 40px;
      &.is-circle {
        padding: 8px;
      }
      :deep(.el-icon-right) {
        font-size: 22px;
        font-weight: bold;
      }
    }
  }
}
</style>