closeStoreDialog.vue 7.11 KB
<!-- 关店确认弹窗 -->
<template>
  <el-dialog title="关店确认" class="close-dialog" :visible.sync="visible" width="700px" @close="handleClose"
    :close-on-click-modal="false">
    <glsx-combi-form ref="editFormRef" class="edit-form-container" default-width="400px" :default-width="defaultWidth"
      :form-data="editFormData" :form-obj="addForm" :form-ref="editChildRef" :form-rules="formRules" :is-block="isBlock"
      :label-width="labelWidth">

      <template #confirmResultHtml>
        <el-radio-group v-model="addForm.confirmResult" @change="handleResultChange">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </template>

    </glsx-combi-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { closeStoreConfirm } from '@api/salesManagement'

export default {
  name: 'CloseStoreDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    recordInfo: {
      type: Object,
      default: () => null
    }
  },
  data() {
    return {
      loading: false,
      labelWidth: "140px",
      defaultWidth: "400px",
      editChildRef: "editChildRef",
      isBlock: true,
      addForm: {
        confirmResult: 1, // 默认选是
        confirmReason: '合作渗透率连续未达标,终止合作', // 默认原因
        confirmRemark: ''
      },
      editFormData: [
        {
          label: "是否关闭此门店",
          name: "confirmResult",
          addHtml: "confirmResultHtml",
        },
        {
          label: "关店原因",
          name: "confirmReason",
          type: "select",
          isClear: true,
          optionList: [
            { label: '合作渗透率连续未达标,终止合作', value: '合作渗透率连续未达标,终止合作' },
            { label: '无人员稳定在岗', value: '无人员稳定在岗' },
            { label: '三方切换', value: '三方切换' },
            { label: '其他', value: '其他' }
          ],
          optionId: "value",
          optionLabel: "label",
          isHide: false,
          changeAttr: (value) => {
            const index = this.editFormData.findIndex(item => item.name === 'confirmRemark');
            if (index > -1) {
              if (value !== '其他') {
                this.addForm.confirmRemark = ''
              }
              this.$set(this.editFormData[index], 'isHide', value !== '其他');
            }
          }
        },
        {
          label: "关店原因",
          name: "confirmRemark",
          placeholder: "请输入",
          type: "textarea",
          showLimit: true,
          isClear: true,
          maxlength: 100,
          rows: 4,
          isHide: true
        }
      ],
      formRules: {
        confirmResult: [
          { required: true, message: '请选择是否关闭此门店', trigger: 'change' }
        ],
        confirmReason: [
          {
            required: true, validator: (rule, value, callback) => {
              if (this.addForm.confirmResult === 1 && !value) {
                callback(new Error('请选择关店原因'));
              } else {
                callback();
              }
            }, trigger: 'change'
          }
        ],
        // confirmRemark: [
        //   {
        //     required: true, validator: (rule, value, callback) => {
        //       if (this.addForm.confirmResult === 1 && this.addForm.confirmReason === '其他' && !value) {
        //         callback(new Error('请输入关店原因'));
        //       } else {
        //         callback();
        //       }
        //     }, trigger: 'blur'
        //   }
        // ]
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.dialogVisible
      },
      set(val) {
        this.$emit('update:dialogVisible', val)
      }
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.addForm = {
          confirmResult: 1,
          confirmReason: '合作渗透率连续未达标,终止合作',
          confirmRemark: ''
        }

        // 重置时也需要恢复动态显示状态
        const reasonIdx = this.editFormData.findIndex(item => item.name === 'confirmReason');
        const remarkIdx = this.editFormData.findIndex(item => item.name === 'confirmRemark');
        if (reasonIdx > -1) this.$set(this.editFormData[reasonIdx], 'isHide', false);
        if (remarkIdx > -1) this.$set(this.editFormData[remarkIdx], 'isHide', true);

        if (this.$refs.editFormRef && this.$refs.editFormRef.$refs[this.editChildRef]) {
          this.$refs.editFormRef.$refs[this.editChildRef].clearValidate()
        }
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    handleResultChange(val) {
      const reasonIdx = this.editFormData.findIndex(item => item.name === 'confirmReason');
      const remarkIdx = this.editFormData.findIndex(item => item.name === 'confirmRemark');

      if (val === 2) {
        if (reasonIdx > -1) this.$set(this.editFormData[reasonIdx], 'isHide', true);
        if (remarkIdx > -1) this.$set(this.editFormData[remarkIdx], 'isHide', true);
        this.addForm.confirmReason = '';
        this.addForm.confirmRemark = '';
      } else {
        if (reasonIdx > -1) this.$set(this.editFormData[reasonIdx], 'isHide', false);
        this.addForm.confirmReason = '合作渗透率连续未达标,终止合作';
        if (remarkIdx > -1) this.$set(this.editFormData[remarkIdx], 'isHide', true);
      }

      if (this.$refs.editFormRef && this.$refs.editFormRef.$refs[this.editChildRef]) {
        this.$refs.editFormRef.$refs[this.editChildRef].clearValidate();
      }
    },
    handleSubmit() {
      this.$refs.editFormRef.$refs[this.editChildRef].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            id: this.recordInfo.id,
            confirmResult: this.addForm.confirmResult,
          }

          if (this.addForm.confirmResult === 1) {
            params.confirmReason = this.addForm.confirmReason
            if (this.addForm.confirmReason === '其他' && this.addForm.confirmRemark !== '') {
              params.confirmReason = '其他-' + this.addForm.confirmRemark
              // params.confirmRemark = this.addForm.confirmRemark
            }
          }

          closeStoreConfirm(params).then(() => {
            this.$message.success('操作成功')
            this.loading = false
            this.visible = false
            this.$emit('refresh')
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.close-dialog {
  ::v-deep {
    .el-input__count {
      right: 10px !important;
      bottom: 1px !important;
      font-size: 12px;
      color: #999999;
    }
  }

  ::v-deep {
    .el-dialog__footer {
      border-top: 1px solid #e5e5e5 !important;
    }
  }

  ::v-deep .el-dialog__header {
    border-bottom: 1px solid #e5e5e5;
  }
}
</style>