var app = getApp();
Component({
  properties: {
    goods: {
      type: null
    },
    styleCon: {
      type: null
    },
    loading: {
      type: null
    },
    jumpSo: {
      type: null
    },
    storeId: {
      type: null
    },
    classifyBol: {
      type: null
    },
    showprice: {
      type: null
    },
    text: {
      type: null
    },
    kind: {
      type: null
    },
    goods_template: {
      type: null
    },
    type: {
      type: null
    },
    bg_color: {
      type: String,
      value: "#f5f5f5"
    }
  },
  data: {
    language: '',
    iPnoneBottomBol: false,
    price: '',
    market_price: '',
    template: '',
    posterId: 0,
    integral: "积分"
  }, // 私有数据，可用于模板渲染
  observers: {
    'classifyBol': function (bol) {
      console.log(bol, 'aaaaaa');
    },
    'goods_template': function (goods_template) { //  'params'是要监听的字段，（params）是已更新变化后的数据
      if (goods_template) {
        this.setData({
          template: goods_template.names
        });
        if (goods_template.names == '02' || goods_template.names == '03') {
          this.setData({
            classifyBol: false
          });
        } else {
          if (!this.data.classifyBol) {
            this.setData({
              classifyBol: false
            });
          }
          console.log('aaaaaa', this.data.classifyBol);
        }
      }

      // let basic_info = wx.getStorageSync("yz_basic_info");
      // if(basic_info && basic_info.home.item.is_decorate === 1) {
      //   if(basic_info.home.item.ViewSet.goodsList.is_default != 0) {
      //     this.setData({
      //       template: basic_info.home.item.ViewSet.goodsList.code.substring(9)
      //     });
      //   }else {
      //     this.setData({
      //       template: '01'
      //     });
      //   }
      // }

    }
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let language = wx.getStorageSync('langIndex');
      this.setData({
        'language': language.en
      });
      // this.getStatus(); //没发现有用
      if (wx.getStorageSync("yz_basic_info").lang) {
        let price = wx.getStorageSync("yz_basic_info").lang.goods ? wx.getStorageSync("yz_basic_info").lang.goods.price : '现价';
        let market_price = wx.getStorageSync("yz_basic_info").lang.goods ? wx.getStorageSync("yz_basic_info").lang.goods.market_price : '原价';
        this.setData({
          price: price,
          market_price: market_price
        });
      }

      const value = wx.getStorageSync('integral');
      if (value) {
        this.setData({
          integral: value
        });
      }
    },
    moved() {},
    detached() {},
  },
  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {
    this.getiPnoneBottom();
  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {},

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    copybtn(e) {
      let inviteCode = e.target.dataset.title;
      let goods_link = e.target.dataset.link;
      if (goods_link) {
        let aa = goods_link.split('0x0A');
        let bb = aa.join('\r');
        wx.setClipboardData({
          data: goods_link ? bb : '',
        });
      } else {
        wx.setClipboardData({
          data: inviteCode ? inviteCode : '',
        });
      }
    },
    getiPnoneBottom() {
      let that = this;
      try {
        var value = wx.getStorageSync('iPhoneBottom');
        if (value) {
          that.setData({
            iPnoneBottomBol: true
          });
          // Do something with return value
        }
      } catch (e) {
        // Do something when catch error
      }
    },
    getShare(ras) {
      var that = this;
      var urlStr = app.getNetAddresss('goods.goods.get-goods-type');
      urlStr += '&id=' + ras;
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let _data = res.data.goods_type;
            let groups_title = res.data.title;
            that.triggerEvent('getShare', {
              ras,
              _data,
              groups_title
            });
            // this.triggerEvent('getShareType',_data)
          }
        },
        fail: function (res) {}
      });

    },
    // getStatus(){
    //   var that = this;
    //   var urlStr = app.getNetAddresss("member.member.member-data");
    //   app._getNetWork({
    //     url: urlStr,
    //     success: function(resdata) {
    //       var res = resdata.data;
    //       if (res.result == 1) {
    //         try{
    //           if(res.data.plugins.ViewSet.goodsList && res.data.plugins.ViewSet.goodsList.name == '02'){
    //             that.setData({
    //               goodsStatus:res.data.plugins.ViewSet.goodsList.name
    //             });
    //           }else{
    //             that.setData({
    //               goodsStatus:'01'
    //             });
    //           }
    //         }catch(err){
    //           console.log(err);
    //           that.setData({
    //             goodsStatus:'01'
    //           });
    //         }
    //       } else {
    //         wx.showToast({
    //           icon: 'none',
    //           title: res.msg,
    //           duration: 1500
    //         });
    //       }
    //
    //     },
    //     fail: function(res) {
    //
    //     }
    //   });
    // },
    gotoDetail(e) {
      let that = this;
      let id = e.currentTarget.dataset.goodsid;
      if (this.data.jumpSo == true) {
        wx.navigateTo({
          url: '/packageA/detail_v2/detail_v2?id=' + id + '&tag=o2o' + '&store_id=' + this.data.storeId
        });
      } else if (this.data.type == 'snatchRegiment') {
        wx.navigateTo({
          url: '/packageA/detail_v2/detail_v2?id=' + id + '&name=69'
        });
        return;
      } else if (that.data.text == 'paddingOrder_Home' || that.data.kind == 'kind') {
        wx.navigateTo({
          url: '/packageA/detail_v2/detail_v2?id=' + id + '&kind=POrder',
        });
      } else {
        wx.navigateTo({
          url: '/packageA/detail_v2/detail_v2?id=' + id
        });
        // console.log(this.data.kind)  没有用到了
        // var that = this;
        // var urlStr = app.getNetAddresss("goods.goods.get-goods");
        // urlStr += "&id=" + id;
        // app._getNetWork({
        //   showToastIn: false,
        //   url: urlStr,
        //   success: function(resdata) {
        //     var res = resdata.data;
        //     if (res.result == 1) {
        //       if (res.data.is_course == 1) {
        //         wx.navigateTo({
        //           url: '/packageA/detail_v2/detail_v2?id=' + id
        //         });
        //         //CourseDetail 	goods_id: id
        //       } else if (res.data.goods_type == 1) {
        //         wx.navigateTo({
        //           url: '/packageA/detail_v2/detail_v2?id=' + id + '&tag=o2o' + '&store_id=' + res.data.store_id
        //         });
        //       } else if (that.data.text == 'paddingOrder_Home'||that.data.kind == 'kind'){
        //         wx.navigateTo({
        //           url: '/packageA/detail_v2/detail_v2?id=' + id + '&kind=POrder',
        //         });
        //       }else {
        //         wx.navigateTo({
        //           url: '/packageA/detail_v2/detail_v2?id=' + id
        //         });
        //       }
        //     } else {
        //       wx.showToast({
        //         icon: 'none',
        //         title: res.msg,
        //         duration: 1500
        //       });
        //     }

        //   },
        //   fail: function(res) {

        //   }
        // });
      }
    },
    saveImg() {
      // var _this = this;
      // var times = _this.data.timeend - _this.data.timestart;
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() {
                console.log('授权成功');
              }
            });
          }
        }
      });
      var imgSrc = this.data.posterImg.image_url;
      wx.downloadFile({
        url: imgSrc,
        success: function (res) {
          //图片保存到本地
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: function (data) {
              wx.showToast({
                title: '保存成功',
                icon: 'none',
                duration: 2000
              });
            },
            fail: function (err) {
              if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                console.log("当初用户拒绝，再次发起授权");
                wx.openSetting({
                  success(settingdata) {
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                      wx.showToast({
                        icon: 'none',
                        title: '获取权限成功,再次长按保存图片',
                        duration: 1500
                      });
                    } else {
                      wx.showToast({
                        icon: 'none',
                        title: '获取权限失败,保存图片需要用户授权',
                        duration: 1500
                      });
                    }
                  }
                });
              }
            },
            complete(res) {
              console.log(res);
            }
          });
        }
      });
    },
    posterClose() {
      this.setData({
        posterShow: false
      });
    },
    postShow(e) {
      let id = e.currentTarget.dataset.goodsid;
      this.getShare(id);
      this.setData({
        posterId: id,
        posterShow: true
      });
      return;
      // let that = this;
      // if (that.data.posterImg) {
      //   this.setData({
      //     posterShow: true
      //   });
      // } else {
      //   wx.showLoading({
      //     title: '海报生成中',
      //   });
      //   this.data.posterId = id;
      //   let urlStr = app.getNetAddresss("goods.goods-poster.generate-goods-poster");
      //   urlStr += "&id=" + id;
      //   app._getNetWork({
      //     showToastIn: false,
      //     url: urlStr,
      //     success: function (resdata) {
      //       var res = resdata.data;
      //       if (res.result == 1) {
      //         that.setData({
      //           posterImg: res.data,
      //           posterShow: true
      //         });
      //       } else {
      //         that.setData({
      //           posterShow: false
      //         });
      //         wx.showToast({
      //           icon: 'none',
      //           title: res.msg,
      //           duration: 1500
      //         });
      //       }
      //       wx.hideLoading();
      //     },
      //     fail: function (res) {
      //       console.log(res);
      //       wx.hideLoading();
      //     }
      //   });
      //   setTimeout(function () {
      //     wx.hideLoading();
      //   }, 5000);
      // }
    },
  }
});