specialTaskApply.js 10.5 KB
import indexApi from '~/api/index/index'
import crewApi from '~/api/crew/index'
import dayjs from 'dayjs'

const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/supervisor-miniprogram/'

Page({
  data: {
    IMG_URL,
    navBarHeight: 84,
    pageTitle: '发起专项任务',
    readonly: false,
    isFullSucreen: false,

    businessList: [
      { label: '延保', value: 1 },
      { label: '权益', value: 3 },
      { label: '轻改', value: 2 }
    ],
    bizTypeOptions: [
      { itemName: '人员更换评估', itemValue: 'staff-replace-access' },
      { itemName: '商务政策更新', itemValue: 'policy-update-access' },
      { itemName: '流量问题改善', itemValue: 'traffic-optimize' }
    ],

    merchantOptions: [],
    staffOptions: [],
    operatorOptions: [],

    form: {
      bizType: '',
      categoryName: '',
      merchantId: '',
      merchantName: '',
      businessType: '',
      problemStaffList: [],
      regionOperatorPhone: '',
      regionOperatorName: '',
      description: ''
    },
    descriptionLength: 0,

    showSelectPopup: false,
    selectOptions: [],
    selectMultiple: false,
    selectValues: [],
    selectValue: null,
    selectTitle: '',
    selectPopupHeight: '55%',
    labelKey: 'itemName',
    valueKey: 'itemValue',
    selectScene: '',
    allowSearch: false
  },

  navbarReady(e) {
    const navBarHeight = e.detail?.height
    if (!navBarHeight) return
    this.setData({ navBarHeight })
  },

  onLoad(options) {
    this.setData({
      pageQuery: options || {},
      readonly: !!options?.id,
      pageTitle: options?.id ? '查看专项任务' : '发起专项任务'
    })
		this.getMerchantList()
		this.getAreaOperatorList()
    if (options?.id) {
      this.getDetail(options.id)
    }
  },

  back() {
    wx.navigateBack()
  },

  getMerchantList() {
    indexApi.getSupervisorMerchantList({}).then(res => {
      const { code, data, message } = res || {}
      if (code == 0) {
        const list = Array.isArray(data) ? data : []
        list.forEach(val => {
          val.itemName = val.merchantName
          val.itemValue = val.merchantId
        })
        this.setData({ merchantOptions: list })
      } else {
        wx.$util.showToast(message || '门店列表获取失败')
      }
    })
  },

  getStaffListByMerchant(merchantId) {
    if (!merchantId) {
      this.setData({ staffOptions: [] })
      return
		}
		let params = {
			pageNum: 1,
			pageSize: 9999,
			merchantIdList: [merchantId]
		}
    crewApi.getParticipantOptions(params).then(res => {
      const { code, data, message } = res || {}
      if (code == 0) {
        const list = Array.isArray(data) ? data : data?.list || []
        list.forEach(val => {
          val.itemName = val.staffName || val.name
          val.itemValue = val.staffPhone || val.phone
        })
        this.setData({ staffOptions: list })
      } else {
        wx.$util.showToast(message || '人员列表获取失败')
      }
    })
  },

  getAreaOperatorList() {
    indexApi.getAreaOperatorList({}).then(res => {
      const { code, data, message } = res || {}
      if (code == 0) {
        const list = Array.isArray(data) ? data : data?.list || []
        list.forEach(v => {
          v.itemName = v.name
          v.itemValue = v.phone
        })
        this.setData({ operatorOptions: list })
      } else {
        wx.$util.showToast(message || '区域运营获取失败')
      }
    })
  },

  getDetail(id) {
    indexApi.getSpecialTaskDetail({ id }).then(res => {
      const { code, data, message } = res || {}
      if (code != 0) {
        wx.$util.showToast(message || '详情获取失败')
        return
      }
      const detail = data || {}
      const bizType = detail.bizTypeAttr || detail.bizType || ''
      const categoryName = detail.categoryName || ''
      const merchantId = detail.merchantId || ''
      const merchantName = detail.merchantName || ''
      const businessType = Number(detail.businessType || detail.business || '') || ''
      const regionOperatorName = detail.regionOperatorName || ''
      const regionOperatorPhone = detail.regionOperatorPhone || ''

      const description = detail.description || ''
      this.setData({
        descriptionLength: description?.length || 0,
        form: {
          ...this.data.form,
          bizType: String(bizType),
          categoryName: categoryName || this.findcategoryName(String(bizType)),
          merchantId,
          merchantName,
          businessType,
          regionOperatorName,
          regionOperatorPhone,
          problemStaffList: detail?.problemStaffName?.split(',')?.map(val => {return {name: val}}),
          description
        }
      })
      this.getStaffListByMerchant(merchantId)
    })
  },

  findcategoryName(bizType) {
    const found = (this.data.bizTypeOptions || []).find(v => String(v.itemValue) === String(bizType))
    return found?.itemName || ''
  },

  openbizType() {
    if (this.data.readonly) return
    this.openPopup({
      scene: 'bizType',
      title: '任务类型',
      options: this.data.bizTypeOptions,
      labelKey: 'itemName',
      valueKey: 'itemValue',
      multiple: false,
      value: this.data.form.bizType || null,
      allowSearch: false
    })
  },

  openMerchant() {
    if (this.data.readonly) return
    this.openPopup({
      scene: 'merchant',
      title: '门店选择',
      options: this.data.merchantOptions,
      labelKey: 'itemName',
      valueKey: 'itemValue',
      multiple: false,
      value: [this.data.form.merchantId] || null,
      allowSearch: true
    })
  },

  openProblemSales() {
    if (this.data.readonly) return
    if (this.data.form.bizType !== 'staff-replace-access') return
    if (!this.data.form.merchantId) return wx.$util.showToast('请先选择归属门店')
    if (!this.data.staffOptions.length) return wx.$util.showToast('当前门店暂无可选人员')
    const selectedPhones = (this.data.form.problemStaffList || []).map(v => v.phone).filter(Boolean)
    this.openPopup({
      scene: 'problemSales',
      title: '问题驻店销售',
      options: this.data.staffOptions,
      labelKey: 'itemName',
      valueKey: 'itemValue',
      multiple: true,
      values: selectedPhones,
      allowSearch: true
    })
  },

  openOperator() {
    if (this.data.readonly) return
    if (!this.data.operatorOptions.length) return wx.$util.showToast('未配置区域运营,请联系总部处理')
    this.openPopup({
      scene: 'operator',
      title: '指定区域运营',
      options: this.data.operatorOptions,
      labelKey: 'itemName',
      valueKey: 'itemValue',
      multiple: false,
      value: [this.data.form.regionOperatorPhone] || null,
      allowSearch: true
    })
  },

  openPopup({ scene, title, options, multiple, value, values, labelKey, valueKey, allowSearch }) {
    this.setData({
      selectScene: scene,
      selectTitle: title,
      selectOptions: options || [],
      selectMultiple: !!multiple,
      selectValue: value !== undefined ? value : null,
      selectValues: Array.isArray(values) ? values : [],
      labelKey: labelKey || 'itemName',
      valueKey: valueKey || 'itemValue',
      allowSearch: !!allowSearch,
      showSelectPopup: true
    })
  },

  hideSelectPopup() {
    this.setData({ showSelectPopup: false })
  },

  handlePopupConfirm(e) {
    const { values, items } = e.detail || {}
    const scene = this.data.selectScene
    this.setData({ showSelectPopup: false })

    if (scene === 'bizType') {
      const val = values?.[0] || ''
      const name = items?.[0]?.itemName || ''
      this.setData({
        'form.bizType': val,
        'form.categoryName': name,
        'form.problemStaffList': val === 'staff-replace-access' ? this.data.form.problemStaffList : []
      })
      return
    }

    if (scene === 'merchant') {
      const val = values?.[0] || ''
      const name = items?.[0]?.itemName || ''
      this.setData({
        'form.merchantId': val,
        'form.merchantName': name,
        'form.problemStaffList': []
      })
      this.getStaffListByMerchant(val)
      return
    }

    if (scene === 'operator') {
      const val = values?.[0] || ''
      const name = items?.[0]?.itemName || ''
      this.setData({
        'form.regionOperatorPhone': val,
        'form.regionOperatorName': name
      })
      return
    }

    if (scene === 'problemSales') {
      const list = (items || []).map(v => ({
        name: v.itemName,
        phone: v.itemValue
      }))
      this.setData({
        'form.problemStaffList': list
      })
    }
  },

  removeProblemSales(e) {
    const idx = Number(e.currentTarget?.dataset?.index)
    if (Number.isNaN(idx)) return
    const next = (this.data.form.problemStaffList || []).slice()
    next.splice(idx, 1)
    this.setData({
      'form.problemStaffList': next
    })
  },

  changeBusiness(e) {
    if (this.data.readonly) return
    const val = e.currentTarget?.dataset?.value
    this.setData({
      'form.businessType': val
    })
  },

  descriptionInput(e) {
    if (this.data.readonly) return
    let val = e.detail?.value || ''
    if (val.length > 500) val = val.substring(0, 500)
    this.setData({
      'form.description': val,
      descriptionLength: val.length
    })
  },

  validate() {
    const f = this.data.form || {}
    if (!f.bizType) return '请选择任务类型'
    if (!f.merchantId) return '请选择归属门店'
		if (!f.businessType) return '请选择归属业务'
		if (f.bizType === 'staff-replace-access') {
      if (!Array.isArray(f.problemStaffList) || !f.problemStaffList.length) return '请选择问题驻店销售'
    }
    if (!f.regionOperatorPhone) return '请选择指定区域运营'
    if (!String(f.description || '').trim()) return '请输入任务事项描述'
    return ''
  },

  submit() {
    const msg = this.validate()
    if (msg) return wx.$util.showToast(msg)

    const f = this.data.form || {}

    const params = {
      bizType: f.bizType,
      categoryName: f.categoryName,
      merchantId: f.merchantId,
      merchantName: f.merchantName,
			businessType: f.businessType,
			problemStaffList: f.problemStaffList?.map(v => {return {name: v.name, phone: v.phone}}),
			regionOperatorList: [{name: f.regionOperatorName, phone: f.regionOperatorPhone}],
      description: f.description
    }
    wx.$util.showLoading()
    indexApi
      .createSpecialTask(params)
      .then(res => {
        wx.$util.hideLoading()
        const { code, message } = res || {}
        if (code == 0) {
          wx.$util.showToast('操作成功')
          this.back()
        } else {
          wx.$util.showToast(message || '提交失败')
        }
      })
      .catch(() => {
        wx.$util.hideLoading()
      })
  }
})