<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" :model="formItem" :label-width="'100px'">
        <el-form-item
          :label="$t('ec.table.name')"
          prop="name"
          :rules="[
            {required: true, message: $t('ec.table.name') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <el-input v-model="formItem.name" type="text" :placeholder="$t('ec.table.name')" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.table.code')"
          prop="code"
          :rules="[
            {required: true, message: $t('ec.table.code') + $t('ec.table.codeEmpty'), trigger:'blur'},
            { validator: validateCode, trigger: 'blur' }
          ]"
        >
          <el-input v-model="formItem.code" type="text" :placeholder="$t('ec.table.code')" />
        </el-form-item>
        <el-form-item :label="$t('ec.org.legal')" prop="orgLegal">
          <ec-user-modal v-model="formItem.orgLegal" />
        </el-form-item>
        <el-form-item v-if="superAdmin" :label="$t('ec.user.tenant')" prop="tenantId">
          <ec-tenant-by-pro v-model="formItem.tenantId" />
        </el-form-item>
        <el-form-item :label="$t('ec.tree.parent')" prop="parent.id">
          <span v-if="parent && parent.id">{{ parent.name }}</span>
          <ec-select-tree
            v-else
            ref="parentTree"
            v-model="formItem.parent.id"
            check-strictly
            url="api/v1/org/saasOrg/data"
            :data="{ tenantId: formItem.tenantId }"
            :params="{ excludeId: record.id }"
          />
        </el-form-item>

        <el-form-item
          :label="$t('ec.org.type')"
          prop="orgType"
          :rules="[
            {required: true, message: $t('ec.org.type') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <ec-select v-model="formItem.orgType" dict-type="orgType" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.table.sort')"
          prop="sort"
          :rules="[
            {required: true, message: $t('ec.table.sort') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <el-input-number v-model="formItem.sort" :placeholder="$t('ec.table.sort')" />
        </el-form-item>
        <el-form-item
          :label="$t('ec.table.enableStatus')"
          prop="status"
          :rules="[
            {required: true, message: $t('ec.table.enableStatus') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <ec-radio-group v-model="formItem.status" dict-type="enableState" />
        </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="confirmLoading" @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, checkCode } from 'ecip-web/api/vue/org/saasOrgApi'
import Vue from 'vue'
import { cloneDeep } from 'ecip-web/utils'

export default {
  name: 'VueOrgSaasOrgFormDetail',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      visible: false,
      loading: false,
      confirmLoading: false,
      method: '',
      title: '',
      record: {},
      parent: {},
      formItem: {
        tenantId: '',
        name: '',
        code: '',
        orgType: '1',
        sort: 30,
        orgLegal: '',
        status: '1',
        parent: {}
      }
    }
  },
  methods: {
    init(method, tenantId, row, parent) {
      this.parent = parent || {}
      this.method = method
      this.record = 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()
        this.$refs.form.resetFields()
        Object.assign(this.$data.formItem, this.$options.data().formItem)
        this.formItem.tenantId = tenantId
        if (method === 'edit' || method === 'view') { // 修改或者查看
          try {
            this.loading = true
            const { data } = await getById(row.id)
            // this.formItem = data
            cloneDeep(this.formItem, data)
            Vue.set(this.formItem, 'parent', { id: this.formItem.parentId, name: this.formItem.parentName })
          } finally {
            this.loading = false
          }
        }
      })
    },
    async validateCode(rule, value, callback) {
      const { data } = await checkCode((this.record && this.record.id) || '', value, this.formItem.tenantId)
      if (data.valid) {
        callback()
      } else {
        callback(new Error(this.$t('ec.table.code') + this.$t('ec.table.exist')))
      }
    },
    save() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            this.formItem.parentId = this.formItem.parent.id

            if (this.method === 'edit') {
              await update(this.record.id, this.formItem)
            } else {
              await save(this.formItem)
            }
            this.$notify.success(this.$t('ec.message.saveSuccess'))
            this.$emit('refreshData')
            this.visible = false
          } finally {
            this.confirmLoading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
