<template>
  <div id="studentList">
    <search-box-new
      ref="searchBox"
      :formData="formData"
      :formList="formList"
      @search="search"
      @init="init"
      ><template slot="customize">
        <el-button size="small" type="success" @click="exportUserList"
          >导出</el-button
        >
      </template></search-box-new
    >
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
      @editInfo="editInfo"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="addClick(props.scope.row, 2)"
          >详情</el-button
        >
        <!-- <el-button type="text" @click="addClick(props.scope.row, 3)"
          >选班</el-button
        > -->
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="formData.pageSize"
      :currentPage="formData.pageNum"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
import * as baseUrls from "@/utils/request.js";
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  name: "StudentList",
  components: { searchBoxNew, tableList, pagination },
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "学员管理",
        index: 0,
        ch: "条",
        num: false,
        border: true,
        choice: true,
        addHide: true,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      //搜索
      formList: [
        {
          prop: "educationTypeId",
          placeholder: "教育类型",
          scope: "educationType",
        },
        {
          prop: "businessId",
          placeholder: "业务层次",
          scope: "businessLevel",
          edu: "educationTypeId",
        },
        {
          prop: "gradePoint",
          placeholder: "是否分班",
          scope: "select",
          options: [
            {
              label: "已分班",
              value: 1,
            },
            {
              label: "未分班",
              value: 2,
            },
          ],
        },
        {
          prop: "hasBindWx",
          placeholder: "是否绑定微信",
          scope: "select",
          options: [
            {
              label: "是",
              value: 1,
            },
            {
              label: "否",
              value: 0,
            },
          ],
        },
        {
          prop: "hasFollowWx",
          placeholder: "是否关注公众号",
          scope: "select",
          options: [
            {
              label: "是",
              value: 1,
            },
            {
              label: "否",
              value: 0,
            },
          ],
        },
        {
          prop1: "startTime",
          prop2: "endTime",
          placeholder1: "创建开始时间",
          placeholder2: "创建结束时间",
          scope: "moreDataPicker",
          Diszing: true,
        },
        {
          prop: "goodsSearchKey",
          placeholder: "请输入商品编码/商品名称",
        },
        {
          prop: "companyName",
          placeholder: "请输入公司名称",
        },
        {
          prop: "realname",
          placeholder: "请输入学员名称",
        },
        {
          prop: "gradeName",
          placeholder: "请输入所属班级",
        },
        {
          prop: "lastVisitPlat",
          placeholder: "最近访问终端",
          scope: "select",
          options: [
            {
              label: "小程序",
              value: 1,
            },
            {
              label: "PC网站",
              value: 2,
            },
          ],
        },
        {
          prop1: "visitStartTime",
          prop2: "visitEndTime",
          placeholder1: "访问开始时间",
          placeholder2: "访问结束时间",
          scope: "moreDataPicker",
          Diszing: true,
        },
        {
          prop: "lastStudyPlat",
          placeholder: "最近学习终端",
          scope: "select",
          options: [
            {
              label: "小程序",
              value: 1,
            },
            {
              label: "PC网站",
              value: 2,
            },
          ],
        },
        {
          prop1: "studyStartTime",
          prop2: "studyEndTime",
          placeholder1: "学习开始时间",
          placeholder2: "学习结束时间",
          scope: "moreDataPicker",
          Diszing: true,
        },
        {
          prop: "idCard",
          placeholder: "学员身份证",
        },
        {
          prop: "telphone",
          placeholder: "学员手机号码",
        },
      ],
      formData: {
        status: "0,1",
        pageSize: 10,
        pageNum: 1,
        getOrderNum: 1,
      },
      // 表单
      tableSet: [
        {
          label: "学员编码",
          prop: "studentCode",
          hidden: true,
        },
        {
          label: "学员姓名",
          prop: "realname",
          hidden: true,
          scope: "editInfo",
        },
        {
          label: "一寸头像",
          prop: "oneInchPhotos",
          hidden: true,
          scope: "img",
          width: "120px",
        },
        {
          label: "学员身份证",
          prop: "idCard",
          hidden: true,
        },
        {
          label: "绑定手机号",
          prop: "telphone",
          hidden: true,
        },
        {
          label: "是否绑定小程序账号",
          prop: "userBindWx",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "是",
              value: 1,
            },
            {
              label: "否",
              value: 0,
            },
          ],
        },
        {
          label: "是否关注公众号",
          prop: "userFollowWx",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "是",
              value: 1,
            },
            {
              label: "否",
              value: 0,
            },
          ],
        },
        {
          label: "最近访问终端",
          prop: "visitFromPlat",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "小程序",
              value: 1,
            },
            {
              label: "PC网站",
              value: 2,
            },
          ],
        },
        {
          label: "最近访问时间",
          prop: "lastVisitTime",
          hidden: true,
          scope: "aTimeList",
          isDiszing:true
        },
        {
          label: "最近学习终端",
          prop: "studyFromPlat",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "小程序",
              value: 1,
            },
            {
              label: "PC网站",
              value: 2,
            },
          ],
        },
        {
          label: "最近学习时间",
          prop: "lastStudyTime",
          hidden: true,
          scope: "aTimeList",
          isDiszing:true
        },
        {
          label: "所在公司",
          prop: "companyName",
          hidden: true,
        },
        {
          label: "创建时间",
          prop: "createTime",
          hidden: true,
          scope: "aTimeList",
        },
        {
          label: "所购商品",
          prop1: "goodsCourseNum",
          prop2: "goodsBankNum",
          prop3: "goodsLiveNum",
          hidden: true,
          scope: "payGoodsList",
        },
        {
          label: "所属班级",
          prop: "classGradeUserGoodsVoList",
          hidden: true,
          scope: "classNums",
          sort: true,
          width: "120px",
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
    };
  },
  mounted() {
    if (this.$route.params.timeType >= 0) {
      this.$set(
        this.formData,
        "startTime",
        this.$methodsTools.timestampConvert(this.$route.params.timeType)[0]
      );
      this.$set(
        this.formData,
        "endTime",
        this.$methodsTools.timestampConvert(this.$route.params.timeType)[1]
      );
    }
    if (this.$route.params.educationId) {
      this.$set(
        this.formData,
        "educationTypeId",
        this.$route.params.educationId
      );
      this.$refs.searchBox.changeEducationType(
        this.$route.params.educationId,
        true
      );
    }
    if (this.$route.params.businessId) {
      this.$set(this.formData, "businessId", this.$route.params.businessId);
      this.$refs.searchBox.changeBusinessLevel(this.$route.params.businessId);
    }
    //学员统计页跳转带参初始化
    if (this.$route.params.row) {
      console.log(this.$route.params);
      let data = JSON.parse(this.$route.params.row);
      switch (this.$route.params.type) {
        case 1:
          //新增学员数
          let data1 = new Date(`${data.date} 00:00:00`).getTime();
          this.formData.startTime = data1;
          this.formData.endTime = data1 + 86399999;
          break;
        case 2:
          //访问学员总数
          let data2 = new Date(`${data.date} 00:00:00`).getTime();
          this.formData.visitStartTime = data2;
          this.formData.visitEndTime = data2 + 86399999;
          break;
        case 3:
          //访问小程序学员数
          let data3 = new Date(`${data.date} 00:00:00`).getTime();
          this.formData.visitStartTime = data3;
          this.formData.visitEndTime = data3 + 86399999;
          this.formData.lastVisitPlat = 1;
          break;
        case 4:
          //访问PC学员数
          let data4 = new Date(`${data.date} 00:00:00`).getTime();
          this.formData.visitStartTime = data4;
          this.formData.visitEndTime = data4 + 86399999;
          this.formData.lastVisitPlat = 2;
          break;
        case 5:
          //学习学员数
          let data5 = new Date(`${data.date} 00:00:00`).getTime();
          this.formData.studyStartTime = data5;
          this.formData.studyEndTime = data5 + 86399999;
          break;
        case 6:
          //小程序学习学员数
          let data6 = new Date(`${data.date} 00:00:00`).getTime();
          this.formData.studyStartTime = data6;
          this.formData.studyEndTime = data6 + 86399999;
          this.formData.lastStudyPlat = 1;
          break;
        case 7:
          //PC学习学员数
          let data7 = new Date(`${data.date} 00:00:00`).getTime();
          this.formData.studyStartTime = data7;
          this.formData.studyEndTime = data7 + 86399999;
          this.formData.lastStudyPlat = 2;
          break;

        default:
          break;
      }
    }
    this.search();
  },
  activated() {
    this.search();
  },
  methods: {
    /**
     * 导出学员列表
     */
    exportUserList() {
      var data = JSON.parse(JSON.stringify(this.formData));
      delete data.pageSize;
      delete data.pageNum;
      this.$api.inquiregradestudentlistStudentExport(data).then((res) => {
        let url = baseUrls.baseURL + "common/download?fileName=" + res.msg;
        let link = document.createElement("a");
        let fileName = "导入模板" + ".xlsx";
        document.body.appendChild(link);
        link.href = url;
        link.dowmload = fileName;
        link.click();
        link.remove();
      });
    },
    editInfo(v) {
      this.addClick(v, 2);
    },
    search(int) {
      this.loading = true;
      if (int === 1) {
        this.formData.pageNum = 1;
      }
      if (int === 2) {
        this.formData = {
          status: "0,1",
          pageSize: 10,
          pageNum: 1,
          getOrderNum: 1,
        };
      }
      var data = JSON.parse(JSON.stringify(this.formData));
      if (this.formData.startTime) {
        data.startTime = parseInt(data.startTime / 1000);
      }
      if (this.formData.endTime) {
        data.endTime = parseInt(data.endTime / 1000);
      }
      if (this.formData.visitStartTime) {
        data.visitStartTime = parseInt(data.visitStartTime / 1000);
      }
      if (this.formData.visitEndTime) {
        data.visitEndTime = parseInt(data.visitEndTime / 1000);
      }
      if (this.formData.studyStartTime) {
        data.studyStartTime = parseInt(data.studyStartTime / 1000);
      }
      if (this.formData.studyEndTime) {
        data.studyEndTime = parseInt(data.studyEndTime / 1000);
      }
      this.$api
        .inquiregradestudentlistStudent(data)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search(2);
    },
    del(v) {
      this.$alert(
        "确定删除此内容？<br />内容删除后将无法恢复，请慎重考虑",
        "提示",
        {
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          var data = {
            chapterExamId: v.chapterExamId,
            status: -1,
          };
          this.$api.editbankchapter(data).then((res) => {
            this.$message.success("删除成功");
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addClick(v, int) {
      // int = 2详情 3选班
      if (v === undefined) {
        // 添加学员
        // this.$router.push({
        //   path: "addClass",
        // });
      } else {
        if (int === 2) {
          const jump = () => {
            //学员详情
            this.$router.push({
              path: "studentXQ",
              query: {
                id: v.userId,
              },
            });
          };
          const statusPage = this.$store.state.tagsView.visitedViews.some(
            (item) => {
              return item.name == "StudentXQ";
            }
          );
          if (statusPage) {
            this.$store
              .dispatch("tagsView/delCachedView", {
                name: "StudentXQ",
              })
              .then((res) => {
                jump();
              });
          } else {
            jump();
          }
        }
        if (int === 3) {
          // 选班
          this.$router.push({
            path: "studentMenu",
            query: {
              id: v.userId,
            },
          });
        }
      }
    },
    handleSizeChange(v) {
      this.formData.pageSize = v;
      this.formData.pageNum = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.formData.pageNum = v;
      this.search();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
</style>

