punch-apply-rule-setting-panel.vue 13.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
<template>
  <div class="punch-apply-rule-panel">
    <el-card class="section-card section-card--setting" shadow="never" v-loading="loading">
      <div slot="header" class="section-card__header">
        <div class="section-card__title-block">
          <div class="section-card__eyebrow">额度设置</div>
          <div class="section-card__title">补卡额度与月度上限</div>
          <div class="section-card__desc">
            先确定每月免费次数与可选的总次数上限;超出免费次数后,再按下方「分档扣款」表计费。
          </div>
        </div>
        <div class="section-card__actions">
          <el-button type="primary" size="small" :loading="saveSettingLoading" @click="handleSaveSetting">保存设置</el-button>
        </div>
      </div>
      <el-form ref="settingFormRef" :model="settingForm" label-width="130px" size="small" class="setting-form">
        <el-form-item label="每月免费次数" prop="freeCount" :rules="[{ required: true, message: '请输入免费次数', trigger: 'blur' }]">
          <el-input-number
            v-model="settingForm.freeCount"
            :min="0"
            :precision="0"
            controls-position="right"
            class="input-num--narrow"
          />
        </el-form-item>
        <el-form-item label="启用月度次数上限">
          <el-switch v-model="settingForm.limitEnabled" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item v-if="Number(settingForm.limitEnabled) === 1" label="月度总次数上限" prop="limitCount"
          :rules="limitCountRules">
          <el-input-number
            v-model="settingForm.limitCount"
            :min="1"
            :precision="0"
            controls-position="right"
            class="input-num--narrow"
            placeholder="必填"
          />
        </el-form-item>
        <div class="setting-flow-hint" role="note">
          <span class="setting-flow-hint__label">与分档关系</span>
          <span class="setting-flow-hint__text">{{ freeTierRelationText }}</span>
        </div>
        <el-form-item label="变更原因" prop="changeReason" class="setting-form__reason">
          <el-input v-model.trim="settingForm.changeReason" type="textarea" :rows="2" maxlength="200" show-word-limit
            placeholder="可选,说明本次调整原因" />
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="section-card section-card--tier" shadow="never">
      <div slot="header" class="section-card__header">
        <div class="section-card__title-block">
          <div class="section-card__eyebrow">分档列表</div>
          <div class="section-card__title">超出免费后的扣款规则</div>
          <div class="section-card__desc">区间按「最小值包含、最大值不包含」,口径与「忘记打卡扣款规则」一致。</div>
        </div>
        <div class="section-card__actions">
          <el-button type="primary" size="small" @click="openTierDialog()">新增分档</el-button>
        </div>
      </div>
      <NCC-table v-loading="listLoading" :data="tierList" border size="mini" height="300">
        <el-table-column prop="minCount" label="最小次数(含)" width="112" align="left">
          <template slot-scope="scope">
            <span :title="String(scope.row.minCount)">{{ scope.row.minCount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="最大次数(不含)" width="130" align="left">
          <template slot-scope="scope">
            <span>{{ renderUpperValue(scope.row.maxCount, '次') }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="deductPerTime" label="每次扣款" width="110" align="left" />
        <el-table-column prop="expressionText" label="展示说明" min-width="170" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.expressionText || '无' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="140" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.remark || '无' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="left" fixed="right">
          <template slot-scope="scope">
            <div class="table-ops">
              <el-button type="text" size="small" class="table-ops__btn" @click="openTierDialog(scope.row)">编辑</el-button>
              <el-button type="text" size="small" class="table-ops__btn" @click="openTierHistory(scope.row)">历史</el-button>
              <el-button type="text" size="small" class="table-ops__btn danger-text" @click="handleTierDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </NCC-table>
      <div v-if="!listLoading && !tierList.length" class="section-empty">
        <el-empty :image-size="60" description="暂无补卡分档扣款规则" />
      </div>
      <pagination :hidden="!pagination.total" :total="pagination.total" :page.sync="query.currentPage"
        :limit.sync="query.pageSize" @pagination="loadTierList" />
    </el-card>

    <attendance-config-item-dialog ref="tierItemDialog" @submit="onTierDialogSubmit" />
    <attendance-config-history-dialog ref="tierHistoryDialog" />
  </div>
</template>

<script>
import { getAttendanceConfigList, getPunchApplySettingInfo, saveAttendanceConfig, savePunchApplySetting, deleteAttendanceConfig } from '@/api/extend/attendanceSetting'
import AttendanceConfigHistoryDialog from './attendance-config-history-dialog'
import AttendanceConfigItemDialog from './attendance-config-item-dialog'

const MODULE_TIER = 'punchApplyRule'

export default {
  name: 'PunchApplyRuleSettingPanel',
  components: {
    AttendanceConfigHistoryDialog,
    AttendanceConfigItemDialog
  },
  data() {
    return {
      loading: false,
      saveSettingLoading: false,
      listLoading: false,
      settingForm: {
        freeCount: 3,
        limitEnabled: 0,
        limitCount: null,
        changeReason: ''
      },
      tierList: [],
      query: {
        currentPage: 1,
        pageSize: 10,
        keyword: ''
      },
      pagination: {
        total: 0
      },
      limitCountRules: [
        { required: true, message: '请输入月度上限次数', trigger: 'blur' },
        { type: 'number', min: 1, message: '须大于 0', trigger: 'blur' }
      ]
    }
  },
  created() {
    this.bootstrap()
  },
  computed: {
    freeTierRelationText() {
      const free = Number(this.settingForm.freeCount)
      const n = Number.isFinite(free) ? free : 0
      const limitOn = Number(this.settingForm.limitEnabled) === 1
      const limit = this.settingForm.limitCount
      let base
      if (n <= 0) {
        base = '当前无免费次数,当月补卡从第 1 次起即按下方分档扣款。'
      } else {
        base = `当月第 1~${n} 次补卡免费;自第 ${n + 1} 次起按下方分档逐次扣款。`
      }
      if (limitOn) {
        if (limit != null && limit !== '' && Number(limit) >= 1) {
          return `${base} 已启用月度总次数上限(${limit} 次),超出后将不可再补卡。`
        }
        return `${base} 已启用月度总次数上限:请填写有效的上限次数。`
      }
      return `${base} 未启用月度总次数上限时,不限制总次数(仍按分档计费)。`
    }
  },
  methods: {
    renderUpperValue(value, suffix) {
      return value === null || value === undefined || value === '' ? '无上限' : `${value}${suffix || ''}`
    },
    async bootstrap() {
      this.loading = true
      try {
        await Promise.all([this.loadSetting(), this.loadTierList()])
      } finally {
        this.loading = false
      }
    },
    async loadSetting() {
      const res = await getPunchApplySettingInfo()
      const data = res.data || {}
      this.settingForm = {
        freeCount: Number(data.freeCount) || 0,
        limitEnabled: Number(data.limitEnabled) || 0,
        limitCount: data.limitCount != null && data.limitCount !== '' ? Number(data.limitCount) : null,
        changeReason: ''
      }
    },
    handleSaveSetting() {
      this.$refs.settingFormRef.validate(async valid => {
        if (!valid) return
        if (Number(this.settingForm.limitEnabled) === 1 &&
          (this.settingForm.limitCount === null || this.settingForm.limitCount === undefined || this.settingForm.limitCount < 1)) {
          this.$message.warning('开启月度上限时请填写有效的上限次数')
          return
        }
        this.saveSettingLoading = true
        try {
          await savePunchApplySetting({
            freeCount: this.settingForm.freeCount,
            limitEnabled: this.settingForm.limitEnabled,
            limitCount: Number(this.settingForm.limitEnabled) === 1 ? this.settingForm.limitCount : null,
            changeReason: this.settingForm.changeReason || ''
          })
          this.$message.success('保存成功')
          this.settingForm.changeReason = ''
          this.$emit('saved')
        } finally {
          this.saveSettingLoading = false
        }
      })
    },
    async loadTierList() {
      this.listLoading = true
      try {
        const res = await getAttendanceConfigList({
          moduleType: MODULE_TIER,
          keyword: this.query.keyword,
          currentPage: this.query.currentPage,
          pageSize: this.query.pageSize
        })
        const data = res.data || {}
        this.tierList = data.list || []
        this.pagination = {
          total: data.pagination ? data.pagination.total : 0
        }
      } finally {
        this.listLoading = false
      }
    },
    openTierDialog(row) {
      this.$refs.tierItemDialog.open({
        moduleType: MODULE_TIER,
        title: `${row && row.id ? '编辑' : '新增'}补卡超出分档规则`,
        data: row ? { ...row } : null,
        mode: row && row.id ? 'edit' : 'create'
      })
    },
    async onTierDialogSubmit({ moduleType, data, done }) {
      try {
        if (moduleType !== MODULE_TIER) return
        await saveAttendanceConfig({
          moduleType: MODULE_TIER,
          changeReason: data.changeReason,
          data: this.buildTierSavePayload(data)
        })
        this.$message.success('保存成功')
        await this.loadTierList()
        this.$emit('saved')
        done()
      } catch (e) {
        done(false)
      }
    },
    buildTierSavePayload(data) {
      const payload = { ...data }
      delete payload.changeReason
      return payload
    },
    openTierHistory(row) {
      if (!row || !row.id) {
        this.$message.warning('请先保存后再查看历史')
        return
      }
      this.$refs.tierHistoryDialog.open({
        moduleType: MODULE_TIER,
        bizId: row.id,
        title: this.buildTierTitle(row)
      })
    },
    buildTierTitle(row) {
      const min = row.minCount
      const max = row.maxCount
      if (max === null || max === undefined || max === '') {
        return `补卡分档 ≥${min}次`
      }
      return `补卡分档 ${min}-${max}次`
    },
    async handleTierDelete(row) {
      if (!row || !row.id) return
      try {
        const { value } = await this.$prompt('请输入删除原因(可选)', '删除确认', {
          confirmButtonText: '确认删除',
          cancelButtonText: '取消',
          inputPlaceholder: '例如:规则调整',
          inputPattern: /^.{0,200}$/,
          inputErrorMessage: '删除原因不能超过200个字符'
        })
        await deleteAttendanceConfig({
          moduleType: MODULE_TIER,
          id: row.id,
          changeReason: value || ''
        })
        this.$message.success('删除成功')
        await this.loadTierList()
        this.$emit('saved')
      } catch (e) {
        if (e === 'cancel' || e === 'close') return
      }
    },
    /** 父页「重新加载」调用 */
    refresh() {
      return this.bootstrap()
    }
  }
}
</script>

<style lang="scss" scoped>
.punch-apply-rule-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.section-card--setting ::v-deep .el-card__body {
  padding-top: 10px;
}

.section-card--tier ::v-deep .el-card__body {
  padding-top: 10px;
}

.setting-form {
  max-width: 100%;
}

.setting-form ::v-deep .el-form-item {
  margin-bottom: 10px;
}

.setting-form ::v-deep .el-form-item:last-child {
  margin-bottom: 0;
}

.setting-form__reason ::v-deep .el-textarea__inner {
  min-height: 72px;
}

.input-num--narrow {
  width: 120px;
}

.setting-flow-hint {
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 8px 12px;
  margin: 2px 0 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: #f7faff;
  border: 1px solid #e8f0fa;
}

.setting-flow-hint__label {
  flex-shrink: 0;
  padding: 0 8px;
  border-radius: 4px;
  background: #ecf5ff;
  color: #409eff;
  font-size: 12px;
  font-weight: 600;
  line-height: 22px;
}

.setting-flow-hint__text {
  flex: 1;
  min-width: 200px;
  color: #606266;
  font-size: 12px;
  line-height: 1.65;
}

.section-empty {
  padding: 12px 0 4px;
}

.danger-text {
  color: #f56c6c;
}

.table-ops {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 2px;
}

.table-ops__btn {
  padding: 0 4px;
}

.section-card__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.section-card__title-block {
  min-width: 0;
}

.section-card__eyebrow {
  margin-bottom: 4px;
  color: #409eff;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
}

.section-card__title {
  color: #1f2d3d;
  font-size: 15px;
  font-weight: 600;
}

.section-card__desc {
  margin-top: 6px;
  color: #909399;
  font-size: 12px;
  line-height: 1.6;
}

.section-card__actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}
</style>