addExpenseDialog.vue 6.9 KB
<!-- 新增支出 -->
<template>
  <div>
    <d2-dialog
      :title="'新增支出'"
      :is-visible="expenseVisible"
      width="700px"
      @closeDialog="handleAdd"
      @confirmDialog="handleAddConfirm"
    >
      <glsx-combi-form class="edit-form-container"
                    ref="editFormRef"
                    label-width="110px"
                    :is-block="true"
                    form-ref="editChildRef"
                    :form-obj="expenseForm"
                    :form-data="formData"
                    :form-rules="formRules">
        <template slot="staff">
          <el-select v-model="expenseForm.staffPhone" placeholder="请选择员工"
                            style="width: 432px" remote reserve-keyword @visible-change="visibleChange"
                           :remote-method="remoteMethod" v-el-select-loadmore="interestsLoadmore" @change="handleStaffChange" filterable>
            <el-option v-for="item in baseData.staffList" :key="item.staffPhone" :label="item.staffName" :value="item.staffPhone"></el-option>
          </el-select>
        </template>
        <template slot="syncUnit"><span class="unit">元</span></template>
      </glsx-combi-form>
    </d2-dialog>
  </div>
</template>

<script>
import { mapActions } from "vuex"
import { limitPrice } from '@/common/format'
import { addIncentivePoolApi, getLevelStaffListApi } from '@/api/resident'
import { staffLevelList } from '@/common/constants.js'
import { filterStr } from '../../inStoreManage/util.js'
export default {
  name: "AddExpenseDialog",
  data() {
    return {
      expenseForm: {
        staffPhone: "",
        jobType: "",
        merchantName: "",
        amount: "",
        remark: ""
      },
      formData: [
        {
          label: '员工',
          name: 'staffName',
          addHtml: 'staff',
          rules: [{ required: true, message: '请选择员工', trigger: 'change' }]
        },
        {
          label: '员工手机号',
          name: 'staffPhone',
          type: 'input',
          isDisabled: true
        },
        {
          label: '员工职级',
          name: 'jobTypeStr',
          type: 'input',
          isDisabled: true
        },
        {
          label: '归属门店',
          name: 'merchantName',
          type: 'input',
          isDisabled: true
        },
        {
          label: '支出金额',
          name: 'amount',
          type: 'input',
          width: '136px',
          inputFn: (e) => {
            e = e.replace(/[^\d.]/g, '')
            let oldValue = this.expenseForm['oldamount'] || ''
            this.$set(this.expenseForm, 'amount', limitPrice(e, oldValue, 6, 2, 0))
            this.$set(this.expenseForm, 'oldamount', this.expenseForm['amount'])
          },
          suffixSlot: 'syncUnit'
        },
        {
          label: '备注',
          name: 'remark',
          type: 'input',
          inputType: 'textarea',
          rows: 4,
          maxlength: 100,
          showLimit: true
        }
      ],
      formRules: {
        amount: [
          { required: true, message: "请输入支出金额", trigger: "change" },
          {
            required: true,
            trigger: 'blur',
            validator: (rule, value, callback) => {
              (+value > +this.expensePoolInfo.totalBalance) ? callback(new Error('不得超过激励池总额')) : callback()
            }
          }
        ],
        remark: [
          { required: true, message: "请输入备注", trigger: "blur" }
        ]
      },
      // 基本数据
      baseData: {
        staffList: []
      },
      staffName: "",
      currentPage: 1,
      pageSize: 20
    }
  },

  props: {
    expenseVisible: {
      type: Boolean,
      default: false
    },
    expensePoolInfo: {
      type: Object,
      default: () => {}
    }
  },

  watch: {
    expenseVisible(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs.addExpenseForm?.resetFields()
        })
      }
    }
  },

  directives: {
    "el-select-loadmore": {
      bind(el, binding) {
        setTimeout(() => {
          const SELECTWRAP_DOM = el.querySelector(
            '.el-select-dropdown .el-select-dropdown__wrap'
          )
          if (SELECTWRAP_DOM) {
            SELECTWRAP_DOM.addEventListener('scroll', function () {
              const condition =
                this.scrollHeight - this.scrollTop <= this.clientHeight
              if (condition) {
                binding.value()
              }
            })
          }
        }, 500)
      }
    }
  },

  mounted() {
  },

  methods: {
    ...mapActions({
      get: "d2admin/base/get"
    }),
    visibleChange(data) {
      // 获取焦点重新渲染下拉框数据
      if (data) {
        this.staffName = ""
        this.currentPage = 1
        this.baseData.staffList = []
        this.getSelectingList()
      }
    },
    // 获取配置职级的员工
    remoteMethod(query) {
      this.staffName = query
      this.currentPage = 1
      this.baseData.staffList = []
      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,
        staffName: this.staffName
      }
      getLevelStaffListApi(params).then(res => {
        this.baseData.staffList = this.baseData.staffList.concat(res.list || [])
        this.allPages = res.pages
      })
    },
    // 选择员工 将附带信息赋值到输入框
    handleStaffChange(val) {
      this.baseData.staffList.forEach(item => {
        if (item.staffPhone === val) {
          this.$set(this.expenseForm, 'staffPhone', item.staffPhone)
          this.$set(this.expenseForm, 'jobTypeStr', filterStr(item.jobType, staffLevelList, item.hireType))
          this.$set(this.expenseForm, 'merchantName', item.merchantName)
          this.$set(this.expenseForm, 'staffName', item.staffName)
          this.$set(this.expenseForm, 'jobType', item.jobType)
        }
      })
    },
    handleAdd() {
      this.$emit("expenseClose")
    },
    handleAddConfirm() {
      this.$refs.editFormRef?.$refs.editChildRef?.validate((valid) => {
        if (valid) {
          let params = {
            staffPhone: this.expenseForm?.staffPhone,
            staffName: this.expenseForm?.staffName,
            jobType: this.expenseForm?.jobType,
            expensePrice: this.expenseForm?.amount,
            remark: this.expenseForm?.remark
          }
          addIncentivePoolApi(params).then(res => {
            this.$message.success('添加成功')
            this.$emit("expenseClose", true)
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.unit{
  margin-left: 4px;
}
</style>