const _Http = getApp().globalData.http;
Component({
  properties: {
    data: {
      value: '',
      type: {}
    },
    changeQty: {
      value: false,
      type: Boolean
    },
    model: {
      type: String,
      value: 1
    },
    changeItem: Function,
    delete: Function
  },
  methods: {
    onIconTap() {
      this.triggerEvent("delete", this.data.data);
    },
    inputChange(e) {
      this.data.data.qty = e.detail.value
      this.triggerEvent("input", this.data.data);
    },
    newOnBlur(e) {
      let value = e.detail.value,
        data = this.data.data,
        that = this;
      if (data.oldQty == value) return;
      wx.showModal({
        title: `提示`,
        content: `是否确定修改“${data.itemname}”数量为:${value}`,
        complete: (res) => {
          if (res.cancel) {
            that.setData({
              "data.qty": data.oldQty
            })
          }
          if (res.confirm) that.triggerEvent("changeItem", data)
        }
      })
    },
    onBlur(e) {
      let {
        value
      } = e.detail,
        data = this.data.data,
        name = e.currentTarget.dataset.name;
      console.log(data)
      if (data[name] == value) return;
      if (name == 'qty' && value <= 0) {
        wx.showToast({
          title: '数量不符合规则,已重置为1',
        })
        data[name] = 1
      } else {
        data[name] = value
      }
      _Http.basic({
        "id": "20230215201903",
        "content": {
          "sa_workorderid": data.sa_workorderid,
          "iteminfos": [data]
        }
      }).then(res => {
        console.log("产品修改", res)
        if (res.msg != '成功') return wx.showToast({
          title: res.msg,
          icon: "none"
        });
        this.triggerEvent("changeItem", data)
      });
    }
  }
})