<template>
  <div class="app-container">
    <!-- <h1>superAdminMyStu</h1> -->
    <!-- <el-button
      :loading="downloadLoading"
      style="margin:0 0 20px 20px;"
      type="primary"
      icon="el-icon-document"
      @click="handleDownload"
    >
      Export Excel
    </el-button> -->
    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
        @sort-change="changeTableSort"
      >
        <!-- <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column> -->

        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form label-position="left" inline class="demo-table-expand">
              <!-- <el-form-item
                v-for="(item, index) in scope.row.auditCourseList"
                :key="index"
                :label="item.auditSchool"
              >
                <span>{{ item }}</span>
              </el-form-item> -->
              <div
                class="box1"
                v-for="(item, index) in scope.row.auditCourseList"
                :key="index"
              >
                <el-form-item label="审课大学" align="center">
                  {{ item.auditSchool }}
                </el-form-item>
                <!-- {{ item }} -->

                <div
                  v-for="(item2, index2) in item.termCourseList"
                  :key="index2"
                >
                  <el-form-item label="年份" align="center">
                    {{ item2.yearTime }}
                  </el-form-item>
                  <el-form-item label="学期" align="center">
                    {{ item2.term }}
                  </el-form-item>
                  <el-form-item label="所选课程数" align="center">
                    {{ item2.num }}
                  </el-form-item>
                  <!-- {{ item2.userCourseList }} -->
                  <div
                    v-for="(item3, index3) in item2.userCourseList"
                    :key="index3"
                  >
                    <el-form-item
                      v-if="item3.ownCourseCode"
                      label="所在学校课程代码"
                      align="center"
                    >
                      {{ item3.ownCourseCode }}
                    </el-form-item>

                    <el-form-item
                      v-if="item3.csoCourseCode"
                      label="cso课程代码"
                      align="center"
                    >
                      {{ item3.csoCourseCode }}
                    </el-form-item>

                    <el-form-item
                      v-if="item3.auditCourseCode"
                      label="审课学校的课程代码"
                      align="center"
                    >
                      {{ item3.auditCourseCode }}
                    </el-form-item>

                    <el-form-item v-if="item3.gpa" label="成绩" align="center">
                      {{ item3.gpa }}
                    </el-form-item>

                    <el-form-item
                      v-if="item3.state"
                      label="课程状态"
                      align="center"
                    >
                      {{
                        item3.state == 1
                          ? "选课待确认"
                          : item3.state == 2
                          ? "审课中"
                          : item3.state == 3
                          ? "审课通过"
                          : "审课未通过"
                      }}
                    </el-form-item>

                    <el-form-item
                      v-if="item3.professor"
                      label="匹配教授"
                      align="center"
                    >
                      {{ item3.professor }}
                    </el-form-item>
                  </div>
                </div>
              </div>
            </el-form>
          </template>
        </el-table-column>

        <el-table-column label="姓名" align="center">
          <template slot-scope="scope">
            <div
              class="handleStuDetail"
              @click="handleStuDetail(scope.row.userId)"
            >
              {{ scope.row.name }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="性别" align="center">
          <template slot-scope="scope">{{
            scope.row.sex == 0 ? "女" : scope.row.sex == 1 ? "男" : ""
          }}</template>
        </el-table-column> -->
        <el-table-column label="所在学校" align="center">
          <template slot-scope="scope">{{ scope.row.school }}</template>
        </el-table-column>
        <el-table-column label="年级" align="center">
          <template slot-scope="scope">
            <div v-show="scope.row.grade == 1">大学一年级</div>
            <div v-show="scope.row.grade == 2">大学二年级</div>
            <div v-show="scope.row.grade == 3">大学三年级</div>
            <div v-show="scope.row.grade == 4">大学四年级</div>
            <div v-show="scope.row.grade == 5">大学五年级</div>
          </template>
        </el-table-column>

        <el-table-column label="身份证" align="center">
          <template slot-scope="scope">{{ scope.row.idCard }}</template>
        </el-table-column>

        <el-table-column label="学号" align="center">
          <template slot-scope="scope">{{ scope.row.stuNumber }}</template>
        </el-table-column>

        <!-- <el-table-column label="报课数量" align="center">
          <template slot-scope="scope">{{ scope.row.courseNum }}</template>
        </el-table-column> -->

        <!-- <el-table-column label="授课形式" align="center">
          <template slot-scope="scope">{{ scope.row.teachWay }}</template>
        </el-table-column> -->

        <!-- <el-table-column
          label="分配时间"
          :sortable="'custom'"
          align="center"
          prop="nextChatTime"
        >
          <template slot-scope="scope">{{
            scope.row.studyCareAllocateTime
          }}</template>
        </el-table-column> -->

        <!-- <el-table-column
          label="提交时间"
          :sortable="'custom'"
          align="center"
          prop="addTime"
        >
          <template slot-scope="scope">{{ scope.row.addTime }}</template>
        </el-table-column> -->

        <!-- <el-table-column label="选课数量" align="center">
          <template slot-scope="scope">{{
            scope.row.chooseCourseNum
          }}</template>
        </el-table-column> -->

        <!-- <el-table-column label="审课通过数" align="center">
          <template slot-scope="scope">{{ scope.row.passCourseNum }}</template>
        </el-table-column> -->

        <!-- <el-table-column label="审课大学" align="center">
          <template slot-scope="scope">{{ scope.row.auditSchool }}</template>
        </el-table-column> -->

        <!-- <el-table-column label="最近沟通记录" align="center">
          <template slot-scope="scope">{{ scope.row.context }}</template>
        </el-table-column> -->

        <!-- <el-table-column
          label="下次沟通时间"
          :sortable="'custom'"
          align="center"
          prop="nextChatTime"
        >
          <template slot-scope="scope">{{ scope.row.nextChatTime }}</template>
        </el-table-column> -->
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next,jumper"
        :page-size="pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { adminAllUser, school, courseTime } from "@/api/cso";
import { formatDate } from "@/utils/date";
import { parseTime } from "@/utils";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "superAdminMyStu",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      pageSize: null,
      pageNum: null,
      listLoading: true,
      multipleSelection: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      inputTime: [],
      chatTime: [],
      paymentTime: [],
      schoolList: [],
      courseTimeList: [],
      passStateList: [
        {
          value: 1,
          label: "全部信息待确认"
        },
        {
          value: 2,
          label: "转学分待确认"
        },
        {
          value: 3,
          label: "已转学分"
        }
      ],
      isLopFile: false,
      isCreditFile: false,
      downloadLoading: false,
      filename: "",
      autoWidth: true,
      bookType: "xlsx"
    };
  },
  // computed: {
  //   starInputTime() {
  //     return this.inputTime[0]
  //   },
  //   endInputTime() {
  //     return this.inputTime[1]
  //   }
  // },
  watch: {
    inputTime(newValue, oldValue) {
      if (this.inputTime) {
        [
          this.listQuery.starAllocateTime,
          this.listQuery.endAllocateTime
        ] = this.inputTime;
      } else {
        this.listQuery.starAllocateTime = undefined;
        this.listQuery.endAllocateTime = undefined;
      }
    },
    chatTime(newValue, oldValue) {
      if (this.chatTime) {
        [
          this.listQuery.starChatTime,
          this.listQuery.endChatTime
        ] = this.chatTime;
      } else {
        this.listQuery.starChatTime = undefined;
        this.listQuery.endChatTime = undefined;
      }
    },
    paymentTime(newValue, oldValue) {
      if (this.paymentTime) {
        [
          this.listQuery.starPaymentTime,
          this.listQuery.endPaymentTime
        ] = this.paymentTime;
      } else {
        this.listQuery.starPaymentTime = undefined;
        this.listQuery.endPaymentTime = undefined;
      }
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    }
  },
  created() {
    this.getList();
    this.getSchool();
    this.getCourseTime();
  },

  methods: {
    test(aa) {
      if (this.userStatus == "") {
        this.userStatus = undefined;
      }
    },
    getList() {
      if (this.isLopFile) {
        this.listQuery.isLopFile = 0;
      }
      if (this.isCreditFile) {
        this.listQuery.isCreditFile = 0;
      }
      this.listLoading = true;
      adminAllUser(this.listQuery).then(res => {
        this.listLoading = false;

        this.list = res.data.list;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;
        console.log(this.list, "this.adminAllUserlist");
        this.$forceUpdate();
      });
    },
    getSchool() {
      school().then(res => {
        // this.schoolList = res.data;
        // console.log(this.schoolList, "this.schoolList");
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.schoolList.push(obj);
        });
      });
    },
    getCourseTime() {
      courseTime().then(res => {
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.courseTimeList.push(obj);
        });
      });
    },

    handleSearchList() {
      //搜索
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleAddProduct() {
      // 添加
      this.$router.push({ path: "/pms/addProduct" });
    },
    handleStuDetail(id) {
      // 测试用的
      this.$router.push({
        path: "/pms/stuDetail",
        query: {
          id: id
        }
      });
      // console.log(123);
    },

    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleSizeChange(val) {
      //切换展示条数
      this.listQuery.pageSize = val;
      this.getList();
    },

    handleResetSearch(formName) {
      this.$refs[formName].resetFields();
      // 重置
      // this.selectProductCateValue = [];
      this.inputTime = undefined;
      this.isCreditFile = false;
      this.isLopFile = false;
      // this.chatTime = undefined;
      // this.paymentTime = undefined;
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    changeTableSort(column) {
      // console.log("prop", column.prop);
      // console.log("order", column.order);
      // console.log("column", column);
      if (column.prop == "addTime") {
        this.listQuery.secondSort = null;
        if (column.order == "ascending") {
          this.listQuery.firstSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.firstSort = 1;
        }
      }
      if (column.prop == "nextChatTime") {
        this.listQuery.firstSort = null;
        if (column.order == "ascending") {
          this.listQuery.secondSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.secondSort = 1;
        }
      }
      if (column.order == null) {
        this.listQuery.secondSort = this.listQuery.firstSort = null;
      }
      this.getList();
      console.log("changeTableSort");
    },
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then(excel => {
        const tHeader = [
          "姓名",
          "所在学校",
          "年级",
          "身份证",
          "学号",
          "课程信息"
        ];
        const filterVal = [
          "name",
          "school",
          "grade",
          "idCard",
          "stuNumber",
          "auditCourseList"
        ];
        const list = this.list;
        const data = this.formatJson(filterVal, list);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    }
  }
};
</script>
<style lang="scss" scoped>
.handleStuDetail {
  cursor: pointer;
}
.box1 {
  margin: 20px;
  padding: 20px;
  border-radius: 4px;
  transition: 0.5s;
  &:hover {
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
    background-color: rgba(0, 0, 0, 0.03);
  }
}
</style>
