// pages/friendHelper/index.js
const app = getApp();
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    status: 2,
    currentTab: 1,
    templateList: [],
    jobIds: [],
    iosDialog: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options);
    this.setData({
      status: options.status,
    });
    this.checkTab({
      target: {
        dataset: {
          index: 1,
        },
      },
    });
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    console.log(123123);
    console.log(this.data.jobIds);
    this.getList();
  },

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

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

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

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

  /**
   * 用户点击右上角分享
   */
onShareAppMessage() {
  return app.sharePageImage()
},
  checkTab (e) {
    let val = e.target.dataset;
    if (e.target.dataset.index && e.target.dataset.index != this.data.currentTab) {
      console.log(val.index);
      this.data.currentTab = val.index;
      this.setData({
        currentTab: this.data.currentTab,
      });
      this.getList();
    }
  },
  navigationTo (e) {
    if (e.currentTarget.dataset.type == 1) {
      wx.navigateTo({
        url: `../jobSelectPage/index?status=${this.data.currentTab}`,
      });
    } else {
      wx.navigateTo({
        url: "../setAnnunciateImg/index?status=1",
      });
    }
  },
  previewImg (e) {
    console.log(e.currentTarget.dataset.src);
    console.log(e);
    wx.previewImage({
      current: e.currentTarget.dataset.src, // 图片的地址url
      urls: app.isNotEmptyCheck(e.currentTarget.dataset.srclist) ? e.currentTarget.dataset.srclist : [e.currentTarget.dataset.src], // 预览的地址url
      success: function (e) {
        console.log(e);
      },
      fail: function (e) {
        console.log(e);
      },
    });
  },
  seaveImg (item1) {
    console.log(item1);

    item1.imgList.forEach((item) => {
      /**
       * 调用外部路径图片时候执行的函数
       */
      if (item.slice(0, 10).indexOf("http") > -1) {
        console.log(123);
        wx.downloadFile({
          url: item,
          success: function (res) {
            console.log(res);
            // 下载成功后将文件转为临时路径
            if (res.statusCode === 200) {
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success (res) {
                  console.log(res);
                  if (res.errMsg == "saveImageToPhotosAlbum:ok") {
                    wx.showToast({
                      title: "文字已复制，图片已保存",
                      icon: "none",
                      duration: 1500,
                    });
                  }
                },
                fail (res) {
                  console.log(res);
                  wx.showToast({
                    title: "图片保存失败",
                    icon: "none",
                    duration: 1500,
                  });
                },
              });
            }
          },
        });
      } else {
        wx.saveImageToPhotosAlbum({
          filePath: item,
          success (res) {
            console.log(res);
            if (res.errMsg == "saveImageToPhotosAlbum:ok") {
              wx.showToast({
                title: "文字已复制，图片已保存",
                icon: "none",
                duration: 1500,
              });
            }
          },
          fail (res) {
            console.log(res);
            wx.showToast({
              title: "图片保存失败",
              icon: "none",
              duration: 1500,
            });
          },
        });
      }
    });
  },
  getCopy (e) {
    let that = this;
    let item = e.currentTarget.dataset.item;
    console.log(item);
    wx.setClipboardData({
      data: item.title,
      success (res) {
        wx.getClipboardData({
          success (res) {
            that.seaveImg(item);
          },
          fail (res) {
            if (res.errMsg) {
              wx.showToast({
                title: "内容复制失败, 请重试",
                icon: "none",
                duration: 1500,
              });
              that.seaveImg(item);
            }
          },
        });
      },
    });
  },
  getList () {
    let that = this;
    let url;
    if (this.data.currentTab == 1) {
      url = "/job/hot";
    } else if (this.data.currentTab == 2) {
      url = "/jobs/three";
    } else if (this.data.currentTab == 3) {
      url = "/jobs/nine";
    } else if (this.data.currentTab == 4) {
      url = "/job/hot";
    } else if (this.data.currentTab == 5) {
      url = "/job/new";
    }
    console.log(url);
    customRequest("/yishoudan/friends/helper" + url,{header:'headers', method: 'GET', data: {}}).then(({data})=>{
      if (data.status == 200) {
        console.log(data);
        let arr = [];
        data.data.forEach((item, index) => {
          arr[index] = item;
          if (item.imgs) {
            arr[index]["imgList"] = [...item.imgs];
          } else if (item.img) {
            arr[index]["imgList"] = [item.img];
          }
        });
        that.setData({
          templateList: arr,
        });
        console.log(that.data.templateList);
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/friends/helper" + url,
    //   header: app.globalData.headers,
    //   method: "GET",
    //   success: function ({ data }) {
    //     console.log(data);
    //     if (data.status == 200) {
    //       console.log(data);
    //       let arr = [];
    //       data.data.forEach((item, index) => {
    //         arr[index] = item;
    //         if (item.imgs) {
    //           arr[index]["imgList"] = [...item.imgs];
    //         } else if (item.img) {
    //           arr[index]["imgList"] = [item.img];
    //         }
    //       });
    //       that.setData({
    //         templateList: arr,
    //       });
    //       console.log(that.data.templateList);
    //     }
    //     // console.log(res);
    //   },
    // });
  },
});
