PageScreen.js 8.38 KB
// pages/tabWorkOrder/components/OrderScreen/OrderScreen.js
import { IMG_URL, SCENELIST, COOPERATIONMODELIST, TRSFFICSTATUSLIST } from '~/utils/constantData'
import CustomerApi from '~/api/customerApi/index'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    userInfo: Object,
    navBarHeight: String,
    show: {
      type: Boolean,
      observer: function (n, o) {
        if (n) {
          this.setViewData()
          let { userInfo } = this.data
          if (userInfo.loginRole == 1) {
            // 总监获取销售人员数据
            this.getSalesList()
            // 总监获取省份数据
            this.getAreaData()
          }
          const page = wx.$router.getPageInstance()
          const criteria = (this.data.currentTab === 1 ? page.data?.storeSearchObj : page.data?.searchObj) || {}
          this.applySelectedCriteria(criteria)
        } else {
          this.setData({
            x: this.data.windowWidth
          })
        }
      }
    },
    currentTab: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL,
    form: {
      businessTypeList: [{
        name: '全部',
        active: true,
        value: ''
      }, ...SCENELIST],
      cooperationModelList: [{
        name: '全部',
        active: true,
        value: ''
      }, ...COOPERATIONMODELIST],
      trafficStatusList: [{
        name: '全部',
        active: true,
        value: ''
      }, ...TRSFFICSTATUSLIST],
      provinces: [],
      salesStaffList: [],
      selectedMainBrand: []
    },
    salesStaffShowMore: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    applySelectedCriteria(criteria = {}) {
      const toVal = (item) => {
        // 统一取值优先级:value > id > staffId > code > name
        return item?.value ?? item?.id ?? item?.staffId ?? item?.code ?? item?.name
      }
      const setSingleActive = (list = [], selected) => {
        const selectedVal = selected ? toVal(selected) : undefined
        return (list || []).map((opt, idx) => {
          const isAll = idx === 0 // “全部”项
          const match = selectedVal !== undefined && toVal(opt) === selectedVal
          return { ...opt, active: match ? true : (isAll && selectedVal === undefined) }
        })
      }
      const setMultiActive = (list = [], selectedArr = []) => {
        const selectedVals = Array.isArray(selectedArr) ? selectedArr.map(toVal).filter(v => v !== undefined) : []
        const next = (list || []).map((opt, idx) => {
          const isAll = idx === 0
          const match = selectedVals.includes(toVal(opt))
          return { ...opt, active: match ? true : false }
        })
        const anySelected = next.some((opt, idx) => idx !== 0 && opt.active)
        // 如果多选为空,则激活“全部”
        next[0] = { ...next[0], active: !anySelected }
        return next
      }
  
      // 业务类型(多选)
      const businessTypeList = setMultiActive(this.data.form.businessTypeList, criteria.businessTypeList)
  
      // 合作模式 / 流量情况 / 省份 / 销售人员(单选)
      const cooperationModelList = setSingleActive(this.data.form.cooperationModelList, criteria.cooperationModelList)
      const trafficStatusList = setSingleActive(this.data.form.trafficStatusList, criteria.trafficStatusList)
      const provinces = setSingleActive(this.data.form.provinces, criteria.provinces)
      const salesStaffList = setSingleActive(this.data.form.salesStaffList, criteria.salesStaffList)
  
      // 主营品牌(数组直接回填)
      const selectedMainBrand = Array.isArray(criteria.selectedMainBrand) ? criteria.selectedMainBrand : []
  
      this.setData({
        'form.businessTypeList': businessTypeList,
        'form.cooperationModelList': cooperationModelList,
        'form.trafficStatusList': trafficStatusList,
        'form.provinces': provinces,
        'form.salesStaffList': salesStaffList,
        'form.selectedMainBrand': selectedMainBrand,
        isReset: false
      })
    },
    setViewData() {
      let { navBarHeight } = this.data
      wx.$api.getViewInfo(
        '.btn-box',
        res => {
          this.setData({
            height: navBarHeight,
            btnHeight: res[0].height
          })
        },
        this
      )
    },
    // 选择主营品牌
    chooseCarBrand() {
      this.setData({
        carBrandPopupShow: true
      })
    },
    getSalesList() {
      CustomerApi.getSalesPersonList({}).then(res => {
        res?.forEach(val => {
          val.value = val.userId,
          val.name = val.userName
        })
        res.unshift({
          name: '全部',
          active: true,
          value: ''
        })
        this.setData({
          'form.salesStaffList': res
        }, () => {
          const page = wx.$router.getPageInstance()
          const criteria = (this.data.currentTab === 1 ? page.data?.storeSearchObj : page.data?.searchObj) || {}
          this.applySelectedCriteria(criteria)
        })
      })
    },
    getAreaData() {
      CustomerApi.getAreaTreeList({}).then(res => {
        const data = res?.districts[0]?.districts
        data.unshift({
          name: '全部',
          active: true,
          value: ''
        })
        data?.forEach(v => {
          v.value = v.code
        })
        this.setData({
          'form.provinces': data
        }, () => {
          const page = wx.$router.getPageInstance()
          const criteria = (this.data.currentTab === 1 ? page.data?.storeSearchObj : page.data?.searchObj) || {}
          this.applySelectedCriteria(criteria)
        })
      })
    },
    showMore(e) {
      let { type } = e.currentTarget.dataset
      let prop = type
      this.setData({
        [prop]: !this.data[type]
      })
    },
    choose(e) {
      let { index, listName } = e.currentTarget.dataset
      let target = this.data.form[listName]
      // 多选
      if (listName === 'businessTypeList') {
        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 { businessTypeList, cooperationModelList, trafficStatusList, provinces, salesStaffList } = this.data.form
      const setActiveStatus = (list) => {
        return list?.map((item, index) => ({
          ...item,
          active: index === 0
        }));
      };
      this.setData({
        'form.businessTypeList': setActiveStatus(businessTypeList),
        'form.cooperationModelList': setActiveStatus(cooperationModelList),
        'form.trafficStatusList': setActiveStatus(trafficStatusList),
        'form.provinces': setActiveStatus(provinces),
        'form.salesStaffList': setActiveStatus(salesStaffList),
        'form.selectedMainBrand': [],
        isReset: true
      })
    },
    brandConfirm() {
      let brandMoudle = this.selectComponent('#car-brand')
      this.setData({
        'form.selectedMainBrand': brandMoudle && brandMoudle.handleConfirm() || []
      })
      this.hideBrandPopup()
    },
  
    hideBrandPopup() {
      this.setData({
        carBrandPopupShow: !this.data.carBrandPopupShow
      })
    },
  
    handleChooseBrand() {
      // wx.$router.go('packageMine.carBrandList')
      this.hideBrandPopup()
    },
    confirm() {
      let { form, isReset, currentTab } = this.data
      let obj = {}
      Object.keys(form).forEach(e => {
        let arr = []
        form[e]?.map(i => {
          if (i.active) {
            obj[e] = i
            if (e == 'businessTypeList') {
              arr.push(i)
              obj[e] = arr
            }
          }
        })
      })
      obj.selectedMainBrand = form.selectedMainBrand
      let page = wx.$router.getPageInstance()
      if (isReset) {
        obj = {}
      }
      const currentTarget = currentTab == 0 ? 'searchObj' : 'storeSearchObj'
      page.setData({
        [currentTarget]: obj
      })
      page.back()
      page.search()
    }
  },
  
  attached() {
    wx.$api.getSystemInfo(info => {
      let { windowWidth } = info
      this.setData({
        x: windowWidth,
        windowWidth
      })
    })
  }
})