<template>
  <div class="annex">
    <div class="annex-list">
      <div class="annex-item">
        <div class="annex-item__left">
          <image :src="img" @click="preview" />
          <h2>{{ name }}</h2>
        </div>
        <i class="iconfont" @click="download">&#xe725;</i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'progressAnnex',
  data() {
    return {
      img: '',
      name: '',
    }
  },

  onLoad(query) {
    this.img = query.img
    this.name = query.name
  },

  methods: {
    preview() {
      uni.previewImage({
        urls: [this.img],
        current: this.img,
      })
    },

    download() {
      uni.downloadFile({
        url: this.img,
        success: (res) => {
          const img = res.tempFilePath
          this.$hideLoading()
          uni.saveImageToPhotosAlbum({
            filePath: img,
            success: () => {
              this.$toast('保存成功')
            },
            fail: () => {
              uni.getSetting({
                success: (res) => {
                  if (!res.authSetting['scope.writePhotosAlbum']) {
                    this.$dialog
                      .confirm({
                        content: '未授权',
                        confirmText: '去设置',
                      })
                      .then(() => {
                        wx.openSetting({
                          success: (res) => {
                            if (!res.authSetting['scope.writePhotosAlbum']) {
                              this.$toast({
                                title: '此功能需获取相册权限',
                                duration: 3000,
                              })
                            } else {
                            }
                          },
                        })
                      })
                  }
                },
              })
            },
          })
        },
        fail: () => {
          this.$hideLoading()
          this.$toast('保存失败')
        },
      })
    },
  },
}
</script>

<style scoped lang="scss">
.annex {
  min-height: 100vh;
  background: #fff;
}

.annex-list {
  padding-top: 40px;

  .annex-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 032px i {
      font-size: 52px;
      color: #197df0;
    }

    .annex-item__left {
      display: flex;
      align-items: center;

      image {
        position: relative;
        top: 4px;
        width: 72px;
        height: 72px;
        margin-right: 18px;
      }

      h2 {
        font-size: 32px;
        font-weight: 600;
        color: #333;
      }
    }
  }
}
</style>
