const api = require('../../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkText:'请选择'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that=this
    wx.request({
      url: api.setImg,
      data:{userId:wx.getStorageSync('userId')},
      success(res){
        that.data.arr=[]
        that.data.arr1=[]
        for(var i=0;i<res.data.data.items.length;i++){
          var list = { 'id': res.data.data.items[i].id, 'materialName': res.data.data.items[i].materialName, isno: 0, 'materialUrl': [{ 'url': res.data.data.items[i].materialUrl, 'id': res.data.data.items[i].id}] }
          that.data.arr1.push(list)
          for(var j=0;j<res.data.data.items.length;j++){
            if (res.data.data.items[i].materialName == res.data.data.items[j].materialName && i!=j){
              var objList={}
              objList.url = res.data.data.items[j].materialUrl
              objList.id = res.data.data.items[j].id
              list.materialUrl.push(objList)
              list.isno = 1
              that.data.arr.push(list)
              res.data.data.items[j]=''
            }
          }
          
        }
        for(var i=0;i<that.data.arr1.length;i++){
          if(!that.data.arr1[i].id){
            that.data.arr1.splice(i,1);
            i--
          }
        }
        console.log(that.data.arr1)
        that.setData({
          image: that.data.arr1,
          url:res.data.data.prefix
        })
      }
    })
  },
  checkImg:function(e){
    let that=this
    console.log(e)
    var ishave=false
    for(var i=0;i<that.data.image.length;i++){
      for (var j = 0; j < that.data.image[i].materialUrl.length;j++){
        if (that.data.image[i].materialUrl[j].id == e.currentTarget.dataset.id&&that.data.image[i].materialUrl[j].isno){
          that.data.image[i].materialUrl[j].isno = false
        }else if (that.data.image[i].materialUrl[j].id == e.currentTarget.dataset.id){
          that.data.image[i].materialUrl[j].isno=true
        }
        if (that.data.image[i].materialUrl[j].isno){
          ishave=true
        }else{
          if(!ishave){

          }
        }
      }
    }
    that.setData({
      image: that.data.image,
      checkText:ishave?'完成':'请选择',
      ishave:ishave
    })
  },
  // 选择完成
  nav:function(e){
    let that=this
    var arr=[]
    for (var i = 0; i < that.data.image.length; i++) {
      for (var j = 0; j < that.data.image[i].materialUrl.length; j++) {
        if (that.data.image[i].materialUrl[j].isno) {
          arr.push(that.data.image[i].materialUrl[j].url)
        }
      }
    }
    wx.setStorageSync('imgUrl', arr);
    wx.setStorageSync('httpUrl', e.currentTarget.dataset.httpurl)
    wx.navigateBack({
      url:1
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})