<template>
  <!-- 同步教研 -->
  <div class="ssysContent">
    <el-card>
      <div style="padding: 0 75px">
        <span class="title">协同备课</span>
        <span class="subTitle">我的课程 - 我创建的 - <span style="color: #000">协同备课</span></span>
      </div>
    </el-card>

 <el-card style="padding-bottom: 50px;">
    <div class="searchDiv" style="display: flex; justify-content: flex-end">
      <el-button type="primary" @click="addData()">创建</el-button>
      <div class="searchValue">
        <input type="text" placeholder="搜索关键字" v-model="keyword" />
        <div class="searchBtn" @click="initData">
          <img src="/kczy/searchIcon.png" alt="" />
        </div>
      </div>
    </div>
    <div class="line"></div>
    <div class="nav">
      <div class="search-list">
        <div class="label">年级</div>
        <div class="valueDiv">
          <div class="item pointer" :class="activeGrade==item.grade_id?'active':''" @click="initGrade(item)" v-for="item in gradeData" :key="item.grade_id">{{item.grade_name}}</div>
        </div>
      </div>

      <div class="search-list">
        <div class="label">科目</div>
        <div class="valueDiv">
          <div class="item pointer" :class="activeSubject==item.subject_id?'active':''" @click="initSubject(item)" v-for="item in subjectData" :key="item.subject_id">{{item.subject_name}}</div>
        </div>
      </div>
    </div>
    <el-empty v-if="wholeData.length==0" image-size="100" description="无数据" style="padding: 10px 0;" />
    


 <div class="list-right">
      <div v-for="(item, index) in wholeData" :key="index">
  
          <div class="cover">
            <img :src="item.zx_img" alt="" />
          </div>
          <div class="main pt-3">
            <div style="margin:0; width: 300px;"   class="overflow-hidden main-title">
              <el-tooltip
                class="box-item"
                effect="dark"
                :content="item.zx_name"
                placement="top-start"
              >
                <p class="truncate  cursor-pointer ">{{ item.zx_name }}</p>
              </el-tooltip>
            </div>
            <img style="cursor: pointer" @click="editData(item.zx_id)" class="edit" src="@/assets/img/edit.png" alt="" />
            <img style="cursor: pointer" @click="deleteData(item.zx_id)" class="delete" src="@/assets/img/delete.png" alt="" />
            <div style="width: 300px;" class="main-content overflow-hidden">
              <el-tooltip
                class="box-item"
                effect="dark"
                :content="item.zx_brief"
                placement="top-start"
              >
                 <p class="truncate">{{ item.zx_brief }}</p>
              </el-tooltip>
            </div>
            <div class="cursor-pointer flex items-center text-sm avatar">
              <el-avatar :size="16" :src="getAvatarUrl(userMsg.user_id)" />
              <span class="ml-1">{{ userMsg.user_name }}</span>
            </div>
          </div>
        </div>
      </div>
  





    <div class="paginationDiv">
      <el-pagination background layout="total,prev, pager, next" :total="total" :page-size="limit" v-model:current-page="cur_page"  @current-change="initData()" />
    </div>
  </el-card>
  </div>
</template>

<script>
import {
  grade_list,
  xtbk_list,
  subject_list,
  xtbk_delete,
  user_list,
  user_info,
} from "./api";
import { getFullUrl, getAvatarUrl } from "@/utils/helper";
import { user } from '~/store/user';
export default {
  data() {
    return {
      activeName: "1",
      wholeData: [],
      resultData: [],
      keyword: "",
      https: window.GLOBAL_CONFIG.api,
      gradeList: [],
      subjectList: [],
      total: 0,
      cur_page: 1,
      limit:10,
      grade_id: "",
      subject_id: "",
      gradeData: [],
      activeGrade: "",
      subjectData: [],
      activeSubject: "",
      userMsg: {},
    };
  },
  methods: {
    //年级
    initGradeData() {
      this.gradeData = [
        {
          grade_id: "",
          grade_name: "全部",
        }
      ];
      grade_list().then((res) => {
        if (res.code == 1) {
          this.gradeData = this.gradeData.concat(res.data.page_data);
        }
      });
    },
    initGrade(value) {
      this.activeGrade = value.grade_id;
      this.initData();
    },
    //科目
    initSubjectData() {
      this.subjectData = [
        {
          subject_id: "",
          subject_name: "全部",
        }
      ];
      subject_list().then((res) => {
        if (res.code == 1) {
          this.subjectData = this.subjectData.concat(res.data.page_data);
        }
      });
    },
    initSubject(value) {
      console.log(value, 123);
      this.activeSubject = value.subject_id;
      this.initData();
    },
    // 获取用户信息
    getUser() {
      var locData = window.localStorage.getItem("userInfo");
      var user_id = JSON.parse(locData).user_id; //老师的role_id
      let data = {
        user_id: user_id,
      };
      user_info(data).then((res) => {
        this.userMsg = res.data;
      });
    },
    // 初始化
    initData() {
      let data = {
        admin:1,
        create_user_id: user.user_id,
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        grade_id: this.activeGrade,
        subject_id: this.activeSubject,
      };
      xtbk_list(data).then((res) => {
        if (res.code == 1) {
          this.wholeData = res.data.page_data;
          this.total = res.data.total_rows * 1;
          this.cur_page = Number(res.data.page_now);
        }
      });
    },

    addData() {
      this.$router.push({ name: "xtbk-create" });
    },
    editData(id) {
      this.$router.push({ name: "xtbk-create", query: { id: id } });
      this.setStorage();
    },
    deleteData(id) {
      let data = {
        zx_id: id,
      };
      this.$confirm("确认删除该课程?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          xtbk_delete(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "课程删除成功！",
                type: "success",
              });
              this.initData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => { });
    },
    setStorage() {
      sessionStorage.setItem("keyword", this.keyword);
      sessionStorage.setItem("grade_id", this.activeGrade);
      sessionStorage.setItem("subject_id", this.activeSubject);
      sessionStorage.setItem("page", this.cur_page);
    },
    removeStorage() {
      this.keyword = sessionStorage.getItem("keyword") || "";
      this.activeGrade = sessionStorage.getItem("grade_id") || "";
      this.activeSubject = sessionStorage.getItem("subject_id") || "";
      this.cur_page = Number(sessionStorage.getItem("page")) || "1";

      sessionStorage.removeItem("keyword");
      sessionStorage.removeItem("grade_id");
      sessionStorage.removeItem("subject_id");
      sessionStorage.removeItem("page");
    }
  },
  // async created() {
  //   await this.gradeListData();
  //   await this.subjectListData();
  // },
  mounted() {
    this.removeStorage();
    this.initData();
    this.getUser();
    this.initGradeData();
    this.initSubjectData();
    // const _this = this;
    // document.onkeydown = function (e) {
    //   e = window.event || e;
    //   if (
    //     _this.$route.name == "course-ssys" &&
    //     (e.code === "Enter" || e.code === "enter")
    //   ) {
    //     // 登录事件
    //     _this.initData();
    //   }
    // };
  },
};
</script>

<style lang="scss" scoped>
.ssysContent {
  position:relative;

  .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;
  }
  .searchDiv {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 80px;
    .menuDiv {
      display: flex;
      margin-bottom: -10px;
      .list {
        margin: 0 20px;
        padding: 0 10px;
        color: #949494;
        position: relative;
        line-height: 40px;
        font-size: 14px;
        &::after {
          content: "";
          width: 0;
          height: 2px;
          background: rgba(0, 131, 197, 0.8);
          position: absolute;
          bottom: -10px;
          left: 50%;
          transform: translateX(-50%);
          transition: width linear 0.1s;
        }
        &:hover {
          color: rgba(0, 131, 197, 0.8);
        }
        &.active {
          color: rgba(0, 131, 197, 0.8);
          font-size: 16px;
          &::after {
            width: 100%;
          }
        }
      }
    }
    .el-button {
      width: 100px;
      height: 40px;
      margin-right: 40px;
    }
    .searchValue {
      width: 350px;
      height: 50px;

      background: #ffffff;
      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;
        }
      }
    }
  }
  .line {
    height: 1px;
    background: #eaeaea;
    width: calc(100% + 40px);
    margin-left: -20px;
    margin-top: 10px;
  }
  .nav {
    width: 1400px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    padding: 10px 0;
    .search-list {
      display: flex;
      align-items: baseline;
      font-size: 14px;
      margin: 10px 0;
      .label {
        color: #000;
        margin-right: 25px;
      }
      .valueDiv {
        flex: 1;
        display: flex;
        color: #949494;
        border-bottom: 1px dashed #e4e6e8;
        padding-bottom: 15px;
        font-weight: 400;
        cursor: pointer;
        .item + .item {
          margin-left: 20px;
        }
        .item {
          padding: 3px 5px;
          &.active {
            color: #00a3ff;
            border-radius: 6px;
            background: rgba($color: #00a3ff, $alpha: 0.1);
          }
        }
      }
    }
    div {
      display: flex;
      span {
        padding-top: 10px;
        display: inline-block;
        width: 50px;
        height: 50px;
        font-size: 16px;
        font-family: PingFangSC, PingFangSC-Semibold;
        font-weight: 600;
        text-align: center;
        color: #000000;
        line-height: 21px;
      }
    }
  }
  .list-right {
    width: 1400px;
    display: flex;
    flex-wrap: wrap;
    margin: 0 auto;

    & > div {
      box-sizing: border-box;
      width: calc(50% - 40px);
      height: 150px;
      display: flex;
      margin-bottom: 20px;

      box-shadow: 0px 15px 24px 0px rgba(161, 153, 168, 0.18);

      &:nth-of-type(odd) {
        margin-right: 40px;
      }
      .cover {
        width: 263px;
        border-radius: 6px 0 0 6px;
        margin-right: 12px;
        height: 150px;
        overflow: hidden;
        img {
          width: 100%;
          // height: 100%;
        }
      }

      .main {
        display: flex;
        flex-direction: column;
        // padding: 20px 10px;
        width: 400px;
        box-sizing: border-box;
        position: relative;
        .main-title {
          // width: 203px;
          height: 22px;
          font-size: 16px;
          font-family: PingFangSC, PingFangSC-Medium;
          font-weight: 550;
          text-align: left;
          color: #050026;
          line-height: 22px;
        }
        .edit,
        .delete {
          width: 18px;
          height: 17px;
          position: absolute;
          top: 10px;
          right: 10px;
        }
        .edit {
          position: absolute;
          top: 10px;
          right: 50px;
        }
        .main-content {
          // display: inline-block;
          margin-top: 5px;
          font-size: 14px;
          word-wrap: break-word;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          color: #949494;
        }
        .avatar {
          width: 186px;
          height: 20px;
          display: inline-block;
          font-size: 14px;
          font-family: PingFangSC, PingFangSC-Medium;
          font-weight: 500;
          text-align: left;
          color: #050026;
          line-height: 20px;
          margin-top: 30px;
          img {
            display: inline-block;
            width: 24px;
            height: 24px;
            border-radius: 50%;
            margin-right: 8px;
          }
        }
      }
    }
  }
   .paginationDiv {
    position: absolute;
    right: 30px;
    bottom: 15px;
    padding: 3px;
  }
}
</style>

<style lang="scss" scoped>
.ssysContent {
  .resultCard .el-card__body {
    padding: 0 !important;
  }
}
</style>
