// pages/warn/warn.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:null,
    curRole:'',
    courseVideoPlay: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFkAAABUCAYAAADplZtxAAAAAXNSR0IArs4c6QAAB1lJREFUeF7tnXmsX0MUx7/fEGKPnVoSpRKhkkqbWmpJbbEUKbEHQWmUFrWFiBJLLFGKRpHQRKyxk0ZoLSHtHypFI8QWWymxNSJE4iunmV/dd9/c+7vLzO/9KnOSl9e+N3Pm3M87v3Pnnjkzl+gDkbQbgBEAtgCwZcH39QAsz3x9n/v/cpKv98HlDDKBQ2WUpIMATABwJIDhgez4CcDTAJ4DMJ/kn4H0tlLTM8iSNgRwGIBDARwCYJtWlnfv/IcDPt8B/7p7lzgtokOWtCOAiwGcAGDTOJfRVes/AF4EMHMoQko0yJLWBnCRA7x5VwwDG5gXfue+LPbav38HsDWAYZnvTf5oswHcTvKzmjY1bh4FsqRTHeA9KlhmH+MXADzZAUtyRYV+kLRWBvp4AEcDGFOh748G2nn2XxXat2oSFLKkAxzco7pYtRTAq3aDCv3xlbSTi/12Q7XYXybvOtAPt6LYpXMwyJJuAnBFyXi/ArjD4JJ8O+ZFdXRngJ8DwKaJRfIMgOkkv4hhV2vIkjYAYHHOQkSR3AvgTpIfxbiIbjolrQ/gQgDTAGxW0P4TAJeQfL6bvrq/bwVZ0i4Anijxkmcd3L54SHCebaDPLwF1Fckb64Isa98YsiSLdy8XKF/k4D4W0thQuiSNc159XIHOR0meHGq8RpAlXQfg6gIjbC5q8+K+F0nTAdxWYOhSkiNDXERtyJJeAWCPxD6ZRPKBEIb1SoekUwDcB2Bd35gkazPK66mlQNL9AM4uADCuV7OG0H8AF6st9PlyKAtJ7t1mzMqQJV0J4AbPYF8CGEXylzaG9ENfSW8A2M9jS6sQWAmypJMAPOIZfBHJvfoBUCgbJF0DYIZH32SSc5qM0xWyJINomax1cgOsILlRk0H7vY8km9ef67FzIkl7cKklpZAlbeWmabt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KVw05FwnXKQbX3PIlW98aQrXHHLlKVx6GGkOufLDSHqsbgC57mN1ShA1g1wrQZRSnc0g+yKAP9Vp+iWlpH1N0LWS9g5yWn6qD7n28pNvd2laSC0A33QhNZUE1PDkRiUBLmSk4paKoBsVtzjIqUyrAuS2ZVqp4LAaZF8xeLWCQ+fNqXS2BHTr0lkHORWBl0P2eXG9InAHOm1n8IAOtp3BQU4bc/yQw23McaDTFrMM6OBbzBzktFlyIGSfF1dekE3bfrtM26Jt+3XenDawx97A7kCnoxhiHsXgIKdDRQaHk7CHijjQ6Xic/0CHPx6nozsd9LSSRLyDnjKg05FlFRJIviZdT9PKdkqH7zWjXAuyi9HpGMmarGtDdqDTgag1QDeC7ECno30rgm4M2YFOh1RXAN0KsgOdjlvvAro15MwUL704oAB2MMjOq9MrMDygg0LOeHV6mUsGdhTIzqvTa4kc6GiQM16dXrBVYQYSpEl6VVwQjPWUpJce1uPVuvX//fWd/wKUcmKRHKvirQAAAABJRU5ErkJggg==',
    animationDisappearArray: [],
    videoCVArray: [],
    warnList: [
     
      {
        title: '随便一个pdf文件',
        instroduce: '',
        fileUrl: 'http://39.102.232.151:8030/pdf/1.pdf',
        type: 'pdf'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'jingshi')
    this.setData({
      curRole: app.globalData.curRole,
      name:options.name
    })
    // animationDisappearArray: [],
    // videoCVArray: [],
    // for (let i = 0; i < this.data.warnList.length; i++) {
    //   this.data.animationDisappearArray.push({})
    //   this.data.videoCVArray.push(wx.createVideoContext('video' + i))
    // }
    wx.showLoading({
      title: '正在加载',
    })
    wx.request({
      url: app.globalData.appBaseUrlV2 + '/api/warningEducation/page',
      header: {
        Authorization: app.globalData.Authorization
      },
      data: {
        county:app.globalData.appCounty,
        page: 0,
        size: 99999
      },
      success: (res) => {
        console.log(res, 'warningEducation warningEducation warningEducation')
        let newArr=[]
        if(this.data.name!='null'){
          console.log(this.data.name,111,typeof this.data.name )
          for(let i =0;i<res.data.content.length;i++){

            if(res.data.content[i].category===this.data.name&&!(this.data.name=='null')){
              console.log(this.data.name,1)
              newArr.push(res.data.content[i])
            }
          }
        }else{
          console.log(this.data.name,222)
          for(let i =0;i<res.data.content.length;i++){

            if(res.data.content[i].category==null&&this.data.name=='null'){
              console.log(this.data.name,2)
              newArr.push(res.data.content[i])
            }
          }
        }
        console.log(newArr,'newArr')
        this.setData({
          warnList: [...newArr.map(item => {
            return {
              ...item,
              title: item.title,
              instroduce: item.briefIntroduction,
              //fileUrl: app.globalData.appBaseUrlV3 + '/warning-education/' + item.filePath.split('\\warning-education\\')[1],
			  
			  /**
			    由于视频地址所有区域共用1个(经区)
        kimber to 2022/6/24(周五) 改为经区地址
        fileUrl: 'https://whjqaqscxt.weihai.cn/warning-education/' + item.filePath.split('\\warning-education\\')[1],
			    */
              fileUrl: app.globalData.appBaseUrlV2 +  '/warning-education/' + item.filePath.split('\\warning-education\\')[1],
              // fileUrl: 'https://whjqaqscxt.weihai.cn/warning-education/' + item.filePath.split('\\warning-education\\')[1],
              fileUrl1: item.filePath.split('\\warning-education\\')[1],
              type: (item.filePath.endsWith('.mp4') || item.filePath.endsWith('.m3u8'))?'video':'pdf'
            }
          })]
        })
      },
      complete: () => {
        wx.hideLoading()
      },
      fail: () => {}
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '警示教育'
    })
    wx.setNavigationBarColor({
      backgroundColor: '#090D9C',
      frontColor: '#ffffff',
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  /**
   * 点击类似 pdf 的展示文件
   */
  clickToShowFile(event) {
    // console.log(event.target.dataset.index)
    // wx.downloadFile({
    //   url: this.data.warnList[event.target.dataset.index].fileUrl,
    //   success: function(res) {
    //     const filePath = res.tempFilePath
    //     wx.openDocument({
    //       filePath: filePath,
    //       success: function(res) {
    //         console.log('打开文件成功')
    //       }
    //     })
    //   }
    // })
    wx.openDocument({
      filePath: this.data.warnList[event.target.dataset.index].fileUrl,
      success: function(res) {
        console.log(res, ' 打开文件成功')
      }
    })
  },
  viewPdfFile(event) {
    wx.showLoading({
      title: '正在加载'
    })

    wx.downloadFile({
      url: this.data.warnList[event.currentTarget.dataset.inde].fileUrl,
      success: (res) => {
        console.log(res, 'wx.downloadFile wx.downloadFile')
        wx.hideLoading()
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath
        })
      },
      fail: () => {
        wx.hideLoading()
      }
    })
  },
  playvideo(event) {
    const repAnimation = wx.createAnimation({
      duration: 50,
      timingFunction: 'step-start'
    })
    this.repAnimation = repAnimation
    repAnimation.translateX('-100%').step()
    this.data.animationDisappearArray[event.target.dataset.index] = repAnimation.export()
    this.setData({
      animationDisappearArray: this.data.animationDisappearArray
    })
    this.data.videoCVArray[event.target.dataset.index].play()
  }
})