// component/selectRelatedGoods/selectRelatedGoods.js
var app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: { // 显示标识
      type: Boolean,
      value: false
    },
    proGoods: { // 显示标识
      type: Array,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    proList:[],
    proData:[],
    productName:''
  },

  observers: {
    proGoods(val) {
      for (var i in this.data.proList){
        for(var j in val){
          if (this.data.proList[i].productId == val[j].productId){
            this.data.proList[i].isSelect = true
          }
          this.setData({
            proList: this.data.proList
          })
        }
      }
    },
    show() {
      const { show } = this.data
      if (show) {
        wxService.nextTick(() => {
          this.getProList();
        })
      }
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    hidePopup: function () { // 隐藏弹出框
      this.setData({
        show: false
      })
    },
    _selectCoupon(e){
      console.log(e)
      this.setData({
        proData: e.detail
      })
    },
    confirm(){
      if (this.data.proData.length < 1){
        wx.showToast({
          title: '请选择关联商品',
          icon: 'none'
        })
        return false;
      }
      this.setData({
        show: false
      })
      wxService.nextTick(() => {
        this.triggerEvent('_selectGoods', this.data.proData)
      })
    },

    // 点击键盘完成时触发
    onTapConfirm(e){
      this.setData({
        productName: e.detail.value
      })
    },

    // 搜索商品search
    search(){
      if(!this.data.productName){
        wx.showToast({
          title: '请输入商品名称',
          icon:'none'
        })
        return false
      }
      this.getProList()
    },

    // 商品列表
    getProList() {
      wxService.post(`/sale/product/buyer/listWithMember`,{
        productName: this.data.productName
      }).then(res => {
        const { result, data } = res.data
        if (result == 0) {
          data.forEach(item => {
            item.isSelect = false
          })
          this.setData({
            proList: data
          })
          console.log(data)
        }
      }).finally(() => {
      })

    }
  },

})
