var posterBehavior = require('../../../poster/poster');

var app = getApp();

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

  /**
	 * 组件的初始数据
	 */
  data: {
    listData:{}
  },
  observers:{
    'isshow':function(isshow){
      console.log("==============",this.data.listData[this.data.activeID],this.properties.activeID);
      if(isshow && !this.data.listData[this.data.activeID] && this.data.getFlag){
        this.data.getFlag=false;
        this.getPosterPic();
      }else if(this.data.listData[this.data.activeID] !='' ){
        this.setData({
          poster: this.data.listData[this.data.activeID],
        });
      }
    }
  },
  /**
	 * 组件的方法列表
	 */
  methods: {
    async getPosterPic(){
      var urlStr = app.getNetAddresss("plugin.fight-groups.frontend.controllers.group-poster.generate-goods-poster");
      this.setData({
        poster: "",
      });
      let that = this;
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        data:{
          id:that.properties.activeID,
          store_id:that.data.store_id
        },
        success: (resdata)=> {
          let res = resdata.data;
          if(res.result!=1){
            wx.showToast({
              title: res.msg,
            });
            return;
          }
          this.data.listData[this.data.data] = res.data.image_url;
          this.setData({
            poster: res.data.image_url,
          });
        },
        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);
        }
      });
    },


  }
});
