<template>
  <!-- 创建课程 -->
  <div class="createCourse">
    <el-card>
      <div class="flex justify-between pr-30px">
        <div style="padding: 0 75px">
          <span class="title">自主研学</span>
          <span class="subTitle"
            >我的课程 - 我创建的 -
            <span class="pointer" @click="goBack">自主研学</span> -
            <span style="color: #000">{{
              $route.query.id ? "编辑" : "创建"
            }}</span></span
          >
        </div>
        <div class="to-back" @click="goBack">返回</div>
      </div>
    </el-card>

    <el-card>
      <div style="color: #050026; font-size: 18px; font-weight: 400">
        {{ $route.query.id ? "编辑" : "创建" }}
      </div>
      <div class="formContent">
        <el-form
          v-if="!loading"
          :model="formline"
          ref="formline"
          :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 size="38" color="#9d9ab9" v-if="fileList.length==0">
                    <Picture />
                  </el-icon>
                  <img v-else :src="fileList[0].url" 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="课程封面图" class="kcfmt" prop="zzc_img">
            <file-upload-progress
              v-model:part="zzc_img"
              list-type="picture-card"
              accept=".jpg,.png,.jpeg"
              :size-limit="5 * 1024"
              @compile="handleCompile"
            >
              <template #default>
                <el-icon
                  size="38"
                  color="#9d9ab9"
                  v-if="formline.zzc_img == ''"
                >
                  <Picture />
                </el-icon>
                <img
                  v-else
                  :src="getFullUrl(formline.zzc_img)"
                  alt=""
                  style="width: 100%; height: 100%"
                />
              </template>
              <template #tip>
                <div
                  style="
                    color: #949494;
                    font-size: 12px;
                    margin-left: 20px;
                    display: inline-block;
                    vertical-align: top;
                    line-height: 20px;
                  "
                >
                  支持格式：jpg,png,jpeg（5M以内） 建议尺寸600x320
                </div>
              </template>
            </file-upload-progress>
          </el-form-item>
          <el-form-item label="主讲老师" prop="zzc_jsxx">
            <el-input v-model="formline.zzc_jsxx" />
          </el-form-item>
          <el-form-item label="课程名称" prop="zzc_name">
            <el-input v-model="formline.zzc_name" />
          </el-form-item>
          <el-form-item label="课程简介">
            <el-input v-model="formline.zzc_jj" type="textarea" rows="5" />
          </el-form-item>
          <div class="type flex">
            <el-form-item label="年级" prop="grade_id">
              <!-- <remote-select
                v-model:model-name="formline.grade_name"
                v-model="formline.grade_id"
                to="grade"
              ></remote-select> -->
              <remote-multi-select v-model="formline.grade_id" v-model:model-name="formline.grade_name" to="grade"></remote-multi-select>
            </el-form-item>
            <el-form-item label="学科" prop="subject_id">
              <!-- <remote-select
                v-model:model-name="formline.subject_name"
                v-model="formline.subject_id"
                to="subject"
              ></remote-select> -->
              <remote-multi-select v-model="formline.subject_id" v-model:model-name="formline.subject_name" to="subject"></remote-multi-select>
            </el-form-item>
          </div>

          <div style="display: flex">
            <el-form-item style="flex: 1" label="开始时间" prop="zzc_starttime">
              <el-config-provider :locale="local">
                <el-date-picker
                  v-model="formline.zzc_starttime"
                  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="zzc_endtime"
            >
              <el-config-provider :locale="local">
                <el-date-picker
                  v-model="formline.zzc_endtime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  type="datetime"
                  placeholder="选择日期时间"
                ></el-date-picker>
              </el-config-provider>
            </el-form-item>
          </div>

          <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('formline')"
                >提 交</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,
  zxxx_kc_add,
  zxxx_kc_detail,
  zxxx_kc_edit,
  user_list,
  zxxx_kc_list,
} from "./api";

import { Picture, FolderOpened } from "@element-plus/icons-vue";
import { ElConfigProvider, ElMessage } from "element-plus";
import zhcn from "element-plus/dist/locale/zh-cn.mjs";
import user from "~/store/user";
import { dealWithDateTime } from "~/utils/common-util.js";
export default {
  data() {
    return {
      local: zhcn,
      https: window.GLOBAL_CONFIG.api,
      zzc_id: "",
      wholeData: {},
      zzc_img: "",
      formline: {
        zzc_img: "",
        zzc_jsxx: "",
        zzc_name: "",
        zzc_jj: "",
        zzc_starttime: "",
        zzc_endtime: "",
        subject_id: "",
        grade_id: "",
        grade_name: "",
        subject_name: "",
        grade: {},
        subject: {},
        teacher: [],

      },
      rules: {
        zzc_img: [
          { required: true, message: "请选择课程封面图", trigger: "submit" },
        ],
        zzc_jsxx: [
          { required: true, message: "请输入主讲教师", trigger: "blur" },
        ],
        zzc_name: [
          { required: true, message: "请输入课程名称", trigger: "blur" },
        ],
        // zz_content: [
        //   { required: true, message: "请输入课程简介", trigger: "blur" },
        // ],
        grade_id: [
          {
            required: true,
            message: "请选择年级",
            trigger: "submit",
          },
        ],
        subject_id: [
          {
            required: true,
            message: "请选择学科",
            trigger: "submit",
          },
        ],
        zzc_starttime: [
          {
            required: true,
            message: "请选择开始时间",
            trigger: "submit",
            type: "date",
          },
        ],
        zzc_endtime: [
          {
            required: true,
            message: "请选择结束时间",
            trigger: "submit",
            type: "date",
          },
        ],
      },
      fileList: [],
      mainFileData: ["png", "jpg", "jpeg"],
      mainFileSize: 200,
      mainTip: "",
      gradeData: [],
      subjectData: [],
      isRead: false,
      loading: true,
      scourceFile1: [],
      scourceFile2: [],

    };
  },
  components: { Picture, FolderOpened, ElConfigProvider },
  methods: {

    goBack() {
      this.$router.go(-1);
    },

    // 初始化
    initValue() {
      // 判断是新建还是编辑
      let id = this.$route.query.id;
      // 编辑
      if (id != undefined) {
        let data = {
          zzc_id: this.$route.query.id,
        };
        zxxx_kc_detail(data).then((res) => {
          if (res.code == 1) {
            this.wholeData = res.data.one_info;
            console.log(this.wholeData, 111243);
            for (let key in this.formline) {
              this.formline[key] = res.data.one_info[key];
            }

            this.fileList = [
              {
                url: getFullUrl(this.formline.zzc_img),
              },
            ];
            // this.wholeData.fjData = JSON.parse(this.wholeData.zzc_fj_json);
            // this.scourceFile1 = [];
            // this.scourceFile2 = [];
            // if (this.wholeData.fjData != "") {
            //   this.wholeData.fjData.map((item) => {
            //     let suffix = item.file_url.substr(
            //       item.file_url.lastIndexOf(".") + 1
            //     );
            //     if (suffix == "mp4") {
            //       this.scourceFile2.push(item);
            //     } else {
            //       this.scourceFile1.push(item);
            //     }
            //   });
            // }
            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;
      }
    },
    handleCompile(fileList) {
      let data = fileList[fileList.length - 1];
      this.formline.zzc_img = data.response.data.url;
    },
    // 封面图
    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.zzc_img = res.data.url;
          console.log(file.raw, 1123);
        } else {
          this.mainTip = "上传失败";
        }
      });
    },
    // sourceUpload(data, type) {
    //   this["scourceFile" + type] = [];
    //   data.map((item) => {
    //     let obj = {
    //       file_url: "",
    //       file_name: item.name,
    //       upload_user: user.user_realname,
    //       upload_time: dealWithDateTime(new Date()),
    //     };
    //     if (item.part_url) {
    //       obj.file_url = item.part_url;
    //     } else if (item.response && item.response.data) {
    //       obj.file_url = item.response.data.url;
    //     }
    //     if (!this.wholeData.fjData) {
    //       this.wholeData.fjData.map((value) => {
    //         if (obj.file_url == value.file_url) {
    //           obj.upload_user = value.upload_user;
    //           obj.upload_time = value.upload_time;
    //           obj.file_name = value.file_name;
    //         }
    //       });
    //     }
    //     this["scourceFile" + type].push(obj);
    //   });

    //   console.log(this.wholeData.fjData, this["scourceFile" + type]);
    // },
    // 提交
    submitForm(formName) {
      if (!this.isRead) {
        this.$message({
          message: "请勾选课程上传协议书。",
          type: "error",
        });
        return;
      }
      let id = this.$route.query.id;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formline.zzc_img == "") {
            this.mainTip = "请上传课程封面图";
            return;
          }
          if (
            new Date(this.formline.zzc_endtime).getTime() <=
            new Date(this.formline.zzc_starttime).getTime()
          ) {
            ElMessage.warning("结束时间必须大于开始时间！");
            return;
          }
          let arr = this.scourceFile2.concat(this.scourceFile1);
          console.log(JSON.stringify(arr));

          let data = {
            zzc_img: this.formline.zzc_img,
            zzc_jsxx: this.formline.zzc_jsxx,
            zzc_name: this.formline.zzc_name,
            zzc_jj: this.formline.zzc_jj,
            grade_id: this.formline.grade_id,
            subject_id: this.formline.subject_id,
            grade_name: this.formline.grade_name,
            subject_name: this.formline.subject_name,
            zzc_starttime: this.formline.zzc_starttime,
            zzc_endtime: this.formline.zzc_endtime,
            // zz_fj: this.formline.zz_fj,
            // zz_video: this.formline.zz_video,
            // zz_fj_json: JSON.stringify(arr),
          };

          if (id == undefined) {
            zxxx_kc_add(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "添加成功！",
                  type: "success",
                });
                this.$router.push("/personal/course-zxxx");
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            try {
              Object.assign(data, { zzc_id: id });
              zxxx_kc_edit(data).then((res) => {
                if (res.code == 1) {
                  this.$message({
                    message: "编辑成功！",
                    type: "success",
                  });
                  this.$router.push("/personal/course-zxxx");
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
            } 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-zxxx");
    },
  },
  created() {
    this.initValue();

  },
};
</script>

<style lang="scss" scoped>
.to-back {
  width: 100px;
  height: 40px;
  background: rgba(0, 163, 255, 0.8);
  border-radius: 6px;
  font-size: 14px;
  font-family: PingFang SC, PingFang SC-Regular;
  font-weight: 400;
  text-align: center;
  color: #ffffff;
  line-height: 40px;
}
.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;
        }
        .mainUpload {
          .el-upload--picture-card {
            width: 180px;
            height: 96px;
          }
          .el-upload-list--picture-card .el-upload-list__item {
            display: none;
          }
        }
        .attachUpload {
          .el-upload-dragger {
            width: 65px;
            height: 65px;
            padding: 0;
            line-height: 65px;
            background: #f9f9f9;
          }
        }
      }
    }
    :deep() {
      .kcfmt {
        .el-upload--picture-card {
          width: 180px;
          height: 96px;
        }

        .el-upload-list.el-upload--picture-card,
        .el-upload-list__item {
          display: none;
        }
      }
    }
    a .el-upload-list__item-name {
      width: 300px;
    }
    .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>
