<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.resource.defaultPermissionCode')"
          prop="code"
          :rules="[
            {required: true, message: $t('ec.resource.defaultPermissionCode') + $t('ec.table.emptyError'), trigger:'blur'}
          ]"
        >
          <el-input v-model="formItem.code" type="text" :placeholder="$t('ec.resource.defaultPermissionCode')" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ $t('ec.table.cancel') }}</el-button>
        <el-button type="primary" :loading="saveLoading" @click="save">{{ $t('ec.table.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addPermission } from 'ecip-web/api/vue/resource/sysrResourceApi'

export default {
  name: 'DefaultPermissionVue',
  data() {
    return {
      visible: false,
      loading: false,
      saveLoading: false,
      title: '',
      formItem: {
        code: ''
      }
    }
  },
  methods: {
    init(record) {
      this.visible = true
      this.loading = false
      this.saveLoading = false
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        Object.assign(this.$data.formItem, this.$options.data().formItem)
        this.formItem = this.$deepClone(record)
        this.title = this.$t('ec.resource.defaultPermission') + (record && record.rsrcName)
      })
    },
    save() {
      this.$confirm(this.$t('ec.resource.addPermissionConfirm'), this.$t('ec.message.prompt'), {
        confirmButtonText: this.$t('ec.message.confirm'),
        cancelButtonText: this.$t('ec.message.cancel'),
        type: 'warning'
      }).then(async() => {
        try {
          this.saveLoading = true
          await addPermission(this.formItem)
          this.$notify.success(this.$t('ec.resource.defaultPermission') + this.$t('ec.message.success'))
          this.$emit('refreshData')
        } finally {
          this.saveLoading = false
          this.visible = false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
