<template>
  <view :class="['page__view', hasPoster ? 'with-bottom' : '']">
    
		<!-- 导航栏 -->
		<navbar :title="details.title" leftClick @leftClick="$utils.navigateBack" />

    <view class="cover-image" :style="{ height: coverImageHeight }">
      <image class="img" :src="details.image" mode="scaleToFill" ></image>
      <view class="shadow" :style="{ height: coverImageShadowHeight }"></view>
    </view>
    <view class="page-title">
      <view v-if="details.process || (details.educationProcessList && details.educationProcessList.length)">{{ details.processTitle || '发表全流程辅导' }}</view>
      <view v-else>{{ details.title }}</view>
      <view class="page-title-line"></view>
    </view>

    <!-- 发表全流程辅导 -->
    <view class="process" v-if="details.process || (details.educationProcessList && details.educationProcessList.length)">
      <view class="flex process-custom" v-if="difficulty">
        <view class="process-custom-title">难度</view>
        <view class="flex star">
          <uv-icon v-for="(item, index) in new Array(difficulty).fill(1)" :key="index" name="star-fill" color="#3378EA" size="30rpx"></uv-icon>
        </view>
      </view>
      <view class="process-item" v-for="item in details.educationProcessList" :key="item.id">
        <view class="process-item-title">{{ item.title }}</view>
        <view class="process-item-content">{{ item.content }}</view>
      </view>
    </view>

    <!-- 可发表方向 -->
    <view class="section" v-if="details.educationTargetList && details.educationTargetList.length">
      <view class="section-header">
        <view class="section-header-line"></view>
        <view>{{ details.targetTitle || '可发表方向' }}</view>
      </view>
      <view class="section-content direction">
        <uv-read-more show-height="300rpx" :toggle="true" textIndent="0" closeText="展开" :shadowStyle="{ backgroundImage: 'linear-gradient(-180deg, rgba(255, 255, 255, 0) 0%, #fff 80%)', paddingTop: '100rpx', marginTop: '-100rpx' }">
          <view class="table">
            <view class="table-row" v-for="row in details.educationTargetList" :key="row.id">
              <view class="table-cell">{{ row.title }}</view>
              <view class="table-cell">{{ row.description }}</view>
            </view>
          </view>
        </uv-read-more>
      </view>
    </view>
    
    <!-- 师资介绍 -->
    <view class="section" v-if="details.educationTeacherList && details.educationTeacherList.length">
      <view class="section-header">
        <view class="section-header-line"></view>
        <view>{{ details.teacherTitle || '师资介绍' }}</view>
      </view>
      <view class="section-content teachers">
        <view class="card" v-for="item in details.educationTeacherList" :key="item.id">
          <view class="info">
            <view class="pic">
              <image class="img" :src="item.image" mode="aspectFill" ></image>
            </view>
            <view class="title">
              <view class="name">{{ item.title }}</view>
              <view class="label" v-if="item.career">
                <view class="label-text">{{ item.career }}</view>
                <view class="label-line"></view>
                <view class="label-text display">{{ item.career }}</view>
              </view>
            </view>
            <view class="tag">
              <image class="tag-icon" src="@/static/image/icon-degree.png" mode="widthFix"></image>
              <view>{{ item.qualification }}</view>
            </view>
            <view class="desc desc-top">专业经历：</view>
            <view class="desc desc-bottom">{{ item.experience }}</view>
          </view>
        </view>
      </view>
    </view>

    <!-- 课程安排 -->
    <view class="section" v-if="details.educationCourseList && details.educationCourseList.length">
      <view class="section-header">
        <view class="section-header-line"></view>
        <view>{{ details.courseTitle || '课程安排' }}</view>
      </view>
      <view class="section-content">
        <view class="table">
          <view class="table-row" v-for="row in details.educationCourseList" :key="row.id">
            <view class="table-cell">{{ row.title }}</view>
            <view class="table-cell">{{ row.description }}</view>
          </view>
        </view>
      </view>
    </view>

    <!-- 适用人群 -->
    <view class="section" v-if="details.suit">
      <view class="section-header">
        <view class="section-header-line"></view>
        <view>{{ details.suitTitle || '适用人群' }}</view>
      </view>
      <view class="section-content target-audience">
        <view class="paragraph">
          <uv-parse :content="details.suit"></uv-parse>
        </view>
      </view>
    </view>

    <!-- 期刊推荐 -->
    <view class="section" v-if="details.educationPeriodicalList && details.educationPeriodicalList.length">
      <view class="section-header">
        <view class="section-header-line"></view>
        <view>{{ details.periodicalTitle || '期刊推荐' }}</view>
      </view>
      <view class="section-content journal">
        <view class="box" v-for="item in details.educationPeriodicalList" :key="item.id">
          <view class="card">
            <view class="top">
              <view class="pic">
                <image class="img" :src="item.image" mode="aspectFill"></image>
              </view>
              <view class="info">
                <view class="name">{{ item.title }}</view>
                <view class="desc">{{ item.shortTitle }}</view>
              </view>
            </view>
            <view class="main">
              {{ item.description }}
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 附加材料 -->
    <view class="section" v-if="details.educationDocumentList && details.educationDocumentList.length">
      <view class="section-header">
        <view class="section-header-line"></view>
        <view>{{ details.documentTitle || '附加材料' }}</view>
      </view>
      <view class="section-content attachment">
        <view class="flex file" v-for="item in details.educationDocumentList" :key="item.id">
          <view class="file-info">
            <template v-if="item.type == 'pdf'">
              <image class="file-icon" src="@/static/image/icon-pdf.png" mode="widthFix"></image>
            </template>
            <template v-else>
              <image class="file-icon" src="@/static/image/icon-word.png" mode="widthFix"></image>
            </template>
            <view class="file-detail">
              <view class="title">{{ item.title }}</view>
              <!-- <view class="desc">{{ `${getFileType(item.document)} ${item.size || '-'}MB` }}</view> -->
            </view>
          </view>
          <button class="btn" @click="downloadFile(item.document)">下载</button>
        </view>
      </view>
    </view>
    
    <view class="flex bottom" v-if="hasPoster">
      <view class="flex btns">
        <button class="btn" @click="jumpToPoster">保存海报</button>
        <button class="btn btn-share" open-type="share">分享文章</button>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        details: {},
        coverImageHeight: '566rpx',
        coverImageShadowHeight: '113rpx'
      }
    },
    onLoad({ thesisId }) {
      
      const windowWidth = uni.getSystemInfoSync().windowWidth
      const coverImageHeight = (windowWidth) * 566 / 714
      const coverImageShadowHeight = (windowWidth) * 113 / 714
      this.coverImageHeight = `${coverImageHeight}px`
      this.coverImageShadowHeight = `${coverImageShadowHeight}px`
      
      this.getData(thesisId)
      // this.getData('1949729528544800770')
    },
    onShareAppMessage(res) {
      return {
        title: this.posterData.paperDesc,
        imageUrl: this.posterData.paperImage,
        path: this.posterData.path,
      }
    },
    computed: {
      difficulty() {
        const num = parseInt(this.details?.process)

        return isNaN(num) ? 0 : num
      },
      posterData() {
        const { id, title, image, paperDesc, paperImage } = this.details || {}

        return {
          paperDesc: paperDesc || title,
          paperImage: paperImage || image || '',
          path: `pages_order/thesis/index?thesisId=${id}`
        }
      },
      hasPoster() {
        const { paperImage } = this.details || {}

        return !!paperImage
      }
    },
    methods: {
      async getData(thesisId) {

        try {
          this.details = await this.$fetch('queryThesisById', { thesisId })
        } catch (err) {

        }

      },
      getFileType(fileName) {
        const pdfReg = /(.pdf)$/g
        // const officeReg = /(doc|docx|ppt|pptx|xls|xlsx)$/g

        return pdfReg.test(fileName) ? 'pdf' : ''
      },
      downloadFile(url) {
        console.log('downloadFile', url)
        uni.downloadFile({
          url, // 文件地址
          success: (downloadRes) => {
            console.log('downloadRes', downloadRes)
            if (downloadRes.statusCode === 200) {
              uni.openDocument({
                showMenu: true,
                filePath: downloadRes.tempFilePath,
                success: () => console.log('打开成功')
              });
            } else {
              uni.showToast({ title: '下载失败', icon: 'none' });
            }
          },
          fail: (err) => {
            console.log('downloadFile fail', err)
            uni.showToast({ title: '网络异常', icon: 'none' });
          }
        });
      },
      jumpToPoster() {
        uni.setStorageSync('posterData', this.posterData)

        uni.navigateTo({
					url: `/pages_order/thesis/poster`
				})
      },
    },
  }
</script>

<style scoped lang="scss">

  .page__view {
    background: #FFFFFF;
    padding-bottom: 62rpx;

    &.with-bottom {
		  padding-bottom: calc(62rpx + 110rpx + env(safe-area-inset-bottom));
    }
  }

  .cover-image {
    position: relative;
    width: 100%;
    background: #FFFFFF;
    
    .img {
      width: 100%;
      height: 100%;
    }

    .shadow {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: linear-gradient(transparent, #FFFFFF);
    }
  }

  .page-title {
    padding: 0 36rpx;
    font-size: 60rpx;
    font-weight: 700;
    color: #4783F9;
    
    &-line {
      margin: 12rpx 0 24rpx 0;
      width: 120rpx;
      height: 8rpx;
      background: #4783F9;
    }
  }

  .process {
    position: relative;
    width: 100%;
    padding: 0 36rpx;
    box-sizing: border-box;

    &-custom {
      display: inline-flex;
      column-gap: 8rpx;
      padding: 6rpx 18rpx;
      background: #E6EEFD;
      border-top-right-radius: 32rpx;
      border-bottom-right-radius: 32rpx;

      &-title {
        font-size: 30rpx;
        font-weight: 600;
        color: #3378EA;
      }

      .star {
        column-gap: 6rpx;
      }
    }

    &-item {
      margin-top: 24rpx;
      padding: 10rpx 18rpx;
      background: #E6EEFD;
      border-radius: 25rpx;

      &-title {
        font-size: 30rpx;
        font-weight: 600;
        color: #3378EA;
      }

      &-content {
        font-size: 28rpx;
        white-space: pre-wrap;
        color: #000000;
      }

    }
  }

  .section {
    margin-top: 40rpx;
    width: 100%;
    padding: 0 18rpx;
    box-sizing: border-box;

    // & + & {
    //   margin-top: 40rpx;
    // }

    &-header {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      column-gap: 15rpx;
      padding-left: 18rpx;
      font-size: 32rpx;
      font-weight: 700;
      color: #000000;

      &-line {
        width: 11rpx;
        height: 45rpx;
        border-radius: 6rpx;
        background-image: linear-gradient(#FFFFFF, #4883F9);
      }
    }

    &-content {
      margin-top: 37rpx;

      .paragraph {
        width: 100%;
        padding: 22rpx;
        box-sizing: border-box;
        white-space: pre-line;
        font-size: 28rpx;
        color: #000000;
        background: #F8F8F8;
        border-radius: 15rpx;
      }

      .table {
        width: 100%;
        border-radius: 15rpx;
        overflow: hidden;

        &-row {
          display: grid;
          grid-template-columns: 218rpx auto;
          background: #EEEEEE;

          &:nth-child(2n) {
            background: #DEDEDE;
          }
        }

        &-cell {
          display: inline-flex;
          flex-direction: column;
          align-items: flex-start;
          justify-content: center;
          padding: 17rpx;
          box-sizing: border-box;
          font-family: PingFang SC;
          font-weight: 400;
          font-size: 28rpx;
          white-space: pre-wrap;
          color: #080808;

          &:first-child {
            border-right: 1rpx solid #E5E5E5;
          }
        }

      }

      &.direction {
        /deep/ .uv-read-more__toggle {
          justify-content: flex-end;
        }
        /deep/ .uv-read-more__toggle__text .uv-text__value {
          color: $uni-color !important;
          font-size: 22rpx !important;
        }
      }

      &.teachers {

        .card {
          display: flex;
          align-items: center;
          column-gap: 12rpx;
          background: #ffffff;
          border-radius: 15rpx;
          box-shadow: 0rpx 3rpx 6rpx 0rpx rgba(0,0,0,0.16); 

          .info {
            flex: 1;
            padding: 19rpx 17rpx 28rpx 17rpx;

            .title {
              display: flex;
              align-items: center;
              padding-left: 11rpx;
              color: #000000;

              .name {
                font-size: 32rpx;
                font-weight: 700;
              }

              .label {
                margin-left: 12rpx;
                position: relative;
                // height: 30rpx;

                &-line {
                  position: absolute;
                  left: 0;
                  bottom: 0;
                  transform: translate(30rpx, -2rpx);
                  // margin: 24rpx 0 0 25rpx;
                  // width: 195rpx;
                  width: calc(100% - 20rpx);
                  height: 4rpx;
                  background-image: linear-gradient(#4883F9, #FFFFFF);
                  border-radius: 2rpx;
                }

                &-text {
                  font-size: 22rpx;
                  font-weight: 400;
                  opacity: 0;

                  &.display {
                    opacity: 1;
                    position: absolute;
                    left: 0;
                    top: 0;
                  }
                }
              }
            }

            .tag {
              margin: 5rpx 0 18rpx 0;
              display: inline-flex;
              align-items: center;
              padding: 5rpx 43rpx 5rpx 16rpx;
              column-gap: 16rpx;
              font-size: 28rpx;
              color: #FFFFFF;
              background: rgba(72,131,249,0.58);
              border-radius: 30rpx 0rpx 30rpx 0rpx;

              &-icon {
                width: 38rpx;
                height: auto;
              }
            }

            .desc {
              // padding: 12rpx 15rpx;
              font-size: 28rpx;
              font-weight: 500;
              white-space: pre-wrap;
              color: #000000;
              background: #f8f8f8;
              // border-radius: 16rpx;

              &-top {
                padding: 12rpx 15rpx 0 15rpx;
                border-top-left-radius: 16rpx;
                border-top-right-radius: 16rpx;
              }
              &-bottom {
                padding: 0 15rpx 12rpx 15rpx;
                border-bottom-left-radius: 16rpx;
                border-bottom-right-radius: 16rpx;
              }
            }
          }

          .pic {
            // margin: 48rpx 9rpx 48rpx 0;
            margin: 44rpx 0 0 14rpx;
            width: 253rpx;
            height: 345rpx;
            border-radius: 127rpx;
            overflow: hidden;
            float: right;

            .img {
              width: 100%;
              height: 100%;
            }
          }
          
          &:nth-child(2n) {
            flex-direction: row-reverse;

            .info {
              padding: 19rpx 17rpx 28rpx 17rpx;
            }

            .pic {
              // margin: 48rpx 0 48rpx 9rpx;
              margin: 44rpx 14rpx 0 0;
              float: left;
            }
          }
        }

        .card + .card {
          margin-top: 24rpx;
        }
      }
      
      &.journal {
        
        .box {
          padding-top: 73rpx;

          .card {
            padding: 29rpx 12rpx 19rpx 12rpx;
            background: #f6f6f6;
            border-radius: 15rpx;
            box-shadow: 0rpx 3rpx 6rpx 0rpx rgba(0,0,0,0.16); 

            .top {
              position: relative;

              .pic {
                position: absolute;
                left: 42rpx;
                bottom: 0;
                width: 213rpx;
                height: 285rpx;

                .img {
                  width: 100%;
                  height: 100%;
                }
              }

              .info {
                min-height: 181rpx;
                padding: 0 14rpx 0 266rpx;

                .name {
                  padding-left: 6rpx;
                  font-size: 32rpx;
                  font-weight: 700;
                  color: #000000;
                }

                .desc {
                  margin-top: 24rpx;
                  // font-size: 28rpx;
                  font-size: 24rpx;
                  white-space: pre-wrap;
                  font-weight: 700;
                  color: #707070;
                  // overflow: hidden;
                  // text-overflow: ellipsis;
                  // display:-webkit-box; //作为弹性伸缩盒子模型显示。
                  // -webkit-box-orient:vertical; //设置伸缩盒子的子元素排列方式--从上到下垂直排列
                  // -webkit-line-clamp:3; //显示的行
                }
              }
              
            }

            .main {
              margin-top: 18rpx;
              padding: 14rpx;
              font-size: 28rpx;
              white-space: pre-wrap;
              color: #000000;
              background: #ebebeb;
              border-radius: 30rpx 0rpx 30rpx 0rpx;
            }

          }
          
          &:nth-child(2n) {

            .card {

              .top {
                .info {
                  padding: 0 258rpx 0 14rpx;
                }

                .pic {
                  left: unset;
                  right: 29rpx;
                }
              }

            }
          }
        }

        .box + .box {
          margin-top: 28rpx;
        }
      }

      &.attachment {

        .file {
          justify-content: space-between;
          padding: 32rpx 15rpx 32rpx 26rpx;
          background: #f8f8f8;
          border-radius: 15rpx;

          &-info {
            display: flex;
            // align-items: flex-end;
            align-items: center;
          }

          &-icon {
            width: 64rpx;
            height: auto;
          }

          &-detail {
            margin-left: 14rpx;

            .title {
              font-size: 30rpx;
              color: #000000;
            }

            .desc {
              font-size: 28rpx;
              color: #999999;
            }

          }

          .btn {
            padding: 7rpx 30rpx;
            font-size: 28rpx;
            color: #FFFFFF;
            background: #4883f9;
            border-radius: 27rpx;
          }
        }

        .file + .file {
          margin-top: 21rpx;
        }

      }

    }
  }

  .bottom {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100vw;
    height: 110rpx;
		padding-bottom: env(safe-area-inset-bottom);
    background: #FFFFFF;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16); 

    .btns {
      column-gap: 34rpx;
    }

    .btn {
      padding: 20rpx 90rpx;
      font-size: 28rpx;
      color: #FFFFFF;
      background: #4883F9;
      border-radius: 42rpx;

      &-share {
        background: #FFD019;
      }

    }

  }

</style>