index.js 3.73 KB
// tabRepairOrder/components/servicePopup/index.js
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
import api from '../../api'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function(n) {
        if (n) {
          let { selected } = this.data
          this.setData({
            searchValue: '',
            selectedData: wx.$util.deepClone(selected)
          }, () => {
            this.getList(true)
          })
        }
      }
    },
    selected: {
      type: Array,
      observer: function(n) {
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL,
    servicesList: [],
    copyServicesList: [],
    selectedData: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handelSearchInput(e) {
      this.setData({
        searchValue: e.detail.value
      })
    },
    // 获取服务项目数据
    getList(firstFlag) {
      let { searchValue } = this.data
      api.getServiceItemList({serviceItemsName: searchValue}).then(res => {
        let { code, data, message } = res
        if (code == 0) {
          let {selectedData} = this.data
          const selectedIds = selectedData?.reduce((set, item) => {
            item?.detailList?.forEach(({ serviceItemsId }) => set.add(serviceItemsId))
            return set
          }, new Set()) ?? new Set()
          data?.forEach(val => {
            val.title = `${val.categoryName}-${val.itemName}`
            val.detailList = val.serviceItemList?.map(item => ({
              ...item,
              serviceItemsId: item.id,
              active: selectedIds.has(item.id)
            }))
          })
          this.setData({
            servicesList: data
          })
          if (firstFlag) {
            this.setData({
              copyServicesList: wx.$util.deepClone(data || [])
            })
          }
        } else {
          wx.$util.showToast(message)
        }
      })
    },
    search() {
      this.getList()
    },
    // 服务项目选择
    handleChecked(e) {
      let { target } = e.currentTarget.dataset
      let { servicesList, copyServicesList } = this.data
      servicesList?.map(val => {
        val?.detailList?.map(v => {
          if (v.id == target.id) v.active = !v.active
          return v
        })
        return val
      })
      copyServicesList?.map(val => {
        val?.detailList?.map(v => {
          if (v.id == target.id) v.active = !v.active
          return v
        })
        return val
      })
      const filteredServicesList = copyServicesList?.map(val => {
        if (!val?.detailList) return null
        return {
          ...val,
          detailList: val.detailList.filter(v => v.active === true)
        }
      }).filter(val => val && val.detailList.length > 0) || []
      this.setData({
        servicesList,
        copyServicesList,
        selectedData: filteredServicesList
      })
    },
    // 展开收起服务项目
    handleShowItem(e) {
      let {index} = e.currentTarget.dataset
      let {servicesList} = this.data
      this.setData({
        [`servicesList[${index}].showItemFlag`]: !servicesList[index].showItemFlag
      })
    },
    cancel() {
      this.triggerEvent('cancel')
    },
    confirm() {
      let {servicesList, copyServicesList} = this.data
      const result = copyServicesList.map(item => ({
        ...item,
        detailList: item?.detailList.filter(child => child.active)
      })).filter(item => item.detailList?.length > 0)
      const activeNum = result.reduce((total, item) => {
        return total + (item?.detailList?.length || 0);
      }, 0);
      if (activeNum <= 0) return wx.$util.showToast('请选择服务项目')
      this.triggerEvent('confirm', result)
    }
  }
})