<!--
 * @Author: 纪泽龙 jizelong@qq.com
 * @Date: 2022-09-22 10:59:44
 * @LastEditors: 纪泽龙 jizelong@qq.com
 * @LastEditTime: 2022-12-17 14:36:14
 * @FilePath: /danger-manage-web/src/views/lessonsProgram/components/Lession.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="form-wrapper">
    <el-form
      class="form"
      ref="form"
      :model="form"
      label-width="auto"
      :rules="rules"
    >
      <div class="top flex">
        <el-form-item label="题库名称" prop="bankName">
          <el-input style="width: 300px" v-model="form.bankName"></el-input>
        </el-form-item>

        <el-form-item
          label="归属部门"
          prop="deptId"
          label-width="140px"
          ref="treeItem"
        >
          <Treeselect
            class="tree"
            v-model="form.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择归属部门"
            @open="treeOpen"
            @close="treeClose"
            @select="select"
          />
        </el-form-item>
      </div>

      <!-- </div> -->
      <!-- <el-form-item label="课程内容" prop="courseConent">
        <Editor v-model="form.courseConent" :min-height="192" />
        <el-input
          v-show="false"
          disabled
          v-model="form.courseConent"
        ></el-input>
      </el-form-item> -->

      <!-- <div class="flex">
        <el-form-item label="视频上传" v-if="!readOnly" prop="video">
          <FileUpload
            listType="picture"
            @resFun="getFileInfoVideo"
            @remove="listRemoveVideo"
            :fileArr="fileListVideo"
            :fileSize="500"
            :fileType="['mp4']"
          />
          <el-input v-show="false" disabled v-model="form.video"></el-input>
        </el-form-item>
        <el-form-item label="附件上传" v-if="!readOnly" prop="enclosure">
          <FileUpload
            listType="picture"
            @resFun="getFileInfoFile"
            @remove="listRemoveFile"
            :fileArr="fileListFile"
          />
          <el-input v-show="false" disabled v-model="form.enclosure"></el-input>
        </el-form-item>
      </div> -->
    </el-form>
  </div>
</template>

<script>
import Editor from "./Editor";
import FileUpload from "@/components/FileUpload";
import uploadfile from "@/assets/uploadfile.png";
// import { mapGetters } from "vuex";
// import {
//   addLessons,
//   getLessonById,
//   changeLesson,
// } from "@/api/educationPlanExam/lessonsProgram";
import {
  listBank,
  addBank,
  updateBank,
  getBank,
  
} from "@/api/educationPlanExam/questionBank";

// 所有部门
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "",
  props: {
    bankId: {
      type: Number,
    },
  },
  components: {
    Editor,
    FileUpload,
    Treeselect,
  },
  data() {
    return {
      form: {
        bankName: "",
        // courseType: "",
        // courseConent: "",
        // video: "",
        // enclosure: "",
        deptId: null,
      },
      // 归属部门列表
      deptOptions: [],

      fileListVideo: [],
      fileListFile: [],
      readOnly: false,
      rules: {
        bankName: [
          { required: true, trigger: "blur", message: "课程名称不能为空" },
        ],
        deptId: [
          { required: true, trigger: "blur", message: "请选择所属部门" },
        ],
        // courseType: [
        //   { required: true, trigger: "change", message: "课程类型不能为空" },
        // ],
        // courseConent: [
        //   { required: true, trigger: "blur", message: "课程内容不能为空" },
        // ],
        // video: [{ required: true, trigger: "blue", message: "视频不能为空" }],
        // enclosure: [
        //   { required: true, trigger: "blur", message: "附件不能为空" },
        // ],
      },
    };
  },
  computed: {
    // 获取课程类型
    // ...mapGetters(["courseOptions"]),
  },
  created() {
    if (this.bankId) {
      this.getLessonById();
    }
    // 归属部门列表
    this.getTreeselect();
  },
  mounted() {},
  methods: {
    // 添加课程
    addLessons(data) {
      if (!this.bankId) {
        console.log("添加");
        return addBank(data);
      } else {
        console.log("修改");
        return updateBank({ bankId: this.bankId, ...data });
      }
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
        console.log(this.deptOptions);
      });
    },
    // 当树形组件打开
    treeOpen() {
      document.querySelector(".vue-treeselect__control").style.borderColor = "";
    },
    // 当属性组件关闭
    treeClose(a, b) {
      if (!a) {
        // 手动添加报红功能，没值的时候报红
        document.querySelector(".vue-treeselect__control").style.borderColor =
          "red";
        this.save();
      } else {
        document.querySelector(".vue-treeselect__control").style.borderColor =
          "";
        this.$refs.treeItem.clearValidate();
      }
    },
    select() {},
    // 复现
    getLessonById() {
      getBank(this.bankId).then((res) => {
        console.log("res", res);
        if (res.code == 200) {
          this.form = {
            bankName: res.data.bankName,
            deptId: res.data.deptId,
          };
          //   const data = res.data;
          //   const { bankName, courseType, courseConent, video, enclosure } =
          //     data;
          //   this.form = {
          //     bankName,
          //     courseType,
          //     courseConent,
          //     video,
          //     enclosure,
          //   };
          //   this.fileListVideo = [
          //     {
          //       name: bankName + "视频",
          //       url: uploadfile,
          //     },
          //   ];
          //   this.fileListFile = [
          //     {
          //       name: bankName + "附件",
          //       url: uploadfile,
          //     },
          //   ];
        }
      });
    },
    getFileInfoVideo(res) {
      this.form.video = res.url;
      // this.form.videoName = res.fileName;
      this.fileListVideo = [
        {
          name: res.fileName,
          url: uploadfile,
        },
      ];
    },
    listRemoveVideo(e) {
      this.fileListVideo = [];
      this.form.video = "";
      // this.form.videoName = null;
    },
    getFileInfoFile(res) {
      this.form.enclosure = res.url;
      // this.form.enclosureName = res.fileName;
      this.fileListFile = [
        {
          name: res.fileName,
          url: uploadfile,
        },
      ];
    },
    listRemoveFile(e) {
      this.fileListFild = [];
      this.form.enclosure = "";
      // this.form.fileName = null;
    },
    save(num = 2) {
      // 因为富文本编辑器会残留<p><br></p>，所以要清空
      // if (this.form.courseConent === "<p><br></p>") {
      //   this.form.courseConent = "";
      // }
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.addLessons({ ...this.form }).then((res) => {
            // 如果添加会传回来，就用传回来的，如果是修改本身就有，就用本身的
            // console.log('res',res)
            const bankId = this.bankId || res;
            // if (res.code == 200) {
            // 这样调比较纯函数一点
            if (num == 2) {
              this.$message({
                message: "保存题库成功",
                type: "success",
              });
            } else if (num == 3) {
              this.$message({
                message: "保存题库成功,请开始录入题目",
                type: "success",
              });
            }
            this.$parent.$parent.componentsNumChange(num);
            this.$parent.$parent.changeCourseId(bankId);
            this.$parent.$parent.$parent.getList();

            return true;
            // }
          });
        } else {
          if (!this.form.deptId) {
            document.querySelector(
              ".vue-treeselect__control"
            ).style.borderColor = "red";
          } else {
            document.querySelector(
              ".vue-treeselect__control"
            ).style.borderColor = "";
            this.$refs.treeItem.clearValidate();
          }
        }
      });
    },
    // 保存并进入题目
    saveAndNext() {
      this.save(3);
    },
  },
};
</script>
<style lang="scss" scoped>
.form-wrapper {
  padding-top: 22px;
  width: 100%;
  height: 100px;
  // overflow: hidden;
  // padding-bottom: 10px;
  margin-bottom: 20px;

  border-bottom: 1px solid #bbbbbb;
  .top {
    width: 100%;
    justify-content: space-between;
  }
}
</style>
