// packageE/community_buying/components/buying_qrcode/buying_qrcode.js
var posterBehavior = require("../../../../poster/poster.js");

var app = getApp();

Component({
  behaviors: [posterBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    buyingid: {
      value: 0,
      type: Number,
    },
    deliverid: {
      value: 0,
      type: Number,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    listData: {},
  },
  observers: {
    isshow: function (isshow) {
      console.log(this.data.listData[this.data.buyingid]);
      if (
        isshow &&
        !this.data.listData[this.data.buyingid] &&
        this.data.getFlag
      ) {
        this.data.getFlag = false;
        this.getPosterPic();
      } else if (this.data.listData[this.data.buyingid] != "") {
        this.setData({
          poster: this.data.listData[this.data.buyingid],
        });
      }
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async getPosterPic() {
      var urlStr = app.getNetAddresss(
        "plugin.package-deliver.frontend.communityGroups.getPoster"
      );
      this.setData({
        poster: "",
      });
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        data: {
          group_id: this.data.buyingid,
          deliver_id: this.data.deliverid,
          code_type: 2,
          get_type: 1,
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result != 1) {
            wx.showToast({
              title: res.msg,
            });
            return;
          }
          this.data.listData[this.data.buyingid] = res.data;
          this.setData({
            poster: res.data,
          });
        },
        complete: () => {
          this.data.getFlag = true;
        },
      });
    },
    saveImg() {
      console.log("执行", this.data.poster);
      var imgSrc = this.data.poster;
      var imgPath =
        wx.env.USER_DATA_PATH + "/solitaire_qrcode" + "haibao" + ".png";
      var imageData = imgSrc.replace(/^data:image\/\w+;base64,/, "");
      var fs = wx.getFileSystemManager();
      fs.writeFileSync(imgPath, imageData, "base64");
      wx.saveImageToPhotosAlbum({
        filePath: imgPath,
        success: (data) => {
          fs.unlinkSync(imgPath);
          wx.showToast({
            title: "保存成功",
            icon: "none",
            duration: 2000,
          });
        },
        fail: function (err) {
          console.log(err);
        },
      });
    },
  },
});
