tenant-modal.tsx 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

18
import { defineComponent, PropType, toRefs, watch } from 'vue'
19
import Modal from '@/components/modal'
20 21
import { NForm, NFormItem, NInput, NSelect } from 'naive-ui'
import { useModalData } from './use-modalData'
22
import { useI18n } from 'vue-i18n'
23 24 25

const TenantModal = defineComponent({
  name: 'tenant-modal',
26 27 28 29 30 31 32 33 34 35 36 37 38 39
  props: {
    showModalRef: {
      type: Boolean as PropType<boolean>,
      default: false
    },
    statusRef: {
      type: Number as PropType<number>,
      default: 0
    },
    row: {
      type: Object as PropType<any>,
      default: {}
    }
  },
40 41
  emits: ['cancelModal', 'confirmModal'],
  setup(props, ctx) {
42
    const { variables, getListData, handleValidate } = useModalData(props, ctx)
43
    const { t } = useI18n()
44

45
    const cancelModal = () => {
46 47 48 49
      if (props.statusRef === 0) {
        variables.model.tenantCode = ''
        variables.model.description = ''
      }
50 51 52
      ctx.emit('cancelModal', props.showModalRef)
    }

53
    const confirmModal = () => {
54
      handleValidate(props.statusRef)
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
    watch(
      () => props.showModalRef,
      () => {
        props.showModalRef && getListData()
      }
    )

    watch(
      () => props.statusRef,
      () => {
        if (props.statusRef === 0) {
          variables.model.tenantCode = ''
          variables.model.description = ''
        } else {
          variables.model.id = props.row.id
          variables.model.tenantCode = props.row.tenantCode
          variables.model.queueId = props.row.queueId
          variables.model.description = props.row.description
        }
      }
    )

    watch(
      () => props.row,
      () => {
82 83 84 85 86
        variables.model.id = props.row.id
        variables.model.tenantCode = props.row.tenantCode
        variables.model.queueId = props.row.queueId
        variables.model.description = props.row.description
      }
87
    )
88

89
    return { t, ...toRefs(variables), cancelModal, confirmModal }
90 91
  },
  render() {
92
    const { t } = this
93 94 95
    return (
      <div>
        <Modal
96 97 98 99 100
          title={
            this.statusRef === 0
              ? t('security.tenant.create_tenant')
              : t('security.tenant.edit_tenant')
          }
101 102 103
          show={this.showModalRef}
          onCancel={this.cancelModal}
          onConfirm={this.confirmModal}
104 105
          confirmClassName='btn-submit'
          cancelClassName='btn-cancel'
106
          confirmLoading={this.saving}
107 108
        >
          {{
109 110 111 112
            default: () => (
              <NForm
                model={this.model}
                rules={this.rules}
113 114 115
                ref='tenantFormRef'
                require-mark-placement='left'
                size='small'
116 117
                style="{ maxWidth: '240px' }"
              >
118 119 120 121 122
                <NFormItem
                  label={t('security.tenant.tenant_code')}
                  path='tenantCode'
                >
                  <NInput
123
                    class='input-tenant-code'
124 125 126 127
                    disabled={this.statusRef === 1}
                    placeholder={t('security.tenant.tenant_code_tips')}
                    v-model={[this.model.tenantCode, 'value']}
                  />
128
                </NFormItem>
129 130 131 132
                <NFormItem
                  label={t('security.tenant.queue_name')}
                  path='queueId'
                >
133
                  <NSelect
134
                    class='select-queue'
135
                    placeholder={t('security.tenant.queue_name_tips')}
136 137 138 139
                    options={this.model.generalOptions}
                    v-model={[this.model.queueId, 'value']}
                  />
                </NFormItem>
140 141 142 143
                <NFormItem
                  label={t('security.tenant.description')}
                  path='description'
                >
144
                  <NInput
145
                    class='input-description'
146
                    placeholder={t('security.tenant.description_tips')}
147
                    v-model={[this.model.description, 'value']}
148
                    type='textarea'
149 150 151
                  />
                </NFormItem>
              </NForm>
152
            )
153 154 155 156
          }}
        </Modal>
      </div>
    )
157
  }
158 159 160
})

export default TenantModal