allocateStoreDialog.vue 11.2 KB
<!-- 分配门店 -->
<template>
  <baseDialog :dialog-config="dialogConfig" :is-show.sync="visible"
    @handleClick="handleAddConfirm">
    <div class="detail-dialog">
      <glsx-combi-form class="edit-form-container"
                    ref="editFormRef"
                    label-width="110px"
                    :is-block="true"
                    form-ref="editChildRef"
                    :form-obj="infoForm"
                    :form-data="staffFormData"
                    :form-rules="formRules">
        <template slot="staffName">
          <span>{{ infoForm.staffName }}</span>
        </template>
        <template slot="phoneNumber">
          <span>{{ infoForm.staffPhone }}</span>
        </template>
        <template slot="merchantList">
          <el-select v-model="infoForm.merchantId" placeholder="请选择归属门店" :disabled="isDisabled"
                            style="width: 432px" remote reserve-keyword @visible-change="visibleChange"
                          :remote-method="remoteMethod" @change="changeMerchant" v-el-select-loadmore="interestsLoadmore" filterable>
            <template v-if="baseData.merchantList && !baseData.merchantList.length && loadedFlag">
              <el-option :label="'暂无数据'" :value="null" disabled></el-option>
            </template>
            <el-option v-for="(item, index) in baseData.merchantList" :key="index" :label="`${item.shopMerchantName}${item.shopMerchantId == -1 ? '' : item.shopMerchantId}`" :value="item.shopMerchantId"></el-option>
          </el-select>
        </template>
        <template slot="staffSceneLabel">
          <el-checkbox-group v-model="infoForm.staffSceneLabel" @change="changeStaffSceneLabel">
            <el-checkbox v-for="item in baseData.staffSceneLabelList" :key="item.confId" :label="item.confId">{{item.confValue}}</el-checkbox>
          </el-checkbox-group>
        </template>
      </glsx-combi-form>
    </div>
  </baseDialog>
</template>

<script>
import { assignStoreAccountApi, getStoreListBySceneApi, getStoreParkListApi } from '@api/resident'
import { staffSceneLabelList } from '@/common/constants'
import { Util, deepClone } from '@/common/util'
import { baseConfig } from '@/main'
export default {
  name: 'AllocateStoreDialog',
  data() {
    return {
      yqMerchantObj: baseConfig.yqMerchantObj,
      isDisabled: false,
      dialogConfig: {
        title: '分配门店',
        width: '720px'
      },
      staffFormData: [
      {
          label: '员工姓名',
          name: 'staffName',
          addHtml: 'staffName'
        },
        {
          label: '手机号',
          name: 'phoneNumber',
          addHtml: 'phoneNumber'
        },
        {
          label: '职级',
          name: 'jobType',
          type: 'select',
          optionList: [],
          optionId: 'confId',
          optionLabel: 'confValue',
          optionsName: 'jobTypeList',
          changeAttr: (val) => {
            this.getOptions('jobType', val)
          }
        },
        {
          label: '适配业务',
          name: 'staffSceneLabel',
          addHtml: 'staffSceneLabel'
        },
        {
          label: '归属门店',
          name: 'merchantId',
          addHtml: 'merchantList'
        },
        {
          label: '登录园区店',
          name: 'yqMerchantId',
          type: 'select',
          optionList: [],
          optionId: 'yqMerchantId',
          optionLabel: 'yqMerchantIdName',
          optionsName: 'yqMerchantList',
          noDataText: '请选择归属门店'
        }
      ],
      infoForm: {
        staffSceneLabel: []
      },
      formRules: {
        merchantId: [
          { required: true, message: '请选择归属门店', trigger: ['blur', 'change'] }
        ],
        yqMerchantId: [
          { required: true, message: '请选择登录园区店', trigger: ['blur', 'change'] }
        ],
        jobType: [
          { required: true, message: '请选择职级', trigger: ['blur', 'change'] }
        ],
        staffSceneLabel: [
          { required: true, message: '请选择适配业务', trigger: ['blur', 'change'] }
        ]
      },
      baseData: {
        merchantList: [],
        yqMerchantList: [],
        jobTypeList: [],
        staffSceneLabelList: staffSceneLabelList
      },
      currentPage: 1,
      pageSize: 50,
      merchantIdName: '',
      loadedFlag: false
    }
  },
  props: {
    allocateStoreVisible: {
      type: Boolean,
      default: false
    },
    detail: {
      type: Object,
      default: () => ({})
    },
    jobTypeList: {
      type: Array,
      default: () => {}
    }
  },
  directives: {
    "el-select-loadmore": {
      bind(el, binding) {
        setTimeout(() => {
          const SELECTWRAP_DOM = el.querySelector(
            ".el-select-dropdown .el-select-dropdown__wrap"
          )
          SELECTWRAP_DOM.addEventListener("scroll", function () {
            const condition =
              this.scrollHeight - this.scrollTop <= this.clientHeight
            if (condition) {
              binding.value()
            }
          })
        }, 1000)
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.allocateStoreVisible
      },
      set(newVal) {
        this.$emit('dialogClose', { flag: newVal, attrName: 'allocateStoreVisible' })
      }
    }
  },

  mounted() {
    this.infoForm = {
      ...this.infoForm,
      staffName: this.detail.staffName,
      staffPhone: this.detail.staffPhone,
    }
    this.getOptions()
  },
  methods: {
    getOptions(optionsName, val) {
      this.baseData.jobTypeList = deepClone(this.jobTypeList)
      if (optionsName == 'jobType') {
        let index = this.staffFormData.findIndex(item => item.optionsName == 'yqMerchantList')
        const options = {
          '0': {
            yqMerchantList: [this.yqMerchantObj['hzgl']],
            merchantList: [{ shopMerchantId: '-1', shopMerchantName: '无' }],
            yqMerchantId: this.yqMerchantObj['hzgl'].yqMerchantId,
            merchantId: '-1',
            isDisabled: true
          },
          '7': {
            yqMerchantList: [this.yqMerchantObj['glsk']],
            merchantList: [{ shopMerchantId: '-1', shopMerchantName: '无' }],
            yqMerchantId: this.yqMerchantObj['glsk'].yqMerchantId,
            merchantId: '-1',
            isDisabled: true
          },
          '8': {
            yqMerchantList: [this.yqMerchantObj['glsk']],
            merchantList: [{ shopMerchantId: '-1', shopMerchantName: '无' }],
            yqMerchantId: this.yqMerchantObj['glsk'].yqMerchantId,
            merchantId: '-1',
            isDisabled: true
          },
          '9': {
            yqMerchantList: [this.yqMerchantObj['glsk']],
            merchantList: [{ shopMerchantId: '-1', shopMerchantName: '无' }],
            yqMerchantId: this.yqMerchantObj['glsk'].yqMerchantId,
            merchantId: '-1',
            isDisabled: true
          },
          default: {
            yqMerchantId: '',
            merchantId: '',
            isDisabled: false
          }
        }
        let { yqMerchantList, merchantList, yqMerchantId, merchantId, isDisabled } = options[val] || options.default
        this.baseData['yqMerchantList'] = yqMerchantList
        this.baseData['merchantList'] = merchantList
        this.$set(this.infoForm, 'yqMerchantId', yqMerchantId)
        this.$set(this.infoForm, 'merchantId', merchantId)
        this.$set(this.staffFormData[index], 'isDisabled', isDisabled)
        this.isDisabled = isDisabled
        this.clearValidate()
      }
      this.staffFormData.forEach((item, index) => {
        if (item.type === "select") {
          this.$set(
            this.staffFormData[index],
            "optionList",
            this.baseData[item.optionsName]
          )
        }
      })
    },
    clearValidate() {
      this.$nextTick(() => {
        this.$refs.editFormRef?.$refs.editChildRef?.clearValidate()
      })
    },
    changeStaffSceneLabel() {
      if (![0, 7, 8, 9].includes(this.infoForm.jobType)) {
        this.$set(this.infoForm, 'merchantId', '')
        this.$set(this.infoForm, 'yqMerchantId', '')
        this.baseData.merchantList = []
        this.baseData.yqMerchantList = []
        this.getOptions()
        this.clearValidate()
      }
    },
    visibleChange(data) {
      // 获取焦点重新渲染下拉框数据
      if (data) {
        if (!this.infoForm.staffSceneLabel?.length) return this.$message.warning('请先选择适配业务')
        this.merchantIdName = ""
        this.currentPage = 1
        this.baseData.merchantList = []
        this.getSelectingList()
      }
    },
    // 获取所属门店数据
    remoteMethod(query) {
      if (!this.infoForm.staffSceneLabel?.length) return this.baseData.merchantList = []
      this.merchantIdName = query
      this.currentPage = 1
      this.baseData.merchantList = []
      this.getSelectingList()
    },
    // 获取更多门店
    interestsLoadmore() {
      setTimeout(() => {
        this.currentPage = this.currentPage + 1
        if (this.allPages < this.currentPage) {
          this.currentPage = this.allPages
          return
        }
        this.getSelectingList()
      }, 10)
    },
    // 获取门店列表
    getSelectingList() {
      let params = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        sceneLabel: this.infoForm?.staffSceneLabel?.join(","),
        merchantIdName: this.merchantIdName
      }
      getStoreListBySceneApi(params).then(res => {
        this.baseData.merchantList = this.baseData.merchantList.concat(res.list)
        this.allPages = res.total
        this.loadedFlag = true
      })
    },
    changeMerchant(val) {
      this.$set(this.infoForm, 'merchantName', this.baseData.merchantList.find(item => item.shopMerchantId === val)?.shopMerchantName)
      this.$set(this.infoForm, 'yqMerchantId', '')
      this.getYqMerchantList(val)
    },

    getYqMerchantList(val) {
      let params = {
        pageSize: 9999,
        currentPage: 1,
        merchantId: val,
        sceneLabel: this.infoForm?.staffSceneLabel?.join(",")
      }
      getStoreParkListApi(params).then(res => {
        res?.list?.forEach(item => {
          item.yqMerchantIdName = `${item.yqMerchantName}${item.yqMerchantId}`
        })
        this.baseData.yqMerchantList = res?.list || []
        this.getOptions()
      })
    },

    handleAddConfirm() {
      this.$refs.editFormRef?.$refs.editChildRef.validate((valid) => {
        if (valid) {
          let params = {
            staffName: this.detail.staffName,
            staffPhone: this.detail.staffPhone,
            jobType: this.infoForm.jobType,
            staffSceneLabel: this.infoForm.staffSceneLabel?.join(','),
            merchantId: this.infoForm.merchantId == '-1' ? '' : this.infoForm.merchantId,
            merchantName: this.infoForm.merchantName,
            yqMerchantId: this.infoForm.yqMerchantId,
            yqMerchantName: this.baseData.yqMerchantList.find(item => item.yqMerchantId === this.infoForm.yqMerchantId)?.yqMerchantName,
          }
          assignStoreAccountApi(params).then(res => {
            this.$emit('dialogClose', { flag: true, attrName: 'creatStaffVisible' })
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>