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

  /**
   * 组件的属性列表
   */
  properties: {
    userInfo: Object,
    navBarHeight: String,
    show: {
      type: Boolean,
      observer: function (n, o) {
        if (n) {
          this.setViewData()
        } else {
          this.setData({
            x: this.data.windowWidth
          })
        }
      }
    },
    status: {
      type: String,
      observer: function (newVal) {
        if (this._isInitialized) {
          // 初始化完成后,正常执行状态更新逻辑
          this.setOrderStatusList(newVal)
        } else {
          // 初始化前,暂存status值
          this._pendingStatus = newVal
        }
      }
    },
    /** 与列表查询条件同步,用于从首页进施工单等场景下勾选当前技师 */
    presetTechnicianUuidList: {
      type: Array,
      value: [],
      observer: function (newVal) {
        this.applyPresetTechnicianFromParent(newVal)
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL,
    maxDate: new Date().format('yyyy-MM-dd'),
    orderTimeList: [{
      name: '全部',
      active: true
    }, {
      name: '今日',
      orderStartDate: new Date().format('yyyy-MM-dd'),
      orderEndDate: new Date().format('yyyy-MM-dd')
    }, {
      name: '本月',
      orderStartDate: new Date(new Date().setDate(1)).format('yyyy-MM-dd'),
      orderEndDate: new Date().format('yyyy-MM-dd')
    }, {
      orderStartDate: '',
      orderEndDate: '',
      type: 'custom'
    }],
    statusList: [{
      name: '全部',
      active: true,
      value: ''
    },
    {
      name: '已施工',
      value: '1'
    },
    {
      name: '待施工',
      value: '2'
    },
    {
      name: '客户返工',
      value: '3'
    }],
    selectTechnicianList: []
  },

  attached() {
    wx.$util.getSystemInfo(info => {
      let { windowWidth } = info
      this.setData({
        x: windowWidth,
        windowWidth,
        isFullSucreen: wx.$util.isFullSucreen(true),
        userInfo: wx.$util.getUserInfo()
      })
    })
    wx.showLoading()
    // 获取技师数据
    api.getTechnicianList({
      yqMerchantId: wx.$util.getUserInfo()?.isSuper ? '' : wx.$util.getStorageSync('dj_boutique_user_info')?.merchantId
    }).then(respone => {
      wx.hideLoading()
      let arr = [{
        staffName: '全部',
        active: true,
        value: []
      }]
      if (respone?.code == 0) {
        this.setData(
          {
            artificerList: respone.data || [],
            'form.technicianUuidList': arr.concat(respone.data || [])
          },
          () => {
            this.applyPresetTechnicianFromParent(
              this.properties.presetTechnicianUuidList
            )
          }
        )
      }
    })
    this.init()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    applyPresetTechnicianFromParent(uuids) {
      if (wx.$hasPermission.canSelectMultiStore(this.data.userInfo)) return
      const uuidsNorm = Array.isArray(uuids) ? uuids.filter(Boolean) : []
      const list = this.data.form?.technicianUuidList
      if (!list?.length) return
      const match = (a, b) =>
        a != null && b != null && String(a) === String(b)
      if (!uuidsNorm.length) {
        list.forEach((item, idx) => {
          item.active = idx === 0
        })
      } else {
        let any = false
        list.forEach((item, idx) => {
          if (idx === 0) {
            item.active = false
          } else {
            const on = uuidsNorm.some(u => match(u, item.uuid))
            item.active = on
            if (on) any = true
          }
        })
        if (!any) list[0].active = true
      }
      this.setData({
        'form.technicianUuidList': list,
        isReset: false
      })
    },
    setViewData() {
      let { navBarHeight } = this.data
      wx.$util.getViewInfo(
        '.btn-box',
        res => {
          this.setData({
            height: navBarHeight,
            btnHeight: res.height
          })
        },
        this
      )
    },
    init() {
      let { orderTimeList, statusList } = this.data
      this.setData({
        ['form.orderTimeList']: wx.$util.deepClone(orderTimeList),
        ['form.statusList']: wx.$util.deepClone(statusList)
      })
      this._isInitialized = true
      if (this._pendingStatus !== undefined) {
        this.setOrderStatusList(this._pendingStatus)
        this._pendingStatus = undefined
      }
    },
    setOrderStatusList(status) {
      let { statusList } = this.data
      const copyStatus = wx.$util.deepClone(statusList)
      copyStatus.map((e, index) => {
        e.active = e.value == status
      })
      this.setData({
        ['form.statusList']: copyStatus
      })
    },
    rangeSelect(e) {
      let { index, name } = e.currentTarget.dataset
      let data = this.data.form[name]
      let {
        date: { start, end }
      } = e.detail
      data?.map(i => (i.active = false))
      let obj = {
        active: true,
        orderStartDate: start,
        orderEndDate: end,
        type: 'custom'
      }
      data[index] = obj
      this.setData({
        [`form.${name}`]: data,
        isReset: false
      })
    },
    // 日期组件
    showCalendar(e) {
      let calendarModule = this.selectComponent('#orderCalendar')
      calendarModule.showCalendar && calendarModule.showCalendar(e)
    },
    technicianChoose() {
      this.setData({
        'form.selectTechnicianList': []
      })
    },
    showTechnicianSelectPopup() {
      this.setData({artificerSelectPopup: true})
    },
    cancelTechnician() {
      this.setData({artificerSelectPopup: false})
    },
    changeArtificer(e) {
      this.setData({
        ['form.selectTechnicianList']: e?.detail?.selectedTechnicians || [],
        artificerSelectPopup: false
      })
    },
    showMore(e) {
      let { technicianShowMore } = this.data
      this.setData({
        technicianShowMore: !technicianShowMore
      })
    },
    choose(e) {
      let { index, listName } = e.currentTarget.dataset
      let target = this.data.form[listName]
      if (listName == 'technicianUuidList') {
        if (index == 0) {
          target.forEach(i => i.active = false)
          target[index].active = true
        } else {
          target[index].active = !target[index].active
          target[0].active = false
          if (!target.filter(val => val.active).length) target[0].active = true
        }
      } else {
        target.forEach(i => {
          i.active = false
        })
        target[index].active = true
      }
      this.setData({
        [`form.${listName}`]: target,
        isReset: false
      })
    },
    reset() {
      let { technicianUuidList, selectTechnicianList } = this.data.form
      technicianUuidList?.map((val, index) => {
        val.active = false
        if (index == 0) val.active = true
      })
      selectTechnicianList?.map((val, index) => {
        val.active = false
        if (index == 0) val.active = true
      })
      this.setData({
        'form.technicianUuidList': technicianUuidList,
        'form.selectTechnicianList': [],
        isReset: true
      })
      this._pendingStatus = undefined
      this.init()
    },
    confirm() {
      let { form, userInfo, isReset } = this.data
      let obj = {}
      Object.keys(form).forEach(e => {
        let arr = []
        form[e]?.map(i => {
          if (i.active) {
            let { orderStartDate, orderEndDate, value, uuid } = i
            obj[e] = value
            // 施工开始时间
            if (orderStartDate && orderEndDate) {
              orderStartDate = `${orderStartDate} 00:00:00`
              orderEndDate = `${orderEndDate} 23:59:59`
              obj.startTime = orderStartDate
              obj.endTime = orderEndDate
            }
            // 技师
            if (e == 'selectTechnicianList' || e == 'technicianUuidList') {
              arr.push(uuid)
              obj['technicianUuidList'] = arr
            }
          }
        })
      })
      let page = wx.$router.getPageInstance()
      if (isReset) {
        obj = {}
      }
      page.back()
      page.searchScreening(obj)
    }
  }
})