<template>
  <div id="knowledgeManagement">
    <search-box-new
      ref="searchBox"
      :formData="formData"
      :pageSizes="pageSize"
      :pageNums="currentPage"
      :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, 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"
    />
    <el-dialog
      :visible.sync="dialogVisible"
      width="780px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">
          {{ statusPop === 1 ? "添加" : statusPop === 0 ? "修改" : "详情" }}
        </div>
        <div class="rightBoxs">
          <img src="@/assets/images/Close@2x.png" alt="" @click="close" />
        </div>
      </div>
      <div>
        <el-form label-position="right" label-width="110px" :model="listData">
          <el-form-item
            v-for="(items, indexs) in listitem"
            :key="indexs"
            :label="items.label"
            :prop="items.prop"
          >
            <div v-if="items.scope === 'buss'">
              <el-select
                v-model="eduType"
                placeholder="请选择教育类型"
                @change="changeEduType"
              >
                <el-option
                  v-for="(item, index) in eduTypeOptions"
                  :key="index"
                  :label="item.educationName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
              <el-select
                v-model="courType"
                placeholder="请选择业务层次"
                @change="changecourseType"
              >
                <el-option
                  v-for="(item, index) in newCourTypeOptions"
                  :key="index"
                  :label="item.businessName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
              <el-popover
                ref="popovers"
                placement="bottom"
                trigger="click"
                @show="showHandle"
                @hide="hideHandle"
                :disabled="courType ? false : true"
              >
                <el-checkbox-group v-model="sujectArray" class="checkboxSty">
                  <el-checkbox
                    v-for="(item, index) in newSujectOption"
                    :label="item.newId"
                    :key="index"
                    >{{ item.subjectName }}</el-checkbox
                  >
                </el-checkbox-group>
                <div
                  style="display: block; text-align: center; margin-top: 10px"
                >
                  <el-button
                    size="mini"
                    type="primary"
                    @click="submitSujectArray"
                    >确定</el-button
                  >
                </div>
                <el-button slot="reference" style="margin-left: 12px"
                  >请选择科目</el-button
                >
              </el-popover>
            </div>
            <div v-else-if="items.scope === 'activeBox'">
              <div :class="changeHeight ? 'ach' : 'clh'">
                <div
                  v-for="(item, index) in newSujectApis"
                  :key="index"
                  class="listBoxStys"
                >
                  {{
                    item.educationName +
                    " - " +
                    item.projectName +
                    " - " +
                    item.businessName +
                    " - " +
                    item.subjectName
                  }}
                  <i
                    class="el-icon-error closeIcons"
                    @click="closeType(index)"
                  ></i>
                </div>
              </div>
              <el-button
                size="mini"
                v-if="newSujectApis.length > 1"
                @click="changeType"
                >{{ changeHeight ? "展开" : "关闭" }}</el-button
              >
              <span v-if="newSujectApis.length === 0">未选项目类型</span>
            </div>
            <div v-else>
              <div v-if="statusPop === 0">
                {{ listData.knowledgeName }}
              </div>
              <div v-else>
                <div v-if="!BusNameList.length">请在下面输入框添加知识点</div>
                <ul v-else class="BusBoxs">
                  <li
                    class="fot_Le"
                    v-for="(item, index) in BusNameList"
                    :key="index"
                  >
                    {{ item }}
                    <i
                      class="el-icon-error icon_clear"
                      @click="clearBusName(index)"
                    ></i>
                  </li>
                  <div style="clear: both"></div>
                </ul>
                <el-input
                  style="width: 40%; margin-right: 10px"
                  v-model="BusName"
                ></el-input
                ><el-button @click="addBusName">添加</el-button>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  components: { searchBoxNew, tableList, pagination },
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "知识点",
        index: 0,
        ch: "条",
        num: false,
        choice: true,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      //搜索
      formList: [
        {
          prop: "educationId",
          placeholder: "教育类型",
          scope: "educationType",
        },
        {
          prop: "businessId",
          placeholder: "业务层次",
          scope: "businessLevel",
        },
        {
          prop: "subjectId",
          placeholder: "科目",
          scope: "sujectType",
        },
        {
          prop: "knowledgeName",
          placeholder: "请输入知识点名称",
        },
      ],
      // 表单
      tableSet: [
        {
          label: "知识点编码",
          prop: "encoder",
          hidden: true,
        },
        {
          label: "知识点标题",
          prop: "knowledgeName",
          scope: "editInfo",
          hidden: true,
        },
        {
          label: "适用业务层级",
          prop: "courseBusinessList",
          hidden: true,
          scope: "mapTypesMores",
          width: "400",
        },
      ],

      eduTypeOptions: [], //教育类型数据
      projectTypeOptions: [], //项目类型数据
      courTypeOptions: [], //业务层次数据
      newCourTypeOptions: [], //当前业务层次数据
      sujectOption: [], //科目数据
      newSujectOption: [], //当前科目数据数据
      eduType: "", //当前选中教育类型
      courType: "", //当前选中业务层次
      sujectApis: [], //当前存在的科目
      newSujectApis: [],
      sujectArray: [], //选中的科目
      BusName: "", //标签名称
      BusNameList: [], //标签列表

      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      changeHeight: false,
      rules: [],
      listData: {},
      formData: {
        status: "0,1",
        pageSize: 10,
        pageNum: 1,
      },
      listitem: [
        {
          label: "适用业务层级",
          scope: "buss",
        },
        {
          label: "已选科目",
          scope: "activeBox",
        },
        {
          label: "知识点",
        },
      ],
      statusPop: 1,
      dialogVisible: false,
    };
  },
  watch: {
    sujectApis: {
      immediate: true,
      handler(newName, oldName) {
        this.changeTypes();
      },
    },
  },
  mounted() {
    this.getDict();
    this.search();
  },
  methods: {
    addBusName() {
      if (this.BusName.trim()) {
        this.BusNameList.push(this.BusName);
        this.BusName = "";
      } else {
        this.$message({
          message: "请输入知识点内容",
          type: "warning",
          duration: 1200,
        });
        this.BusName = "";
      }
    },
    changeTypes() {
      var self = this;
      var arrays = [];
      this.sujectApis.map((item, index) => {
        this.courTypeOptions.map((items) => {
          if (items.id === item.split("-").map(Number)[0]) {
            var obj = {
              educationTypeId: items.educationId,
              educationName: items.educationName,
              projectId: items.projectId,
              projectName: items.projectName,
              businessId: items.id,
              businessName: items.businessName,
            };
            self.sujectOption.map((i) => {
              if (
                i.id === item.split("-").map(Number)[1] &&
                i.courseArrays.indexOf(items.projectId) !== -1
              ) {
                obj.subjectName = i.subjectName;
                obj.subjectId = i.id;
              }
            });
            arrays.push(obj);
          }
        });
      });
      this.newSujectApis = arrays;
      console.log(this.newSujectApis);
    },
    getDict() {
      this.$api.inquireCourseEducationType({ status: 1 }).then((res) => {
        this.eduTypeOptions = res.rows;
      });
      this.$api.inquireCourseProjectType({ status: 1 }).then((res) => {
        this.projectTypeOptions = res.rows;
      });
      this.$api.inquirebusinessList({ status: 1 }).then((res) => {
        this.courTypeOptions = res.rows;
        this.newCourTypeOptions = res.rows;
      });
      this.$api.inquireCourseSubject({ status: 1 }).then((res) => {
        res.rows.map((item, index) => {
          var array = [];
          item.courseProjectTypes.map((items, indexs) => {
            array.push(items.id);
          });
          item.courseArrays = array;
        });
        this.sujectOption = res.rows;
      });
    },
    editInfo(v) {
      this.addClick(v, 0);
    },
    //int === 1搜索 页码归1 int === 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,
        };
      }
      this.$api.inquireKnowledgeExamListS(this.formData).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.navText.index = res.total;
      });
      this.loading = false;
    },
    init() {
      this.search(2);
    },
    changeType() {
      this.changeHeight = !this.changeHeight;
    },
    changeEduType() {
      if (!(this.courType === undefined || this.courType === "")) {
        this.courType = "";
      }
      var arrays = [];
      this.courTypeOptions.map((item) => {
        if (item.educationId === this.eduType) {
          arrays.push(item);
        }
      });
      this.newCourTypeOptions = arrays;
    },
    changecourseType() {
      this.newCourTypeOptions.map((item, index) => {
        if (item.id === this.courType) {
          this.eduType = item.educationId;
          var array = [];
          this.sujectOption.map((items, indexs) => {
            if (items.courseArrays.indexOf(item.projectId) !== -1) {
              array.push(items);
            }
          });
          this.newSujectOption = array;
        }
      });
      var arrays = [];
      this.courTypeOptions.map((item) => {
        if (item.educationId === this.eduType) {
          arrays.push(item);
        }
      });
      this.newCourTypeOptions = arrays;
      this.$refs.popovers[0].doClose();
    },
    submitSujectArray() {
      var self = this;
      this.sujectApis = this.sujectApis.filter((item, index) => {
        return item.split("-").map(Number)[0] !== Number(self.courType);
      });
      for (let i = 0; i < this.sujectArray.length; i++) {
        this.sujectApis.push(this.sujectArray[i]);
      }
      this.$refs.popovers[0].doClose();
    },
    submit() {
      if (this.statusPop === 1) {
        var dataInfos = {
          status: 1,
          examKnowledgeBusinessAddBoList: this.newSujectApis,
          knowledgeName: this.BusNameList,
        };
        this.$api.addKnowledgeExam(dataInfos).then((res) => {
          this.$message.success("添加成功");
          this.dialogVisible = false;
          this.search();
        });
      } else {
        var dataInfos = {
          status: this.listData.status,
          knowledgeId: this.listData.knowledgeId,
          examKnowledgeBusinessAddBoList: this.newSujectApis,
        };
        this.$api.editKnowledgeExam(dataInfos).then((res) => {
          this.$message.success("修改成功");
          this.dialogVisible = false;
          this.search();
        });
      }
    },
    del(v) {
      this.$alert(
        "确定删除此内容？<br />内容删除后将无法恢复，请慎重考虑",
        "提示",
        {
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          var data = {
            knowledgeId: v.knowledgeId,
            status: -1,
          };
          this.$api.editKnowledgeExam(data).then((res) => {
            this.$message.success("删除成功");
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    hideHandle() {},
    showHandle() {
      var array = [];
      for (let i = 0; i < this.sujectApis.length; i++) {
        if (
          this.sujectApis[i].split("-").map(Number)[0] === Number(this.courType)
        ) {
          array.push(this.sujectApis[i]);
        }
      }
      this.sujectArray = array;
      this.newSujectOption.map((item) => {
        item.newId = this.courType + "-" + item.id;
      });
    },
    addClick(v, int) {
      if (v === undefined) {
        this.statusPop = 1;
        this.listData = {};
        this.eduType = ""; //当前选中教育类型
        this.courType = ""; //当前选中业务层次
        this.sujectApis = []; //当前存在的科目
        this.newSujectApis = [];
        this.sujectArray = []; //选中的科目
        this.BusName = "";
        this.BusNameList = [];
      } else {
        this.statusPop = int;
        this.eduType = ""; //当前选中教育类型
        this.courType = ""; //当前选中业务层次
        this.BusName = "";
        this.BusNameList = [];
        var arrays = [];
        this.listData = v;
        v.courseBusinessList.map((item) => {
          arrays.push(item.businessId + "-" + item.subjectId);
        });
        this.sujectApis = arrays;
      }
      this.$nextTick(() => {
        this.changeHeight = true;
      });
      this.dialogVisible = true;
    },
    close() {
      this.dialogVisible = false;
    },
    closeType(index) {
      this.sujectApis.splice(index, 1);
    },
    clearBusName(index) {
      this.BusNameList.splice(index, 1);
    },
    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;
  }
}
.listBoxStys {
  flex-shrink: 0;
  padding: 0px 10px;
  border-radius: 8px;
  border: 1px solid #eee;
  margin-right: 10px;
  margin-bottom: 6px;
}
.closeIcons {
  color: red;
  cursor: pointer;
  margin-left: 6px;
}
.ach {
  display: flex;
  align-items: center;
  overflow: hidden;
}
.clh {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.BusBoxs {
  min-height: 100px;
  border: 1px solid #a4a4a4;
  margin-bottom: 10px;
  border-radius: 8px;
  max-height: 280px;
  padding: 10px;
  overflow-y: auto;
  .fot_Le {
    border-radius: 12px;
    border: 1px solid yellowgreen;
    padding: 0px 10px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    float: left;
    margin-right: 6px;
    margin-bottom: 6px;
    .icon_clear {
      margin-left: 2px;
      font-size: 15px;
      color: #f56c6c;
      cursor: pointer;
    }
  }
}
</style>

