StoreSelectPopup.js 8.33 KB
// components/StoreSelectPopup/StoreSelectPopup.js
import carLifeApi from '../../../../carEquityApi'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        if (n) {
          this.setData({
            scrollTop: 0,
            nothing: false,
            searchValue: ''
          })
        }
      }
    },
    type: Number,
    list: {
      type: Array,
      observer: function (n) {
        if (n) {

        }
      }
    }


  },
  /**
   * 组件的初始数据
   */
  data: {
    pageNum: 1,
    scrollTop: 0,
    nothing: false,
    tempList: [],
    selectedData:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    open() {
      if (this.data.disabled) return
      this.setData({
        edit: true,
        inputFocus: true
      })
    },
    setValue(value) {
      if (this.properties.filterEmoji) {
        if (wx.$util.haveEmoji(value)) value = wx.$util.replaceEmoji(value)
      }
      this.setData({
        searchValue: value
      })
    },
    input(e) {
      let {
        value
      } = e.detail
      this.setValue(value)
      this.triggerEvent('input', {
        value
      })
    },
    inputConfifm(e) {
      this.emit(e)
    },
    emit(e) {
      let {
        detail: {
          value
        }
      } = e
      this.setData({
        edit: false,
        nothing: false,
        pageNum: 1
      })
      this.setValue(value)
      this.triggerEvent('updata', value)
    },
    choose(e) {
      let {
        index,
        item
      } = e.currentTarget.dataset
      let {
        active
      } = item
      let {
        list
      } = this.data, tmpTarget,res;
      if (list[index].merchantName !== '全部') {
        let target = `list[${index}].active`
        if (list[0].merchantName == '全部') tmpTarget = `list[0].active`
        this.setData({
          [target]: !active,
          [tmpTarget]: false
        }, () => {
          this.setSelectedData()
        })
      } else {
        if (!active) {
          list.map(val => {
            return val.active = false
          })
          list[0].active = true
        }
        this.setData({
          list
        }, () => {
          this.setSelectedData()
        })
      }

    },
    setSelectedData() {
      let {
        list,
        selectedData
      } = this.data,res;

      if (list.length >0 && !list[0].merchantNo) {
        res = list.some(val => val.active)
        if (!res) {
          list[0].active = true
        }
      }

      selectedData = list.filter(val => {
        if (val.active) return val
      })
      this.setData({
        selectedData,
        list
      })
      wx.$router.getPageInstance().filterStore(list)
    },
    hidePopup() {
      this.triggerEvent('cancel')
    },
    confirm() {
      let {
        selectedData
      } = this.data
      this.triggerEvent('confirm', selectedData)
    }
  }
})








// components/StoreSelectPopup/StoreSelectPopup.js
// import carLifeApi from '../../../../carEquityApi'
// Component({

//   /**
//    * 组件的属性列表
//    */
//   properties: {
//     show: {
//       type: Boolean,
//       observer: function (n) {
//         if (n) {
//           this.setData({
//             scrollTop: 0,
//             pageNum: 1,
//             nothing: false,
//             searchValue: ''
//           })
//           this.getList(true)
//         }
//       }
//     },
//     type: Number,
//     selectedData: Array
//   },

//   /**
//    * 组件的初始数据
//    */
//   data: {
//     pageNum: 1,
//     scrollTop: 0,
//     nothing: false
//   },

//   /**
//    * 组件的方法列表
//    */
//   methods: {
//     getList(isReset = false) {
//       let {
//         type,
//         pageNum,
//         searchValue,
//         nothing,
//         selectedData
//       } = this.data
//       // type 1园区店数据 2 4s店数据
//       let requestEvent = 'selectrecmerchantList'
//       let params = {
//         pageSize: 20,
//         pageNum: isReset ? 1 : pageNum,
//         merchantName: searchValue
//       }
//       console.log(selectedData)
//       carLifeApi[requestEvent](params).then(res => {
//         let {
//           code,
//           data,
//           message
//         } = res
//         if (code == 0) {
//           if (isReset) data?.unshift({
//             merchantNo: '',
//             merchantName: '全部',
//             active: true
//           })
//           let tempList = data
//           if (selectedData?.length) {
//             tempList[0].active = false
//             tempList.forEach(val => {
//               val.active = selectedData.some(v => v.merchantNo == val.merchantNo)
//             })
//           }
//           this.setData({
//             list: tempList,
//             tempList
//           })
//         } else {
//           wx.$util.showToast(message)
//         }
//       })
//     },
//     scrollonReachBottom() {
//       // this.getList()
//     },
//     open() {
//       if (this.data.disabled) return
//       this.setData({
//         edit: true,
//         inputFocus: true
//       })
//     },
//     setValue(value) {
//       if (this.properties.filterEmoji) {
//         if (wx.$util.haveEmoji(value)) value = wx.$util.replaceEmoji(value)
//       }
//       this.setData({
//         searchValue: value
//       })
//     },
//     input(e) {
//       let {
//         value
//       } = e.detail
//       this.setValue(value)
//       this.triggerEvent('input', {
//         value
//       })
//     },
//     inputConfifm(e) {
//       this.emit(e)
//     },
//     emit(e) {
//       let {
//         detail: {
//           value
//         },
//         type
//       } = e
//       this.setData({
//         edit: false,
//         nothing: false,
//         pageNum: 1
//       })
//       this.setValue(value)
//       this.updata(true)
//     },
//     updata(e) {
//       let {
//         list,
//         tempList,
//         searchValue
//       } = this.data, arr;
//       if (searchValue) {
//         arr = tempList.filter(item => {
//           if (item.merchantName.includes(searchValue)) {
//             return item
//           }
//         })
//       } else {
//         list = tempList
//       }

//       this.setData({
//         list: searchValue ? arr : tempList,
//         tempList
//       })
//     },
//     choose(e) {
//       let {
//         item,
//         index
//       } = e.currentTarget.dataset
//       let {
//         active
//       } = item
//       let {
//         list
//       } = this.data, tmpTarget;

//       if (list[index].merchantName !== '全部') {
//         let target = `list[${index}].active`
//         if (list[0].merchantName == '全部') {
//           tmpTarget = `list[0].active`
//         }
//         this.setData({
//           [target]: !active,
//           [tmpTarget]: false
//         }, () => {
//           this.setSelectedData()
//         })
//       } else {
//         if (!active) {
//           list.map(val => {
//             return val.active = false
//           })
//           list[0].active = true
//         }
//         this.setData({
//           list
//         }, () => {
//           this.setSelectedData()
//         })
//       }
//     },
//     setSelectedData() {
//       let {
//         list,
//         selectedData,
//         tempList
//       } = this.data
//       let arr = []
//       // 已选的未分页加载出的数据置为选中状态
//       let selectedArr = wx.$util.arrayUnique(selectedData.concat(list), 'merchantId')
//       selectedArr.forEach(val => {
//         if (val.active && val.merchantId) arr.push(val)
//       })
//       let isFlag = tempList.some((item, index) => {
//         if (index != 0) {
//           return item.active
//         }
//       })
//       if (tempList[0].active && isFlag) {
//         tempList[0].active = false
//       }
//       this.setData({
//         // selectedData: list[0]?.active ? [] : arr, // 选择全部清除已选的门店数据
//         list,
//         tempList
//       })
//     },
//     hidePopup() {
//       this.triggerEvent('cancel')
//     },
//     confirm() {
//       let {
//         list,
//         selectedData
//       } = this.data
//       selectedData = list.map(item => {
//         if (item.active) return item
//       })
//       this.triggerEvent('confirm', selectedData)
//     }
//   }
// })