// pages/user/myData/myData.js
const app = getApp(),
  {
    globalData: {
      util: {
        regeneratorRuntime,
        api: {
          myMaterial,
          uploadImg,
          addMaterial,
          delMaterial
        },
        request: {
          get_auth,
          post_auth,
          get
        }
      }
    }
  } = app;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadImg: null,
    dataName: '',
    edit: false,
  },
  dataName: function (e) {
    this.data.dataName = e.detail.value
  },
  // 关闭遮罩层
  clear: function () {
    let that = this;
    that.setData({
      mask: false
    })
  },
  // 预览图片
  preview: function (e) {
    let urls = [],
      urlArr = e.target.dataset.urlarr,
      url = e.target.dataset.url;
    for (var i = 0; i < urlArr.length; i++) {
      urls.push(e.target.dataset.http + urlArr[i].url)
    }
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
  // 编辑材料
  edit: function () {
    let that = this;
    let edit = !that.data.edit;
    that.setData({
      edit,
    })
  },
  // 删除材料
  async deleteImg(e) {
    console.log(e)
    let that = this,
      id = e.target.dataset.id;
    await get(delMaterial, {
      id
    })
    that.dataList();
  },
  // 资料上传
  async uploadFile() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      success(res) {
        wx.showLoading({
          title: '正在上传',
        })

        wx.uploadFile({
          url: uploadImg,
          filePath: res.tempFilePaths[0],
          header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8', },
          name: 'filePath',
          formData: {
            'fileType': 'image'
          },
          success(res1) {
            console.log(res1)
            let obj = {}
            wx.hideLoading()
            obj = JSON.parse(res1.data);
            console.log(obj)
            if (obj.errno == 0) {
              let uploadImg = obj.data;

              that.setData({
                uploadImg,
                mask: true,
              })
              console.log(that.data.uploadImg)
            }
          }
        })
      }
    })
  },
  // 上传资料提交
  async maskSubmit() {
    let that = this;
    let { _err } = await post_auth(addMaterial, {
      materialName: that.data.dataName,
      materialUrl: that.data.uploadImg.filePath
    });
    if (!_err) {
      wx.showToast({
        title: '上传成功',
        icon: "none"
      })
      that.setData({
        dataName: '',
        mask: false,
      })
      that.dataList();
    }
  },
  // 查询我的材料
  async dataList() {
    let that = this;
    let myDataList = await get_auth(myMaterial);
    console.log(myDataList)
    let items = myDataList.items,
      listArr = [],
      listArr1 = [];

    for (let i = 0; i < items.length; i++) {
      let list = {
        'id': items[i].id,
        'materialName': items[i].materialName,
        isno: 0,
        'materialUrl': [{ 'url': items[i].materialUrl, 'id': items[i].id }]
      }
      listArr1.push(list)
      for (var j = i + 1; j < items.length; j++) {
        if (items[i].materialName == items[j].materialName) {
          var objList = {}
          objList.url = items[j].materialUrl
          objList.id = items[j].id
          list.materialUrl.push(objList)
          list.isno = items[j].isno
          listArr.push(list)
          items[j] = ''
        }
      }

    }
    for (let i = 0; i < listArr1.length; i++) {
      if (!listArr1[i].id) {
        listArr1.splice(i, 1);
        i--
      }
    }
    that.setData({
      image: listArr1,
      url: myDataList.prefix
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let that = this;
    that.dataList();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})