<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible"
      top="4%"
    >
      <el-form ref="form" v-loading="loading" status-icon :model="formItem" label-width="120px">
        <el-form-item
          :label="$t('ec.table.name')"
          prop="name"
          :rules="[
            {required: true, message: $t('ec.table.name') + $t('ec.table.emptyError'), trigger:'blur'},
            { validator: validateName, trigger: 'blur' }
          ]"
        >
          <el-input v-model="formItem.name" type="text" :placeholder="$t('ec.table.name')"  maxlength="32" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.table.shortName')"
          prop="simpleName"
        >
          <el-input v-model="formItem.simpleName" type="text" :placeholder="$t('ec.table.shortName')" maxlength="32" />
        </el-form-item>

        <el-form-item
          :label="$t('ec.table.code')"
          prop="code"
          :rules="[
            { required: true, message: $t('ec.table.code') + $t('ec.table.emptyError'), trigger:'blur' },
            { validator: validateCode, trigger: 'blur' }
          ]"
        >
          <el-input v-model="formItem.code" type="text" :placeholder="$t('ec.table.code')" maxlength="64" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.date.effectiveTime')"
          prop="effectiveDate"
          :rules="[
            {required: true, message: $t('ec.date.effectiveTime') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <ec-date v-model="formItem.effectiveDate" type="datetime" format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.date.expirationTime')"
          prop="expireDate"
          :rules="[
            {required: true, message: $t('ec.date.expirationTime') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <ec-date v-model="formItem.expireDate" type="datetime" format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.table.remarks')"
          prop="remark"
          :rules="[
          ]"
        >
          <el-input v-model="formItem.remark" type="textarea" :autosize="{minRows: 3, maxRows: 5}"  maxlength="125" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ $t('ec.table.close') }}</el-button>
        <el-button v-if="method !== 'view'" type="primary" :loading="saveLoading" @click="save">{{ $t('ec.table.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import mixin from 'ecip-web/utils/common-mixin'
import { getById, save, update, checkTenantCode, checkTenantName } from 'ecip-web/api/vue/tenant/saasTenantApi'

export default {
  name: 'VueTenantTenantSaasTenantForm',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      visible: false,
      loading: false, // 页面加载遮罩
      saveLoading: false, //  保存加载
      method: '',
      title: '',
      tenant: {},
      formItem: {
        name: '',
        code: '',
        effectiveDate: '',
        expireDate: '',
        remarks: '',
        simpleName: '',
      }
    }
  },
  methods: {
    init(method, row) {
      this.method = method
      this.tenant = row
      if (method === 'add') {
        this.title = this.$t('ec.table.add')
      } else if (method === 'edit') {
        this.title = this.$t('ec.table.edit') + '-' + row.name
      } else if (method === 'view') {
        this.title = this.$t('ec.table.view') + '-' + row.name
      }
      this.visible = true
      this.loading = false
      this.$nextTick(async() => {
        this.$refs.form.clearValidate()
        Object.assign(this.$data.formItem, this.$options.data().formItem)
        if (method === 'edit' || method === 'view') { // 修改或者查看
          try {
            this.loading = true
            const { data } = await getById(row.id)
            Object.assign(this.formItem, data)
          } finally {
            this.loading = false
          }
        }
      })
    },
    async validateCode(rule, value, callback) {
      const { data } = await checkTenantCode((this.tenant && this.tenant.id) || '', value)
      if (data.valid) {
        callback()
      } else {
        callback(new Error(this.$t('ec.table.code') + this.$t('ec.table.codeExists')))
      }
    },
    async validateName(rule, value, callback) {
      const { data } = await checkTenantName((this.tenant && this.tenant.id) || '', value)
      if (data.valid) {
        callback()
      } else {
        callback(new Error(this.$t('ec.user.tenant') + this.$t('ec.table.exist')))
      }
    },
    save() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            this.saveLoading = true
            if (this.method === 'edit') {
              await update(this.formItem.id, this.formItem)
            } else {
              await save(this.formItem)
            }
            this.$notify.success(this.$t('ec.message.saveSuccess'))
            this.$emit('refreshDataList')
            this.visible = false
          } finally {
            this.saveLoading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
