<template>
  <div id="moduleManagement">
    <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"
      ref="tableList"
      rowKey="moduleId"
    >
      <template slot="customize">
        <el-button size="medium" @click="batchDel" type="warning"
          >批量删除</el-button
        >
      </template>
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="gift(props.scope.row)"
          >赠送题卷</el-button
        >
        <el-button type="text" @click="addClick(props.scope.row, 0)"
          >修改</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"
    />
    <giftQuestionPaper ref="giftQuestionPaper" @backFunc="backFunc" />
  </div>
</template>

<script>
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
import giftQuestionPaper from "./giftQuestionPaper.vue";
import { moduleBatchDel } from "@/api/resource/videoManagement";
export default {
  name: "ModuleManagement",
  components: { searchBoxNew, tableList, pagination, giftQuestionPaper },
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "模块管理",
        index: 0,
        ch: "条",
        num: false,
        choice: true,
        border: true,
        addHide: false,
        openCheckMore: true,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      //搜索
      formList: [
        {
          prop: "educationTypeId",
          placeholder: "教育类型",
          scope: "educationType",
        },
        {
          prop: "businessId",
          placeholder: "业务层次",
          scope: "businessLevel",
          edu: "educationTypeId",
        },
        {
          prop: "subjectId",
          placeholder: "科目",
          scope: "sujectType",
          edu: "educationTypeId",
        },
        {
          prop: "prefixName",
          placeholder: "请输入标题前缀",
        },
        {
          prop: "publishStatus",
          placeholder: "发布状态",
          scope: "select",
          options: [
            {
              label: "发布",
              value: 1,
            },
            {
              label: "未发布",
              value: 0,
            },
          ],
        },
        {
          prop: "moduleName",
          placeholder: "请输入模块标题",
        },
      ],
      formData: {
        status: "0,1",
        pageSize: 10,
        pageNum: 1,
      },
      // 表单
      tableSet: [
        {
          label: "模块编码",
          prop: "code",
          hidden: false,
        },
        {
          label: "标题前缀",
          prop: "prefixName",
          hidden: false,
        },
        {
          label: "模块标题",
          prop: "moduleName",
          hidden: true,
          scope: "editInfo",
        },
        {
          label: "适用业务层级",
          prop: "businessList",
          hidden: false,
          scope: "mapTypesMores",
        },
        {
          label: "关联课程",
          prop: "courseList",
          prop1: "courseName",
          hidden: false,
          scope: "aboutChapter",
          int: 3,
        },
        {
          label: "章总数",
          prop: "chapterNum",
          hidden: false,
        },
        {
          label: "节总数",
          prop: "sectionNum",
          hidden: false,
        },
        {
          label: "学习时长",
          prop: "durationTime",
          hidden: true,
          scope: "convert",
        },
        {
          label: "赠送题卷",
          prop: "freeExamName",
          hidden: true,
        },
        {
          label: "最后编辑时间",
          prop: "updateTime",
          scope: "aTimeList",
          hidden: true,
        },
        {
          label: "创建时间",
          prop: "createTime",
          scope: "aTimeList",
          hidden: true,
        },
        {
          label: "发布状态",
          prop: "publishStatus",
          hidden: true,
          scope: "fabStatus",
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
    };
  },
  mounted() {
    this.search();
  },
  activated() {
    this.search();
  },
  methods: {
    batchDel() {
      if (!this.$refs.tableList.allCheckData.length) {
        this.$message.warning("请勾选需要删除的题目");
        return;
      }
      this.$confirm(
        `此操作将永久删除所勾选的${this.$refs.tableList.allCheckData.length}条模块, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          const ids = this.$refs.tableList.allCheckData.map(
            (item) => item.moduleId
          );
          moduleBatchDel({
            status: -1,
            ids,
          }).then((res) => {
            this.$message.success("批量删除成功");
            this.$refs.tableList.clearMoreActive();
            this.search(1);
          });
        })
        .catch(() => {});
      // console.log(this.$refs.tableList.)
    },
    editInfo(v) {
      this.addClick(v, 0);
    },
    backFunc() {
      this.search();
    },
    search(int) {
      this.loading = true;
      if (int === 1) {
        this.formData.pageNum = 1;
      }
      if (int === 2) {
        this.formData = {
          status: "0,1",
          pageSize: 10,
          pageNum: 1,
        };
      }
      this.$api
        .inquireCourseListmodule(this.formData)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search(2);
    },
    /**
     * 赠送题卷
     */
    gift(row) {
      this.$refs.giftQuestionPaper.openBoxs(row);
    },
    del(v) {
      this.$api.gradecheckGoodsChange({ moduleId: v.moduleId }).then((res) => {
        if (res.data > 0) {
          this.$message.error("已有学员正在学习该模块，无法删除");
          return;
        } else {
          this.$alert(
            "确定删除此内容？<br />内容删除后将无法恢复，请慎重考虑",
            "提示",
            {
              dangerouslyUseHTMLString: true,
            }
          )
            .then(() => {
              var data = {
                moduleId: v.moduleId,
                status: -1,
              };
              this.$api.editCoursemodule(data).then((res) => {
                this.$message.success("删除成功");
                this.search();
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
        }
      });
    },
    addClick(v, int) {
      if (v === undefined) {
        this.$router.push({
          path: "moduleManagementAdd",
        });
      } else {
        const jump = () => {
          this.$router.push({
            path: "moduleManagementEdit",
            query: {
              id: v.moduleId,
            },
          });
        };
        const statusPage = this.$store.state.tagsView.visitedViews.some(
          (item) => {
            return item.name == "ModuleManagementEdit";
          }
        );
        if (statusPage) {
          this.$store
            .dispatch("tagsView/delCachedView", {
              name: "ModuleManagementEdit",
            })
            .then((res) => {
              jump();
            });
        } else {
          jump();
        }
      }
    },
    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>

