batchModifySalaryDialog.vue 5.58 KB
<!-- 批量修改薪资占比 -->
<template>
  <glsk-form-dialog
    ref="addResumeDialog"
    v-if="isUpdateVisible"
    :selectVisible="isUpdateVisible"
    :dialogConfig="isUpdateDialogConfig"
    label-width="150px"
    defaultWidth="290px"
    :formRules="isUpdateRules"
    :editData="isUpdateForm"
    :editFormData="isUpdateFormData"
    :baseData="baseData"
    @handleClose="closeDialogVisible"
    @handleConfirm="handleUpdateConfirm"
    :slotArr="slotArr"
    class="batch-modify-salary-dialog"
  >
    <template slot="effectiveMonth">
      <el-date-picker
        v-model="isUpdateForm.effectiveMonth"
        type="month"
        placeholder="选择月份"
        :picker-options="monthPickerOptions"
        size="small"
        :clearable="false"
        value-format="yyyy-MM"
        class="effectiveMonth"
      >
      </el-date-picker>
    </template>
  </glsk-form-dialog>
</template>
<script>
import { updateBatchSalary } from "@/api/resident"
import dayjs from "dayjs"
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    staffPhoneList: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    isUpdateVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit("update:visible", val)
      }
    }
  },
  data() {
    return {
      slotArr: ["effectiveMonth"],
      monthPickerOptions: {
        // 禁用当前月之前的所有月份(仅允许当月及未来)
        disabledDate: (date) => dayjs(date).isBefore(dayjs(), "month")
      },
      isUpdateDialogConfig: {
        title: "批量修改薪资占比",
        width: "480px",
        okText: "确定"
      },
      isUpdateForm: {
        basicSalaryProportion: "",
        performanceSalaryProportion: "",
        effectiveMonth: ""
      },
      isUpdateFormData: [
        {
          label: "基本工资占比",
          name: "basicSalaryProportion",
          type: "input",
          maxlength: "3",
          placeholder: "请输入",
          append: "%",
          isClear: true
        },
        {
          label: "绩效工资占比",
          name: "performanceSalaryProportion",
          type: "input",
          maxlength: "3", // 联动仅需三位,限制为 1-99
          placeholder: "请输入",
          append: "%",
          isClear: true
        },
        {
          label: "生效月份",
          name: "effectiveMonth",
          addHtml: "effectiveMonth"
        }
      ],
      isUpdateRules: {
        basicSalaryProportion: [
          { required: true, message: "请输入", trigger: "blur" },
          {
            validator: (rule, value, callback) =>
              this.validatePercent(value, callback),
            trigger: "blur"
          }
        ],
        performanceSalaryProportion: [
          { required: true, message: "请输入", trigger: "blur" },
          {
            validator: (rule, value, callback) =>
              this.validatePercent(value, callback),
            trigger: "blur"
          }
        ],
        effectiveMonth: [
          { required: true, message: "请选择", trigger: "change" }
        ]
      },
      baseData: {},
      // 联动更新锁,避免互相更新时的递归触发
      _linkageLock: false
    }
  },
  methods: {
    closeDialogVisible() {
      this.isUpdateVisible = false
    },
    handleUpdateConfirm(data) {
      let params = {
        staffPhoneList: this.staffPhoneList,
        ...data
      }
      updateBatchSalary(params)
        .then((res) => {
          if (res) {
            this.$message.success("修改成功")
          }

          this.$emit("refreshTable")
        })
        .catch(() => {})
        .finally(() => {
          this.isUpdateVisible = false
        })
    },
    // 仅允许 1-100 的正整数;返回 null 表示空或无效输入
    sanitizePercent(val) {
      const s = String(val == null ? "" : val).replace(/\D/g, "")
      if (!s) return null
      let n = parseInt(s, 10)
      if (isNaN(n)) return null
      if (n < 0) n = 0
      if (n > 100) n = 100
      return n
    },
    // 表单校验规则:1-99 的正整数
    validatePercent(value, callback) {
      const v = this.sanitizePercent(value)
      if (v === null) {
        callback(new Error("请输入0-100的正整数"))
        return
      }
      // 联动后总和恒为 100,这里只需校验单项合法
      callback()
    }
  },
  watch: {
    // 基本工资占比变更时联动绩效工资占比
    "isUpdateForm.basicSalaryProportion"(val) {
      if (this._linkageLock) return
      const v = this.sanitizePercent(val)
      if (v === null) {
        this._linkageLock = true
        this.isUpdateForm.performanceSalaryProportion = ""
        this._linkageLock = false
        return
      }
      this._linkageLock = true
      this.isUpdateForm.basicSalaryProportion = v
      this.isUpdateForm.performanceSalaryProportion = 100 - v
      this._linkageLock = false
    },
    // 绩效工资占比变更时联动基本工资占比
    "isUpdateForm.performanceSalaryProportion"(val) {
      if (this._linkageLock) return
      const v = this.sanitizePercent(val)
      if (v === null) {
        this._linkageLock = true
        this.isUpdateForm.basicSalaryProportion = ""
        this._linkageLock = false
        return
      }
      this._linkageLock = true
      this.isUpdateForm.performanceSalaryProportion = v
      this.isUpdateForm.basicSalaryProportion = 100 - v
      this._linkageLock = false
    }
  }
}
</script>
<style lang="scss" scoped>
.batch-modify-salary-dialog{
  ::v-deep .el-date-editor.el-input{
    width: 290px!important;
  }
}
</style>