<template>
  <div class="com-container active-info-container" v-loading="loading">
    <div class="active-info card-box">
      <div class="active-info-cover">
        <el-image :src="activeInfo.activityCover" fit="cover"/>
      </div>
      <div class="active-info-cont">
        <div class="active-info-cont-header">
          <template v-if="activeInfo.activityLabel">
            <div class="label"
                 v-for="(item, index) in activeInfo.activityLabel.split(',')"
                 :key="index">{{item}}
            </div>
          </template>
          <div class="title">{{activeInfo.name}}</div>
        </div>
        <div class="active-info-cont-txt1">
          <div class="sign-box" v-if="activeInfo.schoolName">
            <div class="sign"
                 v-for="(item, index) in activeInfo.schoolName.split(',')"
                 :key="index">{{item}}
            </div>
          </div>
          <div class="sign-box" v-if="activeInfo.subjectName">
            <div class="sign"
                 v-for="(item, index) in activeInfo.subjectName.split(',')"
                 :key="index">{{item}}
            </div>
          </div>
          <div class="sign-box" v-if="activeInfo.peopleName">
            <div class="sign"
                 v-for="(item, index) in activeInfo.peopleName.split(',')"
                 :key="index">{{item}}
            </div>
          </div>
        </div>
        <div class="active-info-cont-txt2">
          活动时间：{{formatYMD(activeInfo.activityStartTime)}}
          至 {{formatYMD(activeInfo.activityEndTime)}}
        </div>
        <div class="active-info-cont-txt3">
          <div>报名截止：{{formatYMDChina(activeInfo.bmEndTime)}}</div>
          <div>
            活动费用：￥{{activeInfo.activityFee}}
            <el-tooltip class="item" effect="dark" content="申请审核通过后，才可缴费参与活动" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
          </div>
        </div>
        <div>
          <el-button v-if="activeInfo.status==1" :disabled="true">报名未开放</el-button>
          <el-button v-if="activeInfo.status==2 && activeInfo.source==2" @click="signUp">立即报名</el-button>
          <el-button v-if="activeInfo.status==3" :disabled="true">报名已截止</el-button>
          <el-button v-if="activeInfo.status==4" :disabled="true">已结营</el-button>
        </div>
      </div>
    </div>
    <div class="active-area">
      <div class="left-area card-box">
        <ul v-if="activeContent==1 && anchorList.length>0">
          <li v-for="(item,index) in anchorList" :key="index">
            <a @click="anchorSkip(item.id)">{{item.name}}</a>
          </li>
        </ul>
        <ul v-else-if="activeContent==2 && anchorList2.length>0">
          <li v-for="(item,index) in anchorList2" :key="index">
            <a @click="anchorSkip(item.id)">{{item.name}}</a>
          </li>
        </ul>
        <ul v-else>
          <li>暂无锚点</li>
        </ul>
      </div>
      <div class="center-area card-box" :style="{
          'min-height':minHeight+'px'
        }">
        <div class="center-area-title">
          <div class="center-area-title-box">
            <div class="center-area-title-box-item"
                 :class="activeContent==1?'is-active':''"
                 @click="changeActive(1)"
            >活动简章
            </div>
            <div class="center-area-title-box-item"
                 :class="activeContent==2?'is-active':''"
                 v-if="activeInfo.previousActivities"
                 @click="changeActive(2)"
            >活动回顾
            </div>
          </div>
        </div>
        <div class="center-area-cont">
          <div v-show="activeContent==1"
               v-html="activeInfo.admissionsBrochure"></div>
          <div v-show="activeContent==2"
               v-html="activeInfo.previousActivities"></div>
        </div>
      </div>
      <div class="right-area" ref="RightArea">
        <div class="question-area card-box">
          <div class="question-area-title">
            <div class="question-area-title-box">常见问题</div>
          </div>
          <div class="question-area-cont">
            <div v-if="questionList.length>0">
              <div class="ask-answer-item"
                   v-for="(item,index) in questionList"
                   :key="index">
                <div class="ask bold">问：{{item.question}}</div>
                <div class="answer">答：{{item.answer}}</div>
              </div>
            </div>
            <el-empty v-else description="暂无问题"></el-empty>

            <div class="question-email bold">
              如有其他问题需要咨询，请发送邮件至以下邮箱 xxxxxxx
            </div>
          </div>
        </div>
        <div class="back-area" v-if="currActivity.length>0">
          <div class="back-area-title">当期活动</div>
          <div class="back-area-item card-box"
               v-for="(item,index) in currActivity"
               :key="index"
               @click="toActiveInfo(item.id)">
            <div class="back-area-item-cover">
              <el-image :src="item.activityCover" fit="cover"/>
            </div>
            <div class="back-area-item-cont">
              <div class="back-area-item-cont-txt">{{item.name}}</div>
              <div class="back-area-item-cont-txt">
                <div class="sign-box" v-if="item.activityLabel">
                  <div class="sign"
                       v-for="(item2, index2) in item.activityLabel.split(',')"
                       :key="index2">{{item2}}
                  </div>
                </div>
              </div>
              <div class="back-area-item-cont-txt color_3">
                报名截止时间：{{formatYMDChina(item.bmEndTime)}}
              </div>
            </div>
          </div>
        </div>
        <div class="back-area" v-if="oldActivity.length>0">
          <div class="back-area-title">往期活动</div>
          <div class="back-area-item card-box"
               v-for="(item,index) in oldActivity"
               :key="index"
               @click="toActiveInfo(item.id)">
            <div class="back-area-item-cover">
              <el-image :src="item.activityCover" fit="cover"/>
            </div>
            <div class="back-area-item-cont">
              <div class="back-area-item-cont-txt">{{item.name}}</div>
              <div class="back-area-item-cont-txt">
                <div class="sign-box" v-if="item.activityLabel">
                  <div class="sign"
                       v-for="(item2, index2) in item.activityLabel.split(',')"
                       :key="index2">{{item2}}
                  </div>
                </div>
              </div>
              <div class="back-area-item-cont-txt color_2">
                活动时间：{{formatYMD(item.activityStartTime)}}
                至 {{formatYMD(item.activityEndTime)}}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {GetActiveInfo} from "r/base/active";

  export default {
    name: "ActiveInfo",
    data() {
      return {
        loading: false,
        id: null,// 活动id
        activeInfo: {
          activityCover: null,
          activityLabel: null,
          name: null,
          schoolName: null,
          subjectName: null,
          activityStartTime: null,
          activityEndTime: null,
          bmEndTime: null,
          activityFee: null,
          status: null,
          source: null,
          admissionsBrochure: null,
          previousActivities: null,
        },
        questionList: [],
        currActivity: [],// 当期活动卡片列表
        oldActivity: [],// 往期活动卡片列表
        anchorList: [],
        anchorList2: [],
        activeContent: 1,
        minHeight: '339',
      }
    },
    created() {
      let params = this.$route.params;
      if (params.id) {
        this.id = params.id;
      }
      this.getInfo()
    },
    methods: {
      // 立即报名按钮
      signUp() {
        let allToken = window.localStorage.getItem('index-token-all');
        if (allToken) {
          this.$store.commit('setIndexIdentity',this.activeInfo.identity)
          this.$router.push('/signUp/examInfo?code=' + this.activeInfo.identity);
        } else {
          this.$router.push('/baseLogin');
        }
      },
      // 去活动详情页面（当前活动/往期活动回顾区域）
      toActiveInfo(id) {
        this.$router.push('/active/' + id);
      },
      // 锚点跳转方法
      anchorSkip(id) {
        document.querySelector(id).scrollIntoView({
          behavior: 'smooth',
          block: 'center'
        });
      },
      // 选择活动简章/回顾
      changeActive(val) {
        this.activeContent = val;
      },
      // 分离获取html中的锚点
      getAnchor(content) {
        if(!content) return [];
        let attrAndValueArr = content.match(/<a id=\"(.*?)\"><\/a>/g);
        let valueArr = [] // 放所有该属性的值
        let anchorList = [];
        if (attrAndValueArr && attrAndValueArr.length > 0) {
          for (let i = 0; i < attrAndValueArr.length; i++) {
            valueArr.push(attrAndValueArr[i]
              .replace(/<a id=/g, '')
              .replace(/\"/g, '')
              .replace(/><\/a>/g, ''));
          }
          valueArr.forEach((item, index) => {
            let father = document.getElementById(item).parentNode;
            let value = father.innerHTML.replace(/<(.*?)>/g, '').replace(/<\/(.*?)>/g, '');
            if (value && value.length < 10) {
              anchorList.push({
                id: '#' + item,
                name: value,
              })
            }
          })
          return anchorList;
        }
        return [];
      },
      // 获取数据
      getInfo() {
        if (this.loading) {
          return false;
        }
        this.loading = true;
        GetActiveInfo(this.id).then(res => {
          console.log(res)
          if (res.data.code != 200) {
            this.loading = false;
            return this.$message.error(res.data.msg);
          }
          this.activeContent = 1;
          this.activeInfo = res.data.data.activityDetail;
          this.questionList = res.data.data.question;
          this.currActivity = res.data.data.currActivity;
          this.oldActivity = res.data.data.oldActivity;

          setTimeout(() => {
            let content1 = this.activeInfo.admissionsBrochure;
            let content2 = this.activeInfo.previousActivities;
            this.anchorList = this.getAnchor(content1);
            this.anchorList2 = this.getAnchor(content2);
            this.minHeight = this.$refs['RightArea'].offsetHeight;
          }, 100)
          this.loading = false;
        }).catch(err => {
          console.log(err)
          this.loading = false;
        })
      },
    },
    watch: {
      '$route.params'(val) {
        console.log(val)
        if (val.id) {
          this.id = val.id;
        }
        this.getInfo();
      }
    },
  }
</script>

<style lang="scss" scoped>
  @import "a/scss/common";

  .active-info-container {
    padding: 50px;
    color: #333333;
    font-family: "PingFang SC";

    .bold {
      font-weight: bold;
    }

    .card-box {
      border-radius: 4px;
      background-color: #ffffff;
    }

    .active-info {
      padding: 40px 60px;
      display: flex;
      flex-flow: row;
      margin-bottom: 50px;

      .active-info-cover {
        flex: 1;
        min-height: 250px;
        max-height: 340px;
        overflow: hidden;
        border-radius: 4px;

        ::v-deep .el-image {
          width: 100%;
          height: 100%;
          border-radius: 4px;
          transition: all .5s;
          -webkit-transition: all .5s;

          .el-image__error {
            background-color: #D8D8D8;
          }

          &:hover {
            transform: scale(1.1);
            -ms-transform: scale(1.1); /* IE 9 */
            -moz-transform: scale(1.1); /* Firefox */
            -webkit-transform: scale(1.1); /* Safari 和 Chrome */
            -o-transform: scale(1.1);
          }
        }
      }

      .active-info-cont {
        flex: 1;
        margin-left: 70px;
        display: flex;
        flex-flow: column;
        justify-content: space-around;

        .active-info-cont-header {
          display: flex;
          flex-flow: row;
          align-items: center;

          .label {
            font-size: 16px;
            padding: 0 10px;
            border-radius: 8px;
            color: #ffffff;
            background-color: var(--all_color);
            margin-right: 15px;
            line-height: 28px;
          }

          .title {
            font-size: 22px;
            font-weight: 500;
            line-height: 36px;
          }
        }

        .active-info-cont-txt1 {
          font-size: 16px;
          line-height: 32px;

          .sign-box {
            display: inline-block;
            margin-right: 10px;

            &:last-child {
              margin-right: 0;
            }

            .sign {
              display: inline-block;
              padding: 0 10px;
              border-radius: 4px;
              background-color: #eeeeee;
              margin: 5px 10px 5px 0;

              &:last-child {
                margin-right: 0;
              }
            }
          }

        }

        .active-info-cont-txt2 {
          font-size: 16px;
          color: #999999;
          line-height: 24px;
        }

        .active-info-cont-txt3 {
          font-size: 16px;
          color: #EC808D;
          line-height: 32px;
          display: flex;
          flex-flow: row;
          align-items: center;
          justify-content: space-between;

          .el-tooltip {
            color: #999999;
            margin-left: 10px;
            cursor: pointer;
          }
        }

        .el-button {
          width: 150px;
          height: 40px;
          color: #333333;
          border-color: #333333;
          background-color: #ffffff;

          &:hover {
            color: var(--all_color);
            border-color: var(--all_color);

          }

          &.is-disabled {
            color: #333333;
            border-color: #333333;
            opacity: 0.5;
          }
        }
      }
    }

    .active-area {
      display: flex;
      flex-flow: row;
      align-items: self-start;
      justify-content: space-between;

      .left-area {
        flex: 1;
        min-width: 200px;

        ul {
          padding: 20px 10px;
        }

        li {
          list-style: none;

          a {
            display: inline-block;
            text-decoration: none;
            color: #333333;
            width: 100%;
            font-size: 18px;
            /*height: 44px;*/
            line-height: 44px;
            margin: 10px 0;
            text-align: center;
            border-bottom: 1px solid #cccccc;
            cursor: pointer;

            &:hover,
            &.is-active {
              color: var(--all_color);
              background-color: #f5f5f5;
              border-left: 4px solid var(--all_color);
            }
          }

        }

      }

      .center-area {
        flex: 4;
        margin: 0 20px;

        .center-area-title {
          text-align: center;
          font-size: 22px;
          font-weight: 500;
          height: 50px;
          padding: 10px 20px 0;

          .center-area-title-box {
            height: 50px;
            display: flex;
            flex-flow: row;
            justify-content: left;
            border-bottom: 1px solid #cccccc;

            .center-area-title-box-item {
              margin: 0 20px;
              height: 40px;
              line-height: 40px;
              cursor: pointer;

              &:hover {
                border-bottom: 2px solid #eeeeee;
              }

              &.is-active {
                border-bottom: 2px solid var(--all_color);
              }
            }
          }
        }

        .center-area-cont {
          padding: 20px 30px;
          min-height: 238px;
        }
      }

      .right-area {
        flex: 1.5;
        min-width: 320px;

        .question-area {

          .question-area-title {
            text-align: center;
            font-size: 22px;
            font-weight: 500;
            line-height: 50px;
            padding: 10px 20px 0;

            .question-area-title-box {
              border-bottom: 1px solid #cccccc;
            }
          }

          .question-area-cont {
            padding: 10px 25px 20px;

            .ask-answer-item {
              padding: 10px 5px;
              font-size: 14px;
              line-height: 30px;
              border-bottom: 1px solid #cccccc;
            }

            ::v-deep .el-empty {
              padding: 10px 0;

              .el-empty__image {
                width: 100px;
              }
            }

            .question-email {
              font-size: 14px;
              line-height: 30px;
              margin-top: 20px;
            }
          }
        }

        .back-area {
          .back-area-title {
            text-align: center;
            font-size: 22px;
            font-weight: 500;
            line-height: 50px;
            padding: 10px 20px 0;
          }

          .back-area-item {
            padding: 15px 10px;
            display: flex;
            flex-flow: row;
            margin-bottom: 10px;

            .back-area-item-cover {
              flex: 1;
              height: 88px;
              overflow: hidden;
              border-radius: 4px;

              ::v-deep .el-image {
                width: 100%;
                height: 100%;
                border-radius: 4px;
                transition: all .5s;
                -webkit-transition: all .5s;

                .el-image__error {
                  background-color: #D8D8D8;
                }
              }
            }

            .back-area-item-cont {
              flex: 1;
              margin-left: 10px;
              display: flex;
              flex-flow: column;
              justify-content: space-around;

              .back-area-item-cont-txt {
                font-size: 12px;
                line-height: 18px;

                &.color_2 {
                  color: #999999;
                }

                &.color_3 {
                  color: #EC808D;
                }

                .sign-box {
                  display: inline-block;

                  .sign {
                    display: inline-block;
                    padding: 0 10px;
                    border-radius: 4px;
                    background-color: #eeeeee;
                    margin: 2px 4px 2px 0;

                    &:last-child {
                      margin-right: 0;
                    }
                  }
                }

              }
            }

            &:hover {
              cursor: pointer;
              box-shadow: 6px 6px 15px rgba(0, 0, 0, 0.1);

              .el-image {
                transform: scale(1.1);
                -ms-transform: scale(1.1); /* IE 9 */
                -moz-transform: scale(1.1); /* Firefox */
                -webkit-transform: scale(1.1); /* Safari 和 Chrome */
                -o-transform: scale(1.1);
              }
            }
          }
        }
      }
    }

    ::v-deep .el-loading-spinner {
      top: 200px !important;
    }
  }
</style>
