<template>
  <div class="my-classhour">
    <div class="my-classhour__body">
      <el-tabs :value="activeName" @tab-click="tabChange">
        <el-tab-pane label="学时审核" name="1"></el-tab-pane>
        <el-tab-pane label="学习凭证" name="2"></el-tab-pane>
      </el-tabs>

      <template v-if="activeName == '1'">
        <div class="no-data" v-if="listData.length == 0">
          您暂无相关学时审核记录哦
        </div>

        <template v-else>
          <div class="learn-list">
            <div
              class="classhour-item"
              v-for="(item, index) in listData"
              :key="index"
            >
              <div class="classhour-item__header">
                <div class="state">
                  学时审核状态：
                  <div class="note" v-if="item.periodStatus === -1">
                    不可审核
                  </div>
                  <div
                    class="note note--success"
                    v-if="item.periodStatus === 1"
                  >
                    机构审核通过
                  </div>
                  <div class="note note--wait" v-if="item.periodStatus === 2">
                    等待审核
                  </div>
                  <div class="note note--wait" v-if="item.periodStatus === 3">
                    审核中
                  </div>
                  <div class="note" v-if="item.periodStatus === 0">
                    机构审核不通过，有{{ item.rebuild }}节需要重修
                  </div>
                </div>
              </div>
              <div class="classhour-item__body clearfix">
                <!-- <div class="img"></div> -->
                <div class="text">
                  <div class="title">{{ item.goodsName }}</div>
                  <div class="desc desc--black" v-if="item.periodStatus === 1">
                    <template
                      v-if="
                        item.educationName == '继续教育' &&
                        item.businessName == '二级' &&
                        item.projectName == '建造师'
                      "
                    >
                      机构已审核通过，需等待注册中心复审后即可获得继续教育学时。审核时间约15个工作日，届时请前往官网申请证书延期。
                      <div class="copy-btn">
                        <a @click="copy">复制官网链接</a>
                      </div>
                    </template>
                  </div>
                  <div class="desc" v-if="item.periodStatus === -1">
                    您的学时还未修完，请尽快完成课程学习
                  </div>
                  <div class="desc" v-if="item.periodStatus === 2">
                    学习完成后7-15个工作日完成学时审核。
                  </div>
                  <div class="desc" v-if="item.periodStatus === 0">
                    请查看需重学记录，并及时重学对应课程，以免延误学时审核进度。
                  </div>
                  <div class="desc" v-if="item.periodStatus === 3">
                    会在7-15个工作日左右完成学时审核
                  </div>
                </div>
                <div class="btns-wrap">
                  <div class="btns" v-if="item.periodStatus !== 2">
                    <div
                      class="btn"
                      v-if="
                        item.periodStatus === -1 &&
                        !(
                          (item.interfacePushId > 0 &&
                            item.officialStatus != 1) ||
                          sysTime <= item.serviceStartTime ||
                          sysTime >= item.serviceEndTime ||
                          (item.classStartTime &&
                            sysTime <= item.classStartTime) ||
                          (item.classEndTime && sysTime >= item.classEndTime) ||
                          item.learningStatus == 2 ||
                          item.classStatus == 0 ||
                          (item.learningStatus == 3 &&
                            sysTime < item.learningTimeStart)
                        )
                      "
                      @click="goCourseDetail(item)"
                    >
                      课程学习
                    </div>
                    <div
                      class="btn"
                      v-if="item.periodStatus === 1 && item.applyStatus === 1"
                      @click="appointment(item)"
                    >
                      预约考试
                    </div>
                    <div
                      class="btn"
                      v-if="item.periodStatus === 0 && item.rebuildStatus === 0"
                      @click="getcourseperiodcheat(item)"
                    >
                      查看详情
                    </div>
                    <div
                      class="btn"
                      v-if="item.periodStatus === 0 && item.rebuildStatus === 1"
                      @click="goCourseDetail(item)"
                    >
                      重修目录
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="pagination">
            <el-pagination
              @current-change="currentChange"
              background
              layout="prev, pager, next"
              :total="total"
              :pager-count="5"
              :page-size="param.pageSize"
            >
            </el-pagination>
          </div>
        </template>
      </template>

      <template v-if="activeName == '2'">
        <div class="no-data" v-if="listData1.length == 0">
          您暂无相关学习凭证记录哦
        </div>

        <template v-else>
          <div class="certificate-list">
            <div
              class="certificate-item"
              v-for="(item, index) in listData1"
              :key="index"
            >
              <div class="certificate-item__header">
                <div class="title">证书名称：</div>
                <div class="desc">{{ item.title }}</div>
              </div>
              <div class="certificate-item__body">
                <div class="title">证书编号</div>
                <div class="desc">{{ item.certificateCode }}</div>
              </div>
              <div class="certificate-item__footer">
                <div class="btn" @click="preview(item)">查看</div>
                <div class="btn" @click="download(item)">下载</div>
              </div>
            </div>
          </div>
          <div class="pagination">
            <el-pagination
              @current-change="currentChange"
              background
              layout="prev, pager, next"
              :total="total"
              :pager-count="5"
              :page-size="param.pageSize"
            >
            </el-pagination>
          </div>
        </template>
      </template>
    </div>

    <RebuildModal
      ref="rebuildModal"
      @rebuildSubmit="rebuildSubmit($event)"
    ></RebuildModal>

    <el-image-viewer
      v-if="showViewer"
      :on-close="closeViewer"
      :url-list="[url]"
    />
  </div>
</template>

<script>
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import RebuildModal from "@/components/rebuildModal";
export default {
  name: "MyClasshour",
  components: {
    RebuildModal,
    ElImageViewer,
  },
  data() {
    return {
      copyUrl: "http://gdzczx.gdcic.net/",
      showViewer: false,
      rebuildShow: false,
      showDetailModal: false,
      activeName: "1",
      listData: [],
      listData1: [],
      rebuildItems: [],
      rebuildItem: {},
      param: {
        pageNum: 1,
        pageSize: 10,
      },
      url: "",
      total: 0,
      sysTime: 0,
    };
  },
  mounted() {
    this.sysTime = +this.$tools.timest();
    this.getcourseperiodlistGoods();
  },
  methods: {
    closeViewer() {
      this.showViewer = false;
    },
    goCourseDetail(item) {
      this.$router.push({
        path: `/my-course-detail/${item.goodsId}`,
        query: {
          gradeId: item.gradeId,
          orderGoodsId: item.orderGoodsId,
        },
      });
    },
    tabChange(e) {
      console.log(e.name);
      if (this.activeName == e.name) {
        return;
      }

      this.activeName = e.name;
      this.param.pageNum = 1;

      if (this.activeName == "1") {
        this.getcourseperiodlistGoods();
      } else {
        this.getUserCertificateList();
      }
    },
    currentChange(e) {
      this.param.pageNum = e;

      if (this.activeName == "1") {
        this.getcourseperiodlistGoods();
      } else {
        this.getUserCertificateList();
      }
    },
    appointment(item) {
      var data = {
        goodsId: item.goodsId,
        gradeId: item.gradeId,
      };
      this.$request
        .getApplysubscribe(data)
        .then((res) => {
          this.$router.push({
            path: "/person-center/my-classhour/appointment",
            query: {
              goodsId: item.goodsId,
              gradeId: item.gradeId,
              orderGoodsId: item.orderGoodsId,
            },
          });
        })
        .catch((err) => {
          this.$message({
            type: "success",
            message: err.msg,
          });
        });
    },

    rebuildSubmit() {
      this.$router.push({
        path: `/my-course-detail/${item.goodsId}`,
        query: {
          gradeId: item.gradeId,
          orderGoodsId: item.orderGoodsId,
        },
      });
    },
    getcourseperiodlistGoods() {
      this.$request
        .getcourseperiodlistGoods(this.param)
        .then((res) => {
          this.listData = [...res.rows];
          this.total = res.total;
        })
        .catch((err) => {});
    },

    getUserCertificateList() {
      this.$request
        .getUserCertificateList(this.param)
        .then((res) => {
          this.listData1 = [...res.rows];
          this.total = res.total;
        })
        .catch((err) => {});
    },

    getcourseperiodcheat(item) {
      this.$refs.rebuildModal.showModal(item);
    },

    preview(item) {
      this.url = this.$tools.splitImgHost(item.certificatePath);
      this.showViewer = true;
    },
    download(item) {
      var url = this.$tools.splitImgHost(item.certificatePath, true);
      var a = document.createElement("a");
      var event = new MouseEvent("click");
      a.download = "img";
      a.href = url;
      a.dispatchEvent(event);
    },

    copy() {
      var copyInput = document.createElement("input");
      //val是要复制的内容
      copyInput.setAttribute("value", this.copyUrl);
      document.body.appendChild(copyInput);
      copyInput.select();
      try {
        var copyed = document.execCommand("copy");
        if (copyed) {
          document.body.removeChild(copyInput);
          this.$message.success("复制成功");
        }
      } catch (err) {
        this.$message.error("复制失败，请检查浏览器兼容");
      }
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.my-classhour {
  &__body {
    /deep/ .el-tabs__header {
      margin-bottom: 0;
    }

    .learn-list {
      .classhour-item {
        margin-top: 24px;
        background: #fafbfc;
        border-radius: 8px;
        overflow: hidden;

        &__header {
          height: 40px;
          border-bottom: 1px solid #eee;
          padding: 0 18px;

          .state {
            margin-top: 8px;
            float: left;
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #666666;

            .note {
              vertical-align: middle;
              display: inline-block;
              padding: 0 20px;
              height: 24px;
              background: #ffeceb;
              border: 1px solid #ff3b30;
              border-radius: 12px;
              font-size: 14px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #ff3b30;
              text-align: center;
              line-height: 24px;

              &--success {
                color: #34c759;
                border: 1px solid #34c759;
                background: #e6feea;
              }

              &--wait {
                color: #007aff;
                border: 1px solid #007aff;
                background: rgba(247, 250, 255, 1);
              }
            }
          }

          .time {
            float: right;
            line-height: 40px;
            text-align: right;
            font-size: 12px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #666666;
          }
        }

        &__body {
          padding-bottom: 20px;
          .img {
            float: left;
            width: 160px;
            height: 90px;
            background: #3f8dfd;
          }

          .text {
            width: 700px;
            float: left;
            margin-left: 12px;
            .title {
              margin-top: 10px;
              font-size: 16px;
              font-family: Microsoft YaHei;
              font-weight: bold;
              color: #333333;
              .note {
                display: inline-block;
                vertical-align: middle;
                border: 1px solid #333333;
                border-radius: 4px;
                font-size: 12px;
                font-family: Microsoft YaHei;
                font-weight: 400;
                color: #333333;
                padding: 2px 5px;
                margin-left: 12px;
              }
            }

            .desc {
              font-size: 16px;
              font-family: Microsoft YaHei;
              font-weight: 400;
              color: #ff3b30;
              margin-top: 20px;

              .copy-btn {
                text-decoration: underline;
                padding-top: 10px;
                color: #007aff;
              }

              &--black {
                color: #333;
              }
            }
          }

          .btns-wrap {
            display: table;
            float: right;
            height: 90px;
            width: 130px;

            .btns {
              display: table-cell;
              vertical-align: middle;
              text-align: center;

              .btn {
                cursor: pointer;
                margin: 2px 0;
                width: 122px;
                height: 32px;
                background: #3f8dfd;
                border-radius: 16px;
                display: inline-block;
                text-align: center;
                line-height: 32px;
                color: #fff;

                &--warm {
                  background: #ff3b30;
                }
              }
            }
          }
        }
      }
    }

    .certificate-list {
      font-size: 0;
      .certificate-item {
        display: inline-block;
        margin-top: 24px;
        margin-right: 24px;
        width: 354px;
        height: 240px;
        background: linear-gradient(0deg, #ffffff, #e7f1ff);
        border: 1px solid #eeeeee;
        box-shadow: 0px 0px 7px 1px rgba(0, 0, 0, 0.04);
        border-radius: 10px;

        &:nth-of-type(3n) {
          margin: 0;
        }

        &__header {
          padding-left: 18px;
          overflow: hidden;
          border-bottom: 1px solid #fff;
          height: 95px;

          .title {
            margin-top: 16px;
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: bold;
            color: #333333;
          }

          .desc {
            margin-top: 14px;
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #666666;
          }
        }

        &__body {
          padding-left: 18px;
          overflow: hidden;
          border-bottom: 1px solid #fff;
          height: 80px;

          .title {
            margin-top: 16px;
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: bold;
            color: #333333;
          }

          .desc {
            margin-top: 14px;
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #666666;
          }
        }

        &__footer {
          height: 65px;
          padding-top: 16px;
          text-align: center;

          .btn {
            cursor: pointer;
            margin: 0 12px;
            display: inline-block;
            width: 122px;
            height: 32px;
            border: 1px solid #3f8dfd;
            border-radius: 16px;
            text-align: center;
            line-height: 30px;
            color: #3f8dfd;
            font-size: 16px;
          }
        }
      }
    }

    .no-data {
      text-align: center;
      padding: 50px 0;
      color: #666;
      font-size: 16px;
    }

    .pagination {
      padding: 30px 0;
      text-align: center;
    }
  }
}
</style>
