// miniprogram/pages/marvellous/index.js;
const app = getApp();
Page({
  imgList: [],
  downCount: 0,
  /**
   * 页面的初始数据
   */
  data: {
    pageList: [],
    select: { count: 0 },
    pageType: "",
    showSelect: false,
    showVideo: "",
    showAnVideo: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.title || "精彩瞬间"
    })
    this.init(options);
  },
  init(options) {
    app.globalData.origin[options.type].map(v=> this.imgList.push(v.url))
    console.log( app.globalData.origin,options.type)
    this.setData({
      pageList: app.globalData.origin[options.type],
      pageType: options.type || "img",
      showAnVideo: options.type === "video"
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showImg(e) {
    if (this.data.showSelect) return this.btnSelect(e);
    if (this.data.pageType === "video") return this.showVideo(e);
    if (this.data.pageType === "img") return this.showImage(e);
  },
  showVideo: function (e) {
    this.setData({
      showVideo: this.imgList[e.currentTarget.dataset.o]
    })
  },
  showImage: function (e) {
    wx.previewImage({
      urls: this.imgList,
      current: this.imgList[e.currentTarget.dataset.o],
    })
  },
  longtap: function (e) {
    // this.setData({
    //   showSelect: true
    // })
  },
  longClose: function (e) {
    let pageList = this.data.pageList;
    let select = this.data.select;
    let keys = Object.keys(select);
    for (let i = 0; i < keys.length; i++) {
      let v = keys[i];
      if (v === 'count') continue;
      let li = v.split("-");
      pageList[li[0]].list[li[1]].select = false;
    }
    this.setData({
      showSelect: false,
      pageList,
      select: {
        count: 0
      }
    })
  },
  btnSelect: function (e) {
    if (this.data.select.count > 9) return;
    let d = e.currentTarget.dataset;
    let pageList = this.data.pageList;
    let select = this.data.select;
    pageList[d.key].list[d.o].select = !pageList[d.key].list[d.o].select;
    if (pageList[d.key].list[d.o].select) {
      select[d.key + "-" + d.o] = d.url;
      select.count++;
    } else {
      select[d.key + "-" + d.o] = undefined;
      select.count--;
    }
    this.setData({
      pageList: pageList,
      select
    })
  },
  format: function (res) {
    if (!res) return ""
    let T = new Date(res || 0);
    let year = T.getFullYear();
    let month = T.getMonth() + 1;
    let day = T.getDate();
    let hour = T.getHours();
    let min = T.getMinutes() + 1;
    let sec = T.getSeconds();

    return year + "-" + (month > 9 ? month : "0" + month) + "-" + day + " " + (hour > 9 ? hour : '0' + hour) + ":" + (min > 9 ? min : '0' + min) + ":" + (sec > 9 ? sec : '0' + sec);
  },
  saveAsset: function () {
    if (this.data.select.count === 0) return;
    let _this = this;
    // 权限判断
    wx.getSetting({
      success: function (res) {
        if (!res['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: function (res) {
              wx.showLoading();
              let keys = Object.keys(_this.data.select);
              _this.downCount = keys.length - 1;
              for (let i = 0; i < keys.length; i++) {
                const v = keys[i];
                if (v === 'count') continue;
                _this.downFile(v);
              }
            },
            fail: function (err) {
              wx.showToast({
                title: '未获取权限',
                icon: "none"
              })
            }
          })
          return
        }
        wx.showLoading();
        let keys = Object.keys(this.data.select);
        _this.downCount = keys.length - 1;
        for (let i = 0; i < keys.length; i++) {
          const v = keys[i];
          if (v === 'count') continue;
          _this.downFile(v);
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '未获取权限',
          icon: "none"
        })
      }
    })
  },
  downFile: function (v) {
    let _this = this;
    wx.cloud.downloadFile({
      fileID: _this.data.select[v],
      success: function (res) {
        if (_this.data.pageType === "img") {
          _this.saveImage(res.tempFilePath)
        } else {
          _this.savevideo(res.tempFilePath)
        }
      },
      fail: function (err) {
        wx.showToast({
          title: "下载失败：" + _this.data.select[v],
          icon: "none"
        })
        _this.downCount--;
        if (_this.downCount <= 0) {
          wx.hideLoading();
        }
      }
    })
  },
  saveImage: function (tempFilePath) {
    let _this = this;
    wx.saveImageToPhotosAlbum({
      filePath: tempFilePath,
      success: function (res) {
        _this.downEnd();
      },
      fail: function (err) {
        wx.showToast({
          title: '下载失败',
          icon: "none"
        })
        _this.downEnd();
      }
    })
  },
  savevideo: function (tempFilePath) {
    wx.saveVideoToPhotosAlbum({
      filePath: tempFilePath,
      success: function (res) {
        _this.downEnd();
      },
      fail: function (err) {
        wx.showToast({
          title: '下载失败',
          icon: "none"
        })
        _this.downEnd();
      }
    })
  },
  downEnd: function () {
    this.downCount--;
    if (this.downCount <= 0) {
      wx.hideLoading();
    }
  },
  closeVideo: function () {
    this.setData({
      showVideo: ""
    })
  },
  closeAnvido: function () {
    this.setData({
      showAnVideo: false
    })
  }
})