performanceDeductionDialog.vue 22.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
<!-- 绩效工资扣减规则 -->
<template>
  <div class="deduction-dialog" v-if="visible">
    <baseDialog
      :dialog-config="dialogConfig"
      :is-show.sync="visible"
      class="accountDailog"
      @handleClick="handleConfirm"
      @closeDialog="handleClose"
    >
      <el-button type="primary" icon="el-icon-plus" plain @click="addConfig"
        >新增配置</el-button
      >
      <div class="wrap">
        <el-form :model="form" :rules="rules" ref="formRef">
          <template v-for="(config, configIndex) in form.configList">
            <div class="content" :key="configIndex">
              <div class="item">
                <!-- 月底产值为0的规则组 -->
                <div class="rule-group">
                  <h4 class="group-title">若月底产值为0</h4>
                  <div
                    v-for="(item, index) in config.itemList.filter(
                      (i) => i.assessmentType === 1
                    )"
                    :key="'zero-' + index"
                    class="rule-item"
                  >
                    <el-form-item
                      label="当考勤打卡"
                      :prop="`configList.${configIndex}.itemList.${getItemIndex(
                        configIndex,
                        item.id
                      )}`"
                      :rules="rules.attendanceRange"
                    >
                      <el-input
                        v-model="item.minAssessmentValue"
                        placeholder="最小值整数(包含)"
                        style="width: 150px"
                        @blur="validateNumber(item, 'minAssessmentValue', 30)"
                      ></el-input>

                      <span class="range-separator">-</span>
                      <el-input
                        v-model="item.maxAssessmentValue"
                        placeholder="最大值整数(不包含)"
                        style="width: 170px"
                        @blur="validateNumber(item, 'maxAssessmentValue', 31)"
                      ></el-input>
                      <span class="unit-text">天范围内,</span>
                    </el-form-item>

                    <el-form-item
                      label="绩效工资扣减比例"
                      :prop="`configList.${configIndex}.itemList.${getItemIndex(
                        configIndex,
                        item.id
                      )}.performanceSalarDeductionRatio`"
                      :rules="rules.deductionRatio"
                    >
                      <el-input
                        v-model="item.performanceSalarDeductionRatio"
                        placeholder="请输入"
                        style="width: 140px"
                        @blur="validatePercentage(item)"
                      >
                        <template slot="append">%</template>
                      </el-input>
                    </el-form-item>
                    <div class="btn-group">
                      <div
                        v-if="index > 0"
                        type="text"
                        @click="removeItem(configIndex, item.id)"
                        class="delete-btn"
                      >
                        删除
                      </div>
                      <div
                        v-if="
                          index ==
                          config.itemList.filter((i) => i.assessmentType === 1)
                            .length -
                            1
                        "
                        type="text"
                        @click="addItem(1, configIndex)"
                        class="add-btn"
                      >
                        新增
                      </div>
                    </div>
                  </div>
                </div>

                <!-- 月底产值不为0的规则组 -->
                <div class="rule-group">
                  <h4 class="group-title">若月底产值不为0</h4>
                  <div
                    v-for="(item, index) in config.itemList.filter(
                      (i) => i.assessmentType === 2
                    )"
                    :key="'nonzero-' + index"
                    class="rule-item"
                  >
                    <el-form-item
                      label="当个人月度触客总数(含售前+售后触客)"
                      :prop="`configList.${configIndex}.itemList.${getItemIndex(
                        configIndex,
                        item.id
                      )}`"
                      :rules="rules.financeRange"
                    >
                      <el-input
                        v-model="item.minAssessmentValue"
                        placeholder="最小值整数(包含)"
                        style="width: 150px"
                        @blur="
                          validateNumber(item, 'minAssessmentValue', 999999)
                        "
                      ></el-input>
                      <span class="range-separator">-</span>
                      <el-input
                        v-model="item.maxAssessmentValue"
                        placeholder="最大值整数(不包含)"
                        style="width: 170px"
                        @blur="
                          validateNumber(item, 'maxAssessmentValue', 999999)
                        "
                      ></el-input>
                      <span class="unit-text">台数范围内,</span>
                    </el-form-item>

                    <el-form-item
                      label="绩效工资扣减比例"
                      :prop="`configList.${configIndex}.itemList.${getItemIndex(
                        configIndex,
                        item.id
                      )}.performanceSalarDeductionRatio`"
                      :rules="rules.deductionRatio"
                    >
                      <el-input
                        v-model="item.performanceSalarDeductionRatio"
                        placeholder="请输入"
                        style="width: 140px"
                        @blur="validatePercentage(item)"
                      >
                        <template slot="append">%</template>
                      </el-input>
                    </el-form-item>
                    <div class="btn-group">
                      <div
                        v-if="index > 0"
                        type="text"
                        @click="removeItem(configIndex, item.id)"
                        class="delete-btn"
                      >
                        删除
                      </div>
                      <div
                        v-if="
                          index ==
                          config.itemList.filter((i) => i.assessmentType === 2)
                            .length -
                            1
                        "
                        type="text"
                        @click="addItem(2, configIndex)"
                        class="add-btn"
                      >
                        新增
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="staff-wrap">
                <el-form-item
                  label="应用到以下员工:"
                  :prop="`configList.${configIndex}.staffList`"
                  :rules="rules.staffList"
                >
                  <el-button
                    size="medium"
                    icon="el-icon-plus"
                    @click="handleSelectStaff(configIndex)"
                    >选择员工</el-button
                  >
                </el-form-item>
                <div
                  class="tag-wrap"
                  v-if="config.staffList && config.staffList.length > 0"
                >
                  <el-tag
                    @close="removeStaff(configIndex, index)"
                    v-for="(staff, index) in config.staffList"
                    :key="configIndex + '-' + index"
                    closable
                  >
                    {{ staff.staffName }}
                  </el-tag>
                </div>
              </div>
              <div
                class="deleteConfig"
                @click="deleteConfig(configIndex)"
                v-if="configIndex > 0"
              >
                删除配置
              </div>
            </div>
            <el-divider></el-divider>
          </template>
        </el-form>
      </div>
    </baseDialog>
    <emp-dialog
      :relateStoreVisible="groupVisible"
      :config="{}"
      ref="selectTagForm"
      :storeSelected="
        form.configList.length
          ? form.configList[this.configIndex] &&
            form.configList[this.configIndex].staffList
          : []
      "
      :selectingList="selectingList"
      @handleRelateStoreClose="closeStaffDialog"
      @handleRelateStoreConfirm="confirmStaff"
    ></emp-dialog>
  </div>
</template>

<script>
import {
  getPerformanceSalaryConfigAPI,
  updatePerformanceSalaryConfigAPI
} from "@api/resident"
import { isEmpty, filterStr } from "../util.js"
import empDialog from "../components/empDialog"
export default {
  name: "BasicSalaryDialog",
  components: { empDialog },
  data() {
    return {
      dialogConfig: {
        title: "绩效工资扣减规则",
        width: "1200px"
      },
      groupVisible: false,
      selectingList: [],
      configIndex: 0,
      form: {
        configList: []
      },
      rules: {
        //考勤打卡
        attendanceRange: [
          {
            validator: this.validateMinMax,
            type: "attendance",
            trigger: "blur"
          }
        ],
        //触客总数
        financeRange: [
          { validator: this.validateMinMax, type: "finance", trigger: "blur" }
        ],
        //绩效扣减比例
        deductionRatio: [
          { required: true, message: "请输入扣减比例", trigger: "blur" }
          // { validator: this.validatePercentage, trigger: "blur" }
        ],
        staffList: [
          { required: true, message: "请选择员工", trigger: "change" }
        ]
      }
    }
  },
  props: {
    deductionVisible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    visible: {
      get() {
        return this.deductionVisible
      },
      set(newVal) {
        this.$emit("dialogClose", {
          flag: newVal,
          attrName: "deductionVisible"
        })
      }
    }
  },
  mounted() {
    this.getPerformanceSalaryConfig()
  },
  methods: {
    //删除配置
    deleteConfig(deleteConfig) {
      console.log(deleteConfig)
      this.form.configList.splice(deleteConfig, 1)
    },
    getPerformanceSalaryConfig() {
      let params = { currentPage: 1, pageSize: 9999 }
      getPerformanceSalaryConfigAPI(params)
        .then((res) => {
          if (res.list && res.list.length) {
            this.form.configList = res.list
          } else {
            this.addConfig()
          }
          // this.addConfig()
          console.log(res, 1111111111111)
        })
        .catch((err) => {})
    },
    closeStaffDialog() {
      this.groupVisible = false
      this.selectingList = []
    },
    confirmStaff(val) {
      this.selectingList = []
      this.form.configList[this.configIndex].staffList = val
      if (val.length > 0) {
        this.$refs.formRef.clearValidate(
          `configList.${this.configIndex}.staffList`
        )
      }

      this.groupVisible = false
    },
    removeStaff(index, staffIndex) {
      this.form.configList[index].staffList.splice(staffIndex, 1)
    },
    handleSelectStaff(index) {
      // 获取到已选择过的员工
      this.configIndex = index
      this.getSelectingList(index)
      this.groupVisible = true

      this.$refs.selectTagForm.clearData && this.$refs.selectTagForm.clearData()
      this.$refs.selectTagForm.getSelectingList &&
        this.$refs.selectTagForm.getSelectingList()
    },
    getSelectingList(configIndex) {
      this.form.configList.forEach((item, index) => {
        if (index !== configIndex)
          item.staffList.forEach((item1) => {
            this.selectingList.push(item1)
          })
      })
    },
    addConfig() {
      if (this.form.configList.length >= 9) {
        this.$message.error("最多添加10个配置")
      }
      this.form.configList.push({
        itemList: [
          {
            performanceSalarDeductionRatio: "",
            maxAssessmentValue: "",
            assessmentType: 1,
            minAssessmentValue: "",
            id: Date.now() + 1
          },
          {
            performanceSalarDeductionRatio: "",
            maxAssessmentValue: "",
            assessmentType: 1,
            minAssessmentValue: "",
            id: Date.now() + 2
          },
          {
            performanceSalarDeductionRatio: "",
            maxAssessmentValue: "",
            assessmentType: 2,
            minAssessmentValue: "",
            id: Date.now() + 3
          }
        ],
        staffList: []
      })
    },
    // 获取item在数组中的实际索引
    getItemIndex(getItemIndex, id) {
      return this.form.configList[getItemIndex].itemList.findIndex(
        (item) => item.id === id
      )
    },
    removeItem(configIndex, id) {
      console.log(configIndex, id)
      const index = this.form.configList[configIndex].itemList.findIndex(
        (item) => item.id === id
      )
      if (index > 0) {
        this.form.configList[configIndex].itemList.splice(index, 1)
      }
    },
    addItem(type, configIndex) {
      const newId = Date.now()
      this.form.configList[configIndex].itemList.push({
        performanceSalarDeductionRatio: "",
        maxAssessmentValue: "",
        assessmentType: type,
        minAssessmentValue: "",
        id: newId
      })
    },
    getParams() {
      removeStaff
      const result = this.form.configList.map((configItem) => {
        const itemList = configItem.itemList.map((assessmentItem) => ({
          assessmentType: assessmentItem.assessmentType,
          maxAssessmentValue: assessmentItem.maxAssessmentValue,
          minAssessmentValue: assessmentItem.minAssessmentValue,
          performanceSalarDeductionRatio:
            assessmentItem.performanceSalarDeductionRatio
          // id: assessmentItem.id
        }))

        const staffList = configItem.staffList.map((staffItem) => ({
          staffName: staffItem.staffName,
          staffPhone: staffItem.staffPhone
        }))

        return {
          itemList,
          staffList
        }
      })

      return result
    },
    // async handleConfirm() {
    //   console.log(this.$refs.formRef)
    //   this.$refs.formRef.validate((valid, invalidFields) => {
    //     console.log("验证结果:", valid)
    //     console.log("失败字段:", invalidFields)
    //     if (valid) {
    //       let configList = this.form.configList
    //       updatePerformanceSalaryConfigAPI({
    //         configList
    //       }).then((res) => {
    //         console.log(res)
    //       })
    //     }
    //   })
    //   console.log(this.getParams())
    //   let configList = this.getParams()
    //   updatePerformanceSalaryConfigAPI({
    //     configList
    //   }).then((res) => {
    //     console.log(res)
    //   })
    //   this.$emit("dialogClose")
    // },
    async handleConfirm() {
      console.log(this.$refs.formRef)
      try {
        const valid = await this.$refs.formRef.validate()
        console.log("验证结果:", valid) // 应该输出 true
        if (valid) {
          const configList = this.getParams()
          await updatePerformanceSalaryConfigAPI({ configList })
          this.$emit("dialogClose")
        }
      } catch (error) {
        console.log("验证失败:", error)
        // 这里可以处理验证失败的逻辑(如高亮错误字段)
      }
    },
    handleClose() {
      this.$emit("dialogClose")
    },
    // 验证数字输入
    validateNumber(item, field, maxLimit = 31) {
      let value = item[field]
      value = parseInt(value)
      if (isNaN(value)) value = ""
      // 限制最大值
      if (parseInt(value) > maxLimit) {
        value = ""
        this.$message.warning(`输入值不能超过 ${maxLimit}`)
      }
      if (parseInt(value) < 0) {
        value = ""
        this.$message.warning(`输入值不能小于0`)
      }
      this.$set(item, field, value)

      // 自动修正 min <= max(仅对 maxAssessmentValue 生效)
      if (
        field === "maxAssessmentValue" &&
        parseInt(item.minAssessmentValue) >= parseInt(value)
      ) {
        this.$set(item, "maxAssessmentValue", "")
        // this.$message.warning("")
      }
    },
    // 验证百分比输入
    validatePercentage(item) {
      let value = item.performanceSalarDeductionRatio
      // 去除非数字和小数点

      // 限制范围
      let numValue = parseFloat(value)
      if (isNaN(numValue)) numValue = ""
      // if (numValue < 1) numValue = 1
      // if (numValue > 100) numValue = 100
      // 限制最大值
      if (parseInt(value) > 100) {
        numValue = ""
        this.$message.warning(`输入值不能超过100`)
      }
      if (parseInt(value) < 1) {
        numValue = ""
        this.$message.warning(`输入值不能小于1`)
      }
      // value = parseInt(value) || ""
      this.$set(item, "performanceSalarDeductionRatio", numValue.toString())
    },

    // 新增 validateMinMax 方法用于统一校验
    // validateMinMax(rule, value, callback) {
    //   // console.log(rule)
    //   const path = rule.fullField.split(".")
    //   const configIndex = parseInt(path[1])
    //   const itemIndex = parseInt(path[3])
    //   const item = this.form.configList[configIndex]?.itemList[itemIndex]
    //   // console.log(configIndex, itemIndex, item)
    //   if (
    //     !item ||
    //     (!item.minAssessmentValue && !item.minAssessmentValue == 0) ||
    //     (!item.maxAssessmentValue && item.maxAssessmentValue == 0)
    //   ) {
    //     callback(new Error("请输入有效范围"))
    //     return
    //   }

    //   const min = parseInt(item.minAssessmentValue)
    //   const max = parseInt(item.maxAssessmentValue)

    //   if (min >= max) {
    //     callback(new Error("最大值必须大于最小值"))
    //   } else {
    //     // console.log(444444444444)
    //     callback()
    //   }
    // },
    validateMinMax(rule, value, callback) {
      const path = rule.fullField.split(".")
      const configIndex = parseInt(path[1])
      const itemIndex = parseInt(path[3])

      const config = this.form.configList[configIndex]
      const item = config?.itemList[itemIndex]

      if (!item) {
        callback(new Error("找不到对应字段"))
        return
      }

      const min = parseInt(item.minAssessmentValue)
      const max = parseInt(item.maxAssessmentValue)

      // 基础验证:最小值和最大值是否有效
      if (isNaN(min) || isNaN(max)) {
        callback(new Error("请输入有效的数字范围"))
        return
      }

      if (min >= max) {
        callback(new Error("最大值必须大于最小值"))
        return
      }

      // 获取当前类型的所有区间
      const currentType = item.assessmentType
      console.log(currentType, "currentType")
      const sameTypeItems = config.itemList.filter(
        (i) => i.assessmentType === currentType
      )
      console.log(sameTypeItems, "sameTypeItems")

      // 检查是否有重叠区间
      const currentItemRange = [min, max]
      for (let i = 0; i < sameTypeItems.length; i++) {
        const otherItem = sameTypeItems[i]
        // 跳过自己
        if (otherItem.id === item.id) continue

        const otherMin = parseInt(otherItem.minAssessmentValue)
        const otherMax = parseInt(otherItem.maxAssessmentValue)

        if (isNaN(otherMin) || isNaN(otherMax)) continue

        const otherRange = [otherMin, otherMax]
        console.log(otherRange, "otherRange")

        if (this.isOverlapping(currentItemRange, otherRange)) {
          callback(new Error("区间与其他配置重叠,请调整"))
          return
        }
      }

      callback()
    },
    isOverlapping(range1, range2) {
      const [min1, max1] = range1
      const [min2, max2] = range2
      return Math.max(min1, min2) < Math.min(max1, max2)
    },
    // 范围校验器
    validateRange(rule, value, callback) {
      const index = rule.fullField.split(".")[3]
      const item = this.form.configList[0].itemList[index]

      if (!item.minAssessmentValue || !item.maxAssessmentValue) {
        callback(new Error("请输入有效范围"))
        return
      }

      const min = parseInt(item.minAssessmentValue)
      const max = parseInt(item.maxAssessmentValue)

      if (min >= max) {
        callback(new Error("最大值必须大于最小值"))
      } else {
        callback()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.deduction-dialog {
  ::v-deep .el-dialog__header {
    border-bottom: 1px solid #e6e6e6;
  }
  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #666;
    font-weight: 600;
    font-size: 20px;
  }
  ::v-deep .dialog-sub-title {
    color: #333333;
    font-family: PingFangSC-Medium;
    font-size: 20px;
    font-weight: 600;
  }
  ::v-deep .el-form-item__label {
    color: #333333;
  }
  ::v-deep .el-form-item__content {
    display: flex;
  }
  ::v-deep .el-divider--horizontal {
    margin: 16px 0;
  }
  ::v-deep
    .el-form-item.is-required:not(.is-no-asterisk)
    > .el-form-item__label:before {
    color: #f5f7fa;
  }
}
.wrap {
  padding: 16px 0;
  .content {
    box-sizing: border-box;
    width: 100%;
    background: #f5f7fa;
    padding: 16px 20px;
    position: relative;
    .deleteConfig {
      position: absolute;
      right: 20px;
      top: 16px;
      width: 88px;
      height: 32px;
      border-radius: 4px;
      opacity: 1;
      border: 1px solid #d1d1d1;
      background: #e8e8e8;
      color: #2a58ad;
      font-size: 14px;
      text-align: center;
      line-height: 32px;
    }
  }

  .rule-group {
    color: #333333;
  }
  .group-title {
    margin: 0 0 14px 0;
    font-weight: 500;
  }
  .rule-item {
    display: flex;
    align-items: center;
  }
  .rule-content {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
  }
  .range-separator {
    margin: 0 8px;
    color: #606266;
  }
  .unit-text {
    margin-left: 4px;
  }
  .btn-group {
    margin-left: 14px;
    display: flex;
    align-items: center;
    color: #2a58ad;
    font-size: 14px;
    font-weight: 400;
    margin-bottom: 16px;
  }
  .delete-btn {
    margin-right: 6px;
  }
  .staff-wrap {
    margin-top: 8px;
    .tag-wrap {
      width: 580px;
      min-height: 42px;
      padding: 8px;
      border-radius: 4px;
      opacity: 1;
      border: 1px solid #e5e5e5;
      background: #ffffff;
      margin-left: 132px;
      box-sizing: border-box;
      .el-tag {
        margin-right: 8px;
        margin-bottom: 4px;
      }
    }
  }
}
</style>