<template>
  <div id="classList">
    <search-box-new
      ref="searchBox"
      :formData="formData"
      :formList="formList"
      @search="search"
      @init="init"
    />
    <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
        >
        <el-button type="text" @click="del(props.scope.row)">删除</el-button>
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="formData.pageSize"
      :currentPage="formData.pageNum"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  components: { searchBoxNew, tableList, pagination },
  name: "ClassList",
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      navText: {
        border: true,
        title: "班级管理",
        index: 0,
        ch: "条",
        num: false,
        choice: true,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      //搜索
      formList: [
        {
          prop: "educationTypeId",
          placeholder: "教育类型",
          scope: "educationType",
        },
        {
          prop: "businessId",
          placeholder: "业务层次",
          scope: "businessLevel",
          edu: "educationTypeId",
        },
        {
          prop: "schoolId",
          placeholder: "院校",
          scope: "schoolList",
          edu: "educationTypeId",
        },
        {
          prop: "majorId",
          placeholder: "专业",
          scope: "Professional",
          edu: "educationTypeId",
        },
        {
          prop: "subjectId",
          placeholder: "科目",
          scope: "sujectType",
          edu: "educationTypeId",
        },
        {
          prop: "classStatus",
          placeholder: "开班状态",
          scope: "select",
          options: [
            { label: "已开班", value: 1 },
            { label: "预报名(未开班)", value: 0 },
          ],
        },
        {
          prop: "status",
          placeholder: "班级状态",
          scope: "select",
          noClear: false,
          options: [
            { label: "全部状态", value: "0,1" },
            { label: "无效", value: 0 },
            { label: "有效", value: 1 },
          ],
        },
        {
          prop1: "classStartTime",
          prop2: "classEndTime",
          placeholder1: "班级有效期开始时间",
          placeholder2: "班级有效期结束时间",
          scope: "moreDataPicker",
        },
        {
          prop: "searchKey",
          placeholder: "请输入班级名称/商品名称",
        },
        {
          prop: "userName",
          placeholder: "请输入学员姓名",
        },
      ],
      formData: {
        status: "0,1",
        pageSize: 10,
        pageNum: 1,
        classStatus: "",
      },
      // 表单
      tableSet: [
        {
          label: "商品编码",
          prop: "code",
          objProp: "goodsList",
          hidden: false,
          scope: "objType",
        },
        {
          label: "年份",
          prop: "year",
          objProp: "goodsList",
          hidden: false,
          scope: "objType",
        },
        {
          label: "商品业务层级",
          prop1: "educationName",
          prop2: "projectName",
          prop3: "businessName",
          objProp: "goodsList",
          hidden: false,
          scope: "objTypeYW",
        },
        {
          label: "商品名称",
          prop: "goodsName",
          objProp: "goodsList",
          hidden: true,
          scope: "objType",
        },
        {
          label: "科目",
          prop: "subjectNames",
          hidden: true,
        },
        {
          label: "班级编码",
          prop: "gradeCode",
          hidden: false,
        },
        {
          label: "班级号",
          prop: "classStatus",
          prop1: "officialName",
          hidden: true,
          scope: "classTypes",
        },
        {
          label: "班级名称",
          prop: "className",
          hidden: true,
          scope: "editInfo",
        },
        {
          label: "学时",
          prop: "classHours",
          objProp: "goodsList",
          hidden: false,
          scope: "objType",
        },
        {
          label: "班主任",
          prop: "nickName",
          hidden: false,
        },
        {
          label: "班级人数",
          prop1: "studentNum",
          prop2: "studentUpper",
          hidden: true,
          scope: "peopleNums",
        },
        {
          label: "班级总人数",
          prop1: "studentNumAll",
          prop2: "studentUpper",
          hidden: true,
          scope: "peopleNums",
        },
        {
          label: "开放学习时间",
          prop: "learningStatus",
          prop1: "learningTimeStart",
          hidden: true,
          scope: "classTimeTypes",
        },
        {
          label: "班级状态",
          prop: "status",
          hidden: false,
          scope: "isOptions",
          options: [
            {
              label: "有效",
              value: 1,
            },
            {
              label: "无效",
              value: 0,
            },
          ],
        },
        {
          label: "班级有效期",
          prop1: "classStartTime",
          prop2: "classEndTime",
          hidden: true,
          Diszing: false,
          scope: "TimeLists",
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
    };
  },
  async mounted() {
    if (Object.keys(this.$route.params).length) {
      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.userId) {
        this.$set(this.formData, "userId", this.$route.params.userId);
      }
      if (this.$route.params.classStatus >= 0) {
        this.$set(this.formData, "classStatus", this.$route.params.classStatus);
      }
      if (this.$route.params.hasInterface >= 0) {
        this.formData.hasInterface = this.$route.params.hasInterface;
      }
    } else {
      // await this.moRenBus();
    }
    if (this.$route.params.timeType >= 0) {
      this.$set(
        this.formData,
        "classStartTime",
        this.$methodsTools.timestampConvert(this.$route.params.timeType)[0]
      );
      this.$set(
        this.formData,
        "classEndTime",
        this.$methodsTools.timestampConvert(this.$route.params.timeType)[1]
      );
    }
    this.search();
  },
  activated() {
    this.search();
  },
  methods: {
    /**
     * 默认选择继教二建业务层次
     */
    moRenBus() {
      return new Promise((resolve, reject) => {
        this.$api.inquireCourseEducationType({ status: 1 }).then((res) => {
          console.log(res);
          for (let i = 0; i < res.rows.length; i++) {
            if (res.rows[i].educationName === "继续教育") {
              this.$api
                .inquirebusinessList({ status: 1, educationId: res.rows[i].id })
                .then((result) => {
                  for (let j = 0; j < result.rows.length; j++) {
                    if (
                      result.rows[j].projectName === "建造师" &&
                      result.rows[j].businessName === "二级"
                    ) {
                      this.$set(
                        this.formData,
                        "educationTypeId",
                        res.rows[i].id
                      );
                      this.$refs.searchBox.changeEducationType(
                        res.rows[i].id,
                        true
                      );
                      this.$set(this.formData, "businessId", result.rows[j].id);
                      this.$refs.searchBox.changeBusinessLevel(
                        result.rows[j].id
                      );
                      resolve();
                      break;
                    }
                  }
                });
              break;
            }
          }
        });
      });
    },
    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,
          classStatus: "",
        };
      }
      if (int === 4) {
        this.formData.pageNum = 1;
      }
      var data = JSON.parse(JSON.stringify(this.formData));
      if (this.formData.classStartTime) {
        data.classStartTime = parseInt(data.classStartTime / 1000);
      }
      if (this.formData.classEndTime) {
        data.classEndTime = parseInt(data.classEndTime / 1000);
      }
      this.$api
        .inquireGradegradesearchList(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 = {
            gradeId: v.gradeId,
            status: -1,
          };
          this.$api.editGradegrade(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: "manageClass",
              query: {
                id: v.gradeId,
              },
            });
          };
          const statusPage = this.$store.state.tagsView.visitedViews.some(
            (item) => {
              return item.name == "ManageClass";
            }
          );
          if (statusPage) {
            this.$store
              .dispatch("tagsView/delCachedView", {
                name: "ManageClass",
              })
              .then((res) => {
                jump();
              });
          } else {
            jump();
          }
        }
        if (int === 3) {
          // 学员管理
          this.$router.push({
            path: "studentMenu",
            query: {
              id: v.gradeId,
              goodsId: v.goodsList[0].goodsId,
            },
          });
        }
      }
    },
    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>
