TechnicianSelectPopup.js 6.36 KB
import {
  buildSelectArr,
  buildRatioMap,
  mapArtificerList,
  calcShowMainShareRatio,
  buildConfirmPayload,
  getItemKey
} from './technicianSelect.helpers.js'

const IMG_URL =
  'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'

Component({
  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        if (n) {
          let {
            copyArtificerList,
            artificerSelectObj,
            disabledSelectObj,
            deputyFlag,
            idKey
          } = this.data
          const selectedList = Array.isArray(artificerSelectObj)
            ? artificerSelectObj
            : artificerSelectObj
              ? [artificerSelectObj]
              : []
          const selectArr = buildSelectArr(artificerSelectObj, idKey)
          const ratioMap = buildRatioMap(selectedList, idKey)
          copyArtificerList = mapArtificerList(copyArtificerList, {
            artificerSelectObj,
            selectArr,
            ratioMap,
            idKey,
            disabledSelectObj,
            deputyFlag
          })
          const showMainShareRatio = calcShowMainShareRatio(copyArtificerList)
          this.setData({
            artificerList: wx.$util.deepClone(copyArtificerList),
            copyArtificerList,
            selectedTechnicianId: selectArr,
            searchValue: '',
            showMainShareRatio
          })
        }
      }
    },
    title: String,
    orderInfo: Object,
    artificerSelectObj: Object,
    copyArtificerList: Array,
    multiple: Boolean,
    disabledSelectObj: {
      type: Array
    },
    deputyFlag: Boolean,
    /** technicianId:工单页;uuid:repair/施工单 */
    idKey: {
      type: String,
      value: 'technicianId'
    }
  },

  data: {
    orderObj: {},
    IMG_URL,
    artificerList: [],
    searchValue: '',
    showMainShareRatio: 100
  },

  methods: {
    noop() {},
    changeShareRatio(e) {
      let { value } = e.detail
      let { index } = e.currentTarget.dataset
      let { artificerList, copyArtificerList, idKey } = this.data
      let v = parseInt(value, 10)
      if (isNaN(v)) v = 0
      if (v > 100) v = 100
      const sumOthers = (artificerList || [])
        .filter((i, idx) => i.active && idx !== index)
        .map(i => parseInt(i.shareRatio || 0, 10))
        .reduce((a, b) => a + (isNaN(b) ? 0 : b), 0)
      const allowed = Math.max(0, 100 - sumOthers)
      v = Math.min(v, allowed)
      artificerList[index].shareRatio = v
      const targetKey = getItemKey(artificerList[index], idKey)
      if (Array.isArray(copyArtificerList)) {
        const ci = copyArtificerList.findIndex(
          i => getItemKey(i, idKey) === targetKey
        )
        if (ci >= 0) copyArtificerList[ci].shareRatio = v
      }
      const showMainShareRatio = calcShowMainShareRatio(artificerList)
      this.setData({
        artificerList,
        copyArtificerList,
        showMainShareRatio
      })
    },
    search(e) {
      let { copyArtificerList } = wx.$util.deepClone(this.data)
      let { selectedTechnicianId, idKey } = this.data
      let { value } = e.detail
      const matchSelected = i => {
        const key = getItemKey(i, idKey)
        return selectedTechnicianId.includes(key)
      }
      if (!value) {
        copyArtificerList.map(i => {
          i.active = matchSelected(i)
          return i
        })
        this.setData({ artificerList: copyArtificerList })
      } else {
        let arr = []
        copyArtificerList.map(i => {
          i.active = matchSelected(i)
          if (i.staffName.indexOf(value) != -1) arr.push(i)
        })
        this.setData({ artificerList: arr })
      }
    },
    changeItem(e) {
      let { target } = e.currentTarget.dataset
      let { artificerList, multiple, deputyFlag, disabledSelectObj, idKey } =
        this.data
      let { copyArtificerList } = wx.$util.deepClone(this.data)
      if (target.disabled) return
      const targetKey = getItemKey(target, idKey)
      const updateList = list =>
        list.map(i => {
          const itemKey = getItemKey(i, idKey)
          const active =
            itemKey === targetKey ? !i.active : multiple ? i.active : false
          return { ...i, active, shareRatio: active ? i.shareRatio : '' }
        })
      artificerList = updateList(artificerList)
      copyArtificerList = updateList(copyArtificerList)
      const arr = copyArtificerList
        .filter(v => v.active)
        .map(v => getItemKey(v, idKey))
      if (deputyFlag) {
        if (arr.length === 2) {
          const disableIfNotSelected = i => ({
            ...i,
            disabled: !arr.includes(getItemKey(i, idKey))
          })
          artificerList = artificerList.map(disableIfNotSelected)
          copyArtificerList = copyArtificerList.map(disableIfNotSelected)
        } else {
          const isPresetDisabled = i =>
            disabledSelectObj?.findIndex(val => val.uuid == i.uuid) > -1
          const enableWithPreset = i => ({
            ...i,
            disabled: isPresetDisabled(i)
          })
          artificerList = artificerList.map(enableWithPreset)
          copyArtificerList = copyArtificerList.map(enableWithPreset)
        }
      }
      const showMainShareRatio = calcShowMainShareRatio(artificerList)
      this.setData({
        artificerList,
        copyArtificerList,
        selectedTechnicianId: multiple ? arr : [targetKey],
        showMainShareRatio
      })
    },
    cancel() {
      let thisPage = wx.$router.getPageInstance()
      thisPage.setData({ artificerSelectPopup: false })
      this.triggerEvent('cancel')
    },
    goMiniprogram() {
      wx.navigateToMiniProgram({ appId: 'wx2c28337db96364d2' })
    },
    async confirm() {
      let {
        copyArtificerList,
        artificerSelectObj,
        deputyFlag,
        idKey
      } = this.data
      let arr = copyArtificerList?.filter(v => v.active) || []
      if (!arr.length) {
        this.setData({ selectedTechnicianId: [] })
      }
      const result = buildConfirmPayload({
        artificerSelectObj,
        activeArr: arr,
        idKey,
        deputyFlag
      })
      if (!result.ok) {
        if (result.reason === 'empty') {
          return wx.$util.showToast('请选择技师')
        }
        if (result.reason === 'invalid_ratio') {
          return wx.$util.showToast('请填写副手技师分成比例')
        }
        return
      }
      this.triggerEvent('confirm', result.payload)
    }
  }
})