<template>
  <!-- 创建课程 -->
  <div class="createCourse">
    <el-card>
      <div style="padding: 0 75px">
        <span class="title">协同备课</span>
        <span class="subTitle">我的课程 - 我创建的 -
          <span class="pointer" @click="goBack">协同备课</span> -
          <span v-if="zx_id === undefined" style="color: #000">创建课程</span>
          <span v-else style="color: #000">编辑课程</span>
        </span>
      </div>
    </el-card>

    <el-card>
      <div v-if="zx_id === undefined" style="color: #050026; font-size: 18px; font-weight: 400">
        创建课程
      </div>
      <div v-else style="color: #050026; font-size: 18px; font-weight: 400">
        编辑课程
      </div>
      <div class="formContent" v-loading="loading" type="primary">
        <el-form v-if="!loading" :model="formline" ref="formlineRef" :rules="rules" size="large" label-position="top" class="demo-ruleForm">
          <div class="must">
            <div class="label">课程封面图</div>
            <div class="uploadDiv">
              <div style="display: flex">
                <el-upload class="mainUpload" action="" accept=".png, .jpg, .jpeg" :auto-upload="false" list-type="picture-card" :on-change="handleMainChange" :file-list="fileList">
                  <el-icon v-if="formline.zx_img == ''">
                    <Picture />
                  </el-icon>
                  <img v-else :src="https + formline.zx_img" alt="" style="width: 100%; height: 100%" />
                </el-upload>
                <div style="font-size: 12px; margin-left: 20px; position: relative">
                  <div style="color: #949494">
                    支持格式：jpg,png,jpeg（200kb以内） 建议尺寸600x320
                  </div>
                  <div style="color: #f35421; position: absolute; bottom: 0" v-if="fileList.length == 0">
                    {{ mainTip }}
                  </div>
                </div>
              </div>
            </div>
          </div>
          <el-form-item label="课程名称" prop="zx_name">
            <el-input v-model="formline.zx_name" />
          </el-form-item>
          <el-form-item label="课程简介" prop="zx_brief">
            <el-input v-model="formline.zx_brief" type="textarea" rows="5" />
          </el-form-item>
          <div class="type flex">
            <el-form-item label="年级" prop="grade_id">
              <remote-select v-model="formline.grade_id" to="grade"></remote-select>
            </el-form-item>
            <el-form-item label="学科" prop="subject_id">
              <remote-select v-model="formline.subject_id" to="subject"></remote-select>
            </el-form-item>
          </div>
          <div style="display: flex">
            <el-form-item style="flex: 1" label="开始时间" prop="zx_kssj">
              <el-config-provider :locale="local">
                <el-date-picker v-model="formline.zx_kssj" type="datetime" placeholder="选择日期时间" value-format="YYYY-MM-DD HH:mm:ss"></el-date-picker>
              </el-config-provider>
            </el-form-item>
            <el-form-item style="flex: 1; margin-left: 20px" label="结束时间" prop="zx_jssj">
              <el-config-provider :locale="local">
                <el-date-picker v-model="formline.zx_jssj" value-format="YYYY-MM-DD HH:mm:ss" type="datetime" placeholder="选择日期时间"></el-date-picker>
              </el-config-provider>
            </el-form-item>
          </div>

          <el-form-item label="备课老师" style="margin-top: 15px">
            <div class="checkedjs">
              <div v-for="js in isCheckedTeacher" class="checkedEach">
                <span>{{ js.user_realname }}</span>
                <img style="cursor: pointer" @click="deleteJs(js)" src="../../../../assets/img/del.png" alt="" />
              </div>
            </div>
            <div class="check">
              <div class="checkTop flex">
                <div class="searchValue">
                  <input type="text" placeholder="搜索关键字" v-model="searchKey" />
                  <div class="searchBtn" @click="getTeacher">
                    <img src="/kczy/searchIcon.png" alt="" />
                  </div>
                </div>
                <div class="pages flex">
                  <div @click="previousPage" class="previous">
                    <el-icon>
                      <Back />
                    </el-icon>
                  </div>
                  <div @click="nextPage" class="next">
                    <el-icon>
                      <Right />
                    </el-icon>
                  </div>
                </div>
              </div>
              <div class="goupeTeacher flex">
                <el-checkbox-group v-if="xtbkTeacher.length > 0" v-model="checkList">
                  <el-checkbox @change="isCheckJs($event, t)" v-for="t in xtbkTeacher" :label="t.user_id" :key="t.user_id">{{ t.user_realname }}</el-checkbox>
                </el-checkbox-group>
                <div v-else>暂无数据</div>
              </div>
            </div>
            <div style="
                color: #f35421;
                position: absolute;
                bottom: -30px;
                font-size: 12px;
              " v-if="fileList.length == 0">
              {{ checkTip }}
            </div>
          </el-form-item>
          <el-form-item>
            <el-checkbox class="read" v-model="isRead" label="已阅读并同意《课程上传协议》" size="large" />
          </el-form-item>
          <el-form-item>
            <div class="btnDiv">
              <el-button type="primary" @click="submitForm('formlineRef')">提 交</el-button>
              <el-button @click="closeCreate()">关 闭</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>
  
  <script>
import {
  grade_list,
  subject_list,
  upload_file,
  xtbk_add,
  xtbk_detail,
  xtbk_edit,
  user_list,
} from "./api";

import { Picture, FolderOpened } from "@element-plus/icons-vue";
import { ElConfigProvider } from "element-plus";
import zhcn from "element-plus/lib/locale/lang/zh-cn";
export default {
  data() {
    return {
      local: zhcn,
      https: window.GLOBAL_CONFIG.api,
      zx_id: this.$route.query.id,
      wholeData: {},
      formline: {
        ywj: "",
        zx_img: "",
        zx_name: "",
        zx_brief: "",
        zx_kssj: "2022-01-01 11:00",
        zx_jssj: "2022-01-02 11:00",
        subject_id: "",
        grade_id: "",
        grade: {},
        subject: {},
        teacher: [],
      },
      rules: {
        zx_name: [
          { required: true, message: "请输入课程名称", trigger: "blur" },
        ],
        zx_brief: [{ required: true, message: "请输入简介", trigger: "blur" }],
        grade_id: [
          {
            required: true,
            message: "请选择年级",
            trigger: "submit",
          },
        ],
        subject_id: [
          {
            required: true,
            message: "请选择学科",
            trigger: "submit",
          },
        ],
        zx_kssj: [
          {
            required: true,
            message: "请选择开始时间",
            trigger: "submit",
            type: "date",
          },
        ],
        zx_jssj: [
          {
            required: true,
            message: "请选择结束时间",
            trigger: "submit",
            type: "date",
          },
        ],
      },
      fileList: [],
      mainFileData: ["png", "jpg", "jpeg"],
      mainFileSize: 200,
      mainTip: "",
      attachList: [],
      attachFileData: ["ppt", "pptx", "pdf", "mp4"],
      attachFileSize: 500,
      attachTip: "",
      gradeData: [
        // "一年级",
        // "二年级",
        // "三年级",
        // "四年级",
        // "五年级",
        // "六年级",
        // "初一",
        // "初二",
        // "初三",
        // "高一",
        // "高二",
        // "高三",
      ],
      subjectData: [],
      xtbkTeacher: [],
      isCheckedTeacher: [],
      teacherPage: 1,
      checkList: [],
      zx_bkjs: [],
      zx_bkjs_json: "",
      checkTip: "",
      pageData: 1,
      searchKey: "",
      isRead: false,
      loading: true,
      pickerStartTime: (time) => {
        let beginDateVal = this.formline.zx_jssj;
        if (beginDateVal) {
          return time.getTime() > new Date(beginDateVal).getTime();
        }
      },
      pickerEndTime: (time) => {
        let beginDateVal = this.formline.zx_kssj;
        if (beginDateVal) {
          return time.getTime() < new Date(beginDateVal).getTime();
        }
      },
    };
  },
  components: { Picture, FolderOpened, ElConfigProvider },
  methods: {
    goBack() {
      this.$router.go(-1);
    },

    // 用户列表
    getTeacher() {
      let data = {
        page: this.pageData,
        limit: "20",
        keyword: this.searchKey,
        user_name: "",
        user_role_id: "75",
      };
      user_list(data).then((res) => {
        this.xtbkTeacher = res.data.page_data;
        this.teacherPage = res.data.total_page;
      });
    },
    // 选中教师
    isCheckJs(e, t) {
      if (e) {
        this.isCheckedTeacher.push(t);
      } else {
        let index = this.isCheckedTeacher.findIndex(
          (item) => item.user_id == t.user_id
        );
        this.isCheckedTeacher.splice(index, 1);
      }
      // console.log(this.isCheckedTeacher);
      this.zx_bkjs = [];
      this.isCheckedTeacher.forEach((item) => {
        this.zx_bkjs.push({
          user_id: item.user_id,
          user_name: item.user_name,
          user_avatar: item.user_avatar,
        });
      });
      this.zx_bkjs_json = JSON.stringify(this.zx_bkjs);
      // console.log(this.zx_bkjs_json);
    },
    // 删除教师
    deleteJs(js) {
      let index = this.isCheckedTeacher.findIndex(
        (item) => item.user_id == js.user_id
      );
      this.isCheckedTeacher.splice(index, 1);
      this.checkList.splice(index, 1);
    },
    // 上一页
    previousPage() {
      if (this.pageData == 1) {
        this.pageData = 1;

      } else {
        this.pageData--;
      }
      let data = {
        page: this.pageData,
        limit: "20",
        keyword: this.searchKey,
        user_name: "",
        user_role_id: "75",
      };
      user_list(data).then((res) => {
        this.xtbkTeacher = res.data.page_data;
        this.teacherPage = res.data.total_page;
      });
    },
    // 下一页
    nextPage() {
      if (this.pageData == this.teacherPage) {
        this.pageData = this.teacherPage;
      } else {
        this.pageData++;
      }
      let data = {
        page: this.pageData,
        limit: "20",
        keyword: this.searchKey,
        user_name: "",
        user_role_id: "75",
      };
      user_list(data).then((res) => {
        this.xtbkTeacher = res.data.page_data;
        this.teacherPage = res.data.total_page;
      });
    },
    // 初始化
    initValue() {
      // 判断是新建还是编辑
      let id = this.$route.query.id;
      // 编辑
      if (id != undefined) {
        let data = {
          zx_id: this.$route.query.id,
        };
        xtbk_detail(data).then((res) => {
          if (res.code == 1) {
            this.wholeData = res.data.one_info;
            for (let key in this.formline) {
              this.formline[key] = res.data.one_info[key];
            }
            this.checkList = [];
            if (res.data.one_info.zx_bkjs_json) {
              this.isCheckedTeacher = res.data.one_info.zx_bkjs_json;
              res.data.one_info.zx_bkjs_json.forEach((item) => {
                this.checkList.push(item.user_id);
              });
            } else {
              this.isCheckedTeacher = [];
            }
            console.log("object :>> ", this.formline);
            this.loading = false;
          }
        });
      }
      // 新建
      else {
        for (let key in this.formline) {
          this.formline[key] = "";
        }
        if (this.gradeData[0]) {
          this.formline.grade.grade_name = this.gradeData[0].grade_name;
          this.formline.grade.grade_id = this.gradeData[0].grade_id;
        }
        if (this.subjectData[0]) {
          this.formline.subject.subject_name = this.subjectData[0].subject_name;
          this.formline.subject.subject_id = this.subjectData[0].subject_id;
        }
        this.loading = false;
      }
    },
    // 封面图
    handleMainChange(file) {
      let fileType = file.name.substring(file.name.lastIndexOf(".") + 1);
      const isJPG = this.mainFileData.includes(fileType);
      const sizeLimit = file.size / 1024 < this.mainFileSize;
      if (!isJPG) {
        this.mainTip = "上传文件只能是jpg,png,jpeg格式";
      }
      if (!sizeLimit) {
        this.mainTip = "上传文件不能超过200KB";
      }
      if (isJPG && sizeLimit) {
        this.fileList = [file];
      } else {
        this.fileList = [];
      }

      if (this.fileList.length == 0) {
        return false;
      }

      upload_file(file.raw).then((res) => {
        if (res.code == 1) {
          this.formline.zx_img = res.data.url;
        } else {
          this.mainTip = "上传失败";
        }
      });
    },
    // 提交
    submitForm(formName) {
      console.log("submitForm :>> ");
      let id = this.$route.query.id;
      this.$refs[formName].validate((valid) => {
        console.log(valid);
        if (valid) {
          console.log(111);
          if (this.formline.zx_img == "") {
            console.log(2222222);
            this.mainTip = "请上传课程封面图";
            return;
          }
          if (this.isCheckedTeacher.length == 0) {
            console.log(333);
            this.checkTip = "请选择教师";
            return;
          }
          let data = {
            zhjy_xtbk: {
              zx_img: this.formline.zx_img,
              zx_name: this.formline.zx_name,
              zx_brief: this.formline.zx_brief,
              // grade_name: this.formline.grade.grade_name,
              grade_id: this.formline.grade_id,
              subject_id: this.formline.subject_id,
              zx_kssj: this.formline.zx_kssj,
              zx_jssj: this.formline.zx_jssj,
              // subject_name: this.formline.subject.subject_name,
              zx_bkjs_json: this.zx_bkjs_json,
            },
          };
          if (id === undefined) {
            this.$confirm("确认创建该课程?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }).then(() => {
              xtbk_add(data).then((res) => {
                console.log("res", res);
                this.$router.push("/personal/course-xtbk");
              });
            });
          } else {
            // console.log("=== 2:>> ");
            try {
              Object.assign(data, { zx_id: id });
              this.$confirm("确认编辑该课程?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              }).then(() => {
                xtbk_edit(data).then((res) => {
                  console.log(res);
                  this.$router.push("/personal/course-xtbk");
                });
              });
            } catch (error) {
              console.error(error);
            }
          }
        } else {
          return false;
        }
      });
    },
    // 关闭
    closeCreate() {
      let id = this.$route.query.id;
      if (id === undefined) {
        for (let key in this.formline) {
          this.formline[key] = "";
        }
      }
      this.$router.push("/personal/course-xtbk");
    },
  },
  created() {
    this.initValue();
    this.getTeacher();
  },
};
</script>
  
<style lang="scss" scoped>
.flex {
  display: flex;
  flex-wrap: wrap;
}
.createCourse {
  .el-card {
    margin-bottom: 15px;
    background: #fff;
  }
  .pointer {
    cursor: pointer;
  }
  .title {
    font-size: 18px;
    color: #050026;
  }
  .subTitle {
    font-size: 14px;
    color: #949494;
    margin-left: 45px;
  }
  .formContent {
    width: 575px;
    margin: auto;
    .must {
      display: flex;
      align-items: center;
      margin-bottom: 22px;
      .label {
        color: #949494;
        font-size: 14px;
        margin-right: 15px;
        &::after {
          content: "*";
          color: #f35421;
          margin-left: 4px;
        }
      }
      .uploadDiv {
        flex: 1;
        .el-upload--picture-card {
          width: 180px;
          height: 96px;
        }
      }
    }
    .showType {
      width: 100%;
      height: 40px;
      background: #f9f9f9;
      border-radius: 6px;
      display: flex;
      align-items: center;
      padding: 0 20px;
      .typeItem {
        color: #fff;
        font-size: 13px;
        padding: 3px 8px;
        border-radius: 6px;
        margin-right: 20px;
        line-height: 22px;
      }
    }
    .type {
      margin-bottom: 10px;
      justify-content: space-between;
      .el-form-item {
        width: 273px;
      }
      .flex {
        display: flex;
        align-items: baseline;
        .label {
          font-size: 13px;
          color: #333;
          &::after {
            content: "*";
            color: #f35421;
            margin-left: 4px;
          }
        }
        .valueDiv {
          margin-left: 15px;
          flex: 1;
          display: flex;
          flex-wrap: wrap;
          color: #949494;
          padding-bottom: 15px;
          font-weight: 400;
          font-size: 14px;
          .item {
            padding: 3px 5px;
            margin-right: 20px;
            margin-bottom: 5px;
            &.active {
              color: #00a3ff;
              border-radius: 6px;
              background: rgba($color: #00a3ff, $alpha: 0.1);
            }
          }
        }
      }
    }
    .btnDiv {
      text-align: center;
      width: 100%;
      .el-button {
        background: #f4f4f4;
        border: none;
        width: 160px;
        height: 50px;
        color: #41387f;
        font-family: PingFangSC, PingFangSC-Semibold;
      }
      .el-button--primary {
        background: #00a3ff;
        margin-right: 80px;
        color: #fff;
      }
    }
  }
}
.checkedjs {
  width: 100%;
  height: 44px;
  background-color: #f9f9f9;
  border-radius: 6px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  box-sizing: border-box;
  .checkedEach {
    display: flex;
    height: 22px;
    line-height: 22px;
    margin-right: 5px;
    span {
      padding: 0 5px;
      border-radius: 6px;
      background-color: #00a3ff;
      color: #fff;
    }
    img {
      width: 15px;
      height: 15px;
      border-radius: 50%;
      border: 1px solid #00a3ff;
      margin: 5px;
    }
  }
}
.check {
  // width: 516px;
  width: 100%;
  height: 228px;
  background: #ffffff;
  padding: 6px 15px;
  box-sizing: border-box;
  border: 1px solid #e4e6e8;
  border-radius: 6px;
  .checkTop {
    width: 100%;
    justify-content: space-between;

    .searchValue {
      width: 278px;
      height: 39px;
      background: #f6f8fa;
      border-radius: 8px;
      overflow: hidden;
      position: relative;
      input {
        width: 100%;
        height: 100%;
        outline: 0;
        box-sizing: border-box;
        padding-left: 10px;
        background: #f6f8fa;
      }
      .searchBtn {
        width: 26px;
        height: 26px;
        background: #00a3ff;
        border-radius: 6px;
        position: absolute;
        right: 5px;
        top: 50%;
        transform: translate(-50%, -50%);
        text-align: center;
        vertical-align: middle;
        cursor: pointer;
        img {
          display: block;
          text-align: center;
          margin: 6px auto 0;
        }
      }
    }
    .pages {
      width: 70px;
      justify-content: space-between;
      margin-top: 5px;
      .previous,
      .next {
        width: 32px;
        height: 32px;
        line-height: 32px;
        opacity: 0.6;
        text-align: center;
        border: 1px solid #e4e6e8;
        border-radius: 8px;
        cursor: pointer;
      }
    }
  }
  .goupeTeacher {
    padding: 5px 10px;
    box-sizing: border-box;

    .each {
      margin-right: 27px;
      font-size: 16px;
      font-weight: 500;
      color: #949494;
      input {
        width: 17px;
        height: 17px;
        background-color: #e7ebee;
        border-radius: 4px;
        margin-right: 5px;
      }
    }
  }
}
</style>
  
  <style lang="scss">
.createCourse {
  .el-card {
    margin-bottom: 15px;
    background: #fff;
    .el-form {
      .el-form-item {
        &.is-required {
          .el-form-item__label {
            &::before {
              display: none;
            }
            &::after {
              content: "*";
              color: #f35421;
              margin-left: 4px;
            }
          }
        }
        .el-form-item__label {
          color: #949494;
          font-size: 14px;
          position: relative;
          &::after {
            content: "*";
            color: #f35421;
            margin-left: 4px;
          }
        }
        .el-form-item__content {
          .el-select {
            width: 100%;
          }
          .el-input {
            width: 100%;
            .el-input__wrapper {
              width: 100%;
              box-shadow: none;
              background: #f9f9f9;
              border-radius: 6px;
            }
          }
          .el-textarea {
            .el-textarea__inner {
              box-shadow: none;
              background: #f9f9f9;
              border-radius: 6px;
              // color: #d0d0d0;
            }
          }
          .el-checkbox.read {
            .el-checkbox__inner {
              border-radius: 50%;
            }
          }
          .el-input__inner {
            // color: #d0d0d0;
          }
        }
      }
      .uploadDiv {
        .mainUpload {
          .el-upload--picture-card {
            width: 180px;
            height: 96px;
          }
          .el-upload-list--picture-card .el-upload-list__item {
            display: none;
          }
        }
        .attachUpload {
          .el-upload--picture-card {
            width: 65px;
            height: 65px;
          }
        }
      }
    }
  }
}
</style>