const _Http = getApp().globalData.http;
Component({
  properties: {
    list: {
      type: Array
    },
    authName: {
      type: Object,
      value: {
        appName: "wmarket",
        formName: "wmarket"
      }
    }
  },
  data: {
    privacyFieldC: []
  },
  options: {
    addGlobalClass: true
  },
  lifetimes: {
    attached: function () {
      this.setData({
        isdisplaysaleqty:wx.getStorageSync('siteP').isdisplaysaleqty
      })
      setTimeout(() => {
        try {
          const {
            formName,
            appName
          } = this.data.authName;
          let privacyFieldC = wx.getStorageSync('auth')[appName].forms[formName].formcols.map(v => v.title);
          this.setData({
            privacyFieldC,
            isShowFavorites: wx.getStorageSync('auth').wfavorites ? true : false
          })
          console.log("privacyFieldC", privacyFieldC)
        } catch (error) {
          console.error("查询隐私字段", error)
          this.setData({
            privacyFieldC: []
          })
        }
      }, 1000)
    }
  },
  methods: {
    toDetail(e) {
      const {
        item
      } = e.currentTarget.dataset;
      wx.navigateTo({
        url: `/packageA/market/detail?params=${JSON.stringify({
          sa_itemgroupid:item.sa_itemgroupid,
          sa_brandid:item.sa_brandid
        })}`,
      })
    },
    buyMore(e) {
      const {
        name
      } = e.target.dataset;
      if (!name) return;
      const item1 = e.currentTarget.dataset.item,
        index = e.currentTarget.dataset.index,
        item = item1.item[0];

      if (name == '购物车') {
        wx.showModal({
          title: '提示',
          content: `是否确定将“${item.itemname}”加入到购物车`,
          complete: (res) => {
            if (res.confirm) _Http.basic({
              "id": 20220924095102,
              "content": {
                sa_brandid: item1.sa_brandid,
                "itemid": item.itemid, //货品id
                "qty": item.orderminqty, //数量
                itemno: item.itemno, //货品编号
                tradefield: item1.tradefield,
                width: item.width,
                length: item.length
              },
            }).then(res => {
              console.log("加入购物车", res)
              wx.showToast({
                title: res.msg == '成功' ? '加入成功' : res.msg,
                icon: "none"
              });
              if (res.msg == '成功') getApp().globalData.getCollectCount().then(badge => this.setData({
                badge
              }))
            })
          }
        })
      } else {
        let iscollection = item.iscollection == 1 ? false : true
        _Http.basic({
          "id": 20231121143403,
          "content": {
            "itemid": item.itemid, //货品id
            "qty": item.orderminqty, //数量
            iscollection,
            width: item.width,
            length: item.length
          }
        }).then(res => {
          console.log(iscollection + "收藏夹", res)
          wx.showToast({
            title: res.msg == '成功' ? iscollection ? '收藏成功' : "已取消收藏" : res.msg,
            icon: "none"
          });
          if (res.msg == '成功') {
            this.setData({
              [`list[${index}].item[0].iscollection`]: iscollection ? 1 : 0
            })
            getCurrentPages()[getCurrentPages().length - 1].setData({
              [`list[${index}].item[0].iscollection`]: iscollection ? 1 : 0
            })
          }
        })
      }
    },
  }
})